Sails:How to join two different models using waterline - node.js

In MVC peoples are using join query to join the two different tables, but In sails.js what I have to use? There is any method in waterline?

The answer based on database you are using.
For instance, you need to populate values in Mongo not to join. Or you need to join tables if you are using MySQL or similar.
In a nutshell, all this stuff is covered via Waterline. So you can just declare model in api/models with associations. Joining and populating is executing under the Waterline adapter.
For instance, you have User and Comment.
// api/models/User.js
module.exports = {
attributes: {
name: {
type: 'string'
},
comments: {
collection: 'Comment',
via: 'user'
}
}
};
// api/models/Comment.js
module.exports = {
attributes: {
text: {
type: 'string'
},
user: {
model: 'User',
via: 'comments'
}
}
};
Then you are execute User.find() and get already joined\populated tables from database.
But, if you want to execute manual joining, you can use .populate() method on Model instance. For instance:
// api/controllers/AnyController.js
module.exports = {
action: function(req, res) {
User
.findOne('ID_HERE')
.populate('comments')
.then(function(result) {})
.catch(function(error) {});
}
};
You can read more about populate here - http://sailsjs.org/documentation/reference/waterline-orm/queries/populate

Related

How to run another model queries inside the sequelize hooks?

Below is my PurchaseOrder model defined in sequelize. I want to update the Supplier Model whenever there is an update to the PurchaseOrder. I thought of using the hooks to achieve this. But I couldn't able to access another model inside this model. I tried importing and all stuff, but no luck. Is this the right way to use the hooks or what should I use to achieve the same? Any help or direction is much appreciated!
module.exports = (sequelize, Sequelize) => {
const PurchaseOrder = sequelize.define("purchaseOrder", {
totalAmount: {
type: Sequelize.INTEGER
},
paid: {
type: Sequelize.BOOLEAN
},
paymentMode: {
type: Sequelize.ENUM('CASH', 'CHEQUE', 'BANK', 'CARD', 'NA')
}
}, {
freezeTableName: true,
hooks: {
beforeUpdate: (order, options) => {
// here I want to update the another model(Supplier).
// But I couldn't able to access another model inside the hook
Supplier.increment('balance'{
where: { id: order.supplierId }
});
}
}
});
return PurchaseOrder;
};
In my code I have a couple hooks that update other models (audit logging of changes for example). You need to make sure to pass along the options.transaction so that any changes are rolled back if there is an error later in the chain.
This example accesses another table keyed by other_model. When the hooks run the models should all already be registered with Sequelize.
module.exports = function Order(sequelize, DataTypes) {
const Order = sequelize.define(
'order',
{ /* columns */ },
{
hooks: {
beforeUpdate: async function(order, options) {
// get the transaction, if it is set
const { transaction } = options;
// use sequelize.models and make sure to pass the
// transaction so it is rolled back if there is an error
await sequelize.models.supplier.increment(balance, {
where: { id: order.supplierId },
transaction,
});
},
},
},
});
return Order;
}
You can try sequelize['Supplier'] because all models should be already registered in an Sequelize instance.
Nevertheless I suppose it's not a good idea to make modifications in a DB via other models in such hooks because in such cases you should take into account that all operations should be done in the same transaction i.e. should be executed as an atomic operation to avoid inconsistent state of data in a DB if some modifications fail.
Not a relatable answer, but if anyone wants to try querying a model to another model using validate custom functions. You can define your model like sequelize.models.ModelName sequelize shouldn't be imported like require('sequelize') but it should use the sequelize parameter defined in module.exports of your current model.
await sequelize.models.ModelName.findAll()

How to query in sequelize by an association without loading the associated object?

