Published on

Configuring TypeORM

Authors

Configuring TypeORM happens through the ormconfig.json file or similar file that sits in the root of your project.

One thing that can trip people up is knowning when to refer to the transpiled JavaScript or the original TypeScript. In general most of the configuration options use the transpiled JavaScript as TypeORM works at runtime and not at build time. The main case where TypeScript is used in your config is when you are confguring the TypeORM cli which is a build/compile time tool. An example ormconfig.json has been included below:

{
  "type": "postgres",
  "database": "postgres",
  "username": "postgres",
  "password": "your super super secure password",
  "logging": false,
  "migrations": [
    "dist/migration/**/*.js"
  ],
  "entities": [
    "dist/entity/**/*.js"
  ],
  "cli": {
    "entitiesDir": "ts/src/entity",
    "migrationsDir": "ts/src/migration"
  }
}

The best way to use TypeORM is by installing it as a dev dependency in your project's package.json file and adding a typeorm:cli script:

  • yarn add -D typeorm
  • Add a new script in your package.json:
...
    "scripts": {
             ...
        "typeorm:cli": "ts-node ./node_modules/typeorm/cli -f ./ormconfig.json"
    },

You then use this via yarn.

It is much easier doing it this way versus globally installing the cli as with the cli you would need to keep converting the files you are working on to JavaScript first and then running against them