Sails.js Associations many-to-many relation - sails.js

I have a problem with many to many relationship in sails and if somebody can help me it would be great :D
I have two models User and Message and the associations is defined as follows
api/models/User.js
module.exports = {
attributes: {
messages: {
collection: 'message',
via: 'owner',
dominant: true
}
}
};
api/models/Message.js
module.exports = {
attributes: {
owner: {
model: 'user'.
via: 'messages'
}
}
};
I checked in the DB (MySQL) the middle table is created and i successfully inserted the data but i cant retrive the data.
i start sails console and type
User
.find()
.populate('messages')
.exec(function(err,r){
while(r.length){
var thisUser=r.pop();
console.log(thisUser.toJSON())
}
});
But i always recive no data for messages, the messages field is always empty messages: []
my Current version of sails is 0.10.0-rc4

Related

SailsJS relations not working as expected

SailsJS with MongoDB adapter not working as expected. I have following relations defined:
Post.js:
module.exports = {
connection: 'mongodb',
attributes: {
title: 'string',
categories: {
collection: 'postCategory',
via: 'posts'
}
}
};
PostCategory.js
module.exports = {
connection: 'mongodb',
attributes: {
name: 'string',
posts: {
collection: 'post',
via: 'categories'
}
}
};
When I query without criteria and populate categories like:
Post.find()
.populate('categories')
.then(...)
it gives me correct result, the document has categories nested.
But when I try to pass criteria it returns no result. e.g.
Post.find({categories: 'food'})
.populate('categories')
.then(...)
Note: I inserted category _id as string (food, travel, etc) in database
Please help
You will not get any results because in categories it will store the _id.
You can get the results by doing the following query.
PostCategory.find({name: 'food'})
.populate('posts')
.then(...)

Updating association in Sails

I am using SailsJs. Two models exist. Model 'person' contains the following attribute:
books: {
collection: 'book',
via: 'owner'
},
Model 'book' contains:
owner: {
model: 'person'
},
When I create an instance of person, I can use http request and simply put something like the following
Post /person?books=1001,1002
As long as 1001 and 1002 are valid ids of book, it works.
However, when I try to person's books attribute, this does not work
Post /person/1?books=1002,1003
books of person with id 1 becomes empty.
But,
Post /person/1?books=1002
would work.
Why is this? How can I modify collection attribute?
There are 2 options to update a model using the blueprints API:
Send the data as the body of the post request. send the data as a json object in the request body
Post /person/1
body: { "books": [1002,1003]}
Override the blueprint update method
Add this the PersonController.js:
update: function (req, res) {
Person.findOne(req.param('id')).populate('books').exec(function (err, person){
if (err) {
sails.log.error(err);
return;
}
var books = req.param('books').split(',').map(function (book){return parseInt(book);}),
booksToRemove = _.difference(person.books.map(function(book) {return book.id;}), books);
// Remove books that are not in the requested list
person.books.remove(booksToRemove);
// Add new books to the list
person.books.add(books);
person.save(function (err, r) {;
res.json(r);
});
});
}

sailsjs / waterline query "where" not empty

Hy there,
Before going to the hacky / cutom way i wanted to know if there is a built in query way to check for an empty / non empty many to many relationship as i was not successfull neither on google nor the doc.
If i take the example in the doc let's imagine i want to retrive a user only if he has a a Pet or Retrive a Pet without any Owner through a query.
// A user may have many pets
var User = Waterline.Collection.extend({
identity: 'user',
connection: 'local-postgresql',
attributes: {
firstName: 'string',
lastName: 'string',
// Add a reference to Pet
pets: {
collection: 'pet',
via: 'owners',
dominant: true
}
}
});
// A pet may have many owners
var Pet = Waterline.Collection.extend({
identity: 'pet',
connection: 'local-postgresql',
attributes: {
breed: 'string',
type: 'string',
name: 'string',
// Add a reference to User
owners: {
collection: 'user',
via: 'pets'
}
}
});
P.s. i know how to filter results after query execution that's not what i'm asking :)
There is nothing built in (aka User.hasPet() ) or something like that., so the simple answer is NO
If I know of these issues before hand I tend to write my DB in such a way that the queries are fast. IE: the User schema would have a hasPets column. Whenever a pet is added/removed a callbacks hits the user table to mark that field if it has an owner or not. So then I can query User.findAll({hasPet:true}).
Its a little much, but it depends on where you speed is needed.
This is a bit late, but I wanted to let you know it's pretty easy to do this with the Waterline ORM lifecycle functions. I've done it in a few of my projects. Basically, use the beforeCreate and beforeUpdate functions to set your flags. For your user, it might look like...
var User = Waterline.Collection.extend({
identity: 'user',
connection: 'local-postgresql',
beforeCreate: function(values, next) {
if (values.pets) {
values.has_pet = true;
} else {
values.has_pet = false;
}
next();
}
beforeUpdate: function(values, next) {
if (values.pets) {
values.has_pet = true;
} else {
values.has_pet = false;
}
next();
}
attributes: {
firstName: 'string',
lastName: 'string',
// Add a reference to Pet
pets: {
collection: 'pet',
via: 'owners',
dominant: true
},
has_pet: {
type: 'boolean'
}
}
});
Then you can query based on the has_pet attribute

