sequelize + dataInsert - node.js

In my node app i am using sequelize as an ORM for my postgresql database.The following is my model:
User:
var bcrypt = require('bcrypt'),
crypto = require('crypto');
var authTypes = ['github', 'twitter', 'facebook', 'google'];
var map_attributes = function() {
var obj = new Object(),
ctx = this;
ctx.attributes.forEach(
function(attr) {
obj[attr] = ctx[attr];
});
return obj;
};
var validatePresenceOf = function(value) {
return value && value.length;
};
module.exports = function(sequelize, DataTypes) {
return sequelize.define('user', {
name: {
type: DataTypes.STRING,
validate: {
len: {
args: 1,
msg: "Name cannot be blank"
}
}
},
email: {
type: DataTypes.STRING,
validate: {
len: {
args: 1,
msg: "email cannot be blank"
}
}
},
username: {
type: DataTypes.STRING,
validate: {
len: {
args: 1,
msg: "username cannot be blank"
}
}
},
provider: DataTypes.STRING,
//hashed_password: String,
hashed_password: {
type: DataTypes.STRING,
set: function(v) {
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(v, salt);
this.setDataValue('hashed_password', hash);
}
},
salt: DataTypes.STRING,
/*facebook: {},
twitter: {},
github: {},
google: {}*/
}, {
hooks: {
beforeValidate: function(next) {
if (!this.isNew) return next();
if (!validatePresenceOf(this.password) && authTypes.indexOf(this.provider) === -1) next(new Error('Invalid password'));
else next();
}
}
}, {
instanceMethods: {
authenticate: function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
},
makeSalt: function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
},
encryptPassword: function(password) {
if (!password) return '';
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
}
}
});
}
In this model i have to insert data into my database.How can i achieve this. I found this tutorial http://sequelizejs.com/docs/1.7.8/instances but its different from my design , here i am exporting the total model.Thanks in advance.
I tried for this:
index.js:
var Sequelize = require('sequelize');
var util = require('util');
var config = require('config').dbpostgres; // we use node-config to handle environments
// initialize database connection
var sequelize = new Sequelize(
config.dbname,
config.username,
config.password, {
dialect: 'postgres'
});
//var sequelize = new Sequelize('postgres://postgres:postgres#localhost:5432/Geocode', { dialect: 'postgres', protocol: 'postgres' });
// load models
var models = ['user'] //,'sciSphereModel', 'access_token', 'oauth_client', 'request_token', 'article'];
models.forEach(function(model) {
//console.log(model)
var sequelizeModel = sequelize.import(__dirname + '/' + model);
//console.log("seq=" + util.inspect(sequelizeModel));
//sequelizeModel.sync();
/*sequelizeModel.sync().success(function() {
console.log('DB error');
}).error(function(error) {
console.log(error);
})*/
sequelizeModel.sync();
//console.log("sequelizeModel=" + util.inspect(sequelizeModel))
var user = sequelizeModel.build({
name: 'kumar',
email: 'kumar#gmail.com',
username: 'kumar007',
provider: 'local',
hashed_password: 'tyegnaak',
salt: 'uopioann'
})
user.save().complete(function(err) {
console.log("Inside user save");
if ( !! err) {
console.log('The instance has not been saved:', err)
}
else {
console.log('We have a persisted instance now')
}
})
module.exports[model] = sequelizeModel; //Implement sync
});
// export connection
//sequelize.sync();
module.exports.sequelize = sequelize;
app.set('sequelize', sequelize);
Whether i am doing right?.Is this the way to achieve this?

Wouldn't it be sequelize.sync(); not sequelizeModel.sync(); because you'd want to syncronize any model changes with the db. You can also add force to your sync in case the table doesn't match your model.
sequelize.sync({ force: true }); // use to drop before create
I have never tried sync on a model, just create(), save(), find(), updateAttributes() etc.

Related

Error("No Sequelize instance passed") No Sequelize instance passed

