Search code examples
postgresqltypescriptnestjstypeorm

NestJS TypeORM syntax error in migration file


I was going through the NestJS official docs. I set up PostgreSQL on Heroku, connected with TypeORM, run a migration and after that my app started crushing. I tried different approaches and searched blogs/issues on github/questions here, but nothing helped.

Here is an error:

[Nest] 46723   - 05/10/2020, 6:33:42 PM   [InstanceLoader] TypeOrmModule dependencies initialized +84ms
[Nest] 46723   - 05/10/2020, 6:33:43 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +493ms
/Users/Shared/diploma/be/migration/1589119433066-AddUser.ts:1
import {MigrationInterface, QueryRunner} from "typeorm";
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Function.PlatformTools.load (/***/PROJECT_ROOT/node_modules/typeorm/platform/PlatformTools.js:114:28)
    at /***/PROJECT_ROOT/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:69
    at Array.map (<anonymous>)

Here is my ormconfig.json:

  "type": "postgres",
  "url": "postgres://***",
  "ssl": true,
  "extra": {
    "ssl": {
      "rejectUnauthorized": false
    }
  },
  "entities": ["dist/**/*.entity{.ts,.js}"],
  "migrationsTableName": "custom_migration_table",
  "migrations": ["migration/*{.ts,.js}"],
  "cli": {
    "migrationsDir": "migration"
  }
}

migration was generated using ts-node ./node_modules/.bin/typeorm migration:generate -n AddUser I'm using nest start --watch command to start the app.

Migration file {TIMESTAMP}-AddUser.ts:

import {MigrationInterface, QueryRunner} from "typeorm";

export class AddUser1589119433066 implements MigrationInterface {
    name = 'AddUser1589119433066'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE "users" (...)`, undefined);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DROP TABLE "users"`, undefined);
    }

}

Solution

  • Thanks to @Isolated! I've changed the ormconfig.json so entities and migrations files looks like this now and it works fine for me:

    "entities": ["dist/**/*.entity{.ts,.js}"],
    "migrations": ["dist/migration/*{.ts,.js}"],