/** * @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 { Macroable, type Collection, type PaginatedResponse, type PaginationOptions } from '@athenna/common'; import type { Operations } from '#src/types/Operations'; import type { Direction, ModelColumns } from '#src/types'; import type { Driver as DriverImpl } from '#src/database/drivers/Driver'; export declare class QueryBuilder extends Macroable { /** * The drivers responsible for handling database operations. */ protected driver: Driver; /** * The initial table that the query is going to work with. */ protected tableName: string; /** * Creates a new instance of QueryBuilder. */ constructor(driver: Driver, tableName: string); /** * Return the client of driver. */ getClient(): any; /** * Return the query builder of driver. */ getQueryBuilder(): any; /** * Set the query builder of driver. */ setQueryBuilder(queryBuilder: any): this; /** * Return the driver of the query builder. */ getDriver(): Driver; /** * Set the driver of the query builder. */ setDriver(driver: any, tableName?: string): this; /** * Set the driver primary key that will be used * when creating new data. */ setPrimaryKey(primaryKey: string): this; /** * Calculate the average of a given column. */ avg(column: string | ModelColumns): Promise; /** * Calculate the average of a given column. */ avgDistinct(column: string | ModelColumns): Promise; /** * Get the max number of a given column. */ max(column: string | ModelColumns): Promise; /** * Get the min number of a given column. */ min(column: string | ModelColumns): Promise; /** * Sum all numbers of a given column. */ sum(column: string | ModelColumns): Promise; /** * Sum all numbers of a given column. */ sumDistinct(column: string | ModelColumns): Promise; /** * Increment a value of a given column. */ increment(column: string | ModelColumns): Promise; /** * Decrement a value of a given column. */ decrement(column: string | ModelColumns): Promise; /** * Calculate the average of a given column using distinct. */ count(column?: string | ModelColumns): Promise; /** * Calculate the average of a given column using distinct. */ countDistinct(column: string | ModelColumns): Promise; /** * Find a value in database or throw exception if undefined. */ findOrFail(): Promise; /** * Return a single data or, if no results are found, * execute the given closure. */ findOr(callback: () => Promise): Promise; /** * Find value in database but returns only the value of * selected column directly. */ pluck(column: K): Promise; /** * Find many values in database but returns only the * values of selected column directly. */ pluckMany(column: K): Promise; /** * Find a value in database. */ find(): 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 as a Collection. */ collection(): Promise>; /** * Find many values in database and return as paginated response. */ paginate(page?: PaginationOptions | number, limit?: number, resourceUrl?: string): Promise; /** * Create a value in database. */ create(data?: Partial): Promise; /** * Create many values in database. */ createMany(data?: Partial[]): Promise; /** * Create data or update if already exists. */ createOrUpdate(data?: Partial): Promise; /** * Create a value, doing nothing if it would violate a unique constraint. * The current query's where clauses are used to detect the conflict. * Returns the created value, or `null` when it already existed. */ createOrIgnore(data?: Partial): Promise; /** * Find the first value matching the current query or create it, never * throwing on a concurrent unique violation. Always returns a value. */ createOrFirst(data?: Partial): Promise; /** * Update data in database. */ update(data: Partial): Promise; /** * Delete data in database. */ delete(): Promise; /** * Make a raw query in database. */ raw(sql: string, bindings?: any): import("knex").Knex.Raw; /** * Set a new table to work with in query builder. */ table(tableName: string): this; /** * Executes the given closure when the first argument is true. */ when(criteria: any, closure: (query: this, criteriaValue: any) => any | Promise): this; /** * Log in console the actual query built. */ dump(): this; /** * Set the columns that should be selected on query. */ select(...columns: string[] | ModelColumns[]): this; /** * Set the columns that should be selected on query raw. */ selectRaw(sql: string, bindings?: any): this; /** * Set the table that should be used on query. * Different from `table()` method, this method * doesn't change the driver table. */ from(table: string): this; /** * Set the table that should be used on query raw. * Different from `table()` method, this method * doesn't change the driver table. */ fromRaw(sql: string, bindings?: any): this; join(tableName: string): this; join(tableName: string, column: string): this; join(tableName: string, column1: string, column2: string): this; join(tableName: string, column1: string, operation: Operations, column2: string): this; leftJoin(tableName: string): this; leftJoin(tableName: string, column: string): this; leftJoin(tableName: string, column1: string, column2: string): this; leftJoin(tableName: string, column1: string, operation: Operations, column2: string): this; rightJoin(tableName: string): this; rightJoin(tableName: string, column: string): this; rightJoin(tableName: string, column1: string, column2: string): this; rightJoin(tableName: string, column1: string, operation: Operations, column2: string): this; crossJoin(tableName: string): this; crossJoin(tableName: string, column: string): this; crossJoin(tableName: string, column1: string, column2: string): this; crossJoin(tableName: string, column1: string, operation: Operations, column2: string): this; fullOuterJoin(tableName: string): this; fullOuterJoin(tableName: string, column: string): this; fullOuterJoin(tableName: string, column1: string, column2: string): this; fullOuterJoin(tableName: string, column1: string, operation: Operations, column2: string): this; leftOuterJoin(tableName: string): this; leftOuterJoin(tableName: string, column: string): this; leftOuterJoin(tableName: string, column1: string, column2: string): this; leftOuterJoin(tableName: string, column1: string, operation: Operations, column2: string): this; rightOuterJoin(tableName: string): this; rightOuterJoin(tableName: string, column: string): this; rightOuterJoin(tableName: string, column1: string, column2: string): this; rightOuterJoin(tableName: string, column1: string, operation: Operations, column2: string): this; /** * Set a join raw statement in your query. */ joinRaw(sql: string, bindings?: any): this; /** * Set a group by statement in your query. */ groupBy(...columns: string[] | ModelColumns[]): this; /** * Set a group by raw statement in your query. */ groupByRaw(sql: string, bindings?: any): this; /** * Set a having statement in your query. */ having(column: string | ModelColumns, operation?: any | Operations, value?: any): this; /** * Set a having raw statement in your query. */ havingRaw(sql: string, bindings?: any): this; /** * Set a having in statement in your query. */ havingIn(column: string | ModelColumns, values: any[]): this; /** * Set a having not in statement in your query. */ havingNotIn(column: string | ModelColumns, values: any[]): this; /** * Set a having between statement in your query. */ havingBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set a having not between statement in your query. */ havingNotBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set a having null statement in your query. */ havingNull(column: string | ModelColumns): this; /** * Set a having not null statement in your query. */ havingNotNull(column: string | ModelColumns): this; /** * Set an or having statement in your query. */ orHaving(column: string | ModelColumns, operation?: any | Operations, value?: any): this; /** * Set an or having raw statement in your query. */ orHavingRaw(sql: string, bindings?: any): this; /** * Set an or having not in statement in your query. */ orHavingNotIn(column: string | ModelColumns, values: any[]): this; /** * Set an or having between statement in your query. */ orHavingBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set an or having not between statement in your query. */ orHavingNotBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set an or having null statement in your query. */ orHavingNull(column: string | ModelColumns): this; /** * Set an or having not null statement in your query. */ orHavingNotNull(column: string | ModelColumns): this; where(statement: (query: this) => void): this; where(statement: Partial): this; where(statement: Record): this; where(key: string | ModelColumns, value: any): this; where(key: string | ModelColumns, operation: Operations, value: any): this; whereNot(statement: (query: this) => void): this; whereNot(statement: Partial): this; whereNot(statement: Record): this; whereNot(key: string | ModelColumns, value: any): this; /** * Set a where raw statement in your query. */ whereRaw(sql: string, bindings?: any): this; /** * Set a where exists statement in your query. */ whereExists(closure: (query: Driver) => void): this; /** * Set a where not exists statement in your query. */ whereNotExists(closure: (query: Driver) => void): this; /** * Set a where like statement in your query. */ whereLike(column: string | ModelColumns, value: any): this; /** * Set a where ILike statement in your query. */ whereILike(column: string | ModelColumns, value: any): this; /** * Set a where in statement in your query. */ whereIn(column: string | ModelColumns, values: any[]): this; /** * Set a where not in statement in your query. */ whereNotIn(column: string | ModelColumns, values: any[]): this; /** * Set a where between statement in your query. */ whereBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set a where not between statement in your query. */ whereNotBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set a where null statement in your query. */ whereNull(column: string | ModelColumns): this; /** * Set a where not null statement in your query. */ whereNotNull(column: string | ModelColumns): this; whereJson(column: string | ModelColumns, operation: any, value?: any): this; whereJson(column: string | ModelColumns, value: any): this; orWhere(statement: (query: this) => void): this; orWhere(statement: Partial): this; orWhere(statement: Record): this; orWhere(key: string | ModelColumns, value: any): this; orWhere(key: string | ModelColumns, operation: Operations, value: any): this; orWhereNot(statement: (query: this) => void): this; orWhereNot(statement: Partial): this; orWhereNot(statement: Record): this; orWhereNot(key: string | ModelColumns, value: any): this; /** * Set a or where raw statement in your query. */ orWhereRaw(sql: string, bindings?: any): this; /** * Set an or where exists statement in your query. */ orWhereExists(closure: (query: Driver) => void): this; /** * Set an or where not exists statement in your query. */ orWhereNotExists(closure: (query: Driver) => void): this; orWhereLike(statement: Partial): this; orWhereLike(statement: Record): this; orWhereLike(key: string | ModelColumns, value: any): this; orWhereILike(statement: Partial): this; orWhereILike(statement: Record): this; orWhereILike(key: string | ModelColumns, value: any): this; /** * Set an or where in statement in your query. */ orWhereIn(column: string | ModelColumns, values: any[]): this; /** * Set an or where not in statement in your query. */ orWhereNotIn(column: string | ModelColumns, values: any[]): this; /** * Set an or where between statement in your query. */ orWhereBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set an or where not between statement in your query. */ orWhereNotBetween(column: string | ModelColumns, values: [any, any]): this; /** * Set an or where null statement in your query. */ orWhereNull(column: string | ModelColumns): this; /** * Set an or where not null statement in your query. */ orWhereNotNull(column: string | ModelColumns): this; orWhereJson(column: string | ModelColumns, operation: Operations, value?: any): this; orWhereJson(column: string | ModelColumns, value: any): this; /** * Set an order by statement in your query. */ orderBy(column: string | ModelColumns, direction?: Direction): this; /** * Set an order by raw statement in your query. */ orderByRaw(sql: string, bindings?: any): this; /** * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ latest(column?: string | 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?: string | ModelColumns): this; /** * Set the skip number in your query. */ offset(number: number): this; /** * Set the limit number in your query. */ limit(number: number): this; }