gpt4 book ai didi

node.js - 如何获取 Loopback 3 条件 'datasources' 和 'model-config' : following instructions and doesn't work

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:16 27 4
gpt4 key购买 nike

编辑:如果我假设“本地”变体只能覆盖已经定义的结构,我就能让它部分工作。也就是说,我需要在 datasources.json 中定义数据源“DynamoDB”和“mongo”,并在 datasources.local.js 中覆盖它们。然而,这不是我们想要做的——当我们部署到 AWS 时,我们不希望 MongoDB 以任何方式参与其中。非常感谢任何帮助!

<小时/>

我们需要根据 Node env 变量配置 Loopback 数据源和模型配置。具体来说,我们需要在本地开发时使用 MongoDB 作为数据库,在部署时使用 DynamoDB。我按照说明here进行操作,其中说 enter image description here

显然,根据this post ,我认为必须拥有“常规”datasources.jsonmodel-config.json,其他文件只能添加到那里定义的内容。

无论如何,我添加了 datasources.local.jsmodel-config.local.js 但仍然收到错误。 .env 文件中的环境变量 LOOPBACK_ENV 已正确设置(在本例中设置为“mongo”)

AssertionError [ERR_ASSERTION]: Event is referencing a dataSource that does not exist: "mongo"
at configureModel (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback/lib/application.js:584:5)
at Function.app.model (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback/lib/application.js:126:5)
at /Users/abc/Documents/projects/abc-adj-new/node_modules/loopback-boot/lib/executor.js:203:9
at Array.forEach (<anonymous>)
at setupModels (/Users/abc/Documents/projects/chatbot-adj-new/node_modules/loopback-boot/lib/executor.js:199:23)
at execute (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback-boot/lib/executor.js:40:3)
at bootLoopBackApp (/Users/abc/Documents/projects/abc-adj-new/node_modules/loopback-boot/index.js:154:3)
at Object.<anonymous> (/Users/abc/Documents/projects/abc-adj-new/server/server.js:79:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}

我将添加以下文件。这是环回 3, Node 12.13。非常感谢任何帮助!

// server/datasources.json
{
"db": {
"name": "db",
"connector": "memory"
},


"emailDataSource": {
"name": "emailDataSource",
"connector": "mail",
"transports": [
{
"type": "smtp",
"host": "smtp-service",
"secure": false,
"port": 25,
"tls": {
"rejectUnauthorized": false
}
}
]
}
}
<小时/>
  //  server/datasources.local.js
if (process.env.LOOPBACK_ENV === "mongo") {
module.exports = {

"dynamodb": {
"name": "dynamodb",
"connector": "dynamodb",
"region": "ap-northeast-1",
"credentials": "shared",
"profile": "iamrole"
}

};
} else {
module.exports = {

"mongo": {
"url": "${MONGO_URL}",
"name": "mongo",
"connector": "mongodb",
"allowExtendedOperators": true,
"useNewUrlParser": true
}
};
}
<小时/>
//  server/model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models",
"../node_modules/bot-analytics/models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins",
"../node_modules/amf-lb-expose-remote-methods-mixin/lib"
]
},
"Variable": {
"dataSource": "db",
"public": true
}

}
<小时/>
// server/model-config.js
if (process.env.LOOPBACK_ENV === "mongo") {
module.exports ={

"Event": {
"dataSource": "mongo",
"public": true
},
"History": {
"dataSource": "mongo",
"public": true
},
"Email": {
"dataSource": "mongo"
},
"FileUpload": {
"dataSource": "mongo",
"public": true
} ,
"ADJUser" : {
"dataSource": "mongo"
}
}

} else {
module.exports = {

"Event": {
"dataSource": "dynamodb",
"public": true
},
"History": {
"dataSource": "dynamodb",
"public": true
},
"Email": {
"dataSource": "dynamodb"
},
"FileUpload": {
"dataSource": "dynamodb",
"public": true
} ,
"ADJUser" : {
"dataSource": "dynamodb"
}
}

}

最佳答案

好吧,我不确定我能否正确回答您的问题 - 但这是我在项目中通常做的事情:

我在 server 文件夹中有许多文件,具体取决于配置,这些文件根据加载的配置服务于不同的目的。通常喜欢:

/server/middleware.json (for localhost)
/server/middleware.staging.json
/server/middleware.production.json

/server/datasources.json (for localhost)
/server/datasources.staging.json
/server/datasources.production.json

...每个环境和文件依此类推。

然后,根据环境,我使用特定命令启动应用程序:

NODE_ENV=production npm start server/server.js

NODE_ENV 属性将告诉应用程序要读取哪个配置文件。如果您不提供任何内容,则将读取第一个文件(在我的示例中为 /server/middleware.json/server/datasources.json)

希望这有帮助!

关于node.js - 如何获取 Loopback 3 条件 'datasources' 和 'model-config' : following instructions and doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59391432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com