MongoDB - 3 types of roles - database

I have a user model here that currently only have 2 possible roles. One is admin and one is regular user.
const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isAdmin: {
type: Boolean,
required: true,
default: false
}
}, { timestamps: true
})
As you can see, if the isAdmin was set to true, it will automatically become an admin otherwise its a regular user.
But things change when I added a new role like: isOwner which is I added a new field again:
const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isAdmin: {
type: Boolean,
required: true,
default: false
},
isOwner: {
type: Boolean,
required: true,
default: false
},, { timestamps: true
})
As you can see I added a new field isOwner at the top.
My question is given these three roles: isAdmin, isOwner and regular user, am I doing the right way to do this and manage 3 different roles? or is there a better way to do this?
Note: The way this works is that admin has the overall access to all of the job post of the owner while the regular user can only comment on the job post posted by the owner (who posted the job)

In future you may have more roles, It is best to use Array field for user roles.
array reference document
const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isAdmin: {
type: Boolean,
required: true,
default: false
},
roles: {
type:[String],
required: true,
default:["regular"]
}
},
{ timestamps: true
})

Related

How can I insert mongo changes in the deploy website?

My website is running allright and it's connected with Database.
I had to add more informations in my Mongo's collection. (I created the ActionPlanSchema).
import mongoose from "mongoose";
import mongoose_delete from "mongoose-delete";
const VerificationSchema = new mongoose.Schema({
text: { type: String, required: true },
});
const ActionPlanSchema = new mongoose.Schema({
title: { type: String, required: true },
mouraTitle: { type: String, required: true },
text: { type: String, required: true },
});
const RequirementSchema = new mongoose.Schema({
itemNumber: { type: String, required: true },
normPart: {
type: mongoose.Schema.Types.ObjectId,
ref: "NormPart",
required: true,
},
generalRequirement: {
type: mongoose.Schema.Types.ObjectId,
ref: "GeneralRequirement",
required: true,
},
specificRequirementDescription: { type: String, required: true },
subject: { type: mongoose.Schema.Types.ObjectId, ref: "Subject" },
criterion: String,
verification: [VerificationSchema],
actionPlan: [ActionPlanSchema],
});
RequirementSchema.index({ itemNumber: 1, normPart: 1 });
RequirementSchema.index({ specificRequirementDescription: 1 });
RequirementSchema.plugin(mongoose_delete, {
overrideMethods: "all",
deletedAt: true,
deletedBy: true,
indexFields: ["deleted"],
});
export default RequirementSchema;
And using a router in Postman (PUT method), I added some information to the Database ActionPlanSchema. Looking like that:
In my localhost, the actionPlan information it's showing. But when I do the deploy, the actionPlan gets undefined.
I believe that it's because the informations that I added in Postman weren't sent to the database used on the Deploy App.
How can I solve this problem? (I don't know if I could be very clear about my issue)
Anyway, thanks so much!

how to make a dynamic form creation and response mangement system like google forms work?

I was working on a project where we can create a form like google forms and get responses from users, and then we can view those responses in the admin panel.
My English is not that good I tried my best to explain the problem!
I am using MERN Stack so I was tucked into a problem related to how to save the questions and record the responses in MongoDB in a standard way in objects and arrays.
currently I added this as a form model :
const formSchema = new Schema({
usererId: { type: Schema.Types.ObjectId, ref: 'User' },
title: { type: String, required: true },
description: { type: String},
username: { type: String, required: true },
password: { type: String, required: true },
isEnabled: { type: Boolean, default: true },
expiresOn: { type: Date, default: null },
fields: [
{
title: { type: String, required: true },
placeholder: { type: String, required: false },
type: { type: String, required: true },
required: { type: Boolean, required: true },
options: [
{
title: { type: Object, required: true },
priority: { type: Number, default: 0, required: true }
}
],
priority: { type: Number, required: true, default: 0 },
enabled: { type: Boolean, default: true, required: true }
}
],
}, {
timestamps: true
})
and form responses :
const formResponseSchema = new Schema({
digitalFormId: { type: Schema.Types.ObjectId, ref: 'Form' },
isVerified: { type: Boolean, default: false },
formResponse: [{ type: Object, required: true }],
}, {
timestamps: true
})
I don't know if this approach is good, I myself like it's not like a standard one and I m confused, Someone please tell me how should I do it and what is the perfect way of doing it,
If you have a problem understanding what I am saying just tell me simply how a google form works in the backend from API view, like question ID server-side validation and response submit, that time what type of request is sent to the server, something like this !
If you don't mind please take out some time and write a brief approach of what type of Schema should I use and how should I save them.
Something more important is what approach to use while checking the form response data coming from the client to validate on server-side means on express API, like {questionId: idofquestion, value:'Value of the response of the particular question'}, later checking the question label and all other things with the form questions and all validation rules to fetch from them and check, but I don't know how to do that It's really confusing for me.
Thanks in Advance for the help! ❤️

Get only the length of mongoose virtual populate

I would like to know if there is a way in Mongoose of getting the number of matching localField/foreignField as virtual field, without retrieving all the documents through virtual population.
Example:
const ForumThreadSchema = new Schema({
code: { type: Number, required: true, unique: true },
title: { type: String, min: [10, 'Too short title'], max: [200, 'Too long title'], required: true },
description: { type: String, min: [10, 'Too short description'], max: [2000, 'Too long description'], required: true },
creation_date: { type: Date, default: Date.now, required: true },
updated_date: { type: Date, default: Date.now, required: true },
_authorId: { type: Schema.ObjectId, ref: 'User', required: true },
_forumId: { type: Schema.ObjectId, ref: 'Forum', required: true }
}, {
collection: 'ForumThreads',
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
const ForumMessageSchema = new Schema({
code: { type: Number, required: true, unique: true },
content: { type: String, min: [10, 'Too short message content'], max: [2000, 'Too long message content'], required: true },
creation_date: { type: Date, default: Date.now, required: true },
update_date: { type: Date, default: Date.now, required: true },
_authorId: { type: Schema.ObjectId, ref: 'User', required: true },
_forumThreadId: { type: Schema.ObjectId, ref: 'ForumThread', required: true },
_parentMessageId: { type: Schema.ObjectId, ref: 'ForumMessage' }
}, {
collection: 'ForumMessages'
});
The virtual populate on the forum thread schema retrieves me all the message documents. I need a virtual field with only their number if possible.
ForumThreadSchema
.virtual('messages', {
ref: 'ForumMessage',
localField: '_id',
foreignField: '_forumThreadId'
});
ForumThreadSchema
.virtual('messages_count')
.get(function() {
return this.messages.length;
});
The second virtual only works if the population of the first one is done.
I also want to mantain the virtual populate but I would like to find a way of getting the number of matching documents without use it (in server APIs that not need all message documents but only their size).
Is it possible?
You just need to add the count option:
ForumThreadSchema
.virtual('messages_count', {
ref: 'ForumMessage',
localField: '_id',
foreignField: '_forumThreadId',
count: true
});
And then when you can populate just the count:
ForumThread.find({}).populate('messages_count')
Doc: https://mongoosejs.com/docs/populate.html#count

mongodb User with subdocument vs document with UserId

I am using meanjs and I would like to store some user data in a one to many relationship. My case is similar to the articles example but the articles will only ever be accessed through the user. I want the route to be something like
Users/:userId/articles
or
Users/me/articles
Question 1
Should I just stick with the articles model as it is or should I make articles a subdocument of user. e.g.
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
},
articles: [articleModel.schema],
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', 'store', 'admin']
}],
default: ['user']
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
/* For reset password */
resetPasswordToken: {
type: String
},
resetPasswordExpires: {
type: Date
}
});
Question 2 if I make it a subdocument can I still use the $resource function or do I have to make custom functions?
The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

