@athenna/database
Version:
The Athenna database handler for SQL/NoSQL.
417 lines (416 loc) • 15 kB
TypeScript
/**
* @athenna/database
*
* (c) João Lenon <lenon@athenna.io>
*
* 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<T = any, Driver extends DriverImpl = any> extends Macroable {
/**
* The drivers responsible for handling database operations.
*/
private driver;
/**
* 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 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<T>): Promise<string>;
/**
* Calculate the average of a given column.
*/
avgDistinct(column: string | ModelColumns<T>): Promise<string>;
/**
* Get the max number of a given column.
*/
max(column: string | ModelColumns<T>): Promise<string>;
/**
* Get the min number of a given column.
*/
min(column: string | ModelColumns<T>): Promise<string>;
/**
* Sum all numbers of a given column.
*/
sum(column: string | ModelColumns<T>): Promise<string>;
/**
* Sum all numbers of a given column.
*/
sumDistinct(column: string | ModelColumns<T>): Promise<string>;
/**
* Increment a value of a given column.
*/
increment(column: string | ModelColumns<T>): Promise<void>;
/**
* Decrement a value of a given column.
*/
decrement(column: string | ModelColumns<T>): Promise<void>;
/**
* Calculate the average of a given column using distinct.
*/
count(column?: string | ModelColumns<T>): Promise<string>;
/**
* Calculate the average of a given column using distinct.
*/
countDistinct(column: string | ModelColumns<T>): Promise<string>;
/**
* Find a value in database or throw exception if undefined.
*/
findOrFail(): Promise<T>;
/**
* Return a single data or, if no results are found,
* execute the given closure.
*/
findOr(callback: () => Promise<T>): Promise<T>;
/**
* Find value in database but returns only the value of
* selected column directly.
*/
pluck<K extends keyof T = keyof T>(column: K): Promise<T[K]>;
/**
* Find many values in database but returns only the
* values of selected column directly.
*/
pluckMany<K extends keyof T = keyof T>(column: K): Promise<T[K][]>;
/**
* Find a value in database.
*/
find(): Promise<T>;
/**
* Find a value in database and return as boolean.
*/
exists(): Promise<boolean>;
/**
* Find many values in database.
*/
findMany(): Promise<T[]>;
/**
* Find many values in database and return as a Collection.
*/
collection(): Promise<Collection<T>>;
/**
* Find many values in database and return as paginated response.
*/
paginate(page?: PaginationOptions | number, limit?: number, resourceUrl?: string): Promise<PaginatedResponse>;
/**
* Create a value in database.
*/
create(data?: Partial<T>): Promise<T>;
/**
* Create many values in database.
*/
createMany(data?: Partial<T>[]): Promise<T[]>;
/**
* Create data or update if already exists.
*/
createOrUpdate(data?: Partial<T>): Promise<T | T[]>;
/**
* Update data in database.
*/
update(data: Partial<T>): Promise<T | T[]>;
/**
* Delete data in database.
*/
delete(): Promise<T | T[] | void>;
/**
* Make a raw query in database.
*/
raw(sql: string, bindings?: any): import("knex").Knex.Raw<any>;
/**
* 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<any>): this;
/**
* Log in console the actual query built.
*/
dump(): this;
/**
* Set the columns that should be selected on query.
*/
select(...columns: string[] | ModelColumns<T>[]): 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<T>[]): 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<T>, operation?: any | Operations, value?: any): this;
/**
* Set a having raw statement in your query.
*/
havingRaw(sql: string, bindings?: any): this;
/**
* Set a having exists statement in your query.
*/
havingExists(closure: (query: Driver) => void): this;
/**
* Set a having not exists statement in your query.
*/
havingNotExists(closure: (query: Driver) => void): this;
/**
* Set a having in statement in your query.
*/
havingIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set a having not in statement in your query.
*/
havingNotIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set a having between statement in your query.
*/
havingBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set a having not between statement in your query.
*/
havingNotBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set a having null statement in your query.
*/
havingNull(column: string | ModelColumns<T>): this;
/**
* Set a having not null statement in your query.
*/
havingNotNull(column: string | ModelColumns<T>): this;
/**
* Set an or having statement in your query.
*/
orHaving(column: string | ModelColumns<T>, 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 exists statement in your query.
*/
orHavingExists(closure: (query: Driver) => void): this;
/**
* Set an or having not exists statement in your query.
*/
orHavingNotExists(closure: (query: Driver) => void): this;
/**
* Set an or having in statement in your query.
*/
orHavingIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set an or having not in statement in your query.
*/
orHavingNotIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set an or having between statement in your query.
*/
orHavingBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set an or having not between statement in your query.
*/
orHavingNotBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set an or having null statement in your query.
*/
orHavingNull(column: string | ModelColumns<T>): this;
/**
* Set an or having not null statement in your query.
*/
orHavingNotNull(column: string | ModelColumns<T>): this;
where(statement: Partial<T>): this;
where(statement: Record<string, any>): this;
where(key: string | ModelColumns<T>, value: any): this;
where(key: string | ModelColumns<T>, operation: Operations, value: any): this;
whereNot(statement: Partial<T>): this;
whereNot(statement: Record<string, any>): this;
whereNot(key: string | ModelColumns<T>, 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<T>, value: any): this;
/**
* Set a where ILike statement in your query.
*/
whereILike(column: string | ModelColumns<T>, value: any): this;
/**
* Set a where in statement in your query.
*/
whereIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set a where not in statement in your query.
*/
whereNotIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set a where between statement in your query.
*/
whereBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set a where not between statement in your query.
*/
whereNotBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set a where null statement in your query.
*/
whereNull(column: string | ModelColumns<T>): this;
/**
* Set a where not null statement in your query.
*/
whereNotNull(column: string | ModelColumns<T>): this;
orWhere(statement: Partial<T>): this;
orWhere(statement: Record<string, any>): this;
orWhere(key: string | ModelColumns<T>, value: any): this;
orWhere(key: string | ModelColumns<T>, operation: Operations, value: any): this;
orWhereNot(statement: Partial<T>): this;
orWhereNot(statement: Record<string, any>): this;
orWhereNot(key: string | ModelColumns<T>, 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<T>): this;
orWhereLike(statement: Record<string, any>): this;
orWhereLike(key: string | ModelColumns<T>, value: any): this;
orWhereILike(statement: Partial<T>): this;
orWhereILike(statement: Record<string, any>): this;
orWhereILike(key: string | ModelColumns<T>, value: any): this;
/**
* Set an or where in statement in your query.
*/
orWhereIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set an or where not in statement in your query.
*/
orWhereNotIn(column: string | ModelColumns<T>, values: any[]): this;
/**
* Set an or where between statement in your query.
*/
orWhereBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set an or where not between statement in your query.
*/
orWhereNotBetween(column: string | ModelColumns<T>, values: [any, any]): this;
/**
* Set an or where null statement in your query.
*/
orWhereNull(column: string | ModelColumns<T>): this;
/**
* Set an or where not null statement in your query.
*/
orWhereNotNull(column: string | ModelColumns<T>): this;
/**
* Set an order by statement in your query.
*/
orderBy(column: string | ModelColumns<T>, 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<T>): 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<T>): this;
/**
* Set the skip number in your query.
*/
offset(number: number): this;
/**
* Set the limit number in your query.
*/
limit(number: number): this;
}