UNPKG

@athenna/database

Version:

The Athenna database handler for SQL/NoSQL.

439 lines (438 loc) 13.6 kB
/** * @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 { type PaginatedResponse, type PaginationOptions } from '@athenna/common'; import type { Knex } from 'knex'; import { Driver } from '#src/database/drivers/Driver'; import { Transaction } from '#src/database/transactions/Transaction'; import type { ConnectionOptions, Direction, Operations } from '#src/types'; export declare class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> { /** * Connect to database. */ connect(options?: ConnectionOptions): void; /** * Close the connection with database in this instance. */ close(): Promise<void>; /** * Creates a new instance of query builder. */ query(): Knex.QueryBuilder; /** * Sync a model schema with database. */ sync(): Promise<void>; /** * Create a new transaction. */ startTransaction(): Promise<Transaction<Knex.Transaction, Knex.QueryBuilder>>; /** * Commit the transaction. */ commitTransaction(): Promise<void>; /** * Rollback the transaction. */ rollbackTransaction(): Promise<void>; /** * Run database migrations. */ runMigrations(): Promise<void>; /** * Revert database migrations. */ revertMigrations(): Promise<void>; /** * List all databases available. */ getDatabases(): Promise<string[]>; /** * Get the current database name. */ getCurrentDatabase(): Promise<string | undefined>; /** * Verify if database exists. */ hasDatabase(database: string): Promise<boolean>; /** * Create a new database. */ createDatabase(database: string): Promise<void>; /** * Drop some database. */ dropDatabase(database: string): Promise<void>; /** * List all tables available. */ getTables(): Promise<string[]>; /** * Verify if table exists. */ hasTable(table: string): Promise<boolean>; /** * Create a new table in database. */ createTable(table: string, closure: (builder: Knex.TableBuilder) => void | Promise<void>): Promise<void>; /** * Drop a table in database. */ dropTable(table: string): Promise<void>; /** * Remove all data inside some database table * and restart the identity of the table. */ truncate(table: string): Promise<void>; /** * Make a raw query in database. */ raw<T = any>(sql: string, bindings?: any): T; /** * Calculate the average of a given column. */ avg(column: string): Promise<string>; /** * Calculate the average of a given column using distinct. */ avgDistinct(column: string): Promise<string>; /** * Get the max number of a given column. */ max(column: string): Promise<string>; /** * Get the min number of a given column. */ min(column: string): Promise<string>; /** * Sum all numbers of a given column. */ sum(column: string): Promise<string>; /** * Sum all numbers of a given column in distinct mode. */ sumDistinct(column: string): Promise<string>; /** * Increment a value of a given column. */ increment(column: string): Promise<void>; /** * Decrement a value of a given column. */ decrement(column: string): Promise<void>; /** * Calculate the average of a given column using distinct. */ count(column?: string): Promise<string>; /** * Calculate the average of a given column using distinct. */ countDistinct(column: string): Promise<string>; /** * Find a value in database. */ find<T = any>(): Promise<T>; /** * Find many values in database. */ findMany<T = any>(): Promise<T[]>; /** * Find many values in database and return as paginated response. */ paginate<T = any>(page?: PaginationOptions | number, limit?: number, resourceUrl?: string): Promise<PaginatedResponse<T>>; /** * Create a value in database. */ create<T = any>(data?: Partial<T>): Promise<T>; /** * Create many values in database. */ createMany<T = any>(data?: Partial<T>[]): Promise<T[]>; /** * Create data or update if already exists. */ createOrUpdate<T = any>(data?: Partial<T>): Promise<T | T[]>; /** * Update a value in database. */ update<T = any>(data: Partial<T>): Promise<T | T[]>; /** * Delete one value in database. */ delete(): Promise<void>; /** * Set the table that this query will be executed. */ table(table: string): this; /** * Log in console the actual query built. */ dump(): this; /** * Set the columns that should be selected on query. */ select(...columns: string[]): 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; /** * Set a join statement in your query. */ join(table: any, column1?: any, operation?: any | Operations, column2?: any): this; /** * Set a left join statement in your query. */ leftJoin(table: any, column1?: any, operation?: any | Operations, column2?: any): this; /** * Set a right join statement in your query. */ rightJoin(table: any, column1?: any, operation?: any | Operations, column2?: any): this; /** * Set a cross join statement in your query. */ crossJoin(table: any, column1?: any, operation?: any | Operations, column2?: any): this; /** * Set a full outer join statement in your query. */ fullOuterJoin(table: any, column1?: any, operation?: any | Operations, column2?: any): this; /** * Set a left outer join statement in your query. */ leftOuterJoin(table: any, column1?: any, operation?: any | Operations, column2?: any): this; /** * Set a right outer join statement in your query. */ rightOuterJoin(table: any, column1?: any, operation?: any | Operations, column2?: any): 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[]): this; /** * Set a group by raw statement in your query. */ groupByRaw(sql: string, bindings?: any): this; having(column: string): this; having(column: string, value: any): this; having(column: string, operation: 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: MySqlDriver) => void): this; /** * Set a having not exists statement in your query. */ havingNotExists(closure: (query: MySqlDriver) => void): this; /** * Set a having in statement in your query. */ havingIn(column: string, values: any[]): this; /** * Set a having not in statement in your query. */ havingNotIn(column: string, values: any[]): this; /** * Set a having between statement in your query. */ havingBetween(column: string, values: [any, any]): this; /** * Set a having not between statement in your query. */ havingNotBetween(column: string, values: [any, any]): this; /** * Set a having null statement in your query. */ havingNull(column: string): this; /** * Set a having not null statement in your query. */ havingNotNull(column: string): this; orHaving(column: string): this; orHaving(column: string, value: any): this; orHaving(column: string, operation: 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: MySqlDriver) => void): this; /** * Set an or having not exists statement in your query. */ orHavingNotExists(closure: (query: MySqlDriver) => void): this; /** * Set an or having in statement in your query. */ orHavingIn(column: string, values: any[]): this; /** * Set an or having not in statement in your query. */ orHavingNotIn(column: string, values: any[]): this; /** * Set an or having between statement in your query. */ orHavingBetween(column: string, values: [any, any]): this; /** * Set an or having not between statement in your query. */ orHavingNotBetween(column: string, values: [any, any]): this; /** * Set an or having null statement in your query. */ orHavingNull(column: string): this; /** * Set an or having not null statement in your query. */ orHavingNotNull(column: string): this; where(statement: Record<string, any>): this; where(key: string, value: any): this; where(key: string, operation: Operations, value: any): this; whereNot(statement: Record<string, any>): this; whereNot(key: string, 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: MySqlDriver) => void): this; /** * Set a where not exists statement in your query. */ whereNotExists(closure: (query: MySqlDriver) => void): this; /** * Set a where like statement in your query. */ whereLike(column: string, value: any): this; /** * Set a where ILike statement in your query. */ whereILike(column: string, value: any): this; /** * Set a where in statement in your query. */ whereIn(column: string, values: any[]): this; /** * Set a where not in statement in your query. */ whereNotIn(column: string, values: any[]): this; /** * Set a where between statement in your query. */ whereBetween(column: string, values: [any, any]): this; /** * Set a where not between statement in your query. */ whereNotBetween(column: string, values: [any, any]): this; /** * Set a where null statement in your query. */ whereNull(column: string): this; /** * Set a where not null statement in your query. */ whereNotNull(column: string): this; orWhere(statement: Record<string, any>): this; orWhere(key: string, value: any): this; orWhere(key: string, operation: Operations, value: any): this; orWhereNot(statement: Record<string, any>): this; orWhereNot(key: string, 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: MySqlDriver) => void): this; /** * Set an or where not exists statement in your query. */ orWhereNotExists(closure: (query: MySqlDriver) => void): this; /** * Set an or where like statement in your query. */ orWhereLike(column: string, value: any): this; /** * Set an or where ILike statement in your query. */ orWhereILike(column: string, value: any): this; /** * Set an or where in statement in your query. */ orWhereIn(column: string, values: any[]): this; /** * Set an or where not in statement in your query. */ orWhereNotIn(column: string, values: any[]): this; /** * Set an or where between statement in your query. */ orWhereBetween(column: string, values: [any, any]): this; /** * Set an or where not between statement in your query. */ orWhereNotBetween(column: string, values: [any, any]): this; /** * Set an or where null statement in your query. */ orWhereNull(column: string): this; /** * Set an or where not null statement in your query. */ orWhereNotNull(column: string): this; /** * Set an order by statement in your query. */ orderBy(column: string, 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): 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): this; /** * Set the skip number in your query. */ offset(number: number): this; /** * Set the limit number in your query. */ limit(number: number): this; }