has-one.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.hasOne(Project)` the getter will be `user.getProject()`.
  11. * This is almost the same as `belongsTo` with one exception - The foreign key will be defined on the target model.
  12. *
  13. * @see {@link Model.hasOne}
  14. */
  15. class HasOne extends Association {
  16. constructor(source, target, options) {
  17. super(source, target, options);
  18. this.associationType = 'HasOne';
  19. this.isSingleAssociation = true;
  20. this.foreignKeyAttribute = {};
  21. if (this.as) {
  22. this.isAliased = true;
  23. this.options.name = {
  24. singular: this.as
  25. };
  26. } else {
  27. this.as = this.target.options.name.singular;
  28. this.options.name = this.target.options.name;
  29. }
  30. if (_.isObject(this.options.foreignKey)) {
  31. this.foreignKeyAttribute = this.options.foreignKey;
  32. this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
  33. } else if (this.options.foreignKey) {
  34. this.foreignKey = this.options.foreignKey;
  35. }
  36. if (!this.foreignKey) {
  37. this.foreignKey = Utils.camelize(
  38. [
  39. Utils.singularize(this.options.as || this.source.name),
  40. this.source.primaryKeyAttribute
  41. ].join('_')
  42. );
  43. }
  44. if (
  45. this.options.sourceKey
  46. && !this.source.rawAttributes[this.options.sourceKey]
  47. ) {
  48. throw new Error(`Unknown attribute "${this.options.sourceKey}" passed as sourceKey, define this attribute on model "${this.source.name}" first`);
  49. }
  50. this.sourceKey = this.sourceKeyAttribute = this.options.sourceKey || this.source.primaryKeyAttribute;
  51. this.sourceKeyField = this.source.rawAttributes[this.sourceKey].field || this.sourceKey;
  52. this.sourceKeyIsPrimary = this.sourceKey === this.source.primaryKeyAttribute;
  53. this.associationAccessor = this.as;
  54. this.options.useHooks = options.useHooks;
  55. if (this.target.rawAttributes[this.foreignKey]) {
  56. this.identifierField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey;
  57. }
  58. // Get singular name, trying to uppercase the first letter, unless the model forbids it
  59. const singular = _.upperFirst(this.options.name.singular);
  60. this.accessors = {
  61. get: `get${singular}`,
  62. set: `set${singular}`,
  63. create: `create${singular}`
  64. };
  65. }
  66. // the id is in the target table
  67. _injectAttributes() {
  68. const newAttributes = {};
  69. newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
  70. type: this.options.keyType || this.source.rawAttributes[this.sourceKey].type,
  71. allowNull: true
  72. });
  73. if (this.options.constraints !== false) {
  74. const target = this.target.rawAttributes[this.foreignKey] || newAttributes[this.foreignKey];
  75. this.options.onDelete = this.options.onDelete || (target.allowNull ? 'SET NULL' : 'CASCADE');
  76. this.options.onUpdate = this.options.onUpdate || 'CASCADE';
  77. }
  78. Helpers.addForeignKeyConstraints(newAttributes[this.foreignKey], this.source, this.target, this.options, this.sourceKeyField);
  79. Utils.mergeDefaults(this.target.rawAttributes, newAttributes);
  80. this.target.refreshAttributes();
  81. this.identifierField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey;
  82. Helpers.checkNamingCollision(this);
  83. return this;
  84. }
  85. mixin(obj) {
  86. const methods = ['get', 'set', 'create'];
  87. Helpers.mixinMethods(this, obj, methods);
  88. }
  89. /**
  90. * Get the associated instance.
  91. *
  92. * @param {Model|Array<Model>} instances source instances
  93. * @param {Object} [options] find options
  94. * @param {string|boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false
  95. * @param {string} [options.schema] Apply a schema on the related model
  96. *
  97. * @see
  98. * {@link Model.findOne} for a full explanation of options
  99. *
  100. * @returns {Promise<Model>}
  101. */
  102. get(instances, options) {
  103. const where = {};
  104. let Target = this.target;
  105. let instance;
  106. options = Utils.cloneDeep(options);
  107. if (Object.prototype.hasOwnProperty.call(options, 'scope')) {
  108. if (!options.scope) {
  109. Target = Target.unscoped();
  110. } else {
  111. Target = Target.scope(options.scope);
  112. }
  113. }
  114. if (Object.prototype.hasOwnProperty.call(options, 'schema')) {
  115. Target = Target.schema(options.schema, options.schemaDelimiter);
  116. }
  117. if (!Array.isArray(instances)) {
  118. instance = instances;
  119. instances = undefined;
  120. }
  121. if (instances) {
  122. where[this.foreignKey] = {
  123. [Op.in]: instances.map(instance => instance.get(this.sourceKey))
  124. };
  125. } else {
  126. where[this.foreignKey] = instance.get(this.sourceKey);
  127. }
  128. if (this.scope) {
  129. Object.assign(where, this.scope);
  130. }
  131. options.where = options.where ?
  132. { [Op.and]: [where, options.where] } :
  133. where;
  134. if (instances) {
  135. return Target.findAll(options).then(results => {
  136. const result = {};
  137. for (const instance of instances) {
  138. result[instance.get(this.sourceKey, { raw: true })] = null;
  139. }
  140. for (const instance of results) {
  141. result[instance.get(this.foreignKey, { raw: true })] = instance;
  142. }
  143. return result;
  144. });
  145. }
  146. return Target.findOne(options);
  147. }
  148. /**
  149. * Set the associated model.
  150. *
  151. * @param {Model} sourceInstance the source instance
  152. * @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.
  153. * @param {Object} [options] Options passed to getAssociation and `target.save`
  154. *
  155. * @returns {Promise}
  156. */
  157. set(sourceInstance, associatedInstance, options) {
  158. let alreadyAssociated;
  159. options = Object.assign({}, options, {
  160. scope: false
  161. });
  162. return sourceInstance[this.accessors.get](options).then(oldInstance => {
  163. // TODO Use equals method once #5605 is resolved
  164. alreadyAssociated = oldInstance && associatedInstance && this.target.primaryKeyAttributes.every(attribute =>
  165. oldInstance.get(attribute, { raw: true }) === (associatedInstance.get ? associatedInstance.get(attribute, { raw: true }) : associatedInstance)
  166. );
  167. if (oldInstance && !alreadyAssociated) {
  168. oldInstance[this.foreignKey] = null;
  169. return oldInstance.save(Object.assign({}, options, {
  170. fields: [this.foreignKey],
  171. allowNull: [this.foreignKey],
  172. association: true
  173. }));
  174. }
  175. }).then(() => {
  176. if (associatedInstance && !alreadyAssociated) {
  177. if (!(associatedInstance instanceof this.target)) {
  178. const tmpInstance = {};
  179. tmpInstance[this.target.primaryKeyAttribute] = associatedInstance;
  180. associatedInstance = this.target.build(tmpInstance, {
  181. isNewRecord: false
  182. });
  183. }
  184. Object.assign(associatedInstance, this.scope);
  185. associatedInstance.set(this.foreignKey, sourceInstance.get(this.sourceKeyAttribute));
  186. return associatedInstance.save(options);
  187. }
  188. return null;
  189. });
  190. }
  191. /**
  192. * Create a new instance of the associated model and associate it with this.
  193. *
  194. * @param {Model} sourceInstance the source instance
  195. * @param {Object} [values={}] values to create associated model instance with
  196. * @param {Object} [options] Options passed to `target.create` and setAssociation.
  197. *
  198. * @see
  199. * {@link Model#create} for a full explanation of options
  200. *
  201. * @returns {Promise<Model>} The created target model
  202. */
  203. create(sourceInstance, values, options) {
  204. values = values || {};
  205. options = options || {};
  206. if (this.scope) {
  207. for (const attribute of Object.keys(this.scope)) {
  208. values[attribute] = this.scope[attribute];
  209. if (options.fields) {
  210. options.fields.push(attribute);
  211. }
  212. }
  213. }
  214. values[this.foreignKey] = sourceInstance.get(this.sourceKeyAttribute);
  215. if (options.fields) {
  216. options.fields.push(this.foreignKey);
  217. }
  218. return this.target.create(values, options);
  219. }
  220. verifyAssociationAlias(alias) {
  221. if (typeof alias === 'string') {
  222. return this.as === alias;
  223. }
  224. if (alias && alias.singular) {
  225. return this.as === alias.singular;
  226. }
  227. return !this.isAliased;
  228. }
  229. }
  230. module.exports = HasOne;