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)

Most upvoted comments

A user is just one person, the project and the companies behind it must do better:

  • Make sure there aren’t generations and generations of outdated docs lying around
  • Don’t write tutorials that are obviously confusing (just because it’s more ‘clean’)
  • Copy the good parts of Rails and Django (which are still years ahead here) instead of reinventing the wheel

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

  • /db
    • /migrations
    • Dockerfile
    • data-source.ts
    • tsconfig.app.json <---- the tsconfig used to build my migrations
  • /apps
  • /libs
  • /dist
  • …
  • tsconfig.build.json
  • tsconfig.json

// project_folder/db/tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "declaration": false,
    "outDir": "../dist"
  },
  "include": [
    "../db/**/*",
    "../libs/common/src/entities",
    "../libs/common/src/enums"
  ],
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}

Scripts in my package.json

"build:migrations": "npx tsc -p db/tsconfig.app.json",
"typeorm": "npx typeorm-ts-node-commonjs -d dist/db/data-source.js",
"migration:generate": "npm run build:migrations && npm run typeorm migration:generate",
"migration:create": "npm run typeorm -- migration:create",
"migration:run": "npm run typeorm -- migration:run",
"migration:revert": "npm run typeorm -- migration:revert"
  1. Build migration dependencies and generate migrations: npm run migration:generate -- db/migrations/<name of migration>

The migrations go into the /migrations folder.

  1. Run the migration npm run migration:run

NOTE: 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 migrations table 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

import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { dataSourceOptions } from 'db/data-source';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { entities, migrations, ...options }: TypeOrmModuleOptions = dataSourceOptions; // Exclude entities & migrations directory from data source

@Module({
  imports: [TypeOrmModule.forRoot({ ...options, autoLoadEntities: true })],
})
export class DatabaseModule {}

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/

import { DataSource, DataSourceOptions } from 'typeorm';

export const dataSourceOptions: DataSourceOptions = {
  type: 'postgres',
  host: process.env.POSTGRES_HOST || 'localhost',
  port: Number(process.env.POSTGRES_PORT) || 5432,
  username: process.env.POSTGRES_USER
  password: process.env.POSTGRES_PASSWORD
  database: process.env.POSTGRES_DB
  entities: ['dist/libs/common/src/entities/*.entity{.ts,.js}'],
  migrations: ['dist/db/migrations/*{.ts,.js}'],
  migrationsTableName: 'migrations',
  // logging: true, // Enable if require to see DB queries/logs
};

// Used by TypeORM CLI for migrations
const dataSource = new DataSource(dataSourceOptions);

export default dataSource;

Here is an example of my migration scripts in package.json

  "build:migrations": "npx tsc -p db/tsconfig.app.json",
  "typeorm": "npx typeorm-ts-node-commonjs -d dist/db/data-source.js",
  "migration:generate": "npm run typeorm migration:generate",
  "migration:create": "npm run typeorm -- migration:create",
  "migration:run": "npm run typeorm -- migration:run",

Here is an example of how I use them:

  1. Build the migration dependencies: npm run build:migrations
  2. Generating migrations based on a schema: npm 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.