node.js - 生成迁移时如何从秘密存储中动态添加 TypeOrm 配置?
问题描述
我正在尝试与 TypeOrm 建立一个 postgres 连接,该连接从秘密源(异步)动态提取配置并生成新的迁移。
我尝试使用 async await 异步设置在 typeorm-cli 运行时 postgres.config.ts 文件将使用的环境变量和局部变量,但我不断收到错误。
// src/config/defaults/postgres.config.ts
const postgresConfig = (async () => {
try {
const db: PostgresConfig = await secretsSource.getSecret();
return {
type: 'postgres',
host: db.host,
port: db.port,
username: db.username,
password: db.password,
database: db.database,
entities: [path.join(__dirname, '../**/*.entity{.ts,.js}')],
migrationsRun: true,
logging: true,
logger: 'file',
migrations: [path.join(__dirname, '../migrations/**/*{.ts,.js}')],
cli: {
migrationsDir: 'src/migrations',
},
} as ConnectionOptions;
} catch (error) {
// ... handle error
}
})();
export default postgresConfig;
// package.json
{
scripts: {
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/config/defaults/postgres.config.ts",
"typeorm:migrate": "npm run typeorm migration:generate -- -n",
"typeorm:run": "npm run typeorm migration:run"
}
};
我希望在运行“npm run typeorm:migrate initial”时,脚本会指向配置文件异步运行文件,等待代码提供必要的数据库配置,并生成迁移,但似乎脚本没有'不按预期运行任何代码,我不断收到以下错误:
迁移生成期间出错:{ MissingDriverError:错误的驱动程序:给出了“未定义”。支持的驱动程序是:.....}
我需要动态获取数据库配置,因为我有不同的环境需要不同的配置。
解决方案
推荐阅读
- html - 如何更改 .container 的位置
- powerbi - 如何在 DAX 中获取状态持续时间?
- excel - VBA IF Then Else 循环问题,对比较值和逻辑感到困惑
- javascript - Jqgrid:获取应用本地过滤器后的行数
- android - 为什么设置 setGroupSummary 时 Android O 中的通知徽章不显示?
- angular - 如何在 *ngFor 中添加依赖于数据的单个元素?
- c# - 使用 C# 或 Linq 对 MongoDB 和 SQL 使用相同的语法
- mongodb - 在 MongoDb 中按 15 分钟的时间间隔对重叠系列进行分组
- android - 如何使用本地通知获取有关 Firebase 聊天应用程序的通知?
- xamarin.forms - Packacge Sqlite.net.platform.xamarinAndroidN 3.1.1 不兼容 monoandroid9(MonoAndroid, version=v9.0)