如何配置 TypeORM ormconfig.json 文件以从 js dist 文件夹或 ts src 文件夹解析实体?
How configure TypeORM ormconfig.json file to parse Entities from js dist folder or ts src folder?
我将我的 TypeORM 配置实体路径设置为:
"entities": ["src/entities/**/*.ts"]
这在我使用 ts-node 时效果很好。 ts-node src/main.ts
使用 tsc
编译打字稿后,我得到了一个 dist
文件夹,其中包含已编译的应用程序:
但是,typeORM 仍会尝试从 src
文件夹而不是 dist
中获取实体。抛出很多意想不到的语法错误来解析 TS 文件而不是 JS。所以我将休闲字符串更改为实体配置:
"entities": ["dist/entities/**/*.js"]
它适用于节点 node dist/main.js
但不适用于 ts-node src/main.ts
我如何配置 ormconfig.json
才能同时使用两者(node
在 dist
文件夹和 ts-node
在 src
文件夹)?
我建议使用 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
环境变量;我确实注意到 there's a PR 尚未合并,这将完成。
在对现有答案的补充中:
我不太喜欢总是在 dist 文件夹上工作的想法。为什么?因为当我从命令行 运行 命令时,我不想检查 dist 是否是 up-to-date,意思是最近编译的。
所以,我喜欢我的 typeorm
命令,例如 typeorm schema:sync
在 src
上的工作!您可以通过 运行 通过 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 文件的更改。此处描述的方式编译打字稿 on-the-fly 不需要编译。资料来源:https://github.com/typeorm/typeorm/blob/master/docs/faq.md#how-to-use-typeorm-with-ts-node
基于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)
});
我将我的 TypeORM 配置实体路径设置为:
"entities": ["src/entities/**/*.ts"]
这在我使用 ts-node 时效果很好。 ts-node src/main.ts
使用 tsc
编译打字稿后,我得到了一个 dist
文件夹,其中包含已编译的应用程序:
但是,typeORM 仍会尝试从 src
文件夹而不是 dist
中获取实体。抛出很多意想不到的语法错误来解析 TS 文件而不是 JS。所以我将休闲字符串更改为实体配置:
"entities": ["dist/entities/**/*.js"]
它适用于节点 node dist/main.js
但不适用于 ts-node src/main.ts
我如何配置 ormconfig.json
才能同时使用两者(node
在 dist
文件夹和 ts-node
在 src
文件夹)?
我建议使用 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
环境变量;我确实注意到 there's a PR 尚未合并,这将完成。
在对现有答案的补充中:
我不太喜欢总是在 dist 文件夹上工作的想法。为什么?因为当我从命令行 运行 命令时,我不想检查 dist 是否是 up-to-date,意思是最近编译的。
所以,我喜欢我的 typeorm
命令,例如 typeorm schema:sync
在 src
上的工作!您可以通过 运行 通过 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 文件的更改。此处描述的方式编译打字稿 on-the-fly 不需要编译。资料来源:https://github.com/typeorm/typeorm/blob/master/docs/faq.md#how-to-use-typeorm-with-ts-node
基于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)
});