UNPKG

2.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.BelongsToManyAssociation = void 0;
4const base_association_1 = require("../shared/base-association");
5const model_not_initialized_error_1 = require("../../model/shared/model-not-initialized-error");
6const foreign_key_service_1 = require("../foreign-key/foreign-key-service");
7const association_1 = require("../shared/association");
8class BelongsToManyAssociation extends base_association_1.BaseAssociation {
9 constructor(associatedClassGetter, options) {
10 super(associatedClassGetter, options);
11 this.options = options;
12 }
13 getAssociation() {
14 return association_1.Association.BelongsToMany;
15 }
16 getSequelizeOptions(model, sequelize) {
17 const options = Object.assign({}, this.options);
18 const associatedClass = this.getAssociatedClass();
19 const throughOptions = this.getThroughOptions(sequelize);
20 const throughModel = typeof throughOptions === 'object' && typeof throughOptions.model !== 'string'
21 ? throughOptions.model
22 : undefined;
23 options.through = throughOptions;
24 options.foreignKey = (0, foreign_key_service_1.getForeignKeyOptions)(model, throughModel, this.options.foreignKey);
25 options.otherKey = (0, foreign_key_service_1.getForeignKeyOptions)(associatedClass, throughModel, this.options.otherKey);
26 return options;
27 }
28 getThroughOptions(sequelize) {
29 const through = this.options.through;
30 const throughModel = typeof through === 'object' ? through.model : through;
31 const throughOptions = typeof through === 'object' ? Object.assign({}, through) : {};
32 if (typeof throughModel === 'function') {
33 const throughModelClass = sequelize.model(throughModel());
34 if (!throughModelClass.isInitialized) {
35 throw new model_not_initialized_error_1.ModelNotInitializedError(throughModelClass, 'Association cannot be resolved.');
36 }
37 throughOptions.model = throughModelClass;
38 }
39 else {
40 return throughModel;
41 }
42 return throughOptions;
43 }
44}
45exports.BelongsToManyAssociation = BelongsToManyAssociation;
46//# sourceMappingURL=belongs-to-many-association.js.map
\No newline at end of file