Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose schema for Multi-User application

I need to make schema for a multi user application I need to develop using MongoDB, Express, AngularJS and NodeJS. I have 4 types of users in that application, GUEST, REGISTERED_USER, SUBSCRIBER, ADMIN. Once a user is registered the application, I need to display forms and information based on the role(s) of a particular user.

Could somebody give me some idea on how to handle the schema for different roles of a user, for I could later use it for role and access level functionalities?

For example, the following registeredUserSchema is common for REGISTERED_USER, SUBSCRIBER, ADMIN users:

var registeredUserSchema = mongoose.Schema({
    userId: String,
    fullName: String ,
    email: String,
    password: String,
    created: { type: Date, default: Date.now },
    roles: [String]
});

But then I need a lot of information to be asked from a user with SUBSCRIBER role so once he has registered to the application, I would like to display a lot many extra information to show him to be filled up in his acocunt information, than a user with just REGISTERED_USER.

Could somebody help me with it?

EDIT: More Explaination

For example, a REGISTERED_USER will have userId, but a user with SUBSCRIBER role is going to have subscriberId. So I need to decide on how to structure that data as there is data that common for all the users and then there is data different for each user based on his role. I need help on the strategy to choose to structure that data. For example, in Java Persistence API we have various Inheritance Strategies to structure data in relational database tables.

like image 598
skip Avatar asked Oct 12 '14 02:10

skip


People also ask

Does Mongoose need schema?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

What is Mongoose schema types ObjectId?

A SchemaType is different from a type. In other words, mongoose.ObjectId !== mongoose.Types.ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose.ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.

What is difference between mongoClient and Mongoose?

AFAIK, Under the hood, mongoose uses mongoClient to connect to the server; However, I can tell you few differences between these two connects; mongoose returns promise, mongoclient returns null. mongoose connect is backed by an internal configurable connection pool.


2 Answers

Here's the user schema from the MEAN.JS yeoman scaffolding generator:

    var UserSchema = new Schema({
    firstName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your first name']
    },
    lastName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your last name']
    },
    displayName: {
        type: String,
        trim: true
    },
    email: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your email'],
        match: [/.+\@.+\..+/, 'Please fill a valid email address']
    },
    username: {
        type: String,
        unique: 'testing error message',
        required: 'Please fill in a username',
        trim: true
    },
    password: {
        type: String,
        default: '',
        validate: [validateLocalStrategyPassword, 'Password should be longer']
    },
    salt: {
        type: String
    },
    provider: {
        type: String,
        required: 'Provider is required'
    },
    providerData: {},
    additionalProvidersData: {},
    roles: {
        type: [{
            type: String,
            enum: ['user', 'admin']
        }],
        default: ['user']
    },
    updated: {
        type: Date
    },
    created: {
        type: Date,
        default: Date.now
    },
    /* For reset password */
    resetPasswordToken: {
        type: String
    },
    resetPasswordExpires: {
        type: Date
    }
});
like image 87
MEAN Developer Avatar answered Sep 20 '22 22:09

MEAN Developer


I'm using this collections for this part :

var permissions_schema = new mongoose.Schema({
    name : String,
    title : String
}); // this are mostly static data

var roles_schema = new mongoose.Schema({
    name : String,
    title : String,
    _permissions : Array
});// _permissions is an array that contain permissions _id

var contacts_schema = new mongoose.Schema({
    mobile : String,
    password : String,
    first_name : String,
    last_name : String,
    _role : Number,
    _enabled : Boolean,
    _deleted : Boolean,
    _verify_code : Number,
    _groups_id : Array,
    _service_id : String
}); // and at last _role is _id of the role which this user owns.

With something like these collections, you can manage your users easily. I hope these have good ideas for you.

UPDATE : A more flexible schema can have an Array of role_id in contact object, and contact can have many roles and his /her permissions are a merge of all roles permissions.

like image 45
Elyas74 Avatar answered Sep 20 '22 22:09

Elyas74