Skip to content
Add unique tokens to sequelize models
Branch: master
Clone or download
gkozlenko Version 0.2.1
- Upgrade dependencies to fix a potential security vulnerability
Latest commit ada18f2 Apr 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Discontinuing support for End-of-Life Node.js Releases Apr 2, 2019
test
.editorconfig
.eslintrc Discontinuing support for End-of-Life Node.js Releases Apr 2, 2019
.gitignore Change GitHub account Apr 2, 2019
.npmignore
.travis.yml Discontinuing support for End-of-Life Node.js Releases Apr 2, 2019
LICENSE Discontinuing support for End-of-Life Node.js Releases Apr 2, 2019
README.md
index.js Initial commit Jun 1, 2016
package-lock.json Version 0.2.1 Apr 11, 2019
package.json Version 0.2.1 Apr 11, 2019

README.md

sequelize-tokenify

Build Status npm Version Maintainability Test Coverage GitHub License

Add unique tokens to sequelize models

Installation

$ npm install sequelize-tokenify

Usage

var SequelizeTokenify = require('sequelize-tokenify');

module.exports = function(sequelize, Sequelize) {

    var User = sequelize.define('User', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        email: {
            type: Sequelize.STRING,
            unique: true
        },
        recovery_token: {
            type: Sequelize.STRING,
            unique: true
        }
    });

    SequelizeTokenify.tokenify(User, {
        field: 'recovery_token'
    });

    return User;
};

Options

tokenify method takes an options array as it's second parameter.

SequelizeTokenify.tokenify(User, {
    field: 'recovery_token'
});

Available options:

  • field - Field name in the model to store token value. Default value: token.
  • scope - Array of field names in the model that are used to limit the uniqueness token generation.
  • length - Length of the token value. Default value: 10.
  • charset - Character set for the token value. Default value: alphanumeric. Available values:
    • alphanumeric - [0-9a-zA-Z]
    • alphabetic - [a-zA-Z]
    • numeric - [0-9]
    • hex - [0-9a-f]

All options are optional.

Methods

sequelize-tokenify module creates several instance methods with names depend on the token field:

  • generate[TokenField] - Generates a new token without saving the model.
  • update[TokenField] - Generates a new token and saves the model.

For recovery_token field module will create these methods:

  • generateRecoveryToken
  • updateRecoveryToken

So you can use this module several times for the same model:

var SequelizeTokenify = require('sequelize-tokenify');

module.exports = function(sequelize, Sequelize) {

    var User = sequelize.define('User', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        email: {
            type: Sequelize.STRING,
            unique: true
        },
        token: {
            type: Sequelize.STRING,
            unique: true
        },
        recovery_token: {
            type: Sequelize.STRING,
            unique: true
        }
    });

    // For token field
    SequelizeTokenify.tokenify(User);

    // For recovery_token field
    SequelizeTokenify.tokenify(User, {
        field: 'recovery_token'
    });

    return User;
};
You can’t perform that action at this time.