import { Request } from "express"; import { Connection } from "mysql"; import Extendable from "../Extendable.js"; import { Type } from "../Utils.js"; import ModelComponent from "./ModelComponent.js"; import ModelFactory, { PrimaryKeyValue } from "./ModelFactory.js"; import ModelQuery, { ModelFieldData, ModelQueryResult, QueryFields } from "./ModelQuery.js"; import Validator from "./Validator.js"; export default abstract class Model implements Extendable> { static get table(): string; static getPrimaryKeyFields(): string[]; static create(this: ModelType, data: Pick): M; static select(this: ModelType, ...fields: QueryFields): ModelQuery; static update(this: ModelType, data: Pick): ModelQuery; static delete(this: ModelType): ModelQuery; static getById(this: ModelType, ...id: PrimaryKeyValue[]): Promise; static paginate(this: ModelType, request: Request, perPage?: number, query?: ModelQuery): Promise>; protected readonly _factory: ModelFactory; private readonly _components; private readonly _validators; private _exists; [key: string]: ModelFieldData; constructor(factory: ModelFactory, isNew: boolean); protected init?(): void; protected setValidation(propertyName: K): Validator; addComponent(modelComponent: ModelComponent): void; as>(type: Type): C; asOptional>(type: Type): C | null; has>(type: Type): boolean; require>(type: Type): void; updateWithData(data: Pick | Record): void; /** * Override this to automatically fill obvious missing data i.e. from relation or default value that are fetched * asynchronously. */ protected autoFill?(): Promise; protected beforeSave?(connection: Connection): Promise; protected afterSave?(): Promise; save(connection?: Connection, postHook?: (callback: () => Promise) => void): Promise; private saveTransaction; delete(): Promise; validate(onlyFormat?: boolean, connection?: Connection): Promise; exists(): boolean; equals(model: this): boolean; get table(): string; private get _properties(); private hasProperty; getOrFail(k: K): NonNullable; } export interface ModelType extends Type { table: string; new (factory: ModelFactory, isNew: boolean): M; getPrimaryKeyFields(): (keyof M & string)[]; select(this: ModelType, ...fields: QueryFields): ModelQuery; }