如果我的架构如下:
var Configuration = describe('Configuration', function () {
property('name', String);
set('restPath', pathTo.configurations);
});
var Webservice = describe('Webservice', function () {
property('wsid', String);
property('url', String);
});
Configuration.hasMany(Webservice, { as: 'webservices', foreignKey: 'cid'});
这反射(reflect)了这样的数据:
var configurationJson = {
"name": "SafeHouseConfiguration2",
"webServices": [
{"wsid": "mainSectionWs", "url":"/apiAccess/getMainSection" },
{"wsid": "servicesSectionWs", "url":"/apiAccess/getServiceSection" }
]
};
我是否应该能够使用以下内容为 mongoDB 提供种子:
var configSeed = configurationJson;
Configuration.create(configSeed, function (err, configuration) {
)};
这将使用 json 对象中的数据创建下表:
- 配置
- 网络服务
最佳答案
由于这没有按我的预期工作,我的种子文件最终如下:
/db/seeds/development/Configuration.js
var configurationJson = {
"name": "SafeHouseConfiguration2",
"webServices": [
{"wsid": "mainSectionWs", "url":"/apiAccess/getMainSection" },
{"wsid": "servicesSectionWs", "url":"/apiAccess/getServiceSection" }
]
};
Configuration.create(configurationJson, function (err, configuration) {
//Webservices config
var webservicesConfig = configSeed.webServices;
webservicesConfig.forEach(function(wsConfig){
configuration.webservices.create(wsConfig, function(err){
});
});
关于json - compoundJS::如何创建模式来反射(reflect)半复杂的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712678/