@athenna/database
Version:
The Athenna database handler for SQL/NoSQL.
133 lines (132 loc) • 4.37 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 type { Knex } from 'knex';
import type { ConnectionOptions } from '#src/types';
import type { FakeDriver } from '#src/database/drivers/FakeDriver';
import { QueryBuilder } from '#src/database/builders/QueryBuilder';
import { Macroable } from '@athenna/common';
import type { MongoDriver } from '#src/database/drivers/MongoDriver';
import type { MySqlDriver } from '#src/database/drivers/MySqlDriver';
import type { SqliteDriver } from '#src/database/drivers/SqliteDriver';
import type { Driver as DriverImpl } from '#src/database/drivers/Driver';
import type { Transaction } from '#src/database/transactions/Transaction';
import type { PostgresDriver } from '#src/database/drivers/PostgresDriver';
export declare class DatabaseImpl<Driver extends DriverImpl = any> extends Macroable {
/**
* The connection name used for this instance.
*/
connectionName: string;
/**
* The drivers responsible for handling database operations.
*/
driver: Driver;
/**
* Creates a new instance of DatabaseImpl.
*/
constructor(athennaDbOpts?: ConnectionOptions);
connection(con: 'mongo', options?: ConnectionOptions): DatabaseImpl<MongoDriver>;
connection(con: 'mysql', options?: ConnectionOptions): DatabaseImpl<MySqlDriver>;
connection(con: 'sqlite', options?: ConnectionOptions): DatabaseImpl<SqliteDriver>;
connection(con: 'postgres', options?: ConnectionOptions): DatabaseImpl<PostgresDriver>;
connection(con: 'fake', options?: ConnectionOptions): DatabaseImpl<typeof FakeDriver>;
connection(con: 'mongo' | 'mysql' | 'sqlite' | 'postgres' | 'fake' | string, options?: ConnectionOptions): DatabaseImpl<MongoDriver> | DatabaseImpl<MySqlDriver> | DatabaseImpl<SqliteDriver> | DatabaseImpl<PostgresDriver> | DatabaseImpl<typeof FakeDriver>;
/**
* Verify if database is already connected.
*/
isConnected(): boolean;
/**
* Connect to database.
*/
connect(options?: ConnectionOptions): DatabaseImpl<Driver>;
/**
* Close the connection with database in this instance.
*/
close(): Promise<void>;
/**
* Close all the connections with all databases.
*/
closeAll(): Promise<void>;
/**
* Return the client of driver.
*/
getClient(): any;
/**
* Return the query builder of driver.
*/
getQueryBuilder(): any;
/**
* Create a new transaction.
*/
startTransaction(): Promise<Transaction<Driver>>;
/**
* Run database seeders.
*/
runSeeders(options?: {
task?: any;
path?: string;
classes?: string[];
}): 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): Knex.Raw<T>;
/**
* Creates a new instance of QueryBuilder for this table.
*/
table<T = any>(table: string | any): QueryBuilder<T, Driver>;
}