Nest framework TypeScript starter repository.
$ pnpm install
$ docker compose up -d
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
NOTE: the migration script runs inside the Docker container
To be able to run these commands, it's best to expose them in your package.json
file in the scripts
section.
Copy and paste the below:
{
"scripts": {
"migration:create": "ts-node -r tsconfig-paths/register src/cli.ts typeorm migration:create",
"migration:generate": "ts-node -r tsconfig-paths/register src/cli.ts typeorm migration:generate",
"migration:run": "ts-node -r tsconfig-paths/register src/cli.ts typeorm migration:run",
"migration:revert": "ts-node -r tsconfig-paths/register src/cli.ts typeorm migration:revert",
"migration:show": "ts-node -r tsconfig-paths/register src/cli.ts typeorm migration:show",
"schema:drop": "ts-node -r tsconfig-paths/register src/cli.ts typeorm schema:drop",
"schema:log": "ts-node -r tsconfig-paths/register src/cli.ts typeorm schema:log",
"schema:sync": "ts-node -r tsconfig-paths/register src/cli.ts typeorm schema:sync",
"typeorm:cache:clear": "ts-node -r tsconfig-paths/register src/cli.ts typeorm cache:clear",
"typeorm:query": "ts-node -r tsconfig-paths/register src/cli.ts typeorm query",
"typeorm:version": "ts-node -r tsconfig-paths/register src/cli.ts typeorm version"
}
}
If you are not using NestJS's monorepo and you use relative paths for imports, as well as no aliases, you may omit the -r tsconfig-paths/register
part of these commands, for readability.
If you have integrated nest-commander via a different file than src/cli.ts
then replace it here.
Now you have access to the following commands:
npm run migration:create MigrationName
- create an empty migration with the given namenpm run migration:generate MigrationName
- automatically create a migration with the given namenpm run migration:run
- run pending migrationsnpm run migration:revert
-- revert last migrationnpm run migration:show
- show list of migrations and their statusnpm run schema:drop
- drop the db schemanpm run schema:log
- show diff between db schema and codebasenpm run schema:sync
- sync the db schema with the codebasenpm run typeorm:cache:clear
- clear query cachenpm run typeorm:query "SELECT * FROM migrations"
- run an SQL querynpm run typeorm:version
- check TypeORM version
This package simply reimplements the TypeORM CLI commands in a "NestJS-way", injecting the application's DataSource
into the commands, so the configuration can be fully reused. The actual command code is 100% based on the original commands and some small features might be missing.
For full visibility:
- the commands use the default
DataSource
(connection) to the database. Support for handling multiple connections / specifying which connection should be used in on TODO list. migration:generate
andmigration:create
only supports TypeScript migrations, no JS files- Output coloring has been removed for simplicity. It might be added later.
- Error handling is simplified and errors simply uncaught.