1
votes

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);
    }

}
1
Have you tried removing javascript files from the migrations (or vice versa)? Sounds like it's trying to execute as JS and not TS, can you show your migration file or atleast a minimal version of it? - Isolated
@Isolated I added migration file to the main message. TypeORM generates .ts migration and it should be ok (according to their docs). I run it with ts-node (also specified in typeorm docs) and it applied to the database without problems - Daria Peters
I'm not at a PC so this is just basic debugging, my final thought would be set entitiesDir: to your TS entities in CLI, I'll be at my PC shortly so I'll see if I can reproduce it in my nest.js - Isolated

1 Answers

1
votes

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}"],