diff --git a/models/Token.ts b/models/Token.ts index 56b6745..5b3e6ed 100644 --- a/models/Token.ts +++ b/models/Token.ts @@ -1,40 +1,41 @@ -import mongoose, {Schema} from 'mongoose'; +import {sequelize} from "../app"; import * as crypto from "crypto"; +import {DataTypes, InferAttributes, InferCreationAttributes, Model} from "sequelize"; export enum TokenType { API, SESSION } -export interface IToken { - token: string +export interface IToken extends Model, InferCreationAttributes> { + token?: string clientId: number - type: TokenType + type?: TokenType userAgent?: string - created: Date + created?: Date } -const TokenSchema = new Schema({ +const TokenSchema = sequelize.define('tokens', { token: { - type: String, - default: () => crypto.randomBytes(48).toString('hex') + type: DataTypes.STRING, + defaultValue: () => crypto.randomBytes(48).toString('hex'), + primaryKey: true }, clientId: { - type: Number, - required: true + type: DataTypes.BIGINT, + allowNull: false }, type: { - type: Number, - enum: TokenType, - default: TokenType.SESSION + type: DataTypes.STRING, + defaultValue: TokenType.API }, userAgent: { - type: String, - required: false + type: DataTypes.STRING, + allowNull: true }, created: { - type: Date, - default: Date.now + type: DataTypes.DATE, + defaultValue: Date.now } }); -export const Token = mongoose.model('tokens', TokenSchema); \ No newline at end of file +export const Token = TokenSchema; \ No newline at end of file