Hello can somebody help me with this ? I was doing my controllers and I can access to my model like this "const { User } = require("../models/User");" but than when I send my request I had this message "TypeError: Cannot read property 'create' of undefined" so something was missing.
So I change my call model to this "const { User } = require("../models");".
And I went on my model index.js (so connection sequelize) and I add fs function and Objectif.key. After all those changes I have the error "No Sequelize instance passed".
So maybe somebody can help with this because I don't see the problem
So this is my model index.js
//sequelize importe
const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const db = {};
const basename = path.basename(__filename);
let sequelize = new Sequelize("groupomania", "root", "root", {
host: "localhost",
dialect: "mysql",
});
sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully!");
})
.catch((err) => {
console.log("Can't establish database connection:\n" + err);
});
fs.readdirSync(__dirname)
.filter((file) => {
console.log( file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js");
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
This is my model User.js
const { Model } = require("sequelize");
module.exports = (Sequelize, DataTypes) => {
class User extends Model {
toJSON() {
return {
...this.get(),
password: undefined,
};
}
}
User.init(
{
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 },
nom: { type: DataTypes.STRING, allowNull: false },
prenom: { type: DataTypes.STRING, allowNull: false },
email: {
type: DataTypes.STRING,
allowNull: false,
validate: { notNull: true, notEmpty: true, isEmail: true },
},
status: { type: DataTypes.STRING, allowNull: false },
password: { type: DataTypes.STRING, required: true },
},
{ Sequelize, tableName: "users", modelName: "User" }
);
return User;
};
My controllers/user.js
/*const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");*/
const { User } = require("../models/User");
module.exports.signup = async (req, res) => {
try {
const user = await User.create({
nom: "Jp",
prenom: "remy",
email: "remy#gmail.fr",
password: "motdepasse",
});
res.status(200);
throw Error("erreur est survenu");
} catch (erreur) {
console.log(erreur);
res.status(200).json({ erreur });
}
};
My route user
const express = require("express");
const router = express.Router();
const userCtrl = require("../controllers/user");
router.post("/signup", userCtrl.signup);
/*router.post("/login", userCtrl.login);*/
module.exports = router;
Thank you for any help! :)
The answer was that on my model user the "S" of sequelize must have an s minus

E11000 duplicate key error index: mongo.users.$password_1 dup key:

I had this error even though i have sparse index set to true on that feild password
here is user schema:
var roledef = 'Member';
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
sparse: true,
minlength: [5, 'Username must be grater than 5 characters']
},
password: {
type: String,
required: true,
sparse: true,
minlength: [5, 'Username must be grater than 5 characters']
},
email: {
type: String,
required: true
},
profileid: {
type: String,
required: true,
},
roles: {
type: String,
default: roledef,
sparse: true
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
// write encryption
and i am routing throug a index file which works
but i dont know if this is the best way to create a user
here is my user controller file :
const userModel = require('../../models');
const UserController = {};
const User = require('../../models/User')
const sha256 = require('sha256');
module.exports.validateregistration = (req, res) => {
console.log("##########################################################".red);
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
if (req.body.encodedUsername && req.body.encodedPassword && req.body.encodedEmail) {
// res.send(result);
log(ip);
var buffU = new Buffer(req.body.encodedUsername, 'base64');
var buffP = new Buffer(req.body.encodedPassword, 'base64');
var buffE = new Buffer(req.body.encodedEmail, 'base64');
var Username = buffU.toString('ascii');
var Pass = buffP.toString('ascii');
var Email = buffE.toString('ascii');
var hashPass = sha256.x2(Pass);
console.log("Register request for " + "Username: " + Username);
const userData = {
username: Username,
password: hashPass,
email: Email,
profileid: sha256(buffU)
};
const user = new userModel.User({
username: Username,
password: hashPass,
email: Email,
profileid: sha256(buffU)
});
const details = userData;
user.save().then((newuser) => {
res.status(200).json({
success: true,
data: newuser
});
}).catch((err) => {
res.status(500).json({
message: err
});
console.log(err);
});
// User.findOne({ username: userData.username, email: userData.email }, (err, resultofcheck) => {
// console.log(resultofcheck)
// if (resultofcheck == null) {
// User.create(userData, function (err, user) {
// if (err) {
// console.log(err);
// res.send("2")
// } else {
// console.log(user);
// console.log("New user created: " + user.username + " Profile id: " + user.profileid);
// res.redirect("/api/profile/" + user.profileid);
// }
// });
// } else {
// console.log(Username + " is taken")
// console.log(err);
// res.send("2");
// };
// });
}
console.log("##########################################################".red);
};
// if (!result) {
// db.collection('users').insert(details, (err, resultofinsert) => {
// if (err) {
// console.log(err);
// res.sendStatus(500);
// } else {
// res.send(user.profileid);
// console.log("New user created: " + user);
// }
// });
// };
// db.collection('users').insert(user, (err, result) => {
// if (err) {
// res.sendStatus(501);
// res.send({ 'error': 'An error has occured!' });
// } else {
// // res.send(result.ops[0].pass);
// console.log(result);
// };
// });
im calling throug another index file but iworks also but i get this error:
{ MongoError: E11000 duplicate key error index:
mongo.users.$password_1 dup key: { :
"e701ea082879498082d025e0cf9857ec0d19e6e86fa39f92ed3286de55d340e6" }
at Function.create (C:\Users\abinash\Desktop\api\node_modules\mongodb-core\lib\error.js:43:12)
at toError (C:\Users\abinash\Desktop\api\node_modules\mongoose\node_modules\mongodb\lib\utils.js:149:22)
at coll.s.topology.insert (C:\Users\abinash\Desktop\api\node_modules\mongoose\node_modules\mongodb\lib\operations\collection_ops.js:828:39)
at C:\Users\abinash\Desktop\api\node_modules\mongodb-core\lib\connection\pool.js:532:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9) driver: true, name: 'MongoError', index: 0, code: 11000,
errmsg: 'E11000 duplicate key error index: mongo.users.$password_1 dup
key: { :
"e701ea082879498082d025e0cf9857ec0d19e6e86fa39f92ed3286de55d340e6" }',
[Symbol(mongoErrorContextSymbol)]: {} }
and even though the username and email feilds are different and password isnt required to be unique i tryed to use sparse: true but it dont work pls hep point out what i did wrong
the commented outt parts are the things itryed before i found out about mongoose models