I have multiple models which are associated to each other.
e.g:
var User = sequelize.define("user")
var Project = sequelize.define("project")
Project.hasMany(User)
Now I want to query all Projects containing a specific user.
e.g.:
Project.findAll({
include: [
{
model: User,
where: { id }
}
]
})
This works, but loads also the user and attaches it to the project.
How can I tell sequelize, that the user should not be added to the found projects?
I just managed to address the same problem (using Sequelize 4).
You can specify that you don't want any fields of User just using attributes: [], so your code would become:
Project.findAll({
include: [
{
attributes: [],
model: User,
where: { id }
}
]
})
Many-to-many relationships are defined using the belongsToMany() method in sequelize on both sides. For your specific use case you would have to use a through model for the relation and query the through model directly.
var User = sequelize.define("user")
var Project = sequelize.define("project")
var ProjectUser = sequelize.define("projectUser")
Project.belongsToMany(User, {
through: ProjectUser
})
User.belongsToMany(Project, {
through ProjectUser
});
ProjectUser.findAll({
where: {
UserId: 'someId'
},
// We only want the project, not the user.
// You might need to do ProjectUser.belongsTo() for both
// models for this to work.
include: [Project]
})
.then(function(results) {
// Here we are getting an array of ProjectUsers, to return all the projects
// we map it to a new array of only projects.
return results.map(function(userProject) {
return userProject.Project;
});
})

What is populate in SailsJs?

