UNPKG

4.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Sequelize = void 0;
4const sequelize_1 = require("sequelize");
5const model_not_initialized_error_1 = require("../../model/shared/model-not-initialized-error");
6const sequelize_service_1 = require("./sequelize-service");
7const model_service_1 = require("../../model/shared/model-service");
8const scope_service_1 = require("../../scopes/scope-service");
9const hooks_service_1 = require("../../hooks/shared/hooks-service");
10const association_service_1 = require("../../associations/shared/association-service");
11const attribute_service_1 = require("../../model/column/attribute-service");
12const index_service_1 = require("../../model/index/index-service");
13class Sequelize extends sequelize_1.Sequelize {
14 constructor(...args) {
15 const { preparedArgs, options } = (0, sequelize_service_1.prepareArgs)(...args);
16 super(...preparedArgs);
17 if (options) {
18 this.repositoryMode = !!options.repositoryMode;
19 if (options.models)
20 this.addModels(options.models);
21 if (options.modelPaths)
22 this.addModels(options.modelPaths);
23 }
24 else {
25 this.repositoryMode = false;
26 }
27 }
28 model(model) {
29 if (typeof model !== 'string') {
30 return super.model((0, model_service_1.getModelName)(model.prototype));
31 }
32 return super.model(model);
33 }
34 addModels(arg, modelMatch) {
35 const defaultModelMatch = (filename, member) => filename === member;
36 const models = (0, sequelize_service_1.getModels)(arg, modelMatch || this.options.modelMatch || defaultModelMatch);
37 const definedModels = this.defineModels(models);
38 this.associateModels(definedModels);
39 (0, scope_service_1.resolveScopes)(definedModels);
40 (0, hooks_service_1.installHooks)(definedModels);
41 }
42 getRepository(modelClass) {
43 return this.model(modelClass);
44 }
45 associateModels(models) {
46 models.forEach((model) => {
47 const associations = (0, association_service_1.getAssociations)(model.prototype);
48 if (!associations)
49 return;
50 associations.forEach((association) => {
51 const options = association.getSequelizeOptions(model, this);
52 const associatedClass = this.model(association.getAssociatedClass());
53 if (!associatedClass.isInitialized) {
54 throw new model_not_initialized_error_1.ModelNotInitializedError(associatedClass, `Association between ${associatedClass.name} and ${model.name} cannot be resolved.`);
55 }
56 model[association.getAssociation()](associatedClass, options);
57 });
58 });
59 }
60 defineModels(models) {
61 return models.map((model) => {
62 const modelName = (0, model_service_1.getModelName)(model.prototype);
63 const attributes = (0, attribute_service_1.getAttributes)(model.prototype);
64 const indexes = (0, index_service_1.getIndexes)(model.prototype);
65 const modelOptions = (0, model_service_1.getOptions)(model.prototype);
66 if (!modelOptions)
67 throw new Error(`@Table annotation is missing on class "${model['name']}"`);
68 const indexArray = Object.keys(indexes.named)
69 .map((key) => indexes.named[key])
70 .concat(indexes.unnamed);
71 const initOptions = Object.assign(Object.assign(Object.assign({}, (indexArray.length > 0 && { indexes: indexArray })), modelOptions), { modelName, sequelize: this });
72 const definedModel = this.repositoryMode ? this.createRepositoryModel(model) : model;
73 definedModel.initialize(attributes, initOptions);
74 return definedModel;
75 });
76 }
77 createRepositoryModel(modelClass) {
78 return class extends modelClass {
79 };
80 }
81}
82exports.Sequelize = Sequelize;
83//# sourceMappingURL=sequelize.js.map
\No newline at end of file