UNPKG

3.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.resolveModelGetter = exports.getSequelizeTypeByDesignType = exports.addOptions = exports.setOptions = exports.getOptions = exports.getModelName = exports.setModelName = void 0;
4const model_1 = require("../model/model");
5const data_type_service_1 = require("../../sequelize/data-type/data-type-service");
6const MODEL_NAME_KEY = 'sequelize:modelName';
7const OPTIONS_KEY = 'sequelize:options';
8/**
9 * Sets model name from class by storing this
10 * information through reflect metadata
11 */
12function setModelName(target, modelName) {
13 Reflect.defineMetadata(MODEL_NAME_KEY, modelName, target);
14}
15exports.setModelName = setModelName;
16/**
17 * Returns model name from class by restoring this
18 * information from reflect metadata
19 */
20function getModelName(target) {
21 return Reflect.getMetadata(MODEL_NAME_KEY, target);
22}
23exports.getModelName = getModelName;
24/**
25 * Returns sequelize define options from class prototype
26 * by restoring this information from reflect metadata
27 */
28function getOptions(target) {
29 const options = Reflect.getMetadata(OPTIONS_KEY, target);
30 if (options) {
31 return Object.assign({}, options);
32 }
33}
34exports.getOptions = getOptions;
35/**
36 * Sets seuqlize define options to class prototype
37 */
38function setOptions(target, options) {
39 Reflect.defineMetadata(OPTIONS_KEY, Object.assign({}, options), target);
40}
41exports.setOptions = setOptions;
42/**
43 * Adds options be assigning new options to old one
44 */
45function addOptions(target, options) {
46 let _options = getOptions(target);
47 if (!_options) {
48 _options = {};
49 }
50 setOptions(target, Object.assign(Object.assign(Object.assign({}, _options), options), { validate: Object.assign(Object.assign({}, (_options.validate || {})), (options.validate || {})) }));
51}
52exports.addOptions = addOptions;
53/**
54 * Maps design types to sequelize data types;
55 * @throws if design type cannot be automatically mapped to
56 * a sequelize data type
57 */
58function getSequelizeTypeByDesignType(target, propertyName) {
59 const type = Reflect.getMetadata('design:type', target, propertyName);
60 const dataType = (0, data_type_service_1.inferDataType)(type);
61 if (dataType) {
62 return dataType;
63 }
64 throw new Error(`Specified type of property '${propertyName}'
65 cannot be automatically resolved to a sequelize data type. Please
66 define the data type manually`);
67}
68exports.getSequelizeTypeByDesignType = getSequelizeTypeByDesignType;
69/**
70 * Resolves all model getters of specified options object
71 * recursively.
72 * So that {model: () => Person} will be converted to
73 * {model: Person}
74 */
75function resolveModelGetter(options) {
76 const maybeModelGetter = (value) => typeof value === 'function' && value.length === 0;
77 const isModel = (value) => value && value.prototype && value.prototype instanceof model_1.Model;
78 const isOptionObjectOrArray = (value) => value && typeof value === 'object';
79 return Object.keys(options).reduce((acc, key) => {
80 const value = options[key];
81 if (maybeModelGetter(value)) {
82 const maybeModel = value();
83 if (isModel(maybeModel)) {
84 acc[key] = maybeModel;
85 }
86 }
87 else if (isOptionObjectOrArray(value)) {
88 acc[key] = resolveModelGetter(value);
89 }
90 return acc;
91 }, Array.isArray(options) ? [...options] : Object.assign({}, options));
92}
93exports.resolveModelGetter = resolveModelGetter;
94//# sourceMappingURL=model-service.js.map
\No newline at end of file