I recently began to develop on sailsJs and not understanding the subtleties
Please explain to me what is populate in SailsJs and who can please do simple example
Thanks in advance ?
whats is ?
User.find({ name: 'foo' })
.populate('pets', { name: 'fluffy' })
.exec(function(err, users) {
if(err) return res.serverError(err);
res.json(users);
});
populate is used for associations. When your model is something like this:
// User.js
module.exports = {
attributes: {
name: {
type: "string"
},
pet: {
model: "pet"
}
}
}
Here pet attribute of user collection is a reference to pet table. In user table it will store only the id column of pet. However, when you do a populate while find, then it will fetch the entire record of the pet entry and display it here. This is just for one to one association. You can have many to one associations as well as many to many. See this documentation for more details
sailsjs project is use the wateline ORM. you can see the document. if you want to use 'populate()', you need define Associations in the model.
.populate()
populate is used with associations to include any related values specified in a model definition. If a collection attribute is defined in a many-to-many, one-to-many or many-to-many-through association the populate option also accepts a full criteria object. This allows you to filter associations and run limit and skip on the results.
as you example, you need do like this:
User.js
// 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 Pets
pets: {
collection: 'pet',
via: 'owner'
}
}
});
Pet.js
// A pet may only belong to a single user
var Pet = Waterline.Collection.extend({
identity: 'pet',
connection: 'local-postgresql',
attributes: {
breed: 'string',
type: 'string',
name: 'string',
// Add a reference to User
owner: {
model: 'user'
}
}
});
you can ready the doc, and you can use it very easy
To resume what is .populate() (used by waterline) is a little what join is used by SQL.
.populate() allows you to join the tables in your database.
The link identifier is to be defined in your model.
In other words, to associate a user (who is in the "User" table) with a dog (who is in the "Dog" table), you use populate.
To resume your example:
You are looking for the user => User.find ({name: my_user})
You are looking for the dog named "fluffy" => {name: 'fluffy'}
You are looking for the dog 'fluffy' which is associated with your user (belongs) => populate ('pets')
Which give:
User.find({ name: 'foo' })
.populate('pets', { name: 'fluffy' })
.exec(function(err, users) {
if(err) return res.serverError(err);
res.json(users);
}
This association ("pets"), you define it in your models "User" and "Pets" like the example above.
Populate is all about displaying the content of the (id) on which it was refreed
"abc": [{
type: ObjectId,
ref: "xyz"
}],

Sequelize not creating Junction tables for many-to-many relationships

I don't see the junction tables getting created for my code below (for UserEvents as:Attendees). When I try to add 'Attendee' to an Event using event.setAttendees([user]), it sets the EventId on the user table (thus making it a one-to-many relationship)
During sync logging, no tables with 'userevents' related name gets created
var User = db.import(__dirname + '/user');
var Event = db.import(__dirname + '/event');
Event.hasMany(User, { as: 'Attendees'});
User.hasMany(Event);
db.sync({logging: console.log}).success(function() {
logger.info('DB Initialization successful!');
createDefaultData()
}).error(function(err) {
logger.error('DB Initialization failed!', err);
});
var createDefaultData = function() {
User.create({email:"test#test.com", firstName: "tolga", lastName: "ekmen", password: "qwer", location: "13424"});
}
When using an alias ({ as: 'Attendees'}) you have to tell sequelize that you want a join table, by specifying through, which can either be a string or a model:
Event.hasMany(User, { as: 'Attendees', through: 'user_events' });
User.hasMany(Event, { , through: 'user_events' });

Sequelize findall for every findall is possible?

I have this tables. Clients have Projects and Users works in Projects
Clients
- id
- name
Projects
- id
- name
- client_id
Users
- id
- name
UserProject
- user_id
- project_id
I try to return all users of the every project of client for example id=1
Finally result, something like this JSON:
[{
id:1
name:"Project1"
users:[{
id:23
name:"Robert Stark"
},{
id:67
name: "John Snow"
}]
}, {
id:2
name:"Project2"
users:[{
id:1
name:"Aria Stark"
}]
}]
If I find projects it works fine
req.tables.Project.findAll({
where: {
client_id:1
}
}).success(function(projects) {
...
If I find Users of a project it works fine
req.tables.UserProject.findAll({
where: {
project_id:1
},
include: [
{ model: req.tables.User, as: 'User' }
]
}).success(function(UsersProject) {
...
But, how can I combine both finAlls to return all users in every project? Something like the next code, but that works well. How can I do it?
I found this: Node.js multiple Sequelize raw sql query sub queries but It doesn't work for me or I do not know how to use it, because I have 2 loops not only one. I have projects loop and users loop
req.tables.Project.findAll({
where: {
client_id:1
}
}).success(function(projects) {
var ret_projects=[];
projects.forEach(function (project) {
var ret_project={
id:project.id,
name:project.name,
data:project.created,
users:[]
});
req.tables.UserProject.findAll({
where: {
project_id:project.id
},
include: [
{ model: req.tables.User, as: 'User' }
]
}).success(function(UsersProject) {
var ret_users=[];
UsersProject.forEach(function (UserProject) {
ret_users.push({
id:UserProject.user.id,
name:UserProject.user.name,
email:UserProject.user.email
});
});
ret_project.users=ret_users;
ret_project.push(ret_project)
});
});
res.json(projects);
});
Sounds like you already have a solution, but I came across the same issue and came up with this solution.
Very similar to what cvng said, just using nested include. So use:
Project.belongsTo(Client);
Project.hasMany(User);
User.hasMany(Project);
Then:
req.tables.Client.find({
where: { id:req.params.id },
include: [{model: req.tables.Project, include : [req.tables.User]}]
}).success(function(clientProjectUsers) {
// Do something with clientProjectUsers.
// Which has the client, its projects, and its users.
});
}
The ability to 'Load further nested related models' is described through the param 'option.include[].include' here: Sequelize API Reference Model.
Maybe this will be useful to someone else in the future.
Cheers!
I think you would not have to query UserProject entity directly but instead use Sequelize Eager loading methods to retrieve your entities.
Your models associations should look something like this :
Project.belongsTo(Client);
Project.hasMany(User, { as: 'Workers' });
User.hasMany(Project);
and once you have all projects related to client, your finder method :
Project
.findAll({ include: [{ model: User, as: 'Workers' })
.success(function(users) {
// do success things here
}
Take a look at, http://sequelizejs.com/docs/1.7.8/models#eager-loading.
Hope it helps !
Finally!!
cvng, your example helpme a lot, thanks.
But I have 3 levels Client, Project, thi is my final solution, is this a good solution?
req.tables.Client.find({
where: { id:req.params.id },
include: [{ model: req.tables.Project, as: 'Projects' }]
}).success(function(client) {
var ret ={
id:client.id,
name:client.name,
projects:[]
};
done = _.after(client.projects.length, function () {
res.json(ret);
});
client.projects.forEach(function (project) {
project.getUsers().success(function(users) {
var u=[]
users.forEach(function (user) {
u.push({
id:user.id,
name:user.name,
});
});
ret.projects.push({
id:project.id,
name:project.name,
users:u
});
done();
});
});
});

Resources