has-many.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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-many association
  9. *
  10. * In the API reference below, add the name of the association to the method, e.g. for `User.hasMany(Project)` the getter will be `user.getProjects()`.
  11. * If the association is aliased, use the alias instead, e.g. `User.hasMany(Project, { as: 'jobs' })` will be `user.getJobs()`.
  12. *
  13. * @see {@link Model.hasMany}
  14. */
  15. class HasMany extends Association {
  16. constructor(source, target, options) {
  17. super(source, target, options);
  18. this.associationType = 'HasMany';
  19. this.targetAssociation = null;
  20. this.sequelize = source.sequelize;
  21. this.isMultiAssociation = true;
  22. this.foreignKeyAttribute = {};
  23. if (this.options.through) {
  24. throw new Error('N:M associations are not supported with hasMany. Use belongsToMany instead');
  25. }
  26. /*
  27. * If self association, this is the target association
  28. */
  29. if (this.isSelfAssociation) {
  30. this.targetAssociation = this;
  31. }
  32. if (this.as) {
  33. this.isAliased = true;
  34. if (_.isPlainObject(this.as)) {
  35. this.options.name = this.as;
  36. this.as = this.as.plural;
  37. } else {
  38. this.options.name = {
  39. plural: this.as,
  40. singular: Utils.singularize(this.as)
  41. };
  42. }
  43. } else {
  44. this.as = this.target.options.name.plural;
  45. this.options.name = this.target.options.name;
  46. }
  47. /*
  48. * Foreign key setup
  49. */
  50. if (_.isObject(this.options.foreignKey)) {
  51. this.foreignKeyAttribute = this.options.foreignKey;
  52. this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
  53. } else if (this.options.foreignKey) {
  54. this.foreignKey = this.options.foreignKey;
  55. }
  56. if (!this.foreignKey) {
  57. this.foreignKey = Utils.camelize(
  58. [
  59. this.source.options.name.singular,
  60. this.source.primaryKeyAttribute
  61. ].join('_')
  62. );
  63. }
  64. if (this.target.rawAttributes[this.foreignKey]) {
  65. this.identifierField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey;
  66. this.foreignKeyField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey;
  67. }
  68. /*
  69. * Source key setup
  70. */
  71. this.sourceKey = this.options.sourceKey || this.source.primaryKeyAttribute;
  72. if (this.source.rawAttributes[this.sourceKey]) {
  73. this.sourceKeyAttribute = this.sourceKey;
  74. this.sourceKeyField = this.source.rawAttributes[this.sourceKey].field || this.sourceKey;
  75. } else {
  76. this.sourceKeyAttribute = this.source.primaryKeyAttribute;
  77. this.sourceKeyField = this.source.primaryKeyField;
  78. }
  79. // Get singular and plural names
  80. // try to uppercase the first letter, unless the model forbids it
  81. const plural = _.upperFirst(this.options.name.plural);
  82. const singular = _.upperFirst(this.options.name.singular);
  83. this.associationAccessor = this.as;
  84. this.accessors = {
  85. get: `get${plural}`,
  86. set: `set${plural}`,
  87. addMultiple: `add${plural}`,
  88. add: `add${singular}`,
  89. create: `create${singular}`,
  90. remove: `remove${singular}`,
  91. removeMultiple: `remove${plural}`,
  92. hasSingle: `has${singular}`,
  93. hasAll: `has${plural}`,
  94. count: `count${plural}`
  95. };
  96. }
  97. // the id is in the target table
  98. // or in an extra table which connects two tables
  99. _injectAttributes() {
  100. const newAttributes = {
  101. [this.foreignKey]: {
  102. type: this.options.keyType || this.source.rawAttributes[this.sourceKeyAttribute].type,
  103. allowNull: true,
  104. ...this.foreignKeyAttribute
  105. }
  106. };
  107. // Create a new options object for use with addForeignKeyConstraints, to avoid polluting this.options in case it is later used for a n:m
  108. const constraintOptions = { ...this.options };
  109. if (this.options.constraints !== false) {
  110. const target = this.target.rawAttributes[this.foreignKey] || newAttributes[this.foreignKey];
  111. constraintOptions.onDelete = constraintOptions.onDelete || (target.allowNull ? 'SET NULL' : 'CASCADE');
  112. constraintOptions.onUpdate = constraintOptions.onUpdate || 'CASCADE';
  113. }
  114. Helpers.addForeignKeyConstraints(newAttributes[this.foreignKey], this.source, this.target, constraintOptions, this.sourceKeyField);
  115. Utils.mergeDefaults(this.target.rawAttributes, newAttributes);
  116. this.target.refreshAttributes();
  117. this.source.refreshAttributes();
  118. this.identifierField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey;
  119. this.foreignKeyField = this.target.rawAttributes[this.foreignKey].field || this.foreignKey;
  120. this.sourceKeyField = this.source.rawAttributes[this.sourceKey].field || this.sourceKey;
  121. Helpers.checkNamingCollision(this);
  122. return this;
  123. }
  124. mixin(obj) {
  125. const methods = ['get', 'count', 'hasSingle', 'hasAll', 'set', 'add', 'addMultiple', 'remove', 'removeMultiple', 'create'];
  126. const aliases = {
  127. hasSingle: 'has',
  128. hasAll: 'has',
  129. addMultiple: 'add',
  130. removeMultiple: 'remove'
  131. };
  132. Helpers.mixinMethods(this, obj, methods, aliases);
  133. }
  134. /**
  135. * Get everything currently associated with this, using an optional where clause.
  136. *
  137. * @param {Model|Array<Model>} instances source instances
  138. * @param {object} [options] find options
  139. * @param {object} [options.where] An optional where clause to limit the associated models
  140. * @param {string|boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false
  141. * @param {string} [options.schema] Apply a schema on the related model
  142. *
  143. * @see
  144. * {@link Model.findAll} for a full explanation of options
  145. *
  146. * @returns {Promise<Array<Model>>}
  147. */
  148. async get(instances, options = {}) {
  149. const where = {};
  150. let Model = this.target;
  151. let instance;
  152. let values;
  153. if (!Array.isArray(instances)) {
  154. instance = instances;
  155. instances = undefined;
  156. }
  157. options = { ...options };
  158. if (this.scope) {
  159. Object.assign(where, this.scope);
  160. }
  161. if (instances) {
  162. values = instances.map(_instance => _instance.get(this.sourceKey, { raw: true }));
  163. if (options.limit && instances.length > 1) {
  164. options.groupedLimit = {
  165. limit: options.limit,
  166. on: this, // association
  167. values
  168. };
  169. delete options.limit;
  170. } else {
  171. where[this.foreignKey] = {
  172. [Op.in]: values
  173. };
  174. delete options.groupedLimit;
  175. }
  176. } else {
  177. where[this.foreignKey] = instance.get(this.sourceKey, { raw: true });
  178. }
  179. options.where = options.where ?
  180. { [Op.and]: [where, options.where] } :
  181. where;
  182. if (Object.prototype.hasOwnProperty.call(options, 'scope')) {
  183. if (!options.scope) {
  184. Model = Model.unscoped();
  185. } else {
  186. Model = Model.scope(options.scope);
  187. }
  188. }
  189. if (Object.prototype.hasOwnProperty.call(options, 'schema')) {
  190. Model = Model.schema(options.schema, options.schemaDelimiter);
  191. }
  192. const results = await Model.findAll(options);
  193. if (instance) return results;
  194. const result = {};
  195. for (const _instance of instances) {
  196. result[_instance.get(this.sourceKey, { raw: true })] = [];
  197. }
  198. for (const _instance of results) {
  199. result[_instance.get(this.foreignKey, { raw: true })].push(_instance);
  200. }
  201. return result;
  202. }
  203. /**
  204. * Count everything currently associated with this, using an optional where clause.
  205. *
  206. * @param {Model} instance the source instance
  207. * @param {object} [options] find & count options
  208. * @param {object} [options.where] An optional where clause to limit the associated models
  209. * @param {string|boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false
  210. *
  211. * @returns {Promise<number>}
  212. */
  213. async count(instance, options) {
  214. options = Utils.cloneDeep(options);
  215. options.attributes = [
  216. [
  217. this.sequelize.fn(
  218. 'COUNT',
  219. this.sequelize.col(`${this.target.name}.${this.target.primaryKeyField}`)
  220. ),
  221. 'count'
  222. ]
  223. ];
  224. options.raw = true;
  225. options.plain = true;
  226. const result = await this.get(instance, options);
  227. return parseInt(result.count, 10);
  228. }
  229. /**
  230. * Check if one or more rows are associated with `this`.
  231. *
  232. * @param {Model} sourceInstance the source instance
  233. * @param {Model|Model[]|string[]|string|number[]|number} [targetInstances] Can be an array of instances or their primary keys
  234. * @param {object} [options] Options passed to getAssociations
  235. *
  236. * @returns {Promise}
  237. */
  238. async has(sourceInstance, targetInstances, options) {
  239. const where = {};
  240. if (!Array.isArray(targetInstances)) {
  241. targetInstances = [targetInstances];
  242. }
  243. options = {
  244. ...options,
  245. scope: false,
  246. attributes: [this.target.primaryKeyAttribute],
  247. raw: true
  248. };
  249. where[Op.or] = targetInstances.map(instance => {
  250. if (instance instanceof this.target) {
  251. return instance.where();
  252. }
  253. return {
  254. [this.target.primaryKeyAttribute]: instance
  255. };
  256. });
  257. options.where = {
  258. [Op.and]: [
  259. where,
  260. options.where
  261. ]
  262. };
  263. const associatedObjects = await this.get(sourceInstance, options);
  264. return associatedObjects.length === targetInstances.length;
  265. }
  266. /**
  267. * Set the associated models by passing an array of persisted instances or their primary keys. Everything that is not in the passed array will be un-associated
  268. *
  269. * @param {Model} sourceInstance source instance to associate new instances with
  270. * @param {Model|Model[]|string[]|string|number[]|number} [targetInstances] An array of persisted instances or primary key of instances to associate with this. Pass `null` or `undefined` to remove all associations.
  271. * @param {object} [options] Options passed to `target.findAll` and `update`.
  272. * @param {object} [options.validate] Run validation for the join model
  273. *
  274. * @returns {Promise}
  275. */
  276. async set(sourceInstance, targetInstances, options) {
  277. if (targetInstances === null) {
  278. targetInstances = [];
  279. } else {
  280. targetInstances = this.toInstanceArray(targetInstances);
  281. }
  282. const oldAssociations = await this.get(sourceInstance, { ...options, scope: false, raw: true });
  283. const promises = [];
  284. const obsoleteAssociations = oldAssociations.filter(old =>
  285. !targetInstances.find(obj =>
  286. obj[this.target.primaryKeyAttribute] === old[this.target.primaryKeyAttribute]
  287. )
  288. );
  289. const unassociatedObjects = targetInstances.filter(obj =>
  290. !oldAssociations.find(old =>
  291. obj[this.target.primaryKeyAttribute] === old[this.target.primaryKeyAttribute]
  292. )
  293. );
  294. let updateWhere;
  295. let update;
  296. if (obsoleteAssociations.length > 0) {
  297. update = {};
  298. update[this.foreignKey] = null;
  299. updateWhere = {
  300. [this.target.primaryKeyAttribute]: obsoleteAssociations.map(associatedObject =>
  301. associatedObject[this.target.primaryKeyAttribute]
  302. )
  303. };
  304. promises.push(this.target.unscoped().update(
  305. update,
  306. {
  307. ...options,
  308. where: updateWhere
  309. }
  310. ));
  311. }
  312. if (unassociatedObjects.length > 0) {
  313. updateWhere = {};
  314. update = {};
  315. update[this.foreignKey] = sourceInstance.get(this.sourceKey);
  316. Object.assign(update, this.scope);
  317. updateWhere[this.target.primaryKeyAttribute] = unassociatedObjects.map(unassociatedObject =>
  318. unassociatedObject[this.target.primaryKeyAttribute]
  319. );
  320. promises.push(this.target.unscoped().update(
  321. update,
  322. {
  323. ...options,
  324. where: updateWhere
  325. }
  326. ));
  327. }
  328. await Promise.all(promises);
  329. return sourceInstance;
  330. }
  331. /**
  332. * Associate one or more target rows with `this`. This method accepts a Model / string / number to associate a single row,
  333. * or a mixed array of Model / string / numbers to associate multiple rows.
  334. *
  335. * @param {Model} sourceInstance the source instance
  336. * @param {Model|Model[]|string[]|string|number[]|number} [targetInstances] A single instance or primary key, or a mixed array of persisted instances or primary keys
  337. * @param {object} [options] Options passed to `target.update`.
  338. *
  339. * @returns {Promise}
  340. */
  341. async add(sourceInstance, targetInstances, options = {}) {
  342. if (!targetInstances) return Promise.resolve();
  343. targetInstances = this.toInstanceArray(targetInstances);
  344. const update = {
  345. [this.foreignKey]: sourceInstance.get(this.sourceKey),
  346. ...this.scope
  347. };
  348. const where = {
  349. [this.target.primaryKeyAttribute]: targetInstances.map(unassociatedObject =>
  350. unassociatedObject.get(this.target.primaryKeyAttribute)
  351. )
  352. };
  353. await this.target.unscoped().update(update, { ...options, where });
  354. return sourceInstance;
  355. }
  356. /**
  357. * Un-associate one or several target rows.
  358. *
  359. * @param {Model} sourceInstance instance to un associate instances with
  360. * @param {Model|Model[]|string|string[]|number|number[]} [targetInstances] Can be an Instance or its primary key, or a mixed array of instances and primary keys
  361. * @param {object} [options] Options passed to `target.update`
  362. *
  363. * @returns {Promise}
  364. */
  365. async remove(sourceInstance, targetInstances, options = {}) {
  366. const update = {
  367. [this.foreignKey]: null
  368. };
  369. targetInstances = this.toInstanceArray(targetInstances);
  370. const where = {
  371. [this.foreignKey]: sourceInstance.get(this.sourceKey),
  372. [this.target.primaryKeyAttribute]: targetInstances.map(targetInstance =>
  373. targetInstance.get(this.target.primaryKeyAttribute)
  374. )
  375. };
  376. await this.target.unscoped().update(update, { ...options, where });
  377. return this;
  378. }
  379. /**
  380. * Create a new instance of the associated model and associate it with this.
  381. *
  382. * @param {Model} sourceInstance source instance
  383. * @param {object} [values] values for target model instance
  384. * @param {object} [options] Options passed to `target.create`
  385. *
  386. * @returns {Promise}
  387. */
  388. async create(sourceInstance, values, options = {}) {
  389. if (Array.isArray(options)) {
  390. options = {
  391. fields: options
  392. };
  393. }
  394. if (values === undefined) {
  395. values = {};
  396. }
  397. if (this.scope) {
  398. for (const attribute of Object.keys(this.scope)) {
  399. values[attribute] = this.scope[attribute];
  400. if (options.fields) options.fields.push(attribute);
  401. }
  402. }
  403. values[this.foreignKey] = sourceInstance.get(this.sourceKey);
  404. if (options.fields) options.fields.push(this.foreignKey);
  405. return await this.target.create(values, options);
  406. }
  407. verifyAssociationAlias(alias) {
  408. if (typeof alias === 'string') {
  409. return this.as === alias;
  410. }
  411. if (alias && alias.plural) {
  412. return this.as === alias.plural;
  413. }
  414. return !this.isAliased;
  415. }
  416. }
  417. module.exports = HasMany;
  418. module.exports.HasMany = HasMany;
  419. module.exports.default = HasMany;