UNPKG

@megaorm/cli

Version:

This package allows you to communicate with MegaORM via commands directly from the command line interface (CLI).

72 lines 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResetCommand = void 0; const GeneratorHandler_1 = require("../handlers/GeneratorHandler"); const builder_1 = require("@megaorm/builder"); const MegaCommand_1 = require("../MegaCommand"); const MegaConfig_1 = require("../MegaConfig"); const path_1 = require("path"); /** * Represents a command to drop all tables associated with generator files. * * The command performs the following actions: * - Resolves the path to the generators folder based on the configuration. * - Drops all tables associated with the generator files. * * This ensures that the database is cleared of any generator-created tables, * making it easy to reset or reinitialize the database schema. * * @extends MegaCommand */ class ResetCommand extends MegaCommand_1.MegaCommand { /** * Resolves the appropriate path for the generators folder based on the configuration. * * @param config The MegaORM configuration object. * @returns The resolved path for the generators folder. * @throws `MegaCommandError` if `paths.generators` is absolute and TypeScript is enabled. */ static path(config) { if (config.typescript.enabled === true) { // Cannot be absolute if ((0, path_1.isAbsolute)(config.paths.generators)) { throw new MegaCommand_1.MegaCommandError(`paths.generators cannot be absolute if typescript is enabled`); } if ((0, path_1.isAbsolute)(config.typescript.dist)) { return (0, path_1.join)(config.typescript.dist, config.paths.generators); } return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.generators); } if ((0, path_1.isAbsolute)(config.paths.generators)) { return config.paths.generators; } return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.generators); } /** * Executes the command to reset the database by dropping all tables associated with generators. * * The command ensures that all generator-related tables are dropped, providing a fresh start. * This can be useful during development or testing when a clean database state is required. * * @returns A promise that resolves when the tables have been successfully dropped * or rejects with an error if the operation fails. */ static exec() { return new Promise((resolve, reject) => { MegaConfig_1.MegaConfig.load() .then((config) => { const p = this.path(config); MegaConfig_1.MegaConfig.exist(p) .then(() => config.cluster.request(config.default)) .then((con) => Promise.resolve(new builder_1.MegaBuilder(con))) .then((builder) => Promise.resolve(new GeneratorHandler_1.GeneratorHandler(builder))) .then((handler) => handler.reset(p)) .then((message) => resolve(this.success(message))) .catch(reject); }) .catch(reject); }); } } exports.ResetCommand = ResetCommand; //# sourceMappingURL=ResetCommand.js.map