我正在开发 Sails.js 应用程序,我需要在运行时在数据存储配置中动态设置 Mongodb 数据库的数据库 URL。这个想法是从公共服务获取包含用户名和密码的数据库 URL。数据库 URL 在应用程序的引导阶段从外部服务 (Infisical) 获取。但是,尽管在
sails.config.datastores.default.url
中成功获取并设置了数据库 URL,但 Sails 似乎无法识别或应用此动态配置,并且我的应用程序无法使用动态设置的 URL 连接到数据库。
这是我的引导配置的相关部分
config/bootstrap.js
:
module.exports.bootstrap = async function (done) {
console.log('Fetching database URL from Infisical');
try {
const dbConfig = await InfisicalService.fetchDatabaseUrl();
console.log('Fetched database URL from Infisical', dbConfig.secret.secretValue);
// Update the Sails datastore configuration with the fetched URL
sails.config.datastores.default.url = dbConfig.secret.secretValue; // Assumes matching response structure from Infisical
return done();
} catch (error) {
console.error('Failed to fetch database URL from Infisical', error);
return done(error);
}
};
我的
config/datastores.js
最初看起来像这样,准备动态更新:
module.exports.datastores = {
default: {
adapter: 'sails-mongo',
// url: `mongodb://localhost:27017/mydatabase`, // This is supposed to be set dynamically in bootstrap
},
};
问题: 当我启动 Sails 应用程序时,如果 datastore.js 的 URL 错误或被注释,它无法连接到数据库,并且根本无法识别新设置的数据库 URL,就像没有 URL 一样放。我已通过日志确认 URL 已正确获取,并且执行了在
url
中设置 sails.config.datastores.default.url
的代码。
问题:
任何关于如何在 Sails.js 中正确实现此功能的见解或指导将不胜感激。
我有同样的问题,我正在从 AWS Secret Manager 获取数据库凭证并希望在 datastore.js 中使用它 但它抛出一个错误。