DEV Community

Cover image for How to add mail notification to Sequelize
Chiemeke Ifeanyi
Chiemeke Ifeanyi

Posted on

How to add mail notification to Sequelize

Laravel's Eloquent (ORM) offers a notification trait that enables you send notification from the model itself. As sequelize doesn't ship with a notify method as Eloquent, we would be creating our own.

Sounds exciting right? Lets dig in!!!

Note: Basic knowledge of Sequelize and Nodemailer is needed. Try reading up on them first :).

We would be kicking things off by creating our package directory called "sequelizeNotifier".

So, lets get this party started!!!

Create sequelizeNotifier entry point.

We kick things off by creating an entry point for our package. To do this, we would create an index.js file in the root path of our package. We then pass the Sequelize object then extend it by adding a notify function to its prototype. We also pass a config parameter for our transport configuration. Don't bother about the dependicies, we are going to create them let's just keep going.


 const notifier = require('./src/notify');
 const transport = require('./src/mailer');

 module.exports = function (Sequelize,config) {

    Sequelize.Model.prototype.notify = function (options){
       let initTransport = transport(config);
       return  notifier(this,initTransport,options);


Create the Mail Object.

Now let us create a mail function that initializes the nodemailer transport in which returns this transport back to us.


 const nodemailer = require('nodemailer');

 module.exports = function mailer(config) {


            let transport =  nodemailer.createTransport(config);
            return transport;

        }catch (e) {


Create the notifier

Finally, let us create the "notify" method. This is the method that would be called anytime we call the notify method from the sequelize model.

 module.exports = function  notify (model,transport,options) {

        let mail =  transport.sendMail({
    }catch (e) {

Pheewwww we are almost done. It's time to test this beast!!!


I am sure you might have a different approach for configuring your application with Sequelize, but in-respective of your configuration style, just pass the sequelize package into our sequelizeNotifier package with it's configuation and we are good to go

const Sequelize = require('sequelize');
const config = {
//more codes 

Now let's assume we have our register.js file for registering a new user. After creating the user, you would want to notify the user her account has been created. Here is a snippet of register.js:

 const {User} = require('./models');
 module.exports = function register(req,res){
     const user = await User.create({
       name:"chiemeke Ifeanyi",
     //send mail
     const messageObject = {
                        subject: "Account Created Successfully",
                        text: `Hi ${}, welcome to my app`,
    //catch eror


//more codes

For more options to use with the messageObject, check out the nodemailer documentation here for sending messages.

That would be all folks.

Discussion (0)