tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
496 lines (495 loc) • 15 kB
TypeScript
import { Model } from "./Model";
type TExtendType = NumberConstructor | StringConstructor | BooleanConstructor | DateConstructor | readonly TExtendType[] | {
[_: string]: TExtendType;
};
type ResolveType<T> = T extends NumberConstructor ? number : T extends StringConstructor ? string : T extends BooleanConstructor ? boolean : T extends DateConstructor ? Date : T extends readonly (infer U)[] ? ResolveType<U>[] : T extends Record<string, any> ? {
[K in keyof T]: ResolveType<T[K]>;
} : never;
/**
* Class 'Blueprint' is used to make the schema for table
* @example
* import { Schema , Blueprint } from 'tspace-mysql'
* import sql from '../../../build/lib/core/SqlLike';
import { default } from '../../../app/$audit';
* await new Schema().table('users',{
* id : Blueprint.int().notNull().primary().autoIncrement(),
* name : Blueprint.varchar(255).default('my name').index(),
* email : Blueprint.varchar(255).unique().compositeIndex(['verify']), // composite index email,verify
* json : Blueprint.json().null(),
* verify : Blueprint.tinyInt(1).notNull(),
* created_at : Blueprint.timestamp().null(),
* updated_at : Blueprint.timestamp().null(),
* deleted_at : Blueprint.timestamp().null()
* })
*/
declare class Blueprint<T = any> {
private _default;
private _enum;
private _type;
private _attributes;
private _foreignKey;
private _index;
private _compositeIndex;
private _column;
private _isVirtual;
private _isEnum;
private _isNull;
private _sql;
private _valueType;
/**
* Assign type 'virtual' to column
* @static
* @return {Blueprint<T>} Blueprint
*/
static virtualColumn(sql: string | {
select?: string;
where?: string;
orderBy?: string;
groupBy?: string;
}): Blueprint<unknown>;
/**
* Assign type 'virtual' to column
* @static
* @return {Blueprint<T>} Blueprint
*/
virtualColumn(sql: string | {
select?: string;
where?: string;
orderBy?: string;
groupBy?: string;
}): Blueprint<unknown>;
/**
* Assign type 'serial' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static serial(_?: number): Blueprint<number>;
/**
* Assign type 'serial' in table
* @return {Blueprint<T>} Blueprint
*/
serial(_?: number): Blueprint<number>;
/**
* Assign type 'INT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static int(_?: number): Blueprint<number>;
/**
* Assign type 'INT' in table
* @return {Blueprint<T>} Blueprint
*/
int(_?: number): Blueprint<number>;
/**
* Assign type 'INT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static integer(_?: number): Blueprint<number>;
/**
* Assign type 'INT' in table
* @return {Blueprint<T>} Blueprint
*/
integer(_?: number): Blueprint<number>;
/**
* Assign type 'TINYINT' in table
* @static
* @param {number} number
* @return {Blueprint<T>} Blueprint
*/
static tinyInt(number?: number): Blueprint<number | boolean>;
/**
* Assign type 'TINYINT' in table
* @param {number} number
* @return {Blueprint<T>} Blueprint
*/
tinyInt(number?: number): Blueprint<number | boolean>;
/**
* Assign type 'TINYINT' in table
* @static
* @param {number} number
* @return {Blueprint<T>} Blueprint
*/
static tinyint(number?: number): Blueprint<number | boolean>;
/**
* Assign type 'TINYINT' in table
* @param {number} number
* @return {Blueprint<T>} Blueprint
*/
tinyint(number?: number): Blueprint<number | boolean>;
/**
* Assign type 'BIGINT' in table
* @static
* @param {number} number [number = 10]
* @return {Blueprint<T>} Blueprint
*/
static bigInt(number?: number): Blueprint<number>;
/**
* Assign type 'BIGINT' in table
* @param {number} number [number = 10]
* @return {Blueprint<T>} Blueprint
*/
bigInt(number?: number): Blueprint<number>;
/**
* Assign type 'BIGINT' in table
* @static
* @param {number} number [number = 10]
* @return {Blueprint<T>} Blueprint
*/
static bigint(number?: number): Blueprint<number>;
/**
* Assign type 'BIGINT' in table
* @param {number} number [number = 10]
* @return {Blueprint<T>} Blueprint
*/
bigint(number?: number): Blueprint<number>;
/**
* Assign type 'BOOLEAN' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static boolean(): Blueprint<number | boolean>;
/**
* Assign type 'BOOLEAN' in table
* @return {Blueprint<T>} Blueprint
*/
boolean(): Blueprint<number | boolean>;
/**
* Assign type 'DOUBLE' in table
* @static
* @param {number} length between 1-255
* @param {number} decimal 0.000...n
* @return {Blueprint<T>} Blueprint
*/
static double(length?: number, decimal?: number): Blueprint<number>;
/**
* Assign type 'DOUBLE' in table
* @param {number} length between 1-255
* @param {number} decimal 0.000...n
* @return {Blueprint<T>} Blueprint
*/
double(length?: number, decimal?: number): Blueprint<number>;
/**
* Assign type 'FLOAT' in table
* @static
* @param {number} length between 1-255
* @param {number} decimal 0.000...n
* @return {Blueprint<T>} Blueprint
*/
static float(length?: number, decimal?: number): Blueprint<number>;
/**
* Assign type 'FLOAT' in table
* @param {number} length between 1-255
* @param {number} decimal 0.000...n
* @return {Blueprint<T>} Blueprint
*/
float(length?: number, decimal?: number): Blueprint<number>;
/**
* Assign type 'VARCHAR' in table
* @static
* @param {number} length [length = 191] length of string
* @return {Blueprint<T>} Blueprint
*/
static varchar(length?: number): Blueprint<string>;
/**
* Assign type 'VARCHAR' in table
* @param {number} length [length = 191] length of string
* @return {Blueprint<T>} Blueprint
*/
varchar(length?: number): Blueprint<string>;
/**
* Assign type 'UUID' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static uuid(): Blueprint<string>;
/**
* Assign type 'UUID' in table
* @return {Blueprint<T>} Blueprint
*/
uuid(): Blueprint<string>;
/**
* Assign type 'CHAR' in table
* @static
* @param {number} length [length = 1] length of string
* @return {Blueprint<T>} Blueprint
*/
static char(length?: number): Blueprint<string>;
/**
* Assign type 'CHAR' in table
* @param {number} length [length = 1] length of string
* @return {Blueprint<T>} Blueprint
*/
char(length?: number): Blueprint<string>;
/**
* Assign type 'LONGTEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static longText(): Blueprint<string>;
/**
* Assign type 'LONGTEXT' in table
* @return {Blueprint<T>} Blueprint
*/
longText(): Blueprint<string>;
/**
* Assign type 'LONGTEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static longtext(): Blueprint<string>;
/**
* Assign type 'LONGTEXT' in table
* @return {Blueprint<T>} Blueprint
*/
longtext(): Blueprint<string>;
/**
* Assign type 'BINARY' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static binary(): Blueprint<string>;
/**
* Assign type 'BINARY' in table
* @return {Blueprint<T>} Blueprint
*/
binary(): Blueprint<string>;
/**
* Assign type 'JSON' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static json(): Blueprint<Record<string, any> | string>;
/**
* Assign type 'JSON' in table
* @return {Blueprint<T>} Blueprint
*/
json(): Blueprint<Record<string, any> | string>;
/**
* Assign type 'MEDIUMTEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static mediumText(): Blueprint<string>;
/**
* Assign type 'MEDIUMTEXT' in table
* @return {Blueprint<T>} Blueprint
*/
mediumText(): Blueprint<string>;
/**
* Assign type 'MEDIUMTEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static mediumtext(): Blueprint<string>;
/**
* Assign type 'MEDIUMTEXT' in table
* @return {Blueprint<T>} Blueprint
*/
mediumtext(): Blueprint<string>;
/**
* Assign type 'TINYTEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static tinyText(): Blueprint<string>;
/**
* Assign type 'TINYTEXT' in table
* @return {Blueprint<T>} Blueprint
*/
tinyText(): Blueprint<string>;
/**
* Assign type 'TINYTEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static tinytext(): Blueprint<string>;
/**
* Assign type 'TINYTEXT' in table
* @return {Blueprint<T>} Blueprint
*/
tinytext(): Blueprint<string>;
/**
* Assign type 'TEXT' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static text(): Blueprint<string>;
/**
* Assign type 'TEXT' in table
* @return {Blueprint<T>} Blueprint
*/
text(): Blueprint<string>;
/**
* Assign type 'ENUM'
* @static
* @param {...string} enums n1, n2, n3, ...n
* @return {Blueprint<T>} Blueprint
*/
static enum<K extends string | string[] | Record<string, string>>(...enums: (K extends string ? K : K)[]): Blueprint<K extends string ? K : K[keyof K]>;
/**
* Assign type 'ENUM'
* @param {...string} enums n1, n2, n3, ...n
* @return {Blueprint<T>} Blueprint
*/
enum<K extends string | string[] | Record<string, string>>(...enums: (K extends string ? K : K)[]): Blueprint<K extends string ? K : K[keyof K]>;
/**
* Assign type 'DATE' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static date(): Blueprint<Date | string>;
/**
* Assign type 'DATE' in table
* @return {Blueprint<T>} Blueprint
*/
date(): Blueprint<Date | string>;
/**
* Assign type 'DATETIME' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static dateTime(): Blueprint<Date | string>;
/**
* Assign type 'DATETIME' in table
* @return {Blueprint<T>} Blueprint
*/
dateTime(): Blueprint<Date | string>;
/**
* Assign type 'DATETIME' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static datetime(): Blueprint<Date | string>;
/**
* Assign type 'DATETIME' in table
* @return {Blueprint<T>} Blueprint
*/
datetime(): Blueprint<Date | string>;
/**
* Assign type 'TIMESTAMP' in table
* @static
* @return {Blueprint<T>} Blueprint
*/
static timestamp(): Blueprint<Date | string>;
/**
* Assign type 'TIMESTAMP' in table
* @return {Blueprint<T>} Blueprint
*/
timestamp(): Blueprint<Date | string>;
/**
* Assign attributes 'UNSIGNED' in table
* @return {Blueprint<T>} Blueprint
*/
unsigned(): Blueprint<T>;
/**
* Assign attributes 'UNIQUE' in table
* @return {Blueprint<T>} Blueprint
*/
unique(): Blueprint<T>;
/**
* Assign attributes 'NULL' in table
* @return {Blueprint<T>} Blueprint
*/
null(): Blueprint<T | null>;
/**
* Assign attributes 'NOT NULL' in table
* @return {Blueprint<T>} Blueprint
*/
notNull(): Blueprint<T>;
/**
* Assign attributes 'NOT NULL' in table
* @return {Blueprint<T>} Blueprint
*/
notnull(): Blueprint<T>;
/**
* Assign attributes 'PRIMARY KEY' in table
* @return {Blueprint<T>} Blueprint
*/
primary(): Blueprint<T>;
/**
* Assign attributes 'default' in table
* @param {string | number} value default value
* @return {Blueprint<T>} Blueprint
*/
default(value: string | number | boolean): Blueprint<T>;
/**
* Assign attributes 'default currentTimestamp' in table
* @return {Blueprint<T>} Blueprint
*/
currentTimestamp(): Blueprint<T>;
/**
* Assign attributes 'default currentTimestamp' in table
* @return {Blueprint<T>} Blueprint
*/
currenttimestamp(): Blueprint<T>;
/**
* Assign attributes 'autoIncrement' in table
* @return {Blueprint<T>} Blueprint
*/
autoIncrement(): Blueprint<T>;
/**
* Assign attributes 'autoIncrement' in table
* @return {Blueprint<T>} Blueprint
*/
autoincrement(): Blueprint<T>;
/**
* Assign attributes 'foreign' in table
* Reference bettwen Column Main to Column Child
* @param {object} property object { key , value , operator }
* @property {string?} property.reference
* @property {Model | string} property.on
* @property {string?} property.onDelete
* @property {string?} property.onUpdate
* @return {Blueprint<T>} Blueprint
*/
foreign({ references, on, onDelete, onUpdate, }: {
references?: string;
on: (new () => Model) | string;
onDelete?: "CASCADE" | "NO ACTION" | "RESTRICT" | "SET NULL";
onUpdate?: "CASCADE" | "NO ACTION" | "RESTRICT" | "SET NULL";
}): Blueprint<T>;
/**
* Assign attributes 'index' in table
* @param {string} name of index
* @return {Blueprint<T>} Blueprint
*/
index(name?: string): Blueprint<T>;
/**
* Assign attributes 'index' in table
* @param {string} name of index
* @return {Blueprint<T>} Blueprint
*/
compositeIndex(columns: string[], name?: string): Blueprint<T>;
/**
* Assign type to blueprint
* @param {NumberConstructor|StringConstructor|BooleanConstructor|DateConstructor} type
* @return {Blueprint<T>} Blueprint
*/
transform<T extends TExtendType>(type: T): Blueprint<ResolveType<T>>;
get sql(): {
select?: string;
where?: string;
orderBy?: string;
groupBy?: string;
} | null;
get column(): string | null;
get type(): string;
get attributes(): string[];
get foreignKey(): Record<string, any> | null;
get indexKey(): string | null;
get compositeIndexKey(): {
columns: string[];
name?: string;
} | null;
get defaultValue(): string | number | null;
get valueType(): TExtendType;
get enums(): string[];
get isVirtual(): boolean;
get isEnum(): boolean;
get isNull(): boolean;
private _addAssignType;
private _addAssignAttribute;
}
export { Blueprint };
export default Blueprint;