akveo / ngx-admin-bundle-support

Support repository for ngx-admin backend bundles with issues tracking, instructions and code samples
58 stars 32 forks source link

enforcing mongoDB model #20

Closed mhhonline closed 4 years ago

mhhonline commented 4 years ago

Greetings,

I have bought "ngx-admin Backend Bundle Node + E-comm"

concerns: on your structure of backend, I noticed that the model structure for MongoDB is not defined anywhere.

It obtains the model structure during the update/ inserts DTO mapping.

I need to define the structure and enforce it across the collection to add some restriction like the uniqueness of email etc.. or type of the field and do extra validation. mongoose is well known for this. const User = mongoose.model('User', { firstName: String, lastName: String, email: String, category: String, userName: String, password: String });

or at least having something like this: like db.collection('User').createIndex({ email: 1 }, { sparse: true, unique: true });

Please advice.

Thanks

valentinkononov commented 4 years ago

Hi Mohammed,

I think mongoose is the best choice for what you are looking for. Idea of node bundle was to make it pure and not use ORMs, that's why we don't have Mongoose there. But another node solution - NestJS bundle - has it.

Thanks, Valentin

mhhonline commented 4 years ago

Hi Valentin,

I managed to resolve it using db.command({ createIndexes .... for adding indexes to schema db.command({collMod : this.collectionName, validator: { $jsonSchema: { .... for enforcing the schema..

both are implemented on services contractor. the idea, I want to restrict the end-user from creating 100% freely the schema if anyone gets the chance to manipulate the API directly. yes, it is controlled on app frontend level.

thanks

valentinkononov commented 4 years ago

Hi Mohamed,

Next version of node api for bundle (upcoming next week) will have sample of 'adminGuard' middleware, similar idea to CanActivate guard of Angular / NestJS. you can use it like this:

router.get('/', adminGuard, (req, res) => { userService .list(req.query) .then(users => res.send(users)); });

and guard itself is usual middleware like this:

function adminGuard(req, res, next) { if (req.user && req.user.role === 'admin') { next(); } else { return res.status(403).send({ error: 'User should have admin access to use this endpoint' }); } }