UNPKG

1.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.addAttributeOptions = exports.addAttribute = exports.setAttributes = exports.getAttributes = void 0;
4const object_1 = require("../../shared/object");
5const ATTRIBUTES_KEY = 'sequelize:attributes';
6/**
7 * Returns model attributes from class by restoring this
8 * information from reflect metadata
9 */
10function getAttributes(target) {
11 const attributes = Reflect.getMetadata(ATTRIBUTES_KEY, target);
12 if (attributes) {
13 return Object.keys(attributes).reduce((copy, key) => {
14 copy[key] = Object.assign({}, attributes[key]);
15 return copy;
16 }, {});
17 }
18}
19exports.getAttributes = getAttributes;
20/**
21 * Sets attributes
22 */
23function setAttributes(target, attributes) {
24 Reflect.defineMetadata(ATTRIBUTES_KEY, Object.assign({}, attributes), target);
25}
26exports.setAttributes = setAttributes;
27/**
28 * Adds model attribute by specified property name and
29 * sequelize attribute options and stores this information
30 * through reflect metadata
31 */
32function addAttribute(target, name, options) {
33 let attributes = getAttributes(target);
34 if (!attributes) {
35 attributes = {};
36 }
37 attributes[name] = Object.assign({}, options);
38 setAttributes(target, attributes);
39}
40exports.addAttribute = addAttribute;
41/**
42 * Adds attribute options for specific attribute
43 */
44function addAttributeOptions(target, propertyName, options) {
45 const attributes = getAttributes(target);
46 if (!attributes || !attributes[propertyName]) {
47 throw new Error(`@Column annotation is missing for "${propertyName}" of class "${target.constructor.name}"` +
48 ` or annotation order is wrong.`);
49 }
50 attributes[propertyName] = (0, object_1.deepAssign)(attributes[propertyName], options);
51 setAttributes(target, attributes);
52}
53exports.addAttributeOptions = addAttributeOptions;
54//# sourceMappingURL=attribute-service.js.map
\No newline at end of file