Installing is easy via npm, simply:
npm install mongoose-friends --save
I have no experience with MEAN.JS, but it seems to be for the most part a collection of generators to create a CRUD-patterned angular/express app. As such it seems to follow the philosophy of the framework to create the friendship as a CRUD resource.
Using yo
as they suggest:
yo meanjs:crud-module friendship
This will generate the MVC for a friendship model, but will make some incorrect assumptions about the model itself, namely that it is a first class mongoose model. With this plugin, it is not. Rather friendships are part of an embedded collection on the user record, the plugin provides CRUD methods for them.
First off, add the plugin to your user model.
// in app/models/user.server.model.js
var friends = require("mongoose-friends");
// ...
UserSchema.plugin(friends());
The generated model at app/models/friendship
, and references to it in the generated files, will need to be removed. Instead of a Friendship
model, frienships will be CRUD'd through the plugin methods added to your User
model.
The controller generated at app/controllers/friendships.server.controller.js
will likely require the most change.
create
, for example would change from this:
var friendship = new Friendship(req.body);
friendship.user = req.user;
friendship.save(callback);
To something more like:
req.user.requestFriend(req.body.user, callback);
The routes may need to change as well, depending on how your application uses friendships. Friendships of the plugin are not a first-class resource, but rather an embedded collection of a user. As such there's no public /friendships
route, for example. Either that route would need to return only the logged in users friends, or you'd want to map a friendship route specific to the user, e.g. /users/ID/friendships
, in the case where the friendships of a user were viewable by those other than the user itself.
Anyway, this is no doubt woefully incomplete and perhaps even misguided, but I hope it's enough to get you started on the implementation.