CompoundJS ::如何创建模式以反映半复杂的JSON对象?

时间:2013-10-31 17:07:56

标签: json node.js mongodb compoundjs jugglingdb

如果我的架构如下:

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'});

反映了这样的数据:

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对象中的数据创建以下表:

  1. 配置
  2. web服务

1 个答案:

答案 0 :(得分:0)

由于这不像我预期的那样有效,我的种子文件最终如下:

/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){

     });
});