belongs-to.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var Utils = require("./../utils")
  2. , DataTypes = require('./../data-types')
  3. , Helpers = require('./helpers')
  4. , Transaction = require('../transaction')
  5. module.exports = (function() {
  6. var BelongsTo = function(source, target, options) {
  7. this.associationType = 'BelongsTo'
  8. this.source = source
  9. this.target = target
  10. this.options = options
  11. this.isSingleAssociation = true
  12. this.isSelfAssociation = (this.source.tableName == this.target.tableName)
  13. this.as = this.options.as
  14. if (this.isSelfAssociation && !this.options.foreignKey && !!this.as) {
  15. this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.source.tableName, this.source.options.language) + "Id", this.source.options.underscored)
  16. }
  17. if (this.as) {
  18. this.isAliased = true
  19. } else {
  20. this.as = Utils.singularize(this.target.tableName, this.target.options.language)
  21. // Hotfix
  22. if (this.as === this.target.tableName) {
  23. this.as = Utils.singularize(this.target.name, this.target.options.language)
  24. }
  25. }
  26. this.associationAccessor = this.isSelfAssociation
  27. ? Utils.combineTableNames(this.target.tableName, this.as)
  28. : this.as
  29. this.options.useHooks = options.useHooks
  30. this.accessors = {
  31. get: Utils._.camelize('get_' + this.as),
  32. set: Utils._.camelize('set_' + this.as),
  33. create: Utils._.camelize('create_' + this.as)
  34. }
  35. }
  36. // the id is in the source table
  37. BelongsTo.prototype.injectAttributes = function() {
  38. var newAttributes = {}
  39. , targetKeys = Object.keys(this.target.primaryKeys)
  40. , keyType = ((this.target.hasPrimaryKeys && targetKeys.length === 1) ? this.target.rawAttributes[targetKeys[0]].type : DataTypes.INTEGER)
  41. this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName, this.target.options.language) + "Id", this.source.options.underscored)
  42. newAttributes[this.identifier] = { type: this.options.keyType || keyType }
  43. Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options)
  44. Utils._.defaults(this.source.rawAttributes, newAttributes)
  45. // Sync attributes and setters/getters to DAO prototype
  46. this.source.refreshAttributes()
  47. return this
  48. }
  49. BelongsTo.prototype.injectGetter = function(obj) {
  50. var self = this
  51. , primaryKeys = Object.keys(self.target.primaryKeys)
  52. , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
  53. obj[this.accessors.get] = function(params) {
  54. var id = this[self.identifier]
  55. , where = {}
  56. where[primaryKey] = id
  57. if (!Utils._.isUndefined(params)) {
  58. if (!Utils._.isUndefined(params.where)) {
  59. params.where = Utils._.extend(where, params.where)
  60. } else {
  61. params.where = where
  62. }
  63. } else {
  64. params = id
  65. }
  66. return self.target.find(params)
  67. }
  68. return this
  69. }
  70. BelongsTo.prototype.injectSetter = function(obj) {
  71. var self = this
  72. obj[this.accessors.set] = function(associatedObject, options) {
  73. var primaryKeys = !!associatedObject && !!associatedObject.daoFactory ? Object.keys(associatedObject.daoFactory.primaryKeys) : []
  74. , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
  75. this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null
  76. options = Utils._.extend({
  77. fields: [ self.identifier ],
  78. allowNull: [self.identifier],
  79. association: true
  80. }, options)
  81. // passes the changed field to save, so only that field get updated.
  82. return this.save(options)
  83. }
  84. return this
  85. }
  86. BelongsTo.prototype.injectCreator = function(obj) {
  87. var self = this
  88. obj[this.accessors.create] = function(values, fieldsOrOptions) {
  89. var instance = this
  90. , options = {}
  91. if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
  92. options.transaction = fieldsOrOptions.transaction
  93. }
  94. return new Utils.CustomEventEmitter(function(emitter) {
  95. self.target
  96. .create(values, fieldsOrOptions)
  97. .proxy(emitter, { events: ['error', 'sql'] })
  98. .success(function(newAssociatedObject) {
  99. instance[self.accessors.set](newAssociatedObject, options)
  100. .proxy(emitter)
  101. })
  102. }).run()
  103. }
  104. return this
  105. }
  106. return BelongsTo
  107. })()