typescript-mysql-model
Version:
{ "version": "1.2.46", "name": "typescript-mysql-model", "description": "", "main": "index.js", "types": "index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url":
57 lines • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class SchemaOperator {
/**
* Return a copy of the object without any primary keys
*/
removePrimaryKeys(tableName, data) {
const filtered = JSON.parse(JSON.stringify(data)); // copy
this.getPrimaryKeyNames(tableName).forEach(pk => delete filtered[pk]);
return filtered;
}
retainPrimaryKeys(tableName, data) {
const keyData = {};
this.getPrimaryKeyNames(tableName).forEach(key => keyData[key] = data[key]);
return keyData;
}
getPkCols(tableName) {
if (!tableName) {
return [];
}
const cols = [];
const table = this.definition.tables[tableName];
for (const key in table) {
if (table[key].isPrimary) {
cols.push(table[key]);
}
}
return cols;
}
/**
* return a new object where with only attributes that have
* a corresponding column for given table
* @param table
* @param data
*/
stripNoneBelonging(tableName, data) {
const table = this.definition.tables[tableName];
const copy = {};
Object.keys(data).filter(key => !!table[key]).forEach(key => {
copy[key] = data[key];
});
return copy;
}
/**
* List the names of all primary keys in a table
* @param tableName
*/
getPrimaryKeyNames(tableName) {
const cols = this.getPkCols(tableName);
if (!cols || cols.length === 0) {
return [];
}
return cols.map(col => col.field);
}
}
exports.SchemaOperator = SchemaOperator;
//# sourceMappingURL=schema-operator.js.map