@riao/dbal
Version:
200 lines (199 loc) • 5.76 kB
TypeScript
import { DatabaseDriver } from './driver';
import { DatabaseEnv } from '../config';
import { DataDefinitionBuilder, DataDefinitionRepository } from '../ddl';
import { DatabaseConnectionOptions } from './connection-options';
import { DatabaseRecord } from '../record';
import { DatabaseQueryBuilder, QueryRepository, QueryRepositoryOptions } from '../dml';
import { SchemaQueryRepository } from '../schema/schema-query-repository';
import { Schema } from '../schema';
import { Transaction } from './transaction';
/**
* Represents a single database instance, including a driver,
* configuration, connection, etc.
*/
export declare abstract class Database {
/**
* Reconstructors will be used to re-initialize repositories
* after the database has been initialized.
*
* This allows repositories to be created in global scope
* before the database is init()'d
*/
protected queryRepoInitQueue: QueryRepository[];
protected ddlRepoInitQueue: DataDefinitionRepository[];
protected schemaRepoInitQueue: SchemaQueryRepository[];
isLoaded: boolean;
/**
* Database driver class
*/
driverType: typeof DatabaseDriver;
/**
* Database driver intance
*/
driver: DatabaseDriver;
/**
* Database configuration class
*/
envType: typeof DatabaseEnv;
/**
* Database configuration
*/
env: DatabaseEnv;
/**
* Parent path (database path)
*/
databasePath: string;
/**
* Database name (must match folder name!)
*/
name: string;
/**
* Migrations directory, relative to this database
* e.g. If the migrations are in `database/main/migrations`,
* set this to `migrations`
*/
migrations: string;
/**
* Seeds directory, relative to this database
* e.g. If the seeds are in `database/main/seeds`,
* set this to `seeds`
*/
seeds: string;
/**
* Schema storage directory, relative to this database
* e.g. If the schema is in `database/main/.schema`,
* set this to `.schema`
*/
schemaDirectory: string;
/**
* Query builder class type used to create new query builders
*/
queryBuilderType: typeof DatabaseQueryBuilder;
/**
* Query repository class type used to create new repos
*/
queryRepositoryType: typeof QueryRepository;
/**
* Query repository
*/
query: QueryRepository;
/**
* DDL builder class type used to create new builders
*/
ddlBuilderType: typeof DataDefinitionBuilder;
/**
* DDL repository class type used to create new repos
*/
ddlRepositoryType: typeof DataDefinitionRepository;
/**
* Data definition repository
*/
ddl: DataDefinitionRepository;
/**
* Schema Query repository class type used to create new repos
*/
schemaQueryRepositoryType: typeof SchemaQueryRepository;
/**
* Schema query repository
*/
schemaQuery: SchemaQueryRepository;
/**
* Database schema
*/
protected schema: Schema;
/**
* Initialize the database
*
* @param options Database options
*/
init(options?: {
connectionOptions?: DatabaseConnectionOptions;
}): Promise<void>;
/**
* Load the configuration .env
*/
configureFromEnv(): void;
/**
* Connect to the database
*/
connect(): Promise<void>;
/**
* Disconnect from the database
*/
disconnect(): Promise<void>;
/**
* Get the full relative path to this database's migrations folder
*
* @returns Returns the relative file path
*/
getMigrationsDirectory(): string;
/**
* Get the full relative path to this database's seeds folder
*
* @returns Returns the relative file path
*/
getSeedsDirectory(): string;
/**
* Get the full relative path to this database's schema folder
*
* @returns Returns the relative file path
*/
getSchemaDirectory(): string;
/**
* Get a new DDL builder
*
* @returns DDL builder
*/
getDataDefinitionBuilder(): DataDefinitionBuilder;
/**
* Get a new DDL repository
*
* @param options Repository options
* @returns Returns the DDL repository
*/
getDataDefinitionRepository(): DataDefinitionRepository;
/**
* Get a new query builder
*
* @returns Query builder
*/
getQueryBuilder(): DatabaseQueryBuilder;
/**
* Get a new query repository
*
* @param options Repository options
* @returns Returns the query repository
*/
getQueryRepository<T extends DatabaseRecord = DatabaseRecord>(options?: Omit<QueryRepositoryOptions, 'driver' | 'queryBuilderType' | 'functions'>): QueryRepository<T>;
/**
* Get a new schema query repository
*
* @returns Schema Query Repository
*/
getSchemaQueryRepository(): SchemaQueryRepository;
/**
* Build & save the database schema
*/
buildSchema(): Promise<void>;
/**
* Save the database schema
*/
saveSchema(): Promise<void>;
/**
* Load the database schema
*/
loadSchema(): Promise<void>;
/**
* Get schema metadata
*
* @returns Schema
*/
getSchema(): Promise<Schema>;
/**
* Run a transaction
*
* @param fn Transaction callback
* @returns Passes the return from the transaction callback
*/
transaction<T>(fn: (transaction: Transaction) => Promise<T>): Promise<T>;
}