amad4biz
amad4biz

Reputation: 19

Sequelize ClassMethods

I am trying to build a simple api for a deposit and withdrawal with a bank account using sequelize and nodejs ,but I am little confused how I use my methods that I put in the classmethods. Can anyone please show how can i use this into my controllers. below is my model

  'use strict';
  module.exports = function(sequelize, DataTypes) {
    var Account = sequelize.define('Account', {
      name: DataTypes.STRING,
      balance: DataTypes.DOUBLE,
      pin: DataTypes.INTEGER,

  }, {
      classMethods: {
      associate: function(models) {
      // associations can be defined here
    },

   addMoney: function(amount){

       amount = Math.abs(amount);

       return this.increment('balance', {by : amount}).save();


   },

   withdrawMoney: function(amount){

           amount = Math.abs(amount);

       return this.decrement('balance', {by : amount}).save();

         }


       }



       });
        return Account;
       }

Below is my controllers, but i am not sure how to use my class methods in the controllers

     var models = require('../models/index');

    module.exports = {

     newAccount(req, res, next){

        models.Account.create({
          balance: req.body.balance,
          note: req.body.note,
          pin: req.body.pin,





      }).then(function(account){

          res.json(account);

      }).catch(function(error){

          res.json(error)
      })
 },

   listAccount(req, res, next){

     models.Account.
                 findAll({

                 })
                .then(function(accounts) {
                        res.status(200).send(accounts);
                    }).catch(function(error){

                        res.status(400).send(error)
                    });

       }
  }

and this is my routes in case, this is just the routes to avoid posting too much code

app.get('/accounts', accountCtrl.listAccount);
app.post('/account/new', accountCtrl.newAccount);
app.put('/account/:id', accountCtrl.updateAccount);
app.delete('/account/:id', accountCtrl.removeAccount);

Thank you in avdance for any help, I am new to sequelize

Upvotes: 0

Views: 1788

Answers (1)

Zeke Nierenberg
Zeke Nierenberg

Reputation: 2206

You're thinking of instance methods. The this in instance methods would be an account.

With classMethods the this is the class its self. Class methods are useful when you need to define custom functionality about many instances.

In your example, perhaps you want to run a function once a month and charge savings accounts below a certain amount a fee (something that my bank does!)

classMethods: {
  async findAndCharge(n) {
    const accounts = await this.findAll({ where: { balance: { $lte: n } } });

    for (const account of accounts) {
      await account.charge()
    }
  }
 }

This is a somewhat contrived example, but as you can see the this in the class method is Account (with a cap) not an account lower case.

In other contexts this is sometimes a static method.

In your case you should switch to instanceMethods.

Upvotes: 1

Related Questions