Last active
July 25, 2018 10:48
-
-
Save WhatTheFar/a8f26d692ea758ea06238eb1d7f3bc86 to your computer and use it in GitHub Desktop.
Custom sequelize user model for express-gateway-plugin-cas in FA project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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