CompoundJS :: How to create the schema to reflect a semi complex JSON object?

204 Views Asked by At

If my schema is as follows:

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

and that reflects data like so:

var configurationJson = {
    "name": "SafeHouseConfiguration2",
    "webServices": [
        {"wsid": "mainSectionWs", "url":"/apiAccess/getMainSection" },
        {"wsid": "servicesSectionWs", "url":"/apiAccess/getServiceSection" }
    ]
};

shouldn’t I be able to seed mongoDB with the following:

var configSeed = configurationJson;
Configuration.create(configSeed, function (err, configuration) {

)};

which would create the following tables with data from the json object:

  1. Configuration
  2. Webservice
1

There are 1 best solutions below

0
On

Since this did not work as I had expected, my seed file ended up as the following:

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

     });
});