This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/models/ApiKey.js
2018-07-10 20:44:59 -07:00

42 lines
780 B
JavaScript

// @flow
import { DataTypes, sequelize } from '../sequelize';
import randomstring from 'randomstring';
const ApiKey = sequelize.define(
'apiKeys',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: DataTypes.STRING,
secret: { type: DataTypes.STRING, unique: true },
userId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
},
},
},
{
tableName: 'apiKeys',
paranoid: true,
hooks: {
beforeValidate: key => {
key.secret = randomstring.generate(38);
},
},
}
);
ApiKey.associate = models => {
ApiKey.belongsTo(models.User, {
as: 'user',
foreignKey: 'userId',
});
};
export default ApiKey;