UNPKG

@megaorm/cli

Version:

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

111 lines (109 loc) 5.56 kB
import { MegaBuilder } from '@megaorm/builder'; /** * Custom error class for handling errors related to the seeding operations. */ export declare class SeederHandlerError extends Error { } /** * Manages seeder files, including their creation, execution, and cleanup. This class * provides functionality for adding, seeding, clearing, and removing seeder files, allowing * for efficient population and cleanup of initial data in database tables. * */ export declare class SeederHandler { /** * The database builder instance used for executing queries. */ private builder; /** * The absolute path to the assets directory. */ private assets; /** * Instantiates a new SeederHandler with the specified database builder. * * @param builder The database builder instance used for constructing seeder queries. * @throws `SeederHandlerError` if the provided builder is not a valid instance of `MegaBuilder`. */ constructor(builder: MegaBuilder); /** * Collects the absolute paths of seeder files from the specified directory. * * @param path The directory path from which to collect seeder files. * @param map A boolean indicating whether to include seeder map files (e.g., `.js.map`) in the resulting paths. * @returns A promise that resolves with an array of absolute paths to valid seeder files. * @throws `SeederHandlerError` if there is an issue collecting the files or if an invalid seeder file is encountered. */ private collectPaths; /** * Collects exported seeder instances from the specified file paths. * * This method attempts to require each file listed in the provided array of * paths. It expects each file to export either a default instance of `MegaSeeder` * or a named seeder instance. If any of the files do not conform to these * expectations, an error will be thrown. * * ### Behavior * - If no paths are provided or if no valid seeders are found, a * `SeederHandlerError` is thrown with a relevant message. * - For each path, the method tries to: * - Import the module using `require`. * - Check if the imported module is a valid `MegaSeeder` instance. * - If the module exports a named seeder, it will check for the first * property and validate it as a `MegaSeeder`. * * @param paths An array absolute paths to your seeder files. * @returns An array of `MegaSeeder` instances collected from the specified file paths. * @throws `SeederHandlerError` if no seeders are found or if an invalid seeder is encountered in any of the specified paths. */ private collectSeeders; /** * Clears data from a specific table or all tables based on available seeders. * If a table name is provided, only that table is cleared. If not, all tables * associated with seeders in the specified path are cleared. * * @param path The directory path containing seeder files. * @param table (Optional) The name of the table to clear. * @returns A promise resolving with a message indicating the number of tables cleared. * */ clear(path: string, table?: string): Promise<string>; /** * Seeds data into a specific table or all tables based on available seeders. * If a table name is provided, only that table is seeded. If not, all tables * associated with seeders in the specified path are seeded. * * @param path The directory path containing seeder files. * @param table (Optional) The name of the table to seed. * @returns A promise resolving with a message indicating the number of tables seeded. * */ seed(path: string, table?: string): Promise<string>; /** * Creates a new seeder file in the specified path. * * This method creates a new file for the specified table name in the given directory path. * You can choose to create a TypeScript file or a JavaScript file based on the provided boolean flag. * * @param name The snake_case table name for which the seeder is created. * @param path The directory path where the seeder file will be created. * @param ts A boolean indicating whether to create a TypeScript file (`true`) or a JavaScript file (`false`). * @returns A promise that resolves with a success message indicating the path of the created seeder file. * @throws `SeederHandlerError` If the table name is invalid, the path is empty, or if any errors occur during file operations. * @note The table name must be a snake_case table name, e.g., users, category_product. */ add(name: string, path: string, ts?: boolean): Promise<string>; /** * Removes the seeder associated with the given table name from the specified folder path. * * This method first removes the seeder file for the specified table, * and finally updates the numbering of each remaining seeder file in the folder. * * @param name The snake_case table name whose associated seeder is to be removed. * @param path The directory path where the seeder files are located. * @returns A promise that resolves with a success message indicating how many seeder files were removed. * @throws `SeederHandlerError` If the table name is invalid, the path is empty. * @note The table name must be a snake_case table name, e.g., users, category_product. */ remove(name: string, path: string): Promise<string>; }