1

I am rather new to Sails and have been having difficulties with mapping my model associations to OrientDB.

I have 3 models:

//User.js
module.exports = {
attributes: {  
id: {
    type: 'string',
    primaryKey: true,
    columnName: '@rid'
},
name : { 
    type: 'string',
    required: true
},
surname : { 
    type: 'string',
    required: true      
},
email : { 
    type: 'string',
    index: 'unique'
},
devices : {
    collection: 'GPSDevice',
    via: 'user',
    dominant: true
}
};

The user model "Follows" (edge in OrientDB) one or more GPSDevice(s). here is the model for GPSDevice.js

module.exports = {

attributes: {  
id: {
    type: 'string',
    primaryKey: true,
    columnName: '@rid'
},  
brand: {
  type: 'string',
  required: true
},
model: {
    type: 'string',
    required: true
},      
label: {
    type: 'string',
    required: true
},
user: {
    via: 'devices'
}
}
};

The third model is Credentials, which stores User credentials and is linked to User via the LoginsWith edge. I haven't got round to mapping this part as I'm having problems with the first two models.

While I can get to User data in the controller, via:

User.findOne({'email': email}, function(err, user) {
        console.log('user: ' + user.name);
        console.log('user: ' + user.devices);
        res.view({user: user});
    });

the line "user.devices", does not return anything.

What am I missing ? I'd be very grateful if you could explain the use of "via" as I couldn't gather much from the docs. Your help will be immensely appreciated.

  • There are some other sails.js devs here that you can chat with. See if they can help. https://gitter.im/balderdashy/sails – Travis Webb Aug 12 '15 at 02:51

1 Answers1

1

In GPSDevice.js you are missing the model name:

user: {
  model: 'user',
  via: 'devices'
}

And you don't need dominant: true in USer.js as that only applies to many-to-many associations.

You can read more about one-to-many associations in waterline docs - associations.

Also, it's probably not causing your issue but model/collection names are lower case, so collection: 'GPSDevice' should be collection: 'gpsdevice'.

Dário
  • 2,002
  • 1
  • 18
  • 28
  • Hi Dario, thanks for contributing. Believe it or not I had tried adding model. still no joy with retrieviing devices: user.devices i had even added in User.js: joinTableNames: { devices : 'Follows' } which is the edge connecting the User and the GPSDevice. thanks in advance – Luca Iacono Aug 13 '15 at 12:09
  • Which version of OrientDB are you using? – Dário Aug 14 '15 at 14:37
  • 2.0.13 ... FYI, populate doesn't seem to be working ... User.findOne({'email':e mail}).populate('devices').exec(function(err,user) { if(err) { console.log(err); } console.log('user: ' + user); res.view({user: user}); }); – Luca Iacono Aug 15 '15 at 11:17
  • 2.0.x is supported. Do you already have data in your DB? Was this data created using sails-orientdb? – Dário Aug 16 '15 at 12:05
  • BTW, one-to-many associations don't use edges, only many-to-many associations. Details on: https://github.com/appscot/sails-orientdb#associations – Dário Aug 16 '15 at 12:21
  • OrientDB was setup to use lightweight edges, and I have noticed that when I create a User and GPSDevice, the edge created is NOT lightweight. So the creation of both models and edge works fine. I am bit perplexed now on how to retrieve the data from User.devices, because the line User.findOne({'email': email}).populate('devices').exec(function(err, user) {}); returns simply empty. However after creation user.devices returns [ add: [Function: add], remove: [Function: remove] ]. – Luca Iacono Aug 16 '15 at 15:42
  • UPDATE: ... I have simulated the User-Pets app ... and the populate doesn't seem to work ... the data is created via the app (including edges), so the model declarations seem to be fine ... however there doesn't seem to be any data in the pets collection. same thing with mine ... i'm baffled – Luca Iacono Aug 19 '15 at 20:18
  • That's very weird Luca, sails-orientdb passes all [waterline API associations tests](https://github.com/balderdashy/waterline-adapter-tests/tree/master/interfaces/associations). How are you retrieving the data from the pets collection? Can you post a complete working code example? Perhaps in github? There is also a working example that you can try at https://github.com/appscot/sails-orientdb/blob/master/example/associations/many-to-many.js – Dário Aug 20 '15 at 13:58
  • surely must be something I'm missing. as suggested I have posted the "api" and "config" folders on github: https://github.com/lucaiacono/UserPets-App ... I hope it helps you to help me. thanks Dario. – Luca Iacono Aug 21 '15 at 23:33