UNPKG

1.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3class SchemaOperator {
4 /**
5 * Return a copy of the object without any primary keys
6 */
7 removePrimaryKeys(tableName, data) {
8 const filtered = JSON.parse(JSON.stringify(data)); // copy
9 this.getPrimaryKeyNames(tableName).forEach(pk => delete filtered[pk]);
10 return filtered;
11 }
12 retainPrimaryKeys(tableName, data) {
13 const keyData = {};
14 this.getPrimaryKeyNames(tableName).forEach(key => keyData[key] = data[key]);
15 return keyData;
16 }
17 getPkCols(tableName) {
18 if (!tableName) {
19 return [];
20 }
21 const cols = [];
22 const table = this.definition.tables[tableName];
23 for (const key in table) {
24 if (table[key].isPrimary) {
25 cols.push(table[key]);
26 }
27 }
28 return cols;
29 }
30 /**
31 * return a new object where with only attributes that have
32 * a corresponding column for given table
33 * @param table
34 * @param data
35 */
36 stripNoneBelonging(tableName, data) {
37 const table = this.definition.tables[tableName];
38 const copy = {};
39 Object.keys(data).filter(key => !!table[key]).forEach(key => {
40 copy[key] = data[key];
41 });
42 return copy;
43 }
44 /**
45 * List the names of all primary keys in a table
46 * @param tableName
47 */
48 getPrimaryKeyNames(tableName) {
49 const cols = this.getPkCols(tableName);
50 if (!cols || cols.length === 0) {
51 return [];
52 }
53 return cols.map(col => col.field);
54 }
55}
56exports.SchemaOperator = SchemaOperator;
57//# sourceMappingURL=schema-operator.js.map
\No newline at end of file