根据环境使用不同的 ormconfig.json 文件
Using different ormconfig.json files depending on env
我的ormconfig.json当然是静态的,看起来像:
{
"type": "mariadb",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "moove",
"database": "moove_db",
"synchronize": true,
"logging": false,
"entities": [
"dist/entity/**/*.js"
],
"migrations": [
"dist/migration/**/*.js"
],
"subscribers": [
"dist/subscriber/**/*.js"
],
"cli": {
"entitiesDir": "dist/entity",
"migrationsDir": "dist/migration",
"subscribersDir": "dist/subscriber"
}
}
但是如果我想为我们的生产服务器创建另一个配置怎么办?
我是否创建另一个配置文件?如何将 typeorm 指向另一个配置文件?
目前,我只能将 ormconfig.json
更改为 ormconfig.js
,然后使用环境变量,如下所示:
module.exports = {
"port": process.env.port,
"entities": [
// ...
],
"migrations": [
// ...
],
"subscribers": [
// ...
],
"cli": {
// ...
}
}
不要使用 ormconfig.json。您可以将配置对象直接传递给 createConnection(),如
import { createConnection } from "typeorm";
const config:any = {
"port": process.env.port || "28017",
"entities": [
// ...
],
"migrations": [
// ...
],
"subscribers": [
// ...
],
"cli": {
// ...
}
}
createConnection(config).then(async connection => {
await loadPosts(connection);
}).catch(error => console.log(error));
我的ormconfig.json当然是静态的,看起来像:
{
"type": "mariadb",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "moove",
"database": "moove_db",
"synchronize": true,
"logging": false,
"entities": [
"dist/entity/**/*.js"
],
"migrations": [
"dist/migration/**/*.js"
],
"subscribers": [
"dist/subscriber/**/*.js"
],
"cli": {
"entitiesDir": "dist/entity",
"migrationsDir": "dist/migration",
"subscribersDir": "dist/subscriber"
}
}
但是如果我想为我们的生产服务器创建另一个配置怎么办? 我是否创建另一个配置文件?如何将 typeorm 指向另一个配置文件?
目前,我只能将 ormconfig.json
更改为 ormconfig.js
,然后使用环境变量,如下所示:
module.exports = {
"port": process.env.port,
"entities": [
// ...
],
"migrations": [
// ...
],
"subscribers": [
// ...
],
"cli": {
// ...
}
}
不要使用 ormconfig.json。您可以将配置对象直接传递给 createConnection(),如
import { createConnection } from "typeorm";
const config:any = {
"port": process.env.port || "28017",
"entities": [
// ...
],
"migrations": [
// ...
],
"subscribers": [
// ...
],
"cli": {
// ...
}
}
createConnection(config).then(async connection => {
await loadPosts(connection);
}).catch(error => console.log(error));