belongs-to.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. const Utils = require('./../utils');
  3. const Helpers = require('./helpers');
  4. const _ = require('lodash');
  5. const Association = require('./base');
  6. const Op = require('../operators');
  7. /**
  8. * One-to-one association
  9. *
  10. * In the API reference below, add the name of the association to the method, e.g. for `User.belongsTo(Project)` the getter will be `user.getProject()`.
  11. *
  12. * @see {@link Model.belongsTo}
  13. */
  14. class BelongsTo extends Association {
  15. constructor(source, target, options) {
  16. super(source, target, options);
  17. this.associationType = 'BelongsTo';
  18. this.isSingleAssociation = true;
  19. this.foreignKeyAttribute = {};
  20. if (this.as) {
  21. this.isAliased = true;
  22. this.options.name = {
  23. singular: this.as
  24. };
  25. } else {
  26. this.as = this.target.options.name.singular;
  27. this.options.name = this.target.options.name;
  28. }
  29. if (_.isObject(this.options.foreignKey)) {
  30. this.foreignKeyAttribute = this.options.foreignKey;
  31. this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
  32. } else if (this.options.foreignKey) {
  33. this.foreignKey = this.options.foreignKey;
  34. }
  35. if (!this.foreignKey) {
  36. this.foreignKey = Utils.camelize(
  37. [
  38. this.as,
  39. this.target.primaryKeyAttribute
  40. ].join('_')
  41. );
  42. }
  43. this.identifier = this.foreignKey;
  44. if (this.source.rawAttributes[this.identifier]) {
  45. this.identifierField = this.source.rawAttributes[this.identifier].field || this.identifier;
  46. }
  47. if (
  48. this.options.targetKey
  49. && !this.target.rawAttributes[this.options.targetKey]
  50. ) {
  51. throw new Error(`Unknown attribute "${this.options.targetKey}" passed as targetKey, define this attribute on model "${this.target.name}" first`);
  52. }
  53. this.targetKey = this.options.targetKey || this.target.primaryKeyAttribute;
  54. this.targetKeyField = this.target.rawAttributes[this.targetKey].field || this.targetKey;
  55. this.targetKeyIsPrimary = this.targetKey === this.target.primaryKeyAttribute;
  56. this.targetIdentifier = this.targetKey;
  57. this.associationAccessor = this.as;
  58. this.options.useHooks = options.useHooks;
  59. // Get singular name, trying to uppercase the first letter, unless the model forbids it
  60. const singular = _.upperFirst(this.options.name.singular);
  61. this.accessors = {
  62. get: `get${singular}`,
  63. set: `set${singular}`,
  64. create: `create${singular}`
  65. };
  66. }
  67. // the id is in the source table
  68. _injectAttributes() {
  69. const newAttributes = {};
  70. newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
  71. type: this.options.keyType || this.target.rawAttributes[this.targetKey].type,
  72. allowNull: true
  73. });
  74. if (this.options.constraints !== false) {
  75. const source = this.source.rawAttributes[this.foreignKey] || newAttributes[this.foreignKey];
  76. this.options.onDelete = this.options.onDelete || (source.allowNull ? 'SET NULL' : 'NO ACTION');
  77. this.options.onUpdate = this.options.onUpdate || 'CASCADE';
  78. }
  79. Helpers.addForeignKeyConstraints(newAttributes[this.foreignKey], this.target, this.source, this.options, this.targetKeyField);
  80. Utils.mergeDefaults(this.source.rawAttributes, newAttributes);
  81. this.source.refreshAttributes();
  82. this.identifierField = this.source.rawAttributes[this.foreignKey].field || this.foreignKey;
  83. Helpers.checkNamingCollision(this);
  84. return this;
  85. }
  86. mixin(obj) {
  87. const methods = ['get', 'set', 'create'];
  88. Helpers.mixinMethods(this, obj, methods);
  89. }
  90. /**
  91. * Get the associated instance.
  92. *
  93. * @param {Model|Array<Model>} instances source instances
  94. * @param {Object} [options] find options
  95. * @param {string|boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false.
  96. * @param {string} [options.schema] Apply a schema on the related model
  97. *
  98. * @see
  99. * {@link Model.findOne} for a full explanation of options
  100. *
  101. * @returns {Promise<Model>}
  102. */
  103. get(instances, options) {
  104. const where = {};
  105. let Target = this.target;
  106. let instance;
  107. options = Utils.cloneDeep(options);
  108. if (Object.prototype.hasOwnProperty.call(options, 'scope')) {
  109. if (!options.scope) {
  110. Target = Target.unscoped();
  111. } else {
  112. Target = Target.scope(options.scope);
  113. }
  114. }
  115. if (Object.prototype.hasOwnProperty.call(options, 'schema')) {
  116. Target = Target.schema(options.schema, options.schemaDelimiter);
  117. }
  118. if (!Array.isArray(instances)) {
  119. instance = instances;
  120. instances = undefined;
  121. }
  122. if (instances) {
  123. where[this.targetKey] = {
  124. [Op.in]: instances.map(instance => instance.get(this.foreignKey))
  125. };
  126. } else {
  127. if (this.targetKeyIsPrimary && !options.where) {
  128. return Target.findByPk(instance.get(this.foreignKey), options);
  129. }
  130. where[this.targetKey] = instance.get(this.foreignKey);
  131. options.limit = null;
  132. }
  133. options.where = options.where ?
  134. { [Op.and]: [where, options.where] } :
  135. where;
  136. if (instances) {
  137. return Target.findAll(options).then(results => {
  138. const result = {};
  139. for (const instance of instances) {
  140. result[instance.get(this.foreignKey, { raw: true })] = null;
  141. }
  142. for (const instance of results) {
  143. result[instance.get(this.targetKey, { raw: true })] = instance;
  144. }
  145. return result;
  146. });
  147. }
  148. return Target.findOne(options);
  149. }
  150. /**
  151. * Set the associated model.
  152. *
  153. * @param {Model} sourceInstance the source instance
  154. * @param {?<Model>|string|number} [associatedInstance] An persisted instance or the primary key of an instance to associate with this. Pass `null` or `undefined` to remove the association.
  155. * @param {Object} [options={}] options passed to `this.save`
  156. * @param {boolean} [options.save=true] Skip saving this after setting the foreign key if false.
  157. *
  158. * @returns {Promise}
  159. */
  160. set(sourceInstance, associatedInstance, options = {}) {
  161. let value = associatedInstance;
  162. if (associatedInstance instanceof this.target) {
  163. value = associatedInstance[this.targetKey];
  164. }
  165. sourceInstance.set(this.foreignKey, value);
  166. if (options.save === false) return;
  167. options = Object.assign({
  168. fields: [this.foreignKey],
  169. allowNull: [this.foreignKey],
  170. association: true
  171. }, options);
  172. // passes the changed field to save, so only that field get updated.
  173. return sourceInstance.save(options);
  174. }
  175. /**
  176. * Create a new instance of the associated model and associate it with this.
  177. *
  178. * @param {Model} sourceInstance the source instance
  179. * @param {Object} [values={}] values to create associated model instance with
  180. * @param {Object} [options={}] Options passed to `target.create` and setAssociation.
  181. *
  182. * @see
  183. * {@link Model#create} for a full explanation of options
  184. *
  185. * @returns {Promise<Model>} The created target model
  186. */
  187. create(sourceInstance, values, options) {
  188. values = values || {};
  189. options = options || {};
  190. return this.target.create(values, options)
  191. .then(newAssociatedObject => sourceInstance[this.accessors.set](newAssociatedObject, options)
  192. .then(() => newAssociatedObject)
  193. );
  194. }
  195. verifyAssociationAlias(alias) {
  196. if (typeof alias === 'string') {
  197. return this.as === alias;
  198. }
  199. if (alias && alias.singular) {
  200. return this.as === alias.singular;
  201. }
  202. return !this.isAliased;
  203. }
  204. }
  205. module.exports = BelongsTo;
  206. module.exports.BelongsTo = BelongsTo;
  207. module.exports.default = BelongsTo;