/** * @athenna/database * * (c) João Lenon * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { Collection, type PaginatedResponse, type PaginationOptions } from '@athenna/common'; import { type Faker } from '@faker-js/faker'; import { ModelSchema } from '#src/models/schemas/ModelSchema'; import type { ModelColumns, ModelRelations } from '#src/types'; import { ORIGINAL_SYMBOL } from '#src/constants/OriginalSymbol'; import { ModelFactory } from '#src/models/factories/ModelFactory'; import { ModelQueryBuilder } from '#src/models/builders/ModelQueryBuilder'; export declare class BaseModel { /** * Set if the `attributes` method should be called or not. */ private static get isToSetAttributes(); /** * Set if the option annotation `isUnique` * should be verified or not. */ private static get isToValidateUnique(); /** * Set if the option annotation `isNullable` * should be verified or not. */ private static get isToValidateNullable(); /** * The faker instance to create fake data in * definition instance. */ static get faker(): Faker; /** * Set the connection name that model will use * to access database. */ static connection(): any; /** * Set if model should automatically be sync with * database when running DatabaseProvider. * * @default true */ static sync(): boolean; /** * Set the table name of this model instance. */ static table(): string; /** * Set the default values that should be set when creating or * updating the model. */ static attributes(): Record; /** * Set the definition data that will be used when fabricating * instances of your model using factories. */ static definition(): Promise>; /** * Create a new ModelSchema instance from your model. */ static schema(this: T): ModelSchema>; /** * Create a new ModelFactory instance from your model. */ static factory(this: T): ModelFactory, InstanceType>; /** * Enable/disable setting the default attributes properties * when creating/updating models. */ static setAttributes(this: T, value: boolean): T; /** * Enable/disable the `isUnique` property validation of * models columns. */ static uniqueValidation(this: T, value: boolean): T; /** * Enable/disable the `isNullable` property validation of * models columns. */ static nullableValidation(this: T, value: boolean): T; /** * Create a query builder for the model. */ static query(this: T): ModelQueryBuilder, import("../database/drivers/MongoDriver.js").MongoDriver>; /** * Remove all data inside model table * and restart the identity of the table. */ static truncate(): Promise; /** * Find value in database but returns only the value of * selected column directly. */ static pluck>, keyof InstanceType>>(this: T, key: K, where?: Partial>): Promise[K]>; static pluck(this: T, key: ModelColumns>, where?: Partial>): Promise; /** * Find many values in database but returns only the * values of selected column directly. */ static pluckMany>, keyof InstanceType>>(this: T, key: K, where?: Partial>): Promise[K][]>; static pluckMany(this: T, key: ModelColumns>, where?: Partial>): Promise; /** * Find a value in database. */ static find(this: T, where?: Partial>): Promise>; /** * Find a value in database. */ static exists(this: T, where?: Partial>): Promise; /** * Find a value in database or throw exception if undefined. */ static findOrFail(this: T, where?: Partial>): Promise>; /** * Find a value in database or create a new one if it doesn't exist. */ static findOrCreate(this: T, where: Partial>, data: Partial>): Promise>; /** * Return a single data or, if no results are found, * execute the given closure. */ static findOr(this: T, where: Partial>, closure: () => any | Promise): Promise | any>; /** * Find many values in database. */ static findMany(this: T, where?: Partial>): Promise[]>; /** * Find many values in database and return paginated. */ static paginate(this: T, options?: PaginationOptions, where?: Partial>): Promise>>; /** * Find many values in database and return * as a collection instance. */ static collection(this: T, where?: Partial>): Promise>>; /** * Create a value in database. */ static create(this: T, data?: Partial>, cleanPersist?: boolean): Promise>; /** * Create many values in database. */ static createMany(this: T, data: Partial>[], cleanPersist?: boolean): Promise[]>; /** * Create or update a value in database. */ static createOrUpdate(this: T, where: Partial>, data: Partial>, cleanPersist?: boolean): Promise>; /** * Create a value, doing nothing if it would violate a unique constraint. * The `where` is used to detect the conflict. Returns the created model, or * `null` when a matching row already exists. */ static createOrIgnore(this: T, where: Partial>, data: Partial>, cleanPersist?: boolean): Promise>; /** * Find the first value matching `where` or create it, never throwing on a * concurrent unique violation. Always returns a model. */ static createOrFirst(this: T, where: Partial>, data: Partial>, cleanPersist?: boolean): Promise>; /** * Update a value in database. */ static update(this: T, where: Partial>, data: Partial>, cleanPersist?: boolean): Promise | InstanceType[]>; /** * Restore a soft deleted value from database. */ static restore(this: T, where: Partial>, data: Partial>): Promise | InstanceType[]>; /** * Delete or soft delete a value in database. */ static delete(this: T, where: Partial>, force?: boolean): Promise; /** * The original model values when it was fetched * from database. If is undefined, means that model * is a fresh instance and is not available in database * yet. */ private [ORIGINAL_SYMBOL]?; /** * Set the original model values by deep copying * the model state. */ setOriginal(): this; /** * Return a Json object from the actual subclass instance. */ toJSON(options?: { withHidden?: boolean; }): Record; load(relation: string): Promise; load>(relation: K, closure?: (query: ModelQueryBuilder>) => any): Promise; loadOnly(relation: string): Promise; loadOnly>(relation: K, closure?: (query: ModelQueryBuilder>) => any): Promise; /** * Validate if model is persisted in database * or if it's a fresh instance. */ isPersisted(): boolean; /** * Get values only that are different from * the original symbol to avoid updating * data that was not changed. */ dirty(): this | Record; /** * Validate if model has been changed from * it initial state when it was retrieved from * database. */ isDirty(): boolean; /** * Save the changes done in the model in database. */ save(cleanPersist?: boolean): Promise; /** * Create a new instance of the model from retrieving * again the data from database. The existing * model instance WILL NOT BE affected. */ fresh(): Promise; /** * Refresh the model instance data retrieving * model data using the main primary key. The * existing model instance WILL BE affected. */ refresh(): Promise; /** * Verify if model is soft deleted. */ isTrashed(): boolean; /** * Delete or soft delete your model from database. */ delete(force?: boolean): Promise; /** * Restore a soft deleted model from database. */ restore(): Promise; }