UNPKG

@adonisjs/lucid

Version:

SQL ORM built on top of Active Record pattern

98 lines (97 loc) • 3.5 kB
/* * @adonisjs/lucid * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { BaseCommand, flags } from '@adonisjs/core/ace'; import { DatabaseAdministrator } from '../src/database_administrator/index.js'; export default class DbCreate extends BaseCommand { static commandName = 'db:create'; static description = 'Create the database of the given connection'; static options = { startApp: true, }; /** * Not a valid connection */ printNotAValidConnection(connection) { this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`); } /** * Run as a subcommand. Never close database connections or exit * process inside this method */ async runAsSubCommand() { const db = await this.app.container.make('lucid.db'); this.connection = this.connection || db.primaryConnectionName; /** * Invalid database connection */ const managerConnection = db.manager.get(this.connection); if (!managerConnection) { this.printNotAValidConnection(this.connection); this.exitCode = 1; return; } /** * The database to create does not exist yet, therefore we cannot * use the lucid connection and instead connect to the dialect * maintenance database */ const administrator = new DatabaseAdministrator(managerConnection.config); try { const databaseName = administrator.databaseName; if (await administrator.databaseExists()) { this.logger.info(`Database "${databaseName}" already exists`); return; } await administrator.createDatabase(); this.logger.success(`Created database "${databaseName}"`); } finally { await administrator.disconnect(); } } /** * Branching out, so that if required we can implement * "runAsMain" separately from "runAsSubCommand". * * For now, they both are the same */ async runAsMain() { await this.runAsSubCommand(); } /** * Handle command */ async run() { if (this.isMain) { await this.runAsMain(); } else { await this.runAsSubCommand(); } } /** * Lifecycle method invoked by ace after the "run" * method. */ async completed() { if (this.isMain) { const db = await this.app.container.make('lucid.db'); await db.manager.closeAll(true); } } } __decorate([ flags.string({ description: 'Define a custom database connection', alias: 'c' }) ], DbCreate.prototype, "connection", void 0);