Mongoose relationship

I'm studying Mongodb and I'm using Mongoose and I would like to create a very dynamic registration process. I managed the first step of the registration using Passport and everything works fine, I created the user and it is present on the DB. Now my registration process will continue and user have to select a "role": What kind of user you are? There are 2 options: "basic" and "advanced". Basic has just 3 properties and advanced has the 3 basic properties plus few others.
I need to extend the userSchema or to add new fields based on that role but after a week it doesn't work yet and I tried many NPM like: mongoose-relationship or mongoose-schema-extend.
This is basically what I have:
userSchema = new Schema({
id : Number,
email : { type: String, required: true },
role : { type: String, required: true },
profile : { ... } // Profile fields are different for each role.
});
// profile 1
basicSchema = new Schema({
info1 : { type: String, required: true },
info2 : { type: String, required: true },
info3 : { type: String, required: true }
});
// profile 2
advancedSchema = new Schema({
info1 : { type: String, required: true },
info2 : { type: String, required: true },
info3 : { type: String, required: true },
info4 : { type: String, required: true },
info5 : { type: String, required: true },
info6 : { type: String, required: true }
});
The user already exist and he is on a screen where he need to choose a role and populate the chosen profile.
For information I'm using nodejs and expressjs.
I hope you can help. Thanks.
Using mongoose-schema-extend:
Install via npm:$ npm install mongoose-schema-extend
Example using your code:
var mongoose = require('mongoose'),
extend = require('mongoose-schema-extend');
var Schema = mongoose.Schema;
var userSchema = new Schema({
id : Number,
email : { type: String, required: true },
role : { type: String, required: true },
},{ collection : 'users' });
//profile 1
var basicSchema = userSchema.extend({
info1 : { type: String, required: true },
info2 : { type: String, required: true },
info3 : { type: String, required: true }
});
//profile 2
var advancedSchema = userSchema.extend({
info1 : { type: String, required: true },
info2 : { type: String, required: true },
info3 : { type: String, required: true },
info4 : { type: String, required: true },
info5 : { type: String, required: true },
info6 : { type: String, required: true }
});
mongoose-schema-extend
This is what you want. The concept of "inheritance" and discriminatory field. Give it a shot. I used it and works wonders. Basically you have 3 types of users: basic, advanced and expert. Each is based on your core/base model and then you extend with extra properties you need, defining one inheritance per role.
This gives you a lot of advantages, mainly: mongoose will populate the appropriate model and kick any validation you have in the derived models, etc.
https://github.com/briankircho/mongoose-schema-extend

Resources