UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

228 lines 6.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Meta = void 0; class ModelMeta { model; constructor(model) { this.model = model; } /** * * @returns {string} */ table() { return this.model.toTableName(); } /** * * @returns {string} */ column(column) { return column; } /** * * @returns {`${string}.${T.Column<M>}`} */ columnReference(column) { return `\`${this.table()}\`.\`${String(column)}\``; } /** * * @returns {`${string}.${T.Column<M>}`} */ columnRef(column) { return this.columnReference(column); } /** * * @returns {string[]} */ columns() { const schemaModel = this.model.getSchemaModel(); const columns = schemaModel == null ? [] : Object.entries(schemaModel).map(([key]) => { return key; }); return columns; } /** * * @returns {boolean} */ hasColumn(name) { const schemaModel = this.model.getSchemaModel(); const columns = schemaModel == null ? [] : Object.entries(schemaModel).map(([key]) => { return key; }); return columns.includes(name); } /** * * @returns {string | undefined} */ primaryKey() { const schemaModel = this.model.getSchemaModel(); if (schemaModel == null) return undefined; return Object .entries(schemaModel) .find(([key, blueprint]) => { const attr = blueprint['_attributes']; if (Array.from(attr).includes(this.model['$constants']('PRIMARY_KEY'))) { return key; } return undefined; })?.[0]; } /** * * @returns {string[]} */ indexes() { const schemaModel = this.model.getSchemaModel(); if (schemaModel == null) return []; return Object .entries(schemaModel) .map(([key, blueprint]) => { const index = blueprint['_index']; if (index == null) return undefined; if (index === '') return `unknown_index_${key}`; return index; }) .filter(v => v != null); } /** * * @returns {string[]} */ nullable() { const schemaModel = this.model.getSchemaModel(); if (schemaModel == null) return []; return Object .entries(schemaModel) .map(([key, blueprint]) => { const attr = blueprint['_attributes']; if (Array.from(attr).includes(this.model['$constants']('NULL'))) { return key; } return undefined; }) .filter(v => v != null); } /** * * @returns {Partial<T.SchemaModel<M>> | null} */ defaults() { const schemaModel = this.model.getSchemaModel(); if (schemaModel == null) return null; const out = {}; const keyword = this.model['$constants']('DEFAULT'); for (const [key, blueprint] of Object.entries(schemaModel)) { const attr = blueprint['_attributes']; if (!attr) continue; const defaultAttr = Array.from(attr).find(str => str.includes(keyword)); if (!defaultAttr) continue; const match = defaultAttr.match(new RegExp(`${keyword}\\s+(?:'(.*?)'|(\\S+))`)); const rawValue = match?.[1] ?? match?.[2] ?? null; const value = rawValue !== null ? Number.isNaN(rawValue) ? rawValue : Number(rawValue) : null; if (value == null) continue; out[key] = value; } if (!Object.keys(out).length) return null; return out; } /** * * @param {string} column * @returns {string | undefined} */ columnTypeOf(column) { const schemaModel = this.model.getSchemaModel(); if (!schemaModel) return undefined; const entry = Object.entries(schemaModel).find(([key]) => key === column); if (!entry) return undefined; const blueprint = entry[1]; const value = blueprint['_valueType']; if (value === Number) return "number"; if (value === String) return "string"; if (value === Boolean) return "boolean"; if (value === Date) return "date"; return "unknown"; } /** * * @param {string} column * @returns {string | undefined} */ columnType(column) { const schemaModel = this.model.getSchemaModel(); if (!schemaModel) return undefined; const entry = Object.entries(schemaModel).find(([key]) => key === column); if (!entry) return undefined; const blueprint = entry[1]; return blueprint['_type']; } } /** * The 'Meta' used to get the metadata of a Model works only when a schema is added to the Model. * * @example * import { Meta } from 'tspace-mysql'; * import { User } from './Model/User'; * * const meta = Meta(User) * // --- get metadata of User --- * const table = meta.table() * const column = meta.column('id') * const columnRef = meta.columnReference('id') * const columnTypeOf = meta.columnTypeOf('id') * const columnType = meta.columnType('id') * const columns = meta.columns() * const hasColumn = meta.hasColumn('id') * const primaryKey = meta.primaryKey() * const indexes = meta.indexes() * const nullable = meta.nullable() * const defaults = meta.defaults() * * console.log({ * table, * column, * columnRef * columnTypeOf, * columnType * columns, * hasColumn, * primaryKey, * indexes, * nullable, * defaults, * }) */ const Meta = (model) => { return new ModelMeta(new model()); }; exports.Meta = Meta; exports.default = Meta; //# sourceMappingURL=Meta.js.map