typeorm: TypeORM does not work with Typscript and NestJS
Issue description
Can’t generate migrations
Expected Behavior
I expect migration:generate to create a migration file.
Actual Behavior
I run this command:
pnpm run typeorm migration:generate -- ./src/migrations/ -d ./src/books.datasource.ts
Which is adapted from the documentation of how to get the typorm CLI to run in Typescript environments: https://typeorm.io/using-cli#if-entities-files-are-in-typescript.
That seems to run:
> typeorm-ts-node-commonjs "migration:generate" "--" "./src/migrations/" "-d" "./src/books.datasource.ts"
And then comes back with:
Not enough non-option arguments: got 0, need at least 1
ELIFECYCLE Command failed with exit code 1.
Steps to reproduce
See above for the exact commands
My Environment
| Dependency | Version |
|---|---|
| Operating System | macOS |
| Node.js version | 18.17.1 |
| Typescript version | 4.9.5 |
| TypeORM version | 0.3.17 |
Additional Context
No response
Relevant Database Driver(s)
- aurora-mysql
- aurora-postgres
- better-sqlite3
- cockroachdb
- cordova
- expo
- mongodb
- mysql
- nativescript
- oracle
- postgres
- react-native
- sap
- spanner
- sqlite
- sqlite-abstract
- sqljs
- sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don’t know how to start. I would need guidance.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (11 by maintainers)
A user is just one person, the project and the companies behind it must do better:
One user having trouble with a thing is an indication of many more who never bother to write a report.
I think maybe you haven’t configured your tsconfig correctly and I have a different application structure to you so it might not apply to you because I am using a monorepo. But here is an example
project_folder
// project_folder/db/tsconfig.app.json
Scripts in my package.json
npm run migration:generate -- db/migrations/<name of migration>The migrations go into the /migrations folder.
npm run migration:runNOTE: You have to make sure that the configuration of your datasource is correct and you are able to connect to it locally using your client. Test the connection to your DB first before trying to run a migration.
Migrations automatically create a
migrationstable in your DB which it uses to keep track of what/how many migrations have already ran.Also this is the database module I creataed which is used by my services.
// libs/common/src/modules/database.module.ts
TypeORM definitely works with Typescript and NestJs, I am currently using it for my project and have used it with previous projects.
You haven’t provided your typeorm datasource so it’s hard to even begin helping you debug your DataSourceOptions.
Here is an example of my data-source.ts file in db/
Here is an example of my migration scripts in package.json
Here is an example of how I use them:
npm run build:migrationsnpm run migration:generate -- db/migrations/<name of migration>I found out the problem:
Trilon has models and entities with the same name and TypeORM had imported the models without the
Entity()annotation and there’s no way of seeing this.Any way to see an error or to be able to introspect what’s happening other than logging the entire datasource object would have been nice here.