tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
380 lines (379 loc) • 14.9 kB
TypeScript
import { z } from "zod";
import { Model } from "./Model";
import { Blueprint } from "./Blueprint";
import { T } from "./UtilityTypes";
import { AlterTable } from "./Contracts/AlterTable";
declare class Schema {
private $db;
table: (table: string, schemas: Record<string, Blueprint>) => Promise<void>;
static table: (table: string, schemas: Record<string, Blueprint>) => Promise<void>;
createTable: (database: string, table: string, schema: Record<string, any> | string[]) => string;
static createTable: (database: string, table: string, schema: Record<string, any> | string[]) => string;
detectSchema(schema: Record<string, any>): {
type: any;
attributes: any;
};
static detectSchema(schema: Record<string, any>): {
type: any;
attributes: any;
};
/**
*
* The 'Sync' method is used to check for create or update table or columns with your schema in your model.
*
* The schema can define with method 'useSchema'
* @param {string} pathFolders directory to models
* @property {boolean} options.force - forec always check all columns if not exists will be created
* @property {boolean} options.log - show log execution with sql statements
* @property {boolean} options.foreign - check when has a foreign keys will be created
* @property {boolean} options.changed - check when column is changed attribute will be change attribute
* @return {Promise<void>}
* @example
*
* - node_modules
* - app
* - Models
* - User.ts
* - Post.ts
*
* // file User.ts
* class User extends Model {
* constructor(){
* super()
* this.hasMany({ name : 'posts' , model : Post })
* this.useSchema ({
* id : new Blueprint().int().notNull().primary().autoIncrement(),
* uuid : new Blueprint().varchar(50).null(),
* email : new Blueprint().int().notNull().unique(),
* name : new Blueprint().varchar(255).null(),
* created_at : new Blueprint().timestamp().null(),
* updated_at : new Blueprint().timestamp().null(),
* deleted_at : new Blueprint().timestamp().null()
* })
* }
* }
*
* // file Post.ts
* class Post extends Model {
* constructor(){
* super()
* this.hasMany({ name : 'comments' , model : Comment })
* this.belongsTo({ name : 'user' , model : User })
* this.useSchema ({
* id : new Blueprint().int().notNull().primary().autoIncrement(),
* uuid : new Blueprint().varchar(50).null(),
* user_id : new Blueprint().int().notNull().foreign({ references : 'id' , on : User , onDelete : 'CASCADE' , onUpdate : 'CASCADE' }),
* title : new Blueprint().varchar(255).null(),
* created_at : new Blueprint().timestamp().null(),
* updated_at : new Blueprint().timestamp().null(),
* deleted_at : new Blueprint().timestamp().null()
* })
* }
* }
*
*
* await new Schema().sync(`app/Models` , { force : true , log = true, foreign = true , changed = true })
*/
sync(pathFolders: string, { force, log, foreign, changed, index, }?: {
force?: boolean | undefined;
log?: boolean | undefined;
foreign?: boolean | undefined;
changed?: boolean | undefined;
index?: boolean | undefined;
}): Promise<void>;
/**
*
* The 'Sync' method is used to check for create or update table or columns with your schema in your model.
*
* The schema can define with method 'useSchema'
* @param {string} pathFolders directory to models
* @type {object} options
* @property {boolean} options.force - forec always check all columns if not exists will be created
* @property {boolean} options.log - show log execution with sql statements
* @property {boolean} options.foreign - check when has a foreign keys will be created
* @property {boolean} options.changed - check when column is changed attribute will be change attribute
* @property {boolean} options.index - add columns to index
* @return {Promise<void>}
* @example
*
* - node_modules
* - app
* - Models
* - User.ts
* - Post.ts
*
* // file User.ts
* class User extends Model {
* constructor(){
* super()
* this.hasMany({ name : 'posts' , model : Post })
* this.useSchema ({
* id : new Blueprint().int().notNull().primary().autoIncrement(),
* uuid : new Blueprint().varchar(50).null(),
* email : new Blueprint().int().notNull().unique(),
* name : new Blueprint().varchar(255).null(),
* created_at : new Blueprint().timestamp().null(),
* updated_at : new Blueprint().timestamp().null(),
* deleted_at : new Blueprint().timestamp().null()
* })
* }
* }
*
* // file Post.ts
* class Post extends Model {
* constructor(){
* super()
* this.hasMany({ name : 'comments' , model : Comment })
* this.belongsTo({ name : 'user' , model : User })
* this.useSchema ({
* id : new Blueprint().int().notNull().primary().autoIncrement(),
* uuid : new Blueprint().varchar(50).null(),
* user_id : new Blueprint().int().notNull().foreign({ references : 'id' , on : User , onDelete : 'CASCADE' , onUpdate : 'CASCADE' }),
* title : new Blueprint().varchar(255).null(),
* created_at : new Blueprint().timestamp().null(),
* updated_at : new Blueprint().timestamp().null(),
* deleted_at : new Blueprint().timestamp().null()
* })
* }
* }
*
*
* await Schema.sync(`app/Models` , { force : true })
*/
static sync(pathFolders: string, { force, log, foreign, changed, index, }?: {
force?: boolean | undefined;
log?: boolean | undefined;
foreign?: boolean | undefined;
changed?: boolean | undefined;
index?: boolean | undefined;
}): Promise<void>;
/**
* The 'validator' method is used Create runtime validator schema from Model definition.
*
* Generates request validation schema compatible with supported adapters.
*
*
* @param {Model} model Model class used to generate validator schema.
* @type {object} options
* @param {string[]} options.omit
* @param {string[]} options.optional
*
*
* @example
*
* import { Schema } from 'tspace-mysql'
* import { User } from './User'
* import { Elysia } from 'elysia'
*
* new Elysia()
* .post('/', ({ body }) => {
* return { body }
* }, {
* body: Schema
* .validator(User)
* .create({
* omit: ["id", "created_at", "updated_at", "deleted_at"],
* optional: ["uuid"]
* })
* })
* .put('/', ({ body }) => {
* return { body }
* }, {
* body: Schema
* .validator(User)
* .update({
* required: ["id","email"],
* omit: ["uuid"]
* })
* })
* .listen(8000)
*/
validator<M extends Model>(model: new () => M): {
/**
* The 'create' method is used Create runtime validator schema from Model definition.
*
* Generates request validation schema compatible with supported adapters.
*
* @type {object} options
* @param {string[]} options.omit
* @param {string[]} options.optional
*
*
* @example
*
* import { Schema } from 'tspace-mysql'
* import { User } from './User'
* import { Elysia } from 'elysia'
*
* new Elysia()
* .post('/', ({ body }) => {
* return { body }
* }, {
* body: Schema
* .validator(User)
* .create({
* omit: ["id", "created_at", "updated_at", "deleted_at"],
* optional: ["uuid"]
* })
* .listen(8000)
*/
create: <O extends T.ColumnKeys<M, {
OnlyColumn: true;
}>[] = [], Opt extends T.ColumnKeys<M, {
OnlyColumn: true;
}>[] = []>(options?: {
omit?: O;
optional?: Opt;
} & T.NoConflict<O, Opt>) => z.ZodObject<T.ZodShapeCreate<M, O, Opt>, z.core.$strip>;
/**
* The 'update' method is used Create runtime validator schema from Model definition.
*
* Generates request validation schema compatible with supported adapters.
*
* @type {object} options
* @param {string[]} options.required
* @param {string[]} options.omit
* @example
*
* import { Schema } from 'tspace-mysql'
* import { User } from './User'
* import { Elysia } from 'elysia'
*
* new Elysia()
* .post('/', ({ body }) => {
* return { body }
* }, {
* body: Schema
* .validator(User)
* .update({
* required: ["id","email"],
* omit: ["uuid"]
* })
* })
* .listen(8000)
*/
update: <R extends T.ColumnKeys<M, {
OnlyColumn: true;
}>[] = [], O extends T.ColumnKeys<M, {
OnlyColumn: true;
}>[] = []>(options?: {
required?: R;
omit?: O;
} & T.NoConflict<R, O>) => z.ZodObject<T.ZodShapeUpdate<M, R, O>, z.core.$strip>;
};
/**
* The 'validator' method is used Create runtime validator schema from Model definition.
*
* Generates request validation schema compatible with supported adapters.
*
*
* @param {Model} model Model class used to generate validator schema.
* @type {object} options
* @param {string[]} options.omit
* @param {string[]} options.optional
*
*
* @example
*
* import { Schema } from 'tspace-mysql'
* import { User } from './User'
* import { Elysia } from 'elysia'
*
* new Elysia()
* .post('/', ({ body }) => {
* return { body }
* }, {
* body: Schema.createValidator(User, {
* omit: ["id", "created_at", "updated_at", "deleted_at"],
* optional: ["uuid"]
* })
* })
* .listen(8000)
*/
static validator<M extends Model>(model: new () => M): {
/**
* The 'create' method is used Create runtime validator schema from Model definition.
*
* Generates request validation schema compatible with supported adapters.
*
* @type {object} options
* @param {string[]} options.omit
* @param {string[]} options.optional
*
*
* @example
*
* import { Schema } from 'tspace-mysql'
* import { User } from './User'
* import { Elysia } from 'elysia'
*
* new Elysia()
* .post('/', ({ body }) => {
* return { body }
* }, {
* body: Schema
* .validator(User)
* .create({
* omit: ["id", "created_at", "updated_at", "deleted_at"],
* optional: ["uuid"]
* })
* .listen(8000)
*/
create: <O extends (keyof import("../types/decorator").TColumnsDecorator<M, {}> extends never ? import("./UtilityTypes").TSchemaKeyOf<M, ReturnType<M["typeOfSchema"]>> : keyof import("../types/decorator").TColumnsDecorator<M, {}>)[] = [], Opt extends (keyof import("../types/decorator").TColumnsDecorator<M, {}> extends never ? import("./UtilityTypes").TSchemaKeyOf<M, ReturnType<M["typeOfSchema"]>> : keyof import("../types/decorator").TColumnsDecorator<M, {}>)[] = []>(options?: ({
omit?: O | undefined;
optional?: Opt | undefined;
} & T.NoConflict<O, Opt>) | undefined) => z.ZodObject<T.ZodShapeCreate<M, O, Opt>, z.core.$strip>;
/**
* The 'update' method is used Create runtime validator schema from Model definition.
*
* Generates request validation schema compatible with supported adapters.
*
* @type {object} options
* @param {string[]} options.required
* @param {string[]} options.omit
* @example
*
* import { Schema } from 'tspace-mysql'
* import { User } from './User'
* import { Elysia } from 'elysia'
*
* new Elysia()
* .post('/', ({ body }) => {
* return { body }
* }, {
* body: Schema
* .validator(User)
* .update({
* required: ["id","email"],
* omit: ["uuid"]
* })
* })
* .listen(8000)
*/
update: <R extends (keyof import("../types/decorator").TColumnsDecorator<M, {}> extends never ? import("./UtilityTypes").TSchemaKeyOf<M, ReturnType<M["typeOfSchema"]>> : keyof import("../types/decorator").TColumnsDecorator<M, {}>)[] = [], O extends (keyof import("../types/decorator").TColumnsDecorator<M, {}> extends never ? import("./UtilityTypes").TSchemaKeyOf<M, ReturnType<M["typeOfSchema"]>> : keyof import("../types/decorator").TColumnsDecorator<M, {}>)[] = []>(options?: ({
required?: R | undefined;
omit?: O | undefined;
} & T.NoConflict<R, O>) | undefined) => z.ZodObject<T.ZodShapeUpdate<M, R, O>, z.core.$strip>;
};
alterTable<M extends Model>(model: new () => M): AlterTable<M>;
static alterTable<M extends Model>(model: new () => M): AlterTable<M>;
private _createValidator;
private _updateValidator;
private _definitionSchema;
private _import;
protected syncExecute({ models, force, log, foreign, changed, index, }: {
models: (Model | null)[];
force: boolean;
log: boolean;
foreign: boolean;
changed: boolean;
index: boolean;
}): Promise<void>;
private _syncForeignKey;
private _syncIndex;
private _syncChangeColumn;
private _syncMissingColumn;
private _syncTable;
}
export { Schema };
export default Schema;