Deepak Singh Deepak Singh - 1 month ago 12
Node.js Question

node Sequelize how to write static functions

I have experience in writing statics functions in moongose like

var mongoose =require('mongoose');
var Schema = mongoose.Schema;
var adminSchema = new Schema({
fullname : String,
number : Number,
email: String,
auth : {
username: String,
password : String,
salt: String
}

});


adminSchema.statics.usernameInUse = function (username, callback) {
this.findOne({ 'auth.username' : username }, function (err, doc) {
if (err) callback(err);
else if (doc) callback(null, true);
else callback(null, false);
});
};


here usernameInUse is the function I wana write but using sequelize for mysql database

my model

/*
This module is attendant_user table model.
It will store attendants accounts details.
*/

"use strict";

module.exports = function(sequelize, DataTypes) {
var AttendantUser = sequelize.define('AttendantUser', {

username : {
type : DataTypes.STRING,
allowNull : false,
validate : {
isAlpha : true
}
},{
freezeTableName : true,
paranoid : true
});

return AttendantUser;
};


How to add statics function here..??

Answer
AttendantUser.usernameInUse = function (username, callback) {
   ...
};
return AttendantUser;