@megaorm/cli
Version:
This package allows you to communicate with MegaORM via commands directly from the command line interface (CLI).
75 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClearCommand = void 0;
const SeederHandler_1 = require("../handlers/SeederHandler");
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 clear tables using seeder files.
*
* The command performs the following actions:
* - Resolves the path to the seeders folder based on the configuration.
* - Clears all tables associated with seeder files by default.
* - Optionally allows specifying a single table to clear instead of all tables.
*
* This ensures that seeded data can be efficiently removed while providing flexibility
* to target specific tables or clear all tables if needed.
*
* @extends MegaCommand
*/
class ClearCommand extends MegaCommand_1.MegaCommand {
/**
* Resolves the appropriate path for the seeders folder based on the configuration.
*
* @param config The MegaORM configuration object.
* @returns The resolved path for the seeders folder.
* @throws `MegaCommandError` if `paths.seeders` is absolute and TypeScript is enabled.
*/
static path(config) {
if (config.typescript.enabled === true) {
// Cannot be absolute
if ((0, path_1.isAbsolute)(config.paths.seeders)) {
throw new MegaCommand_1.MegaCommandError(`paths.seeders 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.seeders);
}
return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.seeders);
}
if ((0, path_1.isAbsolute)(config.paths.seeders)) {
return config.paths.seeders;
}
return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.seeders);
}
/**
* Executes the command to clear seeded data from tables.
*
* The command clears all tables associated with seeder files by default.
* If a specific table is provided as an argument, only that table will be cleared.
*
* @returns A promise that resolves when the table(s) have been successfully cleared
* or rejects with an error if the clearing operation fails.
*/
static exec() {
return new Promise((resolve, reject) => {
const t = this.argument('table');
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 SeederHandler_1.SeederHandler(builder)))
.then((handler) => handler.clear(p, t))
.then((message) => resolve(this.success(message)))
.catch(reject);
})
.catch(reject);
});
}
}
exports.ClearCommand = ClearCommand;
ClearCommand.syntax = '<? table>';
//# sourceMappingURL=ClearCommand.js.map