bunt
bunt

Reputation: 181

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.

Upvotes: 8

Views: 4127

Answers (2)

Pedro Luz
Pedro Luz

Reputation: 973

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

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

Upvotes: 1

bunt
bunt

Reputation: 181

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.

Upvotes: 10

Related Questions