Reputation: 61
I tried this a lot but now I think I need some help. I have a nx-monorepo with angular-frontend and nestjs backend. Now I added a Postgres-SQL database with TypeORM. Everything is working fine, connection to Database is running, api-calls coming in, etc. But I can't add the generate migration script. I added a run-command for my backend-project:
"generate-migration": {
"builder": "@nrwl/workspace:run-commands",
"outputs": [],
"options": {
"command": "ts-node ../../node_modules/.bin/typeorm migration:generate -n",
"cwd": "apps/api"
}
}
I can run the command by "npm run api:generate-migration" but it results with this error:
Error during migration generation: /apps/api/src/model/base.entity.ts:1 import { __decorate, __metadata } from "tslib"; ^^^^^^
SyntaxError: Cannot use import statement outside a module at wrapSafe (node:internal/modules/cjs/loader:1024:16) at Module._compile (node:internal/modules/cjs/loader:1072:27) at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1056:23) at Module._extensions..js (node:internal/modules/cjs/loader:1137:10) at Object.require.extensions. [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1059:12) at Module.load (node:internal/modules/cjs/loader:973:32) at Function.Module._load (node:internal/modules/cjs/loader:813:14) at Module.require (node:internal/modules/cjs/loader:997:19) at require (node:internal/modules/cjs/helpers:92:18) at /Users/IsabelleGlasmacher/Documents/Programme/save_easytonomy/src/util/DirectoryExportedClassesLoader.ts:41:22 ERROR: Something went wrong in @nrwl/run-commands - Command failed: ts-node ../../node_modules/.bin/typeorm migration:generate -n
ormConfig.json:
{
...
"type": "postgres",
"migrationsTableName": "migration",
"migrations": ["src/migration/*.ts"],
"cli": {
"migrationsDir": "src/migration"
},
"ssl": false
}
Does anybody know something about that and could help me? Please :)
Upvotes: 6
Views: 7298
Reputation: 3041
Hope this is helpful for some people.
For the latest nx@^14
, @nestjs/*@^9
and typeorm@~0.3
:
in your project.json
. Note that "apps/<app-name>"
can be applied to "libs/<lib-name>"
as well
"typeorm": {
"builder": "@nrwl/workspace:run-commands",
"outputs": [],
"options": {
"command": "ts-node --project tsconfig.json <path-to-node_modules>/typeorm/cli",
"cwd": "apps/<app-name>"
}
},
"mig-gen": {
"builder": "@nrwl/workspace:run-commands",
"outputs": [],
"options": {
"command": "nx run <app-name>:typeorm migration:generate <path-to-migration>/{args.name} -d <path-to-typeorm-config>/typeorm.config.ts",
"cwd": "apps/<app-name>"
}
},
In your typeorm.config.ts
import { DataSource, DataSourceOptions } from 'typeorm';
import { config } from 'dotenv';
import { join } from 'path';
config({ path: '<path-to-env>/.env' });
const { DB_TYPE, DB_HOST, DB_USER, DB_PASS, DB_PORT, DB_NAME } =
process.env;
export default new DataSource({
type: DB_TYPE,
host: DB_HOST,
port: Number(DB_PORT),
username: DB_USER,
password: DB_PASS,
database: DB_NAME,
entities: [join(__dirname, '<path-to-entities>/*.entity.{ts,js}')],
migrations: [join(__dirname, '<path-to-migration>/*{.ts,.js}')],
synchronize: false,
} as DataSourceOptions);
in your <path-to-app>/tsconfig.json
make sure inside compilerOptions{...}
, property "emitDecoratorMetadata": true
is set. Properties module
,types
and target
may need to be set as well. See your <root-workspace>/tsconfig.base.json
and documentation https://www.typescriptlang.org/tsconfig for reference
Finally in your terminal, you can run the command like this to generate your migration file
nx run <app-name>:mig-gen --name=whatever-makes-you-happy
Upvotes: 2
Reputation: 6255
I am able to make a backend service using NX@13
, NestJS@8
and [email protected]
,
In project.json
of game-api
, I added some commands like this
"generate-migration": {
"builder": "@nrwl/workspace:run-commands",
"outputs": [],
"options": {
"command": "ts-node --project tsconfig.app.json ../../node_modules/.bin/typeorm migration:generate --pretty -f .env.local",
"cwd": "apps/game-api"
}
},
Code repository: https://github.com/coinconket/conketkemon
Upvotes: 4
Reputation: 9
Adding "module": "CommonJS"
to compilerOptions
in tsconfig.app.json of the related nx app fixed the issue for me.
Upvotes: 0