Generate hashed password in findOneAndUpdate

Here is my query for findOneAndUpdate
const { email, password, id } = req.body
Artist.findOneAndUpdate({ _id: id }, { $set: req.body }).then((artist) => {
return res.json({
success: true,
message: "Invitation sent."
});
})
And here is my schema
var artistSchema = new mongoose.Schema({
name: { type: String, default: '' },
password: { type: String, default: '' }
})
artistSchema.pre('findOneAndUpdate', function (next) {
console.log('------------->>>>>> findOneAndUpdate: ');
console.log(this.password) // why undefined?
next();
});
I want to create a hashed password when user update details
const { email, password, id } = req.body;
Artist.findByIdAndUpdate(id, { $set: req.body }).then(artist => {
return res.json({
success: true,
message: "Invitation sent."
});
});
Example with bcrypt
var artistSchema = new mongoose.Schema({
name: { type: String, default: "" },
password: { type: String, default: "" }
});
artistSchema.pre("update", function(next) {
bcrypt.hash(this.password, 10, function(err, hash) {
if (err) return next(err);
this.password = hash;
next();
});
});
let crypto = require('crypto');
let mongoose = require('../mongoose'),
Schema = mongoose.Schema;
Then Schema
let schema = new Schema({
name: {
type: String,
default: ''
},
hashedPassword: {
type: String,
required: true
},
salt: {
type: String,
required: true
}
});
Then methods and virtuals
schema.methods.encryptPassword = function(password){
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
};
schema.virtual('password').set(function(password){
this._plainPassword = password;
this.salt = Math.random() + '';
this.hashedPassword = this.encryptPassword(password);
}).get(function(){ return this._plainPassword; });
You can check password like that
schema.methods.checkPassword = function(password){
return this.encryptPassword(password) === this.hashedPassword;
};
Export module
module.exports.Artist = mongoose.model('Artist', schema);
Then just save like before
const { email, password, id } = req.body;
Artist.findOneAndUpdate({ _id: id }, { $set: req.body }).then((artist) => {
return res.json({
success: true,
message: "Invitation sent."
});
});
But I sugest you also to use statics. For example:
schema.statics.updateUser = function (data){
// your code
}
And then you can use
Artist.updateUser(req.body).then((res) => {
// response
})
The answer: Writeconsole.log(JSON.stringify(this._update));
My solution for check blank password.
userSchema.pre('findOneAndUpdate', function() {
console.log(JSON.stringify(this._update));
if (this._update.password.length == 0) {
this._update = {
"fullname": this._update.fullname
};
}
else {
this._update = {
"fullname": this._update.fullname,
"password": bcrypt.hashSync(this._update.password, bcrypt.genSaltSync(8), null)
};
}
});

sequelize instanceMethods

