Merge pull request #420 from aol-nnov/fkCustomDbType

Create model foreign key matching type of opposite part of relation (even if it has a custom field type)
This commit is contained in:
Raymond Feng 2015-02-05 10:28:04 -08:00
commit e9c966227d
3 changed files with 96 additions and 39 deletions

13
.editorconfig Normal file
View File

@ -0,0 +1,13 @@
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

View File

@ -1230,8 +1230,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
options: { options: {
idInjection: false // DO NOT add id property idInjection: false // DO NOT add id property
}, },
properties: { properties: {}
}
}; };
schema.options[schemaName] = { schema.options[schemaName] = {
@ -1373,8 +1372,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
options: { options: {
idInjection: false // DO NOT add id property idInjection: false // DO NOT add id property
}, },
properties: { properties: {}
}
}; };
schema.options[schemaName] = { schema.options[schemaName] = {
@ -1711,11 +1709,18 @@ DataSource.prototype.defineForeignKey = function defineForeignKey(className, key
return; return;
} }
var fkDef = {type: pkType};
var foreignMeta = this.columnMetadata(foreignClassName, key);
if(foreignMeta && foreignMeta.dataType) {
fkDef[this.connector.name] = {};
fkDef[this.connector.name].dataType = foreignMeta.dataType;
}
if (this.connector.defineForeignKey) { if (this.connector.defineForeignKey) {
var cb = function (err, keyType) { var cb = function (err, keyType) {
if (err) throw err; if (err) throw err;
fkDef.type = keyType || pkType;
// Add the foreign key property to the data source _models // Add the foreign key property to the data source _models
this.defineProperty(className, key, {type: keyType || pkType}); this.defineProperty(className, key, fkDef);
}.bind(this); }.bind(this);
switch (this.connector.defineForeignKey.length) { switch (this.connector.defineForeignKey.length) {
case 4: case 4:
@ -1728,7 +1733,7 @@ DataSource.prototype.defineForeignKey = function defineForeignKey(className, key
} }
} else { } else {
// Add the foreign key property to the data source _models // Add the foreign key property to the data source _models
this.defineProperty(className, key, {type: pkType}); this.defineProperty(className, key, fkDef);
} }
}; };

View File

@ -0,0 +1,39 @@
var should = require('./init.js');
var jdb = require('../');
var DataSource = jdb.DataSource;
var ds, Item, Variant;
describe('Datasource-specific field types for foreign keys', function () {
before(function () {
ds = new DataSource('memory');
Item = ds.define('Item', {
"myProp": {
"type": "string",
"memory": {
"dataType": "string"
}
}
});
Variant = ds.define('Variant', {}, {
relations: {
"item": {
"type": "belongsTo",
"as": "item",
"model": "Item",
"foreignKey": "myProp"
}
}
});
});
it('should create foreign key with database-specific field type', function (done) {
var VariantDefinition = ds.getModelDefinition('Variant');
should.exist(VariantDefinition);
should.exist(VariantDefinition.properties.myProp.memory);
should.exist(VariantDefinition.properties.myProp.memory.dataType);
VariantDefinition.properties.myProp.memory.dataType.should.be.equal("string");
done();
});
})
;