最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node Sequelize how to write static functions - Stack Overflow

programmeradmin2浏览0评论

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..??

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..??

Share Improve this question edited Oct 15, 2016 at 9:42 Andrei Karpuszonak 9,0442 gold badges41 silver badges50 bronze badges asked Oct 13, 2016 at 11:39 Deepak SinghDeepak Singh 351 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Well, you can easily use Expansion of models

var User = sequelize.define('user', { firstname: Sequelize.STRING });

// Adding a class level method
User.classLevelMethod = function() {
  return 'foo';
};

// Adding an instance level method
User.prototype.instanceLevelMethod = function() {
  return 'bar';
};

OR in some cases you may use getter and setter on your models. See the docs:

A) Defining as part of a property:

var Employee = sequelize.define('employee', {
  name:  {
    type     : Sequelize.STRING,
    allowNull: false,
    get      : function()  {
      var title = this.getDataValue('title');
      // 'this' allows you to access attributes of the instance
      return this.getDataValue('name') + ' (' + title + ')';
    },
  },
  title: {
    type     : Sequelize.STRING,
    allowNull: false,
    set      : function(val) {
      this.setDataValue('title', val.toUpperCase());
    }
  }
});

Employee
  .create({ name: 'John Doe', title: 'senior engineer' })
  .then(function(employee) {
    console.log(employee.get('name')); // John Doe (SENIOR ENGINEER)
    console.log(employee.get('title')); // SENIOR ENGINEER
  })

B) Defining as part of the model:

var Foo = sequelize.define('foo', {
  firstname: Sequelize.STRING,
  lastname: Sequelize.STRING
}, {
  getterMethods   : {
    fullName       : function()  { return this.firstname + ' ' + this.lastname }
  },

  setterMethods   : {
    fullName       : function(value) {
        var names = value.split(' ');

        this.setDataValue('firstname', names.slice(0, -1).join(' '));
        this.setDataValue('lastname', names.slice(-1).join(' '));
    },
  }
});

Hope it helps.

AttendantUser.usernameInUse = function (username, callback) {
   ...
};
return AttendantUser;
发布评论

评论列表(0)

  1. 暂无评论