如何配置 TypeORM 的 ormconfig.json 文件以解析来自 js dist 文件夹或 ts src 文件夹的实体?

9

我将TypeORM配置实体路径设置为:

"entities": ["src/entities/**/*.ts"]

当我使用ts-node时,它可以正常工作。ts-node src/main.ts

使用tsc编译typescript后,我得到了一个带有编译应用程序的dist文件夹:

但是,typeORM仍然试图从src文件夹获取实体,而不是dist。这将导致解析TS文件而不是JS文件出现许多意外的语法错误。因此,我更改了以下字符串以进行实体配置:

"entities": ["dist/entities/**/*.js"]

当使用 node 运行 node dist/main.js 时,它可以正常工作,但是当使用 ts-node src/main.ts 时却无法正常工作。

我应该如何配置 ormconfig.json,以使其能够同时与位于 dist 文件夹下的 node 和位于 src 文件夹下的 ts-node 正常工作?

3个回答

14

我建议使用ormconfig.js而不是JSON版本,并使用环境变量或类似方法切换这两个配置文件。例如,像下面简化的示例一样。

const srcConfig = {
  "entities": [
    "src/entities/**/*.ts"
  ],
}

const distConfig = {
  "entities": [
    "dist/entities/**/*.js"
  ],
}

module.exports = process.env.TS_NODE ? srcConfig : distConfig;

请注意,您需要在某处设置TS_NODE环境变量;我注意到有尚未合并的PR可以做到这一点。


对于正在寻找相关PR的人们,它已经被合并了,通过在进程变量上包含一个符号。如果您正在使用_ts_node_运行,并且想要分支您的代码,您可以检查process[Symbol.for("ts-node.register.instance")]是否为_truthy_。 - alainschaller

0

除了现有的答案之外:

我不太喜欢总是在dist文件夹上工作的想法。为什么?因为当我从命令行运行命令时,我不想检查dist是否是最新的,也就是最近编译过。

所以,我喜欢我的typeorm命令,比如typeorm schema:syncsrc上工作!你可以通过ts-node来运行它们。

因此,不是

typeorm schema:sync

使用

// Linux
ts-node ./node_modules/.bin/typeorm schema:sync

// Windows
ts-node ./node_modules/typeorm/cli.js schema:sync

背景

typeorm cli使用node,它依赖于文件被编译为javascript。因此,这只能在/dist文件夹上工作,也就是编译版本。但是,这需要一个监视器或类似的运行来捕获ORM文件的更改。这里描述的方法可以实时编译typescript,无需进行编译。来源:https://github.com/typeorm/typeorm/blob/master/docs/faq.md#how-to-use-typeorm-with-ts-node


0

基于lock的回答:

我的完整ormconfig.js文件

//npm install --save "detect-ts-node"
const detectTSNode = require('detect-ts-node');

const commonConfig = {
    "type": "mssql",
    "host": "127.0.0.1",
    "port": 1433,
    "username": "sa",
    "password": "$$$$$$",
    "database": "$$$$$$",
    "synchronize": true,
    "logging": false,
    "options": {
        "encrypt": false,
        "enableArithAbort": false
    }
};


const srcConfig = {
    "entities": [
        "src/entity/**/*.ts"
    ],
    "migrations": [
        "src/migration/**/*.ts"
    ],
    "subscribers": [
        "src/subscriber/**/*.ts"
    ],
    "cli": {
        "entitiesDir": "src/entity",
        "migrationsDir": "src/migration",
        "subscribersDir": "src/subscriber"
    }
};

const distConfig = {
    "entities": [
        __dirname +  "/dist/entity/**/*.js"
    ],
    "migrations": [
        __dirname + "/dist/migration/**/*.js"
    ],
    "subscribers": [
        __dirname +  "/dist/subscriber/**/*.js"
    ],
    "cli": {
        "entitiesDir": __dirname +  "/dist/entity",
        "migrationsDir": __dirname +  "/dist/migration",
        "subscribersDir": __dirname + "/dist/subscriber"
    }
};


const result = {};
let key;

// Append common configs to final object
for (key in commonConfig) {
    if (commonConfig.hasOwnProperty(key)) {
        result[key] = commonConfig[key];
    }
}

if (detectTSNode) {
    // if ts-node append src configuration
    for (key in srcConfig) {
        if (srcConfig.hasOwnProperty(key)) {
            result[key] = srcConfig[key];
        }
    }
} else {
    // else append dist configuration
    for (key in distConfig) {
        if (distConfig.hasOwnProperty(key)) {
            result[key] = distConfig[key];
        }
    }

}


module.exports = result;

在 console.ts 中的使用(我的主文件名)

import { createConnection } from "typeorm";
const conf = require('../ormconfig.js');

// Print the result for debuggin purposes
console.log(conf);

createConnection(conf).then(async connection => {
   console.log("do your job here")
}).catch(error => {
    console.log(error)
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接