ljlm0402 / typescript-express-starter

📘 Quick and Easy TypeScript Express Starter
http://npm.im/typescript-express-starter
MIT License
2.71k stars 419 forks source link

TypeORM settings missing for cli to work #210

Open Ctesias opened 1 year ago

Ctesias commented 1 year ago

Describe the Bug (버그 설명)

When you generate the starter with the TypeORM option it doesn't set up everything necessary to work with typeORM, using npx typeorm ... response with the following error:

C:\Users\me\git\project\src\entities\conferences.entity.ts:1
import { IsNotEmpty } from 'class-validator';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at C:\Users\derek_000\git\nilo-backend\node_modules\typeorm\util\ImportUtils.js:29:52
    at step (C:\Users\derek_000\git\nilo-backend\node_modules\tslib\tslib.js:143:27)

Version to Reproduce (현재 사용한 버전)

starter version 9.1.1

Steps to Reproduce (재현 순서)

  1. run the starter tool
  2. choose typeORM
  3. go to the project in your CLI
  4. attempt to generate/run a migration with typeORM CLI npx typeorm migration:generate

Expected Behavior (예상 동작)

typeORM CLI functions should work

Actual Behavior (실제 동작)

typeORM CLI commands won't run, typeORM can't find DB connection information

Additional Context (추가 사항)

I fixed it with the following steps.

  1. Add ormconfig.json to the root of the project directory for typeorm to find connection info
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { config } = require('dotenv');
config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });

const { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_DATABASE } = process.env;

module.exports = {
  type: 'postgres', //use appropriate type here
  host: DB_HOST,
  port: DB_PORT,
  username: DB_USER,
  password: DB_PASSWORD,
  database: DB_DATABASE,
  synchronize: false,
  logging: true,
  migrationsRun: true,
  entities: ['src/entities/**/*.ts'],
  migrations: ['src/database/migrations/**/*.ts'],
  subscribers: ['src/subscribers/**/*.ts'],
  cli: {
    entitiesDir: 'src/entities',
    migrationsDir: 'src/database/migrations',
    subscribersDir: 'src/subscribers',
  },
};
  1. Use ts-node to run typeorm cli commands from .ts files. I made this work by adding this to the scripts section of package.json:

"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"

now you can generate migrations with npm run typeorm -- migration:generate and run them with npm run typeorm -- migration:run