nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better Typescript support in ormconfig?

brandonchinn178 opened this issue · comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

Not sure whether this is a TypeORM thing, or a NestJS thing, or a NestJS/TypeORM integration thing, so I'm making an issue here. Hopefully it can get triaged in the right place, or at least provide a place for people to find in a web search.

Came across this error when configuring NestJS and TypeORM: https://stackoverflow.com/questions/59435293/typeorm-entity-in-nestjs-cannot-use-import-statement-outside-a-module

I would like to use .ts migration files, discovering *.entity.ts files when using the typeorm CLI (as documented by TypeORM, e.g. "typeorm": "ts-node node_modules/typeorm/cli.js" in package.json), but when running nest start, it errors saying that it's trying to load in the Typescript files as Javascript.

// ormconfig.js
{
  ...
  entities: ['**/*.entity.ts'],
  migrations: [migration/*.ts],
}

but if I change these to .js and run rm -rf dist, running typeorm migration:show shows no migrations, because I haven't built anything yet. I would like to generate migrations without needing to build anything.

My current solution is to do the following in package.json:

{
  "scripts": {
    "typeorm": "TS_NODE=true ts-node node_modules/typeorm/cli.js",
  },
}

and then in ormconfig.js do:

const IS_TS_NODE = !!process.env.TS_NODE
{
  entities: [IS_TS_NODE ? '**/*.entity.ts' : 'dist/**/*.entity.js'],
  migrations: [IS_TS_NODE ? 'migration/*.ts' : 'dist/migration/*.js'],
}

which is... less than ideal.

Expected behavior

Either better documentation on how to do this, or some better way to specify this type of configuration, which I imagine is a commonly desired configuration.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

I would imagine most people using NestJS/TypeORM to be using Typescript, and this added complexity is less than ideal.

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Not sure whether this is a TypeORM thing, or a NestJS thing, or a NestJS/TypeORM integration thing, so I'm making an issue here. Hopefully it can get triaged in the right place, or at least provide a place for people to find in a web search.

This is a TypeORM thing :) You can report this issue here https://github.com/typeorm/typeorm

Moving discussion over to typeorm/typeorm#5763