UNPKG

@athenna/database

Version:

The Athenna database handler for SQL/NoSQL.

201 lines (200 loc) 5.24 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 { QueryBuilder } from '#src/database/builders/QueryBuilder'; import { Path, Module, Options, Macroable } from '@athenna/common'; import { ConnectionFactory } from '#src/factories/ConnectionFactory'; export class DatabaseImpl extends Macroable { /** * Creates a new instance of DatabaseImpl. */ constructor(athennaDbOpts) { super(); /** * The connection name used for this instance. */ this.connectionName = Config.get('database.default'); /** * The drivers responsible for handling database operations. */ this.driver = null; this.driver = ConnectionFactory.fabricate(this.connectionName); this.connect(athennaDbOpts); } /** * Change the database connection. */ connection(con, options) { const driver = ConnectionFactory.fabricate(con); const database = new DatabaseImpl(options); database.connectionName = con; database.driver = driver; return database.connect(options); } /** * Verify if database is already connected. */ isConnected() { return this.driver.isConnected; } /** * Connect to database. */ connect(options) { this.driver.connect(options); return this; } /** * Close the connection with database in this instance. */ async close() { await this.driver.close(); } /** * Close all the connections with all databases. */ async closeAll() { const cons = ConnectionFactory.availableConnections(); const promises = cons.map(con => { const driver = ConnectionFactory.fabricate(con); return driver.close().then(() => ConnectionFactory.setClient(con, null)); }); await Promise.all(promises); } /** * Return the client of driver. */ getClient() { return this.driver.getClient(); } /** * Return the query builder of driver. */ getQueryBuilder() { return this.driver.getQueryBuilder(); } /** * Create a new transaction. */ async startTransaction() { return this.driver.startTransaction(); } /** * Run database seeders. */ async runSeeders(options = {}) { options = Options.create(options, { classes: [], task: null, path: Path.seeders() }); const seeds = await Module.getAllFrom(options.path); for (const Seed of seeds) { if (options.classes?.length && !options.classes.includes(Seed.name)) { continue; } if (!options.task) { await new Seed().run(this); continue; } const msg = `Running "${Seed.name}" seeder`; options.task.addPromise(msg, () => new Seed().run(this)); } } /** * Run database migrations. */ async runMigrations() { await this.driver.runMigrations(); } /** * Revert database migrations. */ async revertMigrations() { await this.driver.revertMigrations(); } /** * List all databases available. */ async getDatabases() { return this.driver.getDatabases(); } /** * Get the current database name. */ async getCurrentDatabase() { return this.driver.getCurrentDatabase(); } /** * Verify if database exists. */ async hasDatabase(database) { return this.driver.hasDatabase(database); } /** * Create a new database. */ async createDatabase(database) { await this.driver.createDatabase(database); } /** * Drop some database. */ async dropDatabase(database) { await this.driver.dropDatabase(database); } /** * List all tables available. */ async getTables() { return this.driver.getTables(); } /** * Verify if table exists. */ async hasTable(table) { return this.driver.hasTable(table); } /** * Create a new table in database. */ async createTable(table, closure) { await this.driver.createTable(table, closure); } /** * Alter a table in database. */ async alterTable(table, closure) { await this.driver.alterTable(table, closure); } /** * Drop a table in database. */ async dropTable(table) { await this.driver.dropTable(table); } /** * Remove all data inside some database table * and restart the identity of the table. */ async truncate(table) { return this.driver.truncate(table); } /** * Make a raw query in database. */ raw(sql, bindings) { return this.driver.raw(sql, bindings); } /** * Creates a new instance of QueryBuilder for this table. */ table(table) { return new QueryBuilder(this.driver, table); } }