Git Product home page Git Product logo

mongoose-savehashed's Introduction

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 = require('mongoose-savehashed');

mongoose.plugin(mongoose-savehashed); // To Register For All Schemas

Register The Plugin For Specific Schemas:

const mongoose = require('mongoose');

const mongoose-savehashed = require('mongoose-savehashed');

const Schema = mongoose.Schema;

const someSchema = new Schema({
		someAttr:{
			type:'string'
		}
	});

someSchema.plugin(mongoose-savehashed);

const someModel = mongoose.model('someModel',someSchema);

Calling saveHashed

Consider a userModel.js

Here you want to hash password and a secretKey before storing in MongoDb.

const mongoose = require('mongoose');

const mongoose-savehashed = require('mongoose-savehashed');

const Schema = mongoose.Schema;

const userSchema = new Schema({
		username:{
			type:'string'
		},
		password:{
			type:'string'
		},
		secretKey:{
			type:'string'
		}
	});

userSchema.plugin(mongoose-savehashed);

module.exports = mongoose.model('user',userSchema);

Consider userController.js

const User = require('./userModel.js');

User.create({username:"akshitgrover",password:"1516",secretKey:"151617"}).then((user)=>{
	

	//Pass list of object properties to hash, If no list passed by default "password" is chosen.

	user.saveHashed(["password","secretKey"]).then((str)=>{

		console.log(str);

	}).catch((err)=>{

		console.log(err);

	}); 
});

Calling compareHashed

Consider userController.js

const User = require('./userModel.js');

// 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.compareHashed(query,values).then((users)=>{
	
	console.log(users); // List of matched with hashed values documents.

}).catch((err)=>{

	console.log(err);

});

//Passing Options

User.compareHashed(query,values,options).then((user)=>{
	
	console.log(users); // First Document matched and compared wiht hashed values.

}).catch((err)=>{

	console.log(err);

});

mongoose-savehashed's People

Contributors

akshitgrover avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.