UNPKG

@megaorm/cli

Version:

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

56 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchCommand = void 0; const builder_1 = require("@megaorm/builder"); const MegaCommand_1 = require("../MegaCommand"); const MegaConfig_1 = require("../MegaConfig"); /** * Represents a command to fetch data from a specific table, with an optional row ID filter. * * The `FetchCommand` is designed to retrieve data from a table in the database. You can * specify a particular row by providing its ID. If no ID is provided, all rows from the table * will be fetched. * * The command performs the following actions: * - Constructs a query to fetch data from the specified table. * - Optionally filters the result by the provided row ID. * * This allows for retrieving specific rows from a table, or fetching all rows if no ID is given. * * @extends MegaCommand */ class FetchCommand extends MegaCommand_1.MegaCommand { /** * Executes the command to fetch data from the specified table, optionally filtering by row ID. * * The command fetches all rows from the specified table. If an ID is provided, it filters * the result to include only the row with the given ID. * * @returns A promise that resolves with the fetched data or rejects with an error if the operation fails. */ static exec() { return new Promise((resolve, reject) => { const t = this.argument('table'); const id = this.argument('id'); MegaConfig_1.MegaConfig.load() .then((config) => { config.cluster .request(config.default) .then((con) => { const builder = new builder_1.MegaBuilder(con); const selector = builder.select().from(t); if (id) selector.where((col) => col('id').equal(id)); return selector.exec(); }) .then((r) => resolve(console.log(r))) .catch(reject); }) .catch(reject); }); } } exports.FetchCommand = FetchCommand; // table name is required and id is optional FetchCommand.syntax = '<! table> <? id>'; //# sourceMappingURL=FetchCommand.js.map