UNPKG

lambdaorm-base

Version:
132 lines 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaHelper = void 0; const { v4: uuidv4 } = require('uuid'); class SchemaHelper { // eslint-disable-next-line no-useless-constructor constructor(str) { this.str = str; this.DEFAULT_TYPE = 'string'; this.DEFAULT_LENGTH = 80; } newId() { return uuidv4(); } equalName(name1, name2) { if (name1 === undefined && name2 === undefined) return true; if (name1 === undefined || name2 === undefined) return false; return name1.toLowerCase() === name2.toLowerCase(); } entityName(name) { let entityName = this.str.replace(name, '"', '').replace('\'', '').replace('`', ''); entityName = this.str.plural(entityName); return this.str.notation(entityName, 'pascal'); } propertyName(name) { return this.str.notation(name, 'camel'); } refPropertyName(entityName, propertyName) { return this.str.singular(this.str.notation(entityName, 'camel')) + this.str.capitalize(propertyName); } capitalize(name) { return this.str.capitalize(name); } indexName(name) { return this.str.notation(name, 'camel'); } relationName(name) { return this.str.notation(name, 'camel'); } getPk(objType) { const uniques = objType.properties.filter(p => { var _a, _b; return ((_a = p.type) === null || _a === void 0 ? void 0 : _a.unique) === true || ((_b = p.type) === null || _b === void 0 ? void 0 : _b.onParentDistinctUnique) === true; }); return this.getKey(uniques); } getPkName(objType) { const pk = this.getPk(objType); return pk ? pk.name : '_id'; } getFk(objType) { const uniques = objType.properties.filter(p => { var _a; return ((_a = p.type) === null || _a === void 0 ? void 0 : _a.onParentDistinctUnique) === true; }); const result = this.getKey(uniques); return result; } getKey(uniques) { if (uniques.length === 1) { return uniques[0]; } else if (uniques.length > 1) { const id = uniques.find(p => ['id', 'code', 'key', 'name'].includes(p.name.toLowerCase())); if (id) { return id; } const idNumber = uniques.find(p => { var _a, _b; return ((_a = p.type) === null || _a === void 0 ? void 0 : _a.primitive) === 'number' || ((_b = p.type) === null || _b === void 0 ? void 0 : _b.primitive) === 'integer'; }); if (idNumber) { return idNumber; } return uniques[0]; } else { return undefined; } } type(type, length) { if (type === undefined) return undefined; return type === 'string' && length === Number.MAX_SAFE_INTEGER ? 'text' : type === this.DEFAULT_TYPE ? undefined : type; } length(length) { return length ? length === this.DEFAULT_LENGTH ? undefined : length : undefined; } lengthFromType(type) { if (type.primitive !== 'string') { return undefined; } if (!type.maxLen) { return this.DEFAULT_LENGTH; } if (type.maxLen < 8) { if (type.maxLen === type.minLen) { return type.maxLen; } return Math.round(type.maxLen * 1.3); } const length = Math.round(type.maxLen * 1.3); if (length < 16) { return 16; } else if (length < 32) { return 32; } else if (length < 50) { return 50; } else if (length < this.DEFAULT_LENGTH) { return undefined; } else if (length < 128) { return 128; } else if (length < 256) { return 256; } else if (length < 512) { return 512; } else if (length < 1024) { return 1024; } else if (length < 2048) { return 2048; } else if (length < 4096) { return 4096; } else { return Number.MAX_SAFE_INTEGER; } } } exports.SchemaHelper = SchemaHelper; //# sourceMappingURL=schemaHelper.js.map