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

偶尔善良 提交于 2019-12-06 23:47:07

问题


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.


回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/21173465/how-to-embed-and-write-mongo-objects-in-sails-js-more-than-one-level-deep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!