swaf/dist/db/Model.d.ts

59 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
import Validator from "./Validator";
import { Connection } from "mysql";
import Query from "./Query";
import { Request } from "express";
export default abstract class Model {
static getById<T extends Model>(id: number): Promise<T | null>;
static paginate<T extends Model>(request: Request, perPage?: number): Promise<T[]>;
protected static select(...fields: string[]): Query;
protected static update(data: {
[key: string]: any;
}): Query;
protected static delete(): Query;
protected static models<T extends Model>(query: Query): Promise<T[]>;
static loadRelation<T extends Model>(models: T[], relation: string, model: Function, localField: string): Promise<void>;
private static getFactory;
protected readonly properties: ModelProperty<any>[];
private readonly relations;
id?: number;
[key: string]: any;
constructor(data: any);
protected abstract defineProperties(): void;
protected defineProperty<T>(name: string, validator?: Validator<T> | RegExp): void;
private updateWithData;
protected beforeSave(exists: boolean, connection: Connection): Promise<void>;
protected afterSave(): Promise<void>;
save(connection?: Connection, postHook?: (callback: () => Promise<void>) => void): Promise<void>;
private saveTransaction;
static get table(): string;
get table(): string;
exists(): Promise<boolean>;
delete(): Promise<void>;
validate(onlyFormat?: boolean, connection?: Connection): Promise<void[]>;
private cache;
protected relation<T extends Model>(name: string): T | null;
}
export interface ModelFactory<T extends Model> {
(data: any): T;
}
declare class ModelProperty<T> {
readonly name: string;
private readonly validator;
private val?;
constructor(name: string, validator: Validator<T>);
validate(onlyFormat: boolean, connection?: Connection): Promise<void>;
get value(): T | undefined;
set value(val: T | undefined);
}
export declare class ModelCache {
private static readonly caches;
static cache(instance: Model): void;
static forget(instance: Model): void;
static all(table: string): {
[key: number]: Model;
} | undefined;
static get(table: string, id: number): Model | undefined;
}
export declare const EMAIL_REGEX: RegExp;
export {};