UNPKG

1.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.addFieldToIndex = exports.setIndexes = exports.getIndexes = void 0;
4require("reflect-metadata");
5const INDEXES_KEY = 'sequelize:indexes';
6/**
7 * Returns model indexes from class by restoring this
8 * information from reflect metadata
9 */
10function getIndexes(target) {
11 const { named = {}, unnamed = [] } = Reflect.getMetadata(INDEXES_KEY, target) || {};
12 return { named: Object.assign({}, named), unnamed: [...unnamed] };
13}
14exports.getIndexes = getIndexes;
15/**
16 * Sets indexes
17 */
18function setIndexes(target, indexes) {
19 Reflect.defineMetadata(INDEXES_KEY, indexes, target);
20}
21exports.setIndexes = setIndexes;
22/**
23 * Adds field to index by sequelize index and index field options,
24 * and stores this information through reflect metadata. Returns index ID.
25 */
26function addFieldToIndex(target, fieldOptions, indexOptions, indexId) {
27 const indexes = getIndexes(target);
28 const chosenId = typeof indexId !== 'undefined' ? indexId : indexOptions.name || indexes.unnamed.length;
29 const indexStore = typeof chosenId === 'string' ? indexes.named : indexes.unnamed;
30 if (!indexStore[chosenId])
31 indexStore[chosenId] = Object.assign({}, indexOptions);
32 const index = indexStore[chosenId];
33 if (!index.fields)
34 index.fields = [];
35 index.fields.push(fieldOptions);
36 setIndexes(target, indexes);
37 return chosenId;
38}
39exports.addFieldToIndex = addFieldToIndex;
40//# sourceMappingURL=index-service.js.map
\No newline at end of file