mixin.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. const _ = require("lodash");
  3. const HasOne = require("./has-one");
  4. const HasMany = require("./has-many");
  5. const BelongsToMany = require("./belongs-to-many");
  6. const BelongsTo = require("./belongs-to");
  7. function isModel(model, sequelize) {
  8. return model && model.prototype && model.prototype instanceof sequelize.Sequelize.Model;
  9. }
  10. const Mixin = {
  11. hasMany(target, options = {}) {
  12. if (!isModel(target, this.sequelize)) {
  13. throw new Error(`${this.name}.hasMany called with something that's not a subclass of Sequelize.Model`);
  14. }
  15. const source = this;
  16. options.hooks = options.hooks === void 0 ? false : Boolean(options.hooks);
  17. options.useHooks = options.hooks;
  18. Object.assign(options, _.omit(source.options, ["hooks"]));
  19. if (options.useHooks) {
  20. this.runHooks("beforeAssociate", { source, target, type: HasMany }, options);
  21. }
  22. const association = new HasMany(source, target, options);
  23. source.associations[association.associationAccessor] = association;
  24. association._injectAttributes();
  25. association.mixin(source.prototype);
  26. if (options.useHooks) {
  27. this.runHooks("afterAssociate", { source, target, type: HasMany, association }, options);
  28. }
  29. return association;
  30. },
  31. belongsToMany(target, options = {}) {
  32. if (!isModel(target, this.sequelize)) {
  33. throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`);
  34. }
  35. const source = this;
  36. options.hooks = options.hooks === void 0 ? false : Boolean(options.hooks);
  37. options.useHooks = options.hooks;
  38. options.timestamps = options.timestamps === void 0 ? this.sequelize.options.timestamps : options.timestamps;
  39. Object.assign(options, _.omit(source.options, ["hooks", "timestamps", "scopes", "defaultScope"]));
  40. if (options.useHooks) {
  41. this.runHooks("beforeAssociate", { source, target, type: BelongsToMany }, options);
  42. }
  43. const association = new BelongsToMany(source, target, options);
  44. source.associations[association.associationAccessor] = association;
  45. association._injectAttributes();
  46. association.mixin(source.prototype);
  47. if (options.useHooks) {
  48. this.runHooks("afterAssociate", { source, target, type: BelongsToMany, association }, options);
  49. }
  50. return association;
  51. },
  52. getAssociations(target) {
  53. return Object.values(this.associations).filter((association) => association.target.name === target.name);
  54. },
  55. getAssociationForAlias(target, alias) {
  56. return this.getAssociations(target).find((association) => association.verifyAssociationAlias(alias)) || null;
  57. }
  58. };
  59. function singleLinked(Type) {
  60. return function(target, options = {}) {
  61. const source = this;
  62. if (!isModel(target, source.sequelize)) {
  63. throw new Error(`${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model`);
  64. }
  65. options.hooks = options.hooks === void 0 ? false : Boolean(options.hooks);
  66. options.useHooks = options.hooks;
  67. if (options.useHooks) {
  68. source.runHooks("beforeAssociate", { source, target, type: Type }, options);
  69. }
  70. const association = new Type(source, target, Object.assign(options, source.options));
  71. source.associations[association.associationAccessor] = association;
  72. association._injectAttributes();
  73. association.mixin(source.prototype);
  74. if (options.useHooks) {
  75. source.runHooks("afterAssociate", { source, target, type: Type, association }, options);
  76. }
  77. return association;
  78. };
  79. }
  80. Mixin.hasOne = singleLinked(HasOne);
  81. Mixin.belongsTo = singleLinked(BelongsTo);
  82. module.exports = Mixin;
  83. module.exports.Mixin = Mixin;
  84. module.exports.default = Mixin;
  85. //# sourceMappingURL=mixin.js.map