应该如何配置 TypeScript 项目中的 TypeORM 以使其在任何地方都可以使用? [英] How should TypeORM in a TypeScript project be configured so it works everywhere?

查看:28
本文介绍了应该如何配置 TypeScript 项目中的 TypeORM 以使其在任何地方都可以使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在所有环境中的 TypeScript 项目中找到适用于 TypeORM 的配置.

I'm struggling to find a configuration that works for TypeORM in a TypeScript project in all environments.

例如,从这个ormconfig.js开始:

var dotenv = require("dotenv")
dotenv.config()

var connectionOptions = [
  {
    "name": "default",
    "type": "postgres",
    "url": process.env.DATABASE_URL,
  },
  {
    "name": "testing",
    "type": "postgres",
    "url": `${process.env.DATABASE_URL}_test`,
  }];

module.exports = connectionOptions

但是,当我尝试启动应用程序时,出现此错误:

But then, when I try to start the application, I get this error:

No repository for "User" was found. Looks like this entity is not registered in current "default" connection?

所以,我将实体添加到配置中:

So, I add the entities to the config:

var dotenv = require("dotenv")
dotenv.config()

var connectionOptions = [
  {
    "name": "default",
    "type": "postgres",
    "url": process.env.DATABASE_URL,
    "entities": ["src/entity/**/*"],
  },
  {
    "name": "testing",
    "type": "postgres",
    "url": `${process.env.DATABASE_URL}_test`, // TODO: fix
    "entities": ["src/entity/**/*"],
  }];

module.exports = connectionOptions

此时,在 dev (ts-node-dev src/main.ts) 中运行应用程序有效.但是当我编译它并尝试运行 JavaScript 时,我收到此错误:

At this point, running the app in dev (ts-node-dev src/main.ts) works. But when I compile it and try to run the JavaScript, I get this error:

C:\Users\pupeno\Documents\Flexpoint Tech\js\exp7\backend\src\entity\User.ts:1
import {BaseEntity, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn} from "typeorm"
^^^^^^

SyntaxError: Cannot use import statement outside a module

它正在尝试加载 User.ts 源文件而不是编译后的 User.js.

It's trying to load the User.ts source file instead of the compiled User.js.

我在 prod 中运行我的应用程序的方式是运行 node build/src/main.js,问题是 ormconfig.js 仍然在顶层,打印 __dirname__filename 显示:

The way I'm running my app in prod is by running node build/src/main.js, the problem is that ormconfig.js is still at the top level, printing __dirname and __filename shows:

C:\Users\pupeno\Documents\Flexpoint Tech\js\exp7\backend
C:\Users\pupeno\Documents\Flexpoint Tech\js\exp7\backend\ormconfig.js

你应该如何配置 TypeORM 以在开发和生产中工作?

How are you supposed to configure TypeORM to work in both development and production?

将我的实体重命名为 User.entity.ts 并以这种方式设置实体:

Renaming my entity to User.entity.ts and setting entities this way:

var dotenv = require("dotenv")
dotenv.config()

var connectionOptions = [
  {
    "name": "default",
    "type": "postgres",
    "url": process.env.DATABASE_URL,
    "entities": [__dirname + '/**/*.entity{.ts,.js}'],
  },
  {
    "name": "testing",
    "type": "postgres",
    "url": `${process.env.DATABASE_URL}_test`, // TODO: fix
    "entities": [__dirname + '/**/*.entity{.ts,.js}'],
  }];

console.log(connectionOptions)

module.exports = connectionOptions

导致同样的错误,因为 Node 试图加载一个 TypeScript 文件:

cause the same error, as Node tries to load a TypeScript file:

(node:19344) UnhandledPromiseRejectionWarning: C:\Users\pupeno\Documents\Flexpoint Tech\js\exp7\backend\src\entity\User.entity.ts:1
import {BaseEntity, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn} from "typeorm"
^^^^^^

SyntaxError: Cannot use import statement outside a module

推荐答案

我遇到了类似的问题.解决方案是从构建目录中运行应用程序,而不是项目目录,即执行 cd dist 然后 node app.js 而不是 node dist/app.js.

I got a similar issue. The solution is to run the app from inside the build directory, not the project directory, i.e. do cd dist and then node app.js instead of node dist/app.js.

我的项目结构是:

my-app-name/
-- node_modules/
-- dist/  # there go compiled JS files
-- entity/  # entities in TS
-- migration/  # migrations in TS
-- app.ts
-- tsconfig.json
-- ormconfig.json
-- package.json

# ormconfig.json
{
  # database specific code is omitted
  "entities": [
    "./entity/*.js"
  ],
  "migrations": [
    "./migration/*.js"
  ]
}

# tsconfig.json
{
  "files": [
    "app.ts",
    "entity/author.ts",
    "entity/photo.ts",
    "migration/1618859843962-AddAuthorToPhotos.ts"
  ],
  "compilerOptions": {
    "outDir": "dist",
    "target": "esnext",
    "strict": true,
    "lib": [
      "ES2020"
    ],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node"
  }
}

这篇关于应该如何配置 TypeScript 项目中的 TypeORM 以使其在任何地方都可以使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