Skip to content

Instantly share code, notes, and snippets.

@WhatTheFar
Last active July 25, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhatTheFar/a8f26d692ea758ea06238eb1d7f3bc86 to your computer and use it in GitHub Desktop.
Save WhatTheFar/a8f26d692ea758ea06238eb1d7f3bc86 to your computer and use it in GitHub Desktop.
Custom sequelize user model for express-gateway-plugin-cas in FA project
// @ts-check
/// <reference path="./types.d.ts" />
/// <reference path="../lib/types/sequelize.d.ts" />
const _ = require('lodash');
/** @type {DefineFunciton<CustomUserInstance, ICustomUserAttributes>} */
const defineFunction = (sequelize, Datatypes) => {
/** @type {SequelizeAttributes<ICustomUserAttributes>} */
const attributes = {
username: { type: Datatypes.STRING, allowNull: true, unique: true },
password: { type: Datatypes.STRING, allowNull: false },
firstName: { type: Datatypes.STRING },
lastName: { type: Datatypes.STRING },
email: { type: Datatypes.STRING },
phoneNumber: { type: Datatypes.STRING },
faIdPrefix: { type: Datatypes.STRING }
};
return sequelize.define('users', attributes, {
hooks: {
afterCreate: (user, options) => {
const faId = _.padStart(user.id, 5, '0');
user.username = `${user.faIdPrefix}${faId}`;
return user.save();
}
}
});
};
module.exports = defineFunction;
// Datatypes Reference: http://docs.sequelizejs.com/variable/index.html#static-variable-DataTypes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment