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.

154 lines (153 loc) 5.78 kB
import { type T, Model } from '..'; declare class ModelMeta<M extends Model> { private model; constructor(model: M); /** * Get the table name of the current model. * * @returns {string} The table name. */ table(): string; /** * Get a column key from the model schema. * * @param {T.ColumnKeys<M>} column - The column key. * @returns {T.ColumnKeys<M>} The validated column key. */ column(column: T.ColumnKeys<M>): T.ColumnKeys<M>; /** * Build a fully qualified column reference with an optional alias. * * @param {T.ColumnKeys<M>} column - The column key. * @param {{ alias?: string | null }} [options] - Optional alias for the table. * @returns {`${string}.${T.ColumnKeys<M>}`} The column reference. */ columnReference(column: T.ColumnKeys<M>, { alias }?: { alias?: string | null; }): `${string}.${T.ColumnKeys<M>}`; /** * Alias for {@link columnReference}. * * @param {T.ColumnKeys<M>} column - The column key. * @param {{ alias?: string | null }} [options] - Optional alias for the table. * @returns {`${string}.${T.ColumnKeys<M>}`} The column reference. */ columnRef(column: T.ColumnKeys<M>, { alias }?: { alias?: string | null; }): `${string}.${T.ColumnKeys<M>}`; /** * Get all column keys defined in the model schema. * * @returns {T.ColumnKeys<M>[]} An array of column keys. */ columns(): T.ColumnKeys<M>[]; /** * Check if a given column exists in the model schema. * * @param {string} name - The column name to check. * @returns {boolean} True if the column exists, false otherwise. */ hasColumn(name: string): boolean; /** * Get the primary key column of the model. * * @returns {string | undefined} The primary key column, or undefined if not found. */ primaryKey(): string | undefined; /** * Get all index names defined in the model schema. * * @returns {string[]} An array of index names. */ indexes(): string[]; /** * Get all nullable columns in the model schema. * * @returns {string[]} An array of nullable column keys. */ nullable(): string[]; /** * Get the default values for all columns in the schema. * * @returns {T.SchemaModel<M> | null} An object of default values, or null if none are defined. */ defaults(): T.Columns<M> | null; /** * Get the inferred JavaScript type of a column (based on constructor mapping). * * @param {T.ColumnKeys<M>} column - The column key. * @returns {("number" | "string" | "boolean" | "date" | undefined)} The column type, or undefined if not found. */ columnTypeOf(column: T.ColumnKeys<M>): string | undefined; /** * Get the database type of a column (e.g. varchar, int, etc.). * * @param {T.ColumnKeys<M>} column - The column key. * @returns {string | undefined} The column type, or undefined if not found. */ columnType(column: T.ColumnKeys<M>): string | undefined; /** * Get an enum column as a key-value map. * * @template C * @param {C} column - The column key. * @returns {Record<T.Result<M>[C], T.Result<M>[C]> | null} A record mapping each enum value to itself, or null if not defined. */ enum<C extends T.ColumnEnumKeys<M>>(column: C): Record<T.Result<M>[C], T.Result<M>[C]> | null; /** * Get all enum values for a given column. * * @template C * @param {C} column - The column key. * @returns {T.Result<M>[C][]} */ enums<C extends T.ColumnEnumKeys<M>>(column: C): T.Result<M>[C][]; } /** * The 'Meta' used to get the metadata of a Model works only when a schema is added to the Model. * * @example * import { Meta, Model , Blueprint , type T } from 'tspace-mysql'; * * const schema = { * id : Blueprint.int().notNull().primary().autoIncrement(), * uuid : Blueprint.varchar(50).null(), * email : Blueprint.varchar(255).notNull().index('users.email@index'), * name : Blueprint.varchar(255).null(), * username : Blueprint.varchar(255).notNull(), * password : Blueprint.varchar(255).notNull(), * status : Blueprint.tinyInt().notNull().default(0), * role : Blueprint.enum('admin','user').default('user'), * createdAt : Blueprint.timestamp().null(), * updatedAt : Blueprint.timestamp().null() * } * * type TS = T.Schema<typeof schema> * * class User extends Model<TS> { * constructor() { * super() * this.useSchema(schema) * } * } * * const meta = Meta(User) * // --- get metadata of User --- * const table = meta.table() // 'users' * const column = meta.ColumnKeys('id') // id * const columnRef = meta.ColumnKeysReference('id') // `users`.`id` * const columnTypeOf = meta.ColumnKeysTypeOf('id') // number * const columnType = meta.ColumnKeysType('id') // Int * const columns = meta.ColumnKeyss() // ['id','uuid',...'updatedAt'] * const hasColumn = meta.hasColumn('id') // false * const primaryKey = meta.primaryKey() // 'id' * const indexes = meta.indexes() // ['users.email@index'] * const nullable = meta.nullable() // ['uuid','name','createdAt','updatedAt'] * const defaults = meta.defaults() // { id : null, ..., status : 0, role: 'user', ..updatedAt : null } * const enums = meta.enums('role') // [ 'admin', 'user' ] * const enumsObj = meta.enum('role', { asObject: true }) // { admin: 'admin', user: 'user' } * */ declare const Meta: <M extends Model>(model: new () => M) => ModelMeta<M>; export { Meta }; export default Meta;