Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed and write mongo objects in Sails.js (more than one level deep)?

From sails.js example,

// Person.js

   var Person = {
      attributes: {
        firstName: 'STRING',
        lastName: 'STRING',
        age: {
          type: 'INTEGER',
          max: 150,
          required: true
        }
        birthDate: 'DATE',
        phoneNumber: {
          type: 'STRING',
          defaultsTo: '111-222-3333'
        }
        emailAddress: {
          type: 'email', // Email type will get validated by the ORM
          required: true
        }
      }
    };

Now how would I add emailAddress to have a home and office as embedded fields?

Tried to do it this way:

emailAddress: {   {

                        work: {
                            type: 'email',
                        },
                        personal: {
                            type: 'email',
                        }
                  }
             },

and

emailAddress: {  
                     attributes: {

                        work: {
                            type: 'email',
                        },
                        personal: {
                            type: 'email',
                        }
                  }
             },

both don't work. I get errors such as "No Rules found for attributes" for the second case, "Unexpected token { " in the first case.

like image 204
bunt Avatar asked Jan 16 '14 21:01

bunt


2 Answers

Ok, Following through some threads on this. It seems Sails Waterline has no support for embedded MongoDB schema at this stage. You can write your own contribution or force it, but the out of box support (model validation) etc. need to be hacked too. https://github.com/balderdashy/sails-mongo/issues/44

The other option - sails-mongoose is unfortunately not supported too. From where can we decide collection name in use of "sails-mongoose" package, in node.js + sailsjs?

Update. Starting with V0.10, Sails is supporting associations. Perhaps that will make it work. Still experimental.

Update. With the associations functionality you can force a schema in different models and create references between them, but so far it doesn't seem like you'll be able to embed them - only relate them from different collections/tables.

like image 199
bunt Avatar answered Nov 11 '22 07:11

bunt


https://github.com/balderdashy/sails-mongo/issues/44

It seems they already have it planned as a Feature Request.

like image 30
NotHere Avatar answered Nov 11 '22 08:11

NotHere