I'm trying to configurate my database ,i want the data returned to user doesn't include some sensetive data, so i'm adding an instanceMethods named toPublicJSON that using a function named pick from underscoreJS , but when i use this function (toPublicJSON) I encounter an error : that toPublicJSON is not a function
this my database configuration:
var bcrypt = require("bcrypt");
var _ = require("underscore");
module.exports = function(sequelize, DataType) {
return sequelize.define('users', {
email: {
type: DataType.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
salt: {
type: DataType.STRING
},
hassedPassword: {
type: DataType.STRING
},
password: {
type: DataType.VIRTUAL,
allowNull: false,
validate: {
len: [7, 100]
},
set: function(value) {
var salt = bcrypt.genSaltSync(10);
var hashed_password = bcrypt.hashSync(value, salt);
this.setDataValue('password', value);
this.setDataValue("salt", salt);
this.setDataValue("hassedPassword", hashed_password);
}
}
}, {
hooks: {
beforeValidate: function(user, option) {
if (typeof user.email == 'string')
user.email = user.email.toLowerCase();
}
},
instanceMethods:{
toPublicJSON: function() {
var json = this.toJSON();
return _.pick(json, 'id', 'email', 'createdAt', 'updatedAt');
}
}
});
}
my code :
app.post('/users',function(req,res){
var body=_.pick(req.body,"email","password");
db.users.create(body).then(function(user){
return res.send(user.toPublicJSON());
},function(e){
return res.status(400).json(e);
})
});
and this is the error :
Assuming you are using the latest version of Sequelize they changed slightly how to define instance and class methods. You need to assign functions to the prototype of the result of sequelize.define('users');. You will need to save the results of sequelize.define to a variable, similar to below...
const User = sequelize.define('users', {});
User.prototype.toPublicJSON = function() {
// Your code here
};
return User;
If you're on version 3 let me know.
Good luck :)
EDIT: Further reading material http://docs.sequelizejs.com/manual/tutorial/models-definition.html#expansion-of-models
EDIT 2: You may want to look into scopes. They are essentially pre-baked filters you can apply to your data when querying for them. http://docs.sequelizejs.com/manual/tutorial/scopes.html
If you did that then you could say
sequelize.define('users', {
// Attributes...
}, {
scopes: {
public: {
attributes: ['id', 'email', 'createdAt', 'updatedAt']
}
}
});
db.users.scope('public').findOne({...});

Mongo: hash password only being created on insert not update

Below is a JS for Account mongoose model file for my mongo account collection with a couple of methods. When a an account is created the hashedPassword field is created however how its not working when I update the document. Is it the case that the virtuals are only called on insert rather than update ?
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
crypto = require('crypto');
var AccountSchema = new Schema({
email: {type: String,unique: true,required: true},
username: {type: String,unique: true,required: true},
group:{type:Schema.Types.ObjectId,ref:"Groups",require:false},
hashedPassword: String,
salt: String,
name: {type: String,require:false},
active: {type: Boolean,require:false},
});
/**
* Virtuals
*/
AccountSchema.virtual('password').set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashedPassword = this.encryptPassword(password);
})
.get(function() {
return this._password;
});
AccountSchema.virtual('user_info').get(function () {
return { '_id': this._id, 'username': this.username, 'email': this.email };
});
/**
* Validations
*/
AccountSchema.path('email').validate(function (email) {
var emailRegex = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
return emailRegex.test(email);
}, 'The specified email is invalid.');
AccountSchema.path('email').validate(function(value, respond) {
respond(true);
}, 'The specified email address is already in use.');
AccountSchema.methods = {
authenticate: function(plainText) {
return this.encryptPassword(plainText) === this.hashedPassword;
},
makeSalt: function() {
return crypto.randomBytes(16).toString('base64');
},
encryptPassword: function(password) {
console.log('encryptPassword')
if (!password || !this.salt) return '';
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
}
};
mongoose.model('Account', AccountSchema);
In the controller
$scope.register = function(form) {
Auth.createUser({
provider : "local",
_id: $scope.userData._id,
email: $scope.userData.email,
username: $scope.userData.name,
level : $scope.userData.level,
group : $scope.userData.group,
name : $scope.userData.name,
password: $scope.userData.password,
active: $scope.userData.active
},
function(err) {
$scope.errors = {};
if (!err) {
$state.go('users');
} else {
angular.forEach(err.errors, function(error, field) {
form[field].$setValidity('mongoose', false);
$scope.errors[field] = error.type;
});
}
}
);
The services.js where the createUser func is called
/* Services */
app.factory('User', function ($resource) {
return $resource('/auth/users/:id/', {},
{
'update': {
method:'PUT'
}
});
});
app.factory('Session', function ($resource) {
return $resource('/auth/session/');
});
app.factory('Auth', function Auth($location, $rootScope, Session, User, $cookieStore) {
$rootScope.currentUser = $cookieStore.get('user') || null;
$cookieStore.remove('user');
return {
createUser: function(userinfo, callback) {
var cb = callback || angular.noop;
User.save(userinfo,
function(user) {
$rootScope.currentUser = user;
return cb();
},
function(err) {
return cb(err.data);
});
},
currentUser: function() {
Session.get(function(user) {
$rootScope.currentUser = user;
});
}
};
});

Resources