What is mongoose-savehashed?
mongoose-savehashed is mongoose plugin built for node.js which helps you to save and compare hashed values.
Note: Right Now, This plugin completely functions on promises, Based on feedback callback support will be added.
How to install?
Type (On CLI):
npm install mongoose-savehashed --save
Usage:
NOTE: Register The Plugin Before Calling Model In Any File In Your App, Register Only Once.
Register The Plugin For All Schemas:
const mongoose-savehashed = ; mongoose; // To Register For All Schemas
Register The Plugin For Specific Schemas:
const mongoose = ; const mongoose-savehashed = ; const Schema = mongooseSchema; const someSchema = someAttr: type:'string' ; someSchema; const someModel = mongoose;
Calling saveHashed
Consider a userModel.js
Here you want to hash password and a secretKey before storing in MongoDb.
const mongoose = ; const mongoose-savehashed = ; const Schema = mongooseSchema; const userSchema = username: type:'string' password: type:'string' secretKey: type:'string' ; userSchema; moduleexports = mongoose;
Consider userController.js
const User = ; User;
Calling compareHashed
Consider userController.js
const User = ; // Pass A Query to find the documents and acctual values to compare which are hashed in MongoDb const query = username:"akshitgrover"; const values = password:"1516"secretKey:"151617"; cosnt options = size:1 // To Get A Single Document, As Many Documents Can Match (optional) User; //Passing Options User;