不要怂,就是干,撸起袖子干!

Commit 151a23f6 by Martin Aspeli

Factor FK constraint logic out into a helper function

1 parent 1df90277
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
, Helpers = require('./helpers')
module.exports = (function() { module.exports = (function() {
var BelongsTo = function(srcDAO, targetDAO, options) { var BelongsTo = function(srcDAO, targetDAO, options) {
...@@ -24,18 +25,7 @@ module.exports = (function() { ...@@ -24,18 +25,7 @@ module.exports = (function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options)
if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) {
var primaryKeys = Utils._.filter(Utils._.keys(this.target.rawAttributes), function(key) { return this.target.rawAttributes[key].primaryKey }, this)
if(primaryKeys.length == 1) { // composite keys not supported with this approach
newAttributes[this.identifier].references = this.target.tableName,
newAttributes[this.identifier].referencesKey = primaryKeys[0]
newAttributes[this.identifier].onDelete = this.options.onDelete,
newAttributes[this.identifier].onUpdate = this.options.onUpdate
}
}
Utils._.defaults(this.source.rawAttributes, newAttributes) Utils._.defaults(this.source.rawAttributes, newAttributes)
// Sync attributes to DAO proto each time a new assoc is added // Sync attributes to DAO proto each time a new assoc is added
......
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
, Helpers = require('./helpers')
var HasManySingleLinked = require("./has-many-single-linked") var HasManySingleLinked = require("./has-many-single-linked")
, HasManyMultiLinked = require("./has-many-double-linked") , HasManyMultiLinked = require("./has-many-double-linked")
...@@ -65,18 +66,7 @@ module.exports = (function() { ...@@ -65,18 +66,7 @@ module.exports = (function() {
} else { } else {
var newAttributes = {} var newAttributes = {}
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) {
var primaryKeys = Utils._.filter(Utils._.keys(this.source.rawAttributes), function(key) { return this.source.rawAttributes[key].primaryKey }, this)
if(primaryKeys.length == 1) { // composite keys not supported with this approach
newAttributes[this.identifier].references = this.source.tableName
newAttributes[this.identifier].referencesKey = primaryKeys[0]
newAttributes[this.identifier].onDelete = this.options.onDelete
newAttributes[this.identifier].onUpdate = this.options.onUpdate
}
}
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
} }
......
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
, Helpers = require("./helpers")
module.exports = (function() { module.exports = (function() {
var HasOne = function(srcDAO, targetDAO, options) { var HasOne = function(srcDAO, targetDAO, options) {
...@@ -29,18 +30,7 @@ module.exports = (function() { ...@@ -29,18 +30,7 @@ module.exports = (function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) {
var primaryKeys = Utils._.filter(Utils._.keys(this.source.rawAttributes), function(key) { return this.source.rawAttributes[key].primaryKey }, this)
if(primaryKeys.length == 1) { // composite keys not supported with this approach
newAttributes[this.identifier].references = this.source.tableName,
newAttributes[this.identifier].referencesKey = primaryKeys[0]
newAttributes[this.identifier].onDelete = this.options.onDelete,
newAttributes[this.identifier].onUpdate = this.options.onUpdate
}
}
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
// Sync attributes to DAO proto each time a new assoc is added // Sync attributes to DAO proto each time a new assoc is added
......
var Utils = require("./../utils")
module.exports = {
addForeignKeyConstraints: function(newAttribute, source, target, options) {
// FK constraints are opt-in: users must either rset `foreignKeyConstraints`
// on the association, or request an `onDelete` or `onUpdate` behaviour
if(options.foreignKeyConstraint || options.onDelete || options.onUpdate) {
// Find primary keys: composite keys not supported with this approach
var primaryKeys = Utils._.filter(Utils._.keys(source.rawAttributes), function(key) {
return source.rawAttributes[key].primaryKey
})
if(primaryKeys.length == 1) {
newAttribute.references = source.tableName,
newAttribute.referencesKey = primaryKeys[0]
newAttribute.onDelete = options.onDelete,
newAttribute.onUpdate = options.onUpdate
}
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!