/** * @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 PaginationOptions } from '@athenna/common'; import type { Direction, Operations, ModelColumns, ModelRelations } from '#src/types'; import type { BaseModel } from '#src/models/BaseModel'; import type { Driver } from '#src/database/drivers/Driver'; import { QueryBuilder } from '#src/database/builders/QueryBuilder'; import type { Transaction } from '#src/database/transactions/Transaction'; export declare class ModelQueryBuilder extends QueryBuilder { private Model; private schema; private generator; private primaryKeyName; private primaryKeyProperty; private isToSetAttributes; private isToValidateUnique; private isToValidateNullable; private selectColumns; private DELETED_AT_PROP; private DELETED_AT_NAME; private isSoftDelete; private hasCustomSelect; constructor(model: any, driver: D); /** * Define a transaction to be used by the model query builder. */ setTransaction(trx: Transaction): this; /** * Set a different driver to the model query builder. */ setDriver(driver: Driver, tableName?: string): this; /** * Calculate the average of a given column. */ avg(column: ModelColumns): Promise; /** * Calculate the average of a given column. */ avgDistinct(column: ModelColumns): Promise; /** * Get the max number of a given column. */ max(column: ModelColumns): Promise; /** * Get the min number of a given column. */ min(column: ModelColumns): Promise; /** * Sum all numbers of a given column. */ sum(column: ModelColumns): Promise; /** * Sum all numbers of a given column. */ sumDistinct(column: ModelColumns): Promise; /** * Increment a value of a given column. */ increment(column: ModelColumns): Promise; /** * Decrement a value of a given column. */ decrement(column: ModelColumns): Promise; /** * Calculate the average of a given column using distinct. */ count(column?: ModelColumns): Promise; /** * Calculate the average of a given column using distinct. */ countDistinct(column: ModelColumns): Promise; /** * Find value in database but returns only the value of * selected column directly. */ pluck, keyof M>>(column: K): Promise; pluck(column: ModelColumns): Promise; /** * Find many values in database but returns only the * values of selected column directly. */ pluckMany, keyof M>>(column: K): Promise; pluckMany(column: ModelColumns): Promise; /** * Find a value in database. */ find(): Promise; /** * Find a value in database or throw exception if undefined. */ findOrFail(): Promise; /** * Find a value in database or create a new one if it doesn't exist. */ findOrCreate(data?: Partial): Promise; /** * Return a single data or, if no results are found, * execute the given closure. */ findOr(closure: () => T | Promise): Promise; /** * Find a value in database and return as boolean. */ exists(): Promise; /** * Find many values in database. */ findMany(): Promise; /** * Find many values in database and return paginated. */ paginate(page?: PaginationOptions | number, limit?: number, resourceUrl?: string): Promise>; /** * Find many values in database and return * as a collection instance. */ collection(): Promise>; /** * Create a value in database. */ create(data?: Partial, cleanPersist?: boolean): Promise; /** * Create many values in database. */ createMany(data: Partial[], cleanPersist?: boolean): Promise; /** * Map model properties to columns and stamp the timestamp columns, without * running the (race-prone) unique pre-check. Used by the conflict-aware * persistence methods that rely on the database to enforce uniqueness. */ private toPersistColumns; /** * Create or update a value in database. */ createOrUpdate(data: Partial, cleanPersist?: boolean): Promise; /** * Create a value, doing nothing if it would violate a unique constraint * (`ON CONFLICT DO NOTHING`/`INSERT IGNORE`). The current query's where * clauses detect the conflict. Returns the created model, or `null` when a * matching row already exists. Relies on the database constraint instead of * the race-prone model unique pre-check. */ createOrIgnore(data?: Partial, cleanPersist?: boolean): Promise; /** * Find the first value matching the current query or create it, never * throwing on a concurrent unique violation. Always returns a model. */ createOrFirst(data?: Partial, cleanPersist?: boolean): Promise; /** * Update a value in database. */ update(data: Partial, cleanPersist?: boolean): Promise; /** * Delete or soft delete a value in database. */ delete(force?: boolean): Promise; /** * Restore one or multiple soft deleted models. */ restore(data?: Partial): Promise; /** * Retrieve only the values that are soft deleted in * database. */ onlyTrashed(): this; /** * Retrieve active and soft deleted values from database. */ withTrashed(): this; /** * Enable/disable setting the default attributes properties * when creating/updating models. */ setAttributes(value: boolean): this; /** * Enable/disable the `isUnique` property validation of * models columns. */ uniqueValidation(value: boolean): this; /** * Enable/disable the `isNullable` property validation of * models columns. */ nullableValidation(value: boolean): this; with(relation: string): this; with>(relation: K, closure?: (query: ModelQueryBuilder, Driver>) => any): this; /** * Only returns the data if the closure returns a result. */ whereHas>(relation: K | string, closure?: (query: ModelQueryBuilder, Driver>) => any): this; /** * Same as {@link ModelQueryBuilder.whereHas}, but joins the resulting * `EXISTS (...)` clause to the surrounding WHERE with `OR` instead of `AND`. * * Useful inside a grouped `where(qb => ...)` closure to build expressions * like `(directCol ILIKE x OR relation.col ILIKE x)` without resorting to * raw SQL. */ orWhereHas>(relation: K | string, closure?: (query: ModelQueryBuilder, Driver>) => any): this; /** * Build a grouped OR search across any mix of direct columns and * relation columns in a single `WHERE (...)` clause. * * Each entry in `fields` is either a direct column property (e.g. `name`) * or a `relation.column` path (e.g. `profile.bio`). The resulting SQL is a * single parenthesized group joined exclusively by `OR`. Passing a falsy * `term` short-circuits and the query is left untouched. * * @example * ```ts * User.query().search(['name', 'email', 'profile.bio'], 'john') * ``` */ search(fields: (ModelColumns | ModelRelations | string)[], term: string): this; /** * Executes the given closure when the first argument is true. */ when(criteria: any, closure: (query: this, criteriaValue: any) => any | Promise): this; /** * Set the columns that should be selected on query. */ select(...columns: ModelColumns[]): this; /** * Set the columns that should be selected on query raw. */ selectRaw(sql: string, bindings?: any): this; /** * Set a group by statement in your query. */ groupBy(...columns: ModelColumns[]): this; having(column: ModelColumns): this; having(column: ModelColumns, value: any): this; having(column: ModelColumns, operation: Operations, value: any): this; /** * Set a having in statement in your query. */ havingIn(column: ModelColumns, values: any[]): this; /** * Set a having not in statement in your query. */ havingNotIn(column: ModelColumns, values: any[]): this; /** * Set a having between statement in your query. */ havingBetween(column: ModelColumns, values: [any, any]): this; /** * Set a having not between statement in your query. */ havingNotBetween(column: ModelColumns, values: [any, any]): this; /** * Set a having null statement in your query. */ havingNull(column: ModelColumns): this; /** * Set a having not null statement in your query. */ havingNotNull(column: ModelColumns): this; orHaving(column: ModelColumns): this; orHaving(column: ModelColumns, value: any): this; orHaving(column: ModelColumns, operation: Operations, value: any): this; /** * Set a orHaving not in statement in your query. */ orHavingNotIn(column: ModelColumns, values: any[]): this; /** * Set a orHaving between statement in your query. */ orHavingBetween(column: ModelColumns, values: [any, any]): this; /** * Set a orHaving not between statement in your query. */ orHavingNotBetween(column: ModelColumns, values: [any, any]): this; /** * Set a orHaving null statement in your query. */ orHavingNull(column: ModelColumns): this; /** * Set a orHaving not null statement in your query. */ orHavingNotNull(column: ModelColumns): this; where(statement: (query: this) => void): this; where(statement: Partial): this; where(statement: Record): this; where(key: ModelColumns, value: any): this; where(key: ModelColumns, operation: Operations, value: any): this; whereNot(statement: (query: this) => void): this; whereNot(statement: Partial): this; whereNot(statement: Record): this; whereNot(key: ModelColumns, value: any): this; /** * Set a where like statement in your query. */ whereLike(column: ModelColumns, value: any): this; /** * Set a where ILike statement in your query. */ whereILike(column: ModelColumns, value: any): this; /** * Set a where in statement in your query. */ whereIn(column: ModelColumns, values: any[]): this; /** * Set a where not in statement in your query. */ whereNotIn(column: ModelColumns, values: any[]): this; /** * Set a where between statement in your query. */ whereBetween(column: ModelColumns, values: [any, any]): this; /** * Set a where not between statement in your query. */ whereNotBetween(column: ModelColumns, values: [any, any]): this; /** * Set a where null statement in your query. */ whereNull(column: ModelColumns): this; /** * Set a where not null statement in your query. */ whereNotNull(column: ModelColumns): this; whereJson(column: ModelColumns, value: any): this; whereJson(column: ModelColumns, operation: Operations, value: any): this; orWhere(statement: (query: this) => void): this; orWhere(statement: Partial): this; orWhere(statement: Record): this; orWhere(key: ModelColumns, value: any): this; orWhere(key: ModelColumns, operation: Operations, value: any): this; orWhereNot(statement: (query: this) => void): this; orWhereNot(statement: Partial): this; orWhereNot(statement: Record): this; orWhereNot(key: ModelColumns, value: any): this; orWhereLike(statement: Partial): this; orWhereLike(statement: Record): this; orWhereLike(key: ModelColumns, value: any): this; orWhereILike(statement: Partial): this; orWhereILike(statement: Record): this; orWhereILike(key: ModelColumns, value: any): this; /** * Set a orWhere in statement in your query. */ orWhereIn(column: ModelColumns, values: any[]): this; /** * Set a orWhere not in statement in your query. */ orWhereNotIn(column: ModelColumns, values: any[]): this; /** * Set a orWhere between statement in your query. */ orWhereBetween(column: ModelColumns, values: [any, any]): this; /** * Set a orWhere not between statement in your query. */ orWhereNotBetween(column: ModelColumns, values: [any, any]): this; /** * Set a orWhere null statement in your query. */ orWhereNull(column: ModelColumns): this; /** * Set a orWhere not null statement in your query. */ orWhereNotNull(column: ModelColumns): this; orWhereJson(column: ModelColumns, value: any): this; orWhereJson(column: ModelColumns, operation: Operations, value: any): this; /** * Set an order by statement in your query. */ orderBy(column: ModelColumns, direction?: Direction): this; /** * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ latest(column?: ModelColumns): this; /** * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ oldest(column?: ModelColumns): this; /** * Set the internal selected properties and soft delete * queries. */ private setInternalQueries; /** * Reset select state after terminal custom select queries. */ private resetCustomSelect; /** * Verify that columns with `isNullable` property * can be created in database. */ private validateNullable; /** * Verify that columns with isUnique property * can be created in database. */ private validateUnique; }