CLI
Install the CLI globally, or run it with npx:
npm i -g @meghjs/cliCommand Overview
| Command | Alias | Description |
|---|---|---|
new | Create a new application | |
serve | Start the dev server with watch + rebuild | |
build | Compile using tsconfig.build.json | |
make:model | m:m | Create a model: a plain class, a MeghORM model with --orm (columns from its migrations), or a TypeORM entity with --typeorm |
sync:model | s:m | Sync an existing model's columns with its migrations (was update:model) |
make:resource | m:r | Create a REST resource for a model: DTOs, CRUD service and controller |
make:controller | m:c | Create a controller |
make:service | m:s | Create a service |
make:config | m:cnf | Create a config class |
make:middleware | m:mlwr | Create a middleware |
make:mail | m:mail | Create a Mailable in src/mail/ |
make:event | m:ev | Create an event in src/events/ |
make:listener | m:lsn | Create an event listener in src/listeners/ (--event to type it) |
make:queue | m:q | Create a queue with a job handler in src/queues/ |
make:worker | m:w | Create a worker for a queue in src/workers/ |
make:task | m:task, make:scheduler | Create a scheduled task in src/tasks/ (--cron, --interval) |
make:dto | m:dto | Create a class-validator DTO in src/dtos/ |
make:class | m:cls | Create a plain class in src/classes/ |
make:test | m:t | Create a unit test (<name>.spec.ts) for an existing file |
make:migration | m:mig | Create a database migration |
make:seeder | m:sd | Create a database seeder |
make:factory | m:f | Create a model factory with Faker values |
migrate | Run pending migrations (--seed to seed afterwards) | |
migrate:status | Show which migrations have run | |
migrate:rollback | Undo the last migration (--step <n> for more) | |
migrate:reset | Undo every migration | |
migrate:refresh | Undo every migration, then run them all again (--seed too) | |
db:seed | Run the seeders (--seeder <name> for one) | |
scaffold:domain | sc:domain | Create the tables and models (or resources) of a business domain |
scaffold:auth | sc:auth | Add sign-up and sign-in with JWTs and/or API keys, optionally with roles |
scaffold:rbac | sc:rbac | Add roles and permissions to an app's users |
template:list | templates | List the model templates |
Every make:* command takes -f / --force to overwrite an existing file. The generators except make:model and the database ones (make:migration, make:seeder, make:factory) also write a unit test next to the file; pass --notest (or -nt) to skip it. See Tests. make:model and make:resource also build the table from a template with --template or --fields.
Pages
| Page | Commands |
|---|---|
| Generators | make:resource, make:controller, make:service, make:model, sync:model, make:middleware, make:config, make:mail, make:event, make:listener, make:queue, make:worker, make:task, make:dto, make:class, make:test |
| Database Commands | make:migration, migrate*, make:seeder, db:seed, make:factory |
| Scaffold | --template, --fields, scaffold:domain, scaffold:auth, scaffold:rbac, template:list |
Version and info
npx megh -v # the Megh logo and the CLI version (also --version)
npx megh --info # the logo, CLI, Node.js and OS versions, and the project's Megh packagesThe logo appears only here; megh --help and the other commands print without it. --info also shows the installed @meghjs/core, @meghjs/data and TypeScript versions when run inside an app: paste it into a bug report.
Application
npx @meghjs/cli new myapiYou are prompted for a package manager and optional features: OpenAPI docs, CORS and MeghORM. With MeghORM you also pick the database. Add Mail, Scalar UI or the @meghjs/data plugin afterwards. Non-interactively:
npx @meghjs/cli new myapi --yes --database postgres # also: mysql, sqlite, mssql--database (or the orm feature, which defaults to SQLite) installs the driver, adds app.useMeghORM() to src/app.ts, writes DATABASE_URL to .env and creates src/database/migrations/.
The new project is put under git with git init, unless git isn't installed or the folder is already inside a repository. Pass --skip-git (or set MEGH_SKIP_GIT=1) to skip it. Then:
cd myapi
npm run start:devDevelopment Server
npx megh serveCompiles src/ with SWC into dist/, starts the app and restarts it on change. The entry point comes from megh.entry in package.json, falling back to main.
Build
npx megh buildRemoves the output folder (outDir, usually dist/) and compiles the project with tsconfig.build.json.