Sailsjs add one to many association to a model during beforeCreate

I am trying to give a default association from a user to a pet, whenever a new User created.
Model:: User.js
var User = {
attributes: {
name: {type: 'string'},
// Add a One Way Relation to pet model
pets: {
collection: 'pet'
},
},
/*** This did not work ***/
beforeCreate: function (user, next) {
var defaultPet = {name: 'Default Pet 1'};
Pet.find(defaultPet).exec(function(err, pet) {
user.name = "BEFORECREATE",
user.pets = pet[0].id;
next();
});
},
}
module.exports = User;
However when a new record is created the user.pet is [ ], but user.name is changed to "BEFORECREATE".
How do I get user.pets = [{name: 'Default Pet 1'}] automatically for the new user created?
Or is there a better place for setting such defaults?
----- UPDATE: More info
I am using sails-disk currently.
Model: Pet.js
module.exports = {
attributes: {
name: {
type: 'string',
required: true
}
}
};
You can't add associations to a model in a lifecycle callback like beforeCreate. Currently, Waterline looks for and processes "nested models" like these before lifecycle callbacks run, so by the time beforeCreate is called it's too late. The simplest solution would be to create a User.createUser class method that wraps the logic you want:
createUser: function(data, cb) {
// If there are already pets specified, just use those
if (data.pets) {return User.create(data).exec(cb);}
// Otherwise look up the default pet
Pet.findOne({name:"Default Pet 1"}).exec(function(err,pet) {
// Return in case of error
if (err) {return cb(err);}
// Assuming the default pet exists, attach it
if (pet) {
console.log("SETTING DEFAULT PET", pet.id);
data.pets = [pet.id];
}
// Create the pet
return User.create(data).exec(cb);
});
}
A few notes:
In your example you were setting pets directly to an ID, but since it's a collection you must set it to an array.
If you're using the sails-disk adapter, you'll need to set schema: true in your model for this to work.
The new User model you get back will not be populated; you'll have to do a find with a populate('pets') with the new User ID to get the pet data attached.

Sails.js - Many-to-Many fails to save

I have a simple need to add tags to patients. I followed the Sails and Waterline documentation concerning many-to-many associations, but it's failing at some point (no errors). I'm using MongoDB for data storage. Code below:
Tag Model
module.exports = {
attributes: {
name: 'STRING',
color: {
type: 'STRING',
defaultsTo: '#777777'
},
tagged: {
collection: 'patient',
via: 'tags',
dominant: true
}
}
};
Patient Model
module.exports = {
attributes: {
name: 'STRING',
tags: {
collection: 'tag',
via: 'tagged'
}
}
};
And this is the controller method that tries to associate data:
module.exports = {
addToPatient: function(req, res) {
Patient.findOne({id: req.param('patientId')}).exec(function(err, patient) {
// Queue up a record to be inserted into the join table
patient.tags.add(req.param('tagId'));
// Save the user, creating the new associations in the join table
patient.save(function(err) {});
});
res.send("tag assigned");
}
};
I've inspected the responses at various breaks and everything seems to be passing just fine. The patient is found. The save function shows a tag association in the patient object, but nothing is added in the database. I assume I will see either a join table being created or something in the patient/tag collections to signify an association, but I see nothing. I'm so very confused. If I do an HTTP get, I'm presented with a "tag assigned" response. What am I missing?
Works fine for me, but you're right in the tag and patient collections, you won't see a populated field with the associations. You'll see new join collections that are created that contains the relationships like #sgress454 pointed out.