user3721307
user3721307

Reputation: 199

MEANjs: one crud module on top of another crud module instead of the user module

I'm working on a MEAN.js app and I've created a bunch of crud-module using the Yo generator. All the new crud module i create get linked to the main user module which makes sense although i want to have one crud module called 'flats' that links to the user module

and other crud modules that i create eg: 'bathrooms', 'rooms' etc should be linked to 'flats' so lets say after logging in, in my top right dropdown, i see the list of Flats I've created, when i click on any of, then the 'bathrooms' and 'rooms' menu item in the topbar should be dependent on the rooms and bathrooms that were created under that 'flat'

Curious, how would I go about achieving the above?

Upvotes: 0

Views: 478

Answers (1)

yass
yass

Reputation: 1086

The answer start with modeling your entities, this placed in your mongoose schema, the way you model your entities may differ, so you should read more about mongodb data modeling and mongoose Schemas, Models, Population.

Basically you would have, 3 models (Flat, Room, Bathroom), the Room entity should have reference to the Flat, and the Bathroom should also have reference to the Flat

To accomplish that in meanjs you should have : 3 file under app\models (flat.server.model.js, room.server.model.js, bathroom.server.model.js), each file contains the mongoose schema of that entity, so the room.server.model.js, will have mongoose schema like:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var RoomSchema = new Schema({       
    name: String,
    order: {
        type: Number
    },
    _flat:{
        type:String,
        ref:'Flat' //refers to the Flat model, should be populated with _id of the Flat entity
    }
});

Now when creating a new Room entity, you should add the _id of the flat, like this:

var room=new Room({name: 'living', _flat:'123165464' /*_id of a flat entity*/});
//or:
 var room=new Room();
room._flat='123165464'; //_id of a flat entity

To retrieve the flat rooms, you should have a function in your server controller ( app\controllers\room.server.controller.js), that query the mongodb to get the Rooms of specific Flat, some thing like:

exports.roomsByFlat= function(req, res) {Room.find({'_flat':'35415453'/*_id of a flat entity*/}).exec(function(err, rooms) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(rooms);
    }
    });
};

But this function have the flat._id static, so we have to pass the flat._id to it, there are two ways I know, one is to define a route with a parameter to hold the flat._id , the other is to pass it in the query string. Check the answers to this post, to see how can you do both.

Note that this is one way of doing things, there are other ways, you can make your Flat document (entity) contains all the Room and Bathroom documents (entities) , I encourage you to read further about mongodb data modeling, to figure out what suits you the best.

Upvotes: 1

Related Questions