Many to Many Model Association In Nodejs Mongo DB
Posted By : Parveen Kumar Yadav | 26-Sep-2017
In my previous blog i have defined how to create one-many and many-one relationship in this blog i am going to explain how to create many-many association using Rest-Hapi. In this type of relationship a single user instance can belongs to multiple group instances and vice versa. In many-many relationship there is no requirement for foreign-key reference. For defining many-many association:- Suppose i have a Building model and a User model.
Schema.statics = {
collectionName: modelName,
routeOptions: {
users: {
type: "MANY_MANY",
alias: "user",
model: "user",
linkingModel: "building_user"
},
// other models association
}
}
}
On Occupant Model:-
Schema.statics = {
collectionName: modelName,
routeOptions: {
buildings: {
type: "MANY_MANY",
alias: "building",
model: "building",
linkingModel: "building_user"
},
// other models association
}
}
}
Along with the normal crud operation it also create the following API for both models i.e Building and User GET /user/{ownerId}/building Get all of the buildings for a user POST /user/{ownerId}/building Add multiple buildings for a user DELETE /user/{ownerId}/building Remove multiple buildings from a user's list of groups PUT /user/{ownerId}/building/{childId} Add a single buildings object to a user's list of groups DELETE /user/{ownerId}/building/{childId} Remove a single buildings object from a user's list of groups And for Building:- GET /building/{ownerId}/user Get all of the users for a building POST /building/{ownerId}/user Add multiple users for a building DELETE /building/{ownerId}/user Remove multiple users from a building's list of users PUT /building/{ownerId}/user/{childId} Add a single user object to a building's list of users DELETE /building/{ownerId}/user/{childId} Remove a single user object from a building's list of users Also if you want to include extra data rather than just id than you can use linking_models, same as junction tables in relational DB. like i have one i.e building_user as follow:-
var mongoose = require("mongoose");
module.exports = function() {
var Types = mongoose.Schema.Types;
var Model = {
Schema: {
data: {
type: [Types.Object],
}
},
modelName: "building_user"
};
return Model;
};
In this way you can define many-many association using rest-hapi. In database it stores many many data in array. Note:- In the previous version of rest-hapi have an issue of update means if one fields is associated to other than according to relationship that need to update id in both models but that was not done so that issue is fixed in latest 0.23.2 version of rest-hapi. Hope it would help. Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Parveen Kumar Yadav
Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies