From 57afba8c51181266e87dd88de76031aa4693ead5 Mon Sep 17 00:00:00 2001 From: bitmage Date: Wed, 16 Mar 2016 09:17:55 -0700 Subject: [PATCH] support custom field settings under the connector's namespace --- lib/datasource.js | 18 +++++++++++------- test/discovery.test.js | 17 +++++++++++++++++ test/mock-connectors.js | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/mock-connectors.js diff --git a/lib/datasource.js b/lib/datasource.js index 11d12736..4d8a9020 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1343,8 +1343,6 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) { }; columns.forEach(function (item) { - var i = item; - var propName = nameMapper('column', item.columnName); schema.properties[propName] = { type: item.type, @@ -1358,14 +1356,20 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) { if (pks[item.columnName]) { schema.properties[propName].id = pks[item.columnName]; } - schema.properties[propName][dbType] = { - columnName: i.columnName, - dataType: i.dataType, - dataLength: i.dataLength, + var dbSpecific = schema.properties[propName][dbType] = { + columnName: item.columnName, + dataType: item.dataType, + dataLength: item.dataLength, dataPrecision: item.dataPrecision, dataScale: item.dataScale, - nullable: i.nullable + nullable: item.nullable }; + // merge connector-specific properties + if (item[dbType]) { + for (var k in item[dbType]) { + dbSpecific[k] = item[dbType][k]; + } + } }); // Add current modelName to the visited tables diff --git a/test/discovery.test.js b/test/discovery.test.js index 26e5056c..3a89b2bd 100644 --- a/test/discovery.test.js +++ b/test/discovery.test.js @@ -602,3 +602,20 @@ describe('discoverExportedForeignKeys', function(){ }); }); }); + +describe('Mock connector', function() { + mockConnectors = require('./mock-connectors'); + describe('customFieldSettings', function() { + ds = new DataSource(mockConnectors.customFieldSettings); + + it('should be present in discoverSchemas', function(done) { + ds.discoverSchemas('person', function(err, schemas) { + should.not.exist(err); + schemas.should.be.an.Object; + schemas['public.person'].properties.name + .custom.storage.should.equal('quantum'); + done(); + }); + }); + }); +}); diff --git a/test/mock-connectors.js b/test/mock-connectors.js new file mode 100644 index 00000000..3d067a3f --- /dev/null +++ b/test/mock-connectors.js @@ -0,0 +1,26 @@ +module.exports = { + + // connector which uses custom field settings + customFieldSettings: { + initialize: function(ds, done) { + ds.connector = { + name: 'custom', + discoverModelProperties: function(resource, options, done) { + done(null, [ + { + owner: 'public', + columnName: 'name', + type: 'String', + required: false, + + // custom properties listed under a key matching the connector name + custom: {storage: 'quantum'} + } + ]); + } + }; + ds.connector.dataSource = ds; + } + } + +};