belongs-to.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  6. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  7. var __spreadValues = (a, b) => {
  8. for (var prop in b || (b = {}))
  9. if (__hasOwnProp.call(b, prop))
  10. __defNormalProp(a, prop, b[prop]);
  11. if (__getOwnPropSymbols)
  12. for (var prop of __getOwnPropSymbols(b)) {
  13. if (__propIsEnum.call(b, prop))
  14. __defNormalProp(a, prop, b[prop]);
  15. }
  16. return a;
  17. };
  18. const Utils = require("./../utils");
  19. const Helpers = require("./helpers");
  20. const _ = require("lodash");
  21. const Association = require("./base");
  22. const Op = require("../operators");
  23. class BelongsTo extends Association {
  24. constructor(source, target, options) {
  25. super(source, target, options);
  26. this.associationType = "BelongsTo";
  27. this.isSingleAssociation = true;
  28. this.foreignKeyAttribute = {};
  29. if (this.as) {
  30. this.isAliased = true;
  31. this.options.name = {
  32. singular: this.as
  33. };
  34. } else {
  35. this.as = this.target.options.name.singular;
  36. this.options.name = this.target.options.name;
  37. }
  38. if (_.isObject(this.options.foreignKey)) {
  39. this.foreignKeyAttribute = this.options.foreignKey;
  40. this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
  41. } else if (this.options.foreignKey) {
  42. this.foreignKey = this.options.foreignKey;
  43. }
  44. if (!this.foreignKey) {
  45. this.foreignKey = Utils.camelize([
  46. this.as,
  47. this.target.primaryKeyAttribute
  48. ].join("_"));
  49. }
  50. this.identifier = this.foreignKey;
  51. if (this.source.rawAttributes[this.identifier]) {
  52. this.identifierField = this.source.rawAttributes[this.identifier].field || this.identifier;
  53. }
  54. if (this.options.targetKey && !this.target.rawAttributes[this.options.targetKey]) {
  55. throw new Error(`Unknown attribute "${this.options.targetKey}" passed as targetKey, define this attribute on model "${this.target.name}" first`);
  56. }
  57. this.targetKey = this.options.targetKey || this.target.primaryKeyAttribute;
  58. this.targetKeyField = this.target.rawAttributes[this.targetKey].field || this.targetKey;
  59. this.targetKeyIsPrimary = this.targetKey === this.target.primaryKeyAttribute;
  60. this.targetIdentifier = this.targetKey;
  61. this.associationAccessor = this.as;
  62. this.options.useHooks = options.useHooks;
  63. const singular = _.upperFirst(this.options.name.singular);
  64. this.accessors = {
  65. get: `get${singular}`,
  66. set: `set${singular}`,
  67. create: `create${singular}`
  68. };
  69. }
  70. _injectAttributes() {
  71. const newAttributes = {
  72. [this.foreignKey]: __spreadValues({
  73. type: this.options.keyType || this.target.rawAttributes[this.targetKey].type,
  74. allowNull: true
  75. }, this.foreignKeyAttribute)
  76. };
  77. if (this.options.constraints !== false) {
  78. const source = this.source.rawAttributes[this.foreignKey] || newAttributes[this.foreignKey];
  79. this.options.onDelete = this.options.onDelete || (source.allowNull ? "SET NULL" : "NO ACTION");
  80. this.options.onUpdate = this.options.onUpdate || "CASCADE";
  81. }
  82. Helpers.addForeignKeyConstraints(newAttributes[this.foreignKey], this.target, this.source, this.options, this.targetKeyField);
  83. Utils.mergeDefaults(this.source.rawAttributes, newAttributes);
  84. this.source.refreshAttributes();
  85. this.identifierField = this.source.rawAttributes[this.foreignKey].field || this.foreignKey;
  86. Helpers.checkNamingCollision(this);
  87. return this;
  88. }
  89. mixin(obj) {
  90. const methods = ["get", "set", "create"];
  91. Helpers.mixinMethods(this, obj, methods);
  92. }
  93. async get(instances, options) {
  94. const where = {};
  95. let Target = this.target;
  96. let instance;
  97. options = Utils.cloneDeep(options);
  98. if (Object.prototype.hasOwnProperty.call(options, "scope")) {
  99. if (!options.scope) {
  100. Target = Target.unscoped();
  101. } else {
  102. Target = Target.scope(options.scope);
  103. }
  104. }
  105. if (Object.prototype.hasOwnProperty.call(options, "schema")) {
  106. Target = Target.schema(options.schema, options.schemaDelimiter);
  107. }
  108. if (!Array.isArray(instances)) {
  109. instance = instances;
  110. instances = void 0;
  111. }
  112. if (instances) {
  113. where[this.targetKey] = {
  114. [Op.in]: instances.map((_instance) => _instance.get(this.foreignKey))
  115. };
  116. } else {
  117. if (this.targetKeyIsPrimary && !options.where) {
  118. return Target.findByPk(instance.get(this.foreignKey), options);
  119. }
  120. where[this.targetKey] = instance.get(this.foreignKey);
  121. options.limit = null;
  122. }
  123. options.where = options.where ? { [Op.and]: [where, options.where] } : where;
  124. if (instances) {
  125. const results = await Target.findAll(options);
  126. const result = {};
  127. for (const _instance of instances) {
  128. result[_instance.get(this.foreignKey, { raw: true })] = null;
  129. }
  130. for (const _instance of results) {
  131. result[_instance.get(this.targetKey, { raw: true })] = _instance;
  132. }
  133. return result;
  134. }
  135. return Target.findOne(options);
  136. }
  137. async set(sourceInstance, associatedInstance, options = {}) {
  138. let value = associatedInstance;
  139. if (associatedInstance instanceof this.target) {
  140. value = associatedInstance[this.targetKey];
  141. }
  142. sourceInstance.set(this.foreignKey, value);
  143. if (options.save === false)
  144. return;
  145. options = __spreadValues({
  146. fields: [this.foreignKey],
  147. allowNull: [this.foreignKey],
  148. association: true
  149. }, options);
  150. return await sourceInstance.save(options);
  151. }
  152. async create(sourceInstance, values, options) {
  153. values = values || {};
  154. options = options || {};
  155. const newAssociatedObject = await this.target.create(values, options);
  156. await sourceInstance[this.accessors.set](newAssociatedObject, options);
  157. return newAssociatedObject;
  158. }
  159. verifyAssociationAlias(alias) {
  160. if (typeof alias === "string") {
  161. return this.as === alias;
  162. }
  163. if (alias && alias.singular) {
  164. return this.as === alias.singular;
  165. }
  166. return !this.isAliased;
  167. }
  168. }
  169. module.exports = BelongsTo;
  170. module.exports.BelongsTo = BelongsTo;
  171. module.exports.default = BelongsTo;
  172. //# sourceMappingURL=belongs-to.js.map