Monday 25 May 2020

Data Model confusion

So have a few questions regarding data models that I am not quite sure about, and I apologize if this isn't the proper sub. But I will be giving two examples of 2 different User Models. One from a node-postgres back-end, and another from a node-mongoose back-end.​I am confused on a number of things:Is there a benefit to using an object to move data around (such as a model)? For example : Does it make it easier on the back-end and prevent mixing data up (like keeping a user's data consistently paired with their user_id or token) when the back-end starts to get bombarded with api calls? Does it help abstract data away even though I can easily see what is in a User object using console.log?Are they necessary? (In the bootcamp I attended they claimed that the models were good for abstraction and data hiding (specifically in the postgres model). However, to me, that seems trivial). Also, a few tutorials I have done use models, not with raw data.Why are both examples called models when they both clearly behave differently? Is there no set rule what defines a model?a). Are user models needed in the front end? To allow for comparability when sending information to the back-end? (Sending a user object to the front-end to the back-end to register the user). Or is that one of the reasons for data-abstraction-layers? (although I didn't think they were used anymore)b). If models are needed in the front-end, could someone see what dataparameters,and variable names, make up a user? (Like user_id). And could this be asecurity risk?​​tl;dr: If models are just objects holding associated data, whats the point?​Node-Postgres User Model Example:export class User{ userId=0; username=''; password=''; email=''; accountRoleId=0; token=''; constructor( userId?: number, username?: string, password?: string, email?: string, accountRoleId?: number, token?: string, ){ userId && (this.userId=userId); username && (this.username=username); password && (this.password=password); email && (this.email=email); accountRoleId && (this.accountRoleId=accountRoleId); token && (this.token=token); } } ​Node-Mongoose User Model Example:const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const crypto = require('crypto'); const moment = require("moment"); const SALT_I = 10; require('dotenv').config(); const userSchema = mongoose.Schema({ email:{ type:String, required: true, trim: true, unique: 1 }, password:{ type:String, required: true, minlength: 5 }, name:{ type:String, required: true, maxlength:100 }, lastname:{ type:String, required: true, maxlength:100 }, cart:{ type:Array, default: [] }, history:{ type:Array, default: [] }, role:{ type:Number, default:0 }, token:{ type:String }, resetToken:{ type:String }, resetTokenExp:{ type:Number }, }); userSchema.pre('save',function(next){ var user = this; if(user.isModified('password')){ bcrypt.genSalt(SALT_I,function(err,salt){ if(err) return next(err); bcrypt.hash(user.password,salt,function(err,hash){ if(err) return next(err); user.password = hash; next(); }); }) } else{ next() } }) userSchema.methods.comparePassword = function(candidatePassword,cb){ bcrypt.compare(candidatePassword,this.password,function(err,isMatch){ if(err) return cb(err); cb(null,isMatch) }) } userSchema.methods.generateResetToken = function(cb){ var user = this; crypto.randomBytes(20,function(err,buffer){ var token = buffer.toString('hex'); var today = moment().startOf('day').valueOf(); var tomorrow = moment(today).endOf('day').valueOf(); user.resetToken = token; user.resetTokenExp = tomorrow; user.save(function(err,user){ if(err) return cb(err); cb(null,user); }) }) } userSchema.methods.generateToken = function(cb){ var user = this; var token = jwt.sign(user._id.toHexString(),process.env.SECRET) user.token = token; user.save(function(err,user){ if(err) return cb(err); cb(null,user); }) } userSchema.statics.findByToken = function(token,cb){ var user = this; jwt.verify(token,process.env.SECRET,function(err,decode){ user.findOne({"_id":decode,"token":token},function(err,user){ if(err) return cb(err); cb(null,user); }) }) } const User = mongoose.model('User',userSchema); module.exports = { User } ​tl;dr: If models are just objects holding associated data, whats the point?​​

Submitted May 26, 2020 at 06:44AM by bigbobbyboy5

No comments:

Post a Comment