Sails + sails-orientdb one-to-many and one-to-one associations

145 views Asked by At

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.

1

There are 1 answers

9
Dário On

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'.