NestJS with Angular & NX - How to do TypeORM migrations? #11007
Replies: 4 comments 5 replies
-
Any help on this one? |
Beta Was this translation helpful? Give feedback.
-
Mostly their site. However, I did get my migrations and seeds working by changing into the main directory of my application within the Monorepo ( Database Migration scriptsInitial migration requires empty schema and no migrations data.
In the individual apps directory (IE: apps/timesheet-api) run:
Apply Migrations:
Roll Back:
Generate Seeder (Create a new seeder class):
Apply Seeders (pre-load data)
|
Beta Was this translation helpful? Give feedback.
-
has anyone achieved this? :D |
Beta Was this translation helpful? Give feedback.
-
I have a nestjs app, and many libs. I find this setup to be nice. apps/api/project.json// these targets live in my apps/api/project.json:
"typeorm": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"commands": [
{
"command": "ts-node -r tsconfig-paths/register -O '{\"module\": \"commonjs\"}' --project apps/api/tsconfig.app.json node_modules/typeorm/cli"
}
]
}
},
"typeorm-migration-generate": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"commands": [
{
"command": "nx run api:typeorm migration:generate libs/your/path/to/migrations/{args.name} -d libs/your/path/to/typeorm.datasource.ts --pretty"
}
]
}
},
"typeorm-migration-create": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"commands": [
{
"command": "nx run api:typeorm migration:create libs/your/path/to/migrations/{args.name}"
}
]
}
},
"typeorm-migration-run": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"commands": [
{
"command": "nx run api:typeorm migration:run -d libs/your/path/to/typeorm.datasource.ts"
}
]
}
} package.json ScriptsI have the following scripts in my package.json: "typeorm:migration:run": "NODE_ENV='development' nx run api:typeorm-migration-run",
"typeorm:migration:generate": "NODE_ENV='development' nx run api:typeorm-migration-generate",
"typeorm:migration:create": "NODE_ENV='development' nx run api:typeorm-migration-create", Generating a MigrationThis assumes you've made changes to your entities... yarn typeorm:migration:generate --name HereIsAName Running Migrationsyarn typeorm:migration:run |
Beta Was this translation helpful? Give feedback.
-
I have a project working outside of NX, that uses NestJS to provide the API for the Angular application. I am looking to move this into NX for all the benefits. The tutorial for Angular, even includes working with NestJS. However, the main part missing, that I cannot find any information on, is getting the TypeORM support working in the NX structure.
I can create my entities in my NestJS project and configure the system to use the database. This all works. However, I do not know how to get my database changes dumped as data migrations, using the TypeORM of NestJS, as the package.json is now in the root directory, and if there are multiple NestJS API projects, the basic typeorm command line to generate migrations does not know which project to use.
In NPM, I would have
Then from the command line, you would run:
npm run typeorm migration:generate -- -n migrationNameHere
I believe the command needs to be moved into the project.json of the API projects, but moving the command line into there produces errors. Adding a step for generating migrations:
This would need someway to get some parameters (the name of migration to generate), but even trying to simply call this with a
--help
in the project entry, command line above, returns an error:How do you go about adding in the ability to use migrations to export the database changes that are made, using the TypeORM and NestJS?
Beta Was this translation helpful? Give feedback.
All reactions