has-many-single-linked.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var Utils = require('./../utils')
  2. , Transaction = require('./../transaction')
  3. module.exports = (function() {
  4. var HasManySingleLinked = function(definition, instance) {
  5. this.__factory = definition
  6. this.instance = instance
  7. }
  8. HasManySingleLinked.prototype.injectGetter = function(options) {
  9. var self = this
  10. , where = {}
  11. , smart
  12. options = options || {}
  13. var primaryKey = Object.keys(this.instance.rawAttributes).filter(function(k) { return self.instance.rawAttributes[k].primaryKey === true })
  14. primaryKey = primaryKey.length === 1 ? primaryKey[0] : 'id'
  15. where[this.__factory.identifier] = this.instance[primaryKey]
  16. if (options.where) {
  17. smart = Utils.smartWhere([where, options.where], this.__factory.target.daoFactoryManager.sequelize.options.dialect)
  18. smart = Utils.compileSmartWhere.call(this.__factory.target, smart, this.__factory.target.daoFactoryManager.sequelize.options.dialect)
  19. if (smart.length > 0) {
  20. options.where = smart
  21. }
  22. } else {
  23. options.where = where
  24. }
  25. return this.__factory.target.all(options)
  26. }
  27. HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations, defaultAttributes) {
  28. var self = this
  29. , associationKeys = Object.keys((oldAssociations[0] || newAssociations[0] || {daoFactory: {primaryKeys: {}}}).daoFactory.primaryKeys || {})
  30. , associationKey = (associationKeys.length === 1) ? associationKeys[0] : 'id'
  31. , chainer = new Utils.QueryChainer()
  32. , options = {}
  33. , obsoleteAssociations = oldAssociations.filter(function (old) {
  34. return !Utils._.find(newAssociations, function (obj) {
  35. return obj[associationKey] === old[associationKey]
  36. })
  37. })
  38. , unassociatedObjects = newAssociations.filter(function (obj) {
  39. return !Utils._.find(oldAssociations, function (old) {
  40. return obj[associationKey] === old[associationKey]
  41. })
  42. })
  43. , update
  44. if ((defaultAttributes || {}).transaction instanceof Transaction) {
  45. options.transaction = defaultAttributes.transaction
  46. delete defaultAttributes.transaction
  47. }
  48. if (obsoleteAssociations.length > 0) {
  49. // clear the old associations
  50. var obsoleteIds = obsoleteAssociations.map(function(associatedObject) {
  51. associatedObject[self.__factory.identifier] = (newAssociations.length < 1 ? null : self.instance.id)
  52. return associatedObject[associationKey]
  53. })
  54. update = {}
  55. update[self.__factory.identifier] = null
  56. var primaryKeys = Object.keys(this.__factory.target.primaryKeys)
  57. , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
  58. , updateWhere = {}
  59. updateWhere[primaryKey] = obsoleteIds
  60. chainer.add(this.__factory.target.update(
  61. update,
  62. updateWhere,
  63. Utils._.extend(options, { allowNull: [self.__factory.identifier] })
  64. ))
  65. }
  66. if (unassociatedObjects.length > 0) {
  67. // For the self.instance
  68. var pkeys = Object.keys(self.instance.daoFactory.primaryKeys)
  69. , pkey = pkeys.length === 1 ? pkeys[0] : 'id'
  70. // For chainer
  71. , primaryKeys = Object.keys(this.__factory.target.primaryKeys)
  72. , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
  73. , updateWhere = {}
  74. // set the new associations
  75. var unassociatedIds = unassociatedObjects.map(function(associatedObject) {
  76. associatedObject[self.__factory.identifier] = self.instance[pkey] || self.instance.id
  77. return associatedObject[associationKey]
  78. })
  79. update = {}
  80. update[self.__factory.identifier] = (newAssociations.length < 1 ? null : self.instance[pkey] || self.instance.id)
  81. updateWhere[primaryKey] = unassociatedIds
  82. chainer.add(this.__factory.target.update(
  83. update,
  84. updateWhere,
  85. Utils._.extend(options, { allowNull: [self.__factory.identifier] })
  86. ))
  87. }
  88. chainer
  89. .run()
  90. .success(function() { emitter.emit('success', newAssociations) })
  91. .error(function(err) { emitter.emit('error', err) })
  92. .on('sql', function(sql) { emitter.emit('sql', sql) })
  93. }
  94. HasManySingleLinked.prototype.injectAdder = function(emitterProxy, newAssociation, additionalAttributes) {
  95. var primaryKeys = Object.keys(this.instance.daoFactory.primaryKeys)
  96. , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
  97. , options = {}
  98. if ((additionalAttributes || {}).transaction instanceof Transaction) {
  99. options.transaction = additionalAttributes.transaction
  100. delete additionalAttributes.transaction
  101. }
  102. newAssociation[this.__factory.identifier] = this.instance[primaryKey]
  103. newAssociation.save(options)
  104. .success(function() { emitterProxy.emit('success', newAssociation) })
  105. .error(function(err) { emitterProxy.emit('error', err) })
  106. .on('sql', function(sql) { emitterProxy.emit('sql', sql) })
  107. }
  108. return HasManySingleLinked
  109. })()