Migrated the Token.ts model

This commit is contained in:
Mathias Wagner 2023-11-07 09:17:31 +01:00
parent 5b322bf580
commit 8b4fc500a2
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,40 +1,41 @@
import mongoose, {Schema} from 'mongoose'; import {sequelize} from "../app";
import * as crypto from "crypto"; import * as crypto from "crypto";
import {DataTypes, InferAttributes, InferCreationAttributes, Model} from "sequelize";
export enum TokenType { export enum TokenType {
API, SESSION API, SESSION
} }
export interface IToken { export interface IToken extends Model<InferAttributes<IToken>, InferCreationAttributes<IToken>> {
token: string token?: string
clientId: number clientId: number
type: TokenType type?: TokenType
userAgent?: string userAgent?: string
created: Date created?: Date
} }
const TokenSchema = new Schema<IToken>({ const TokenSchema = sequelize.define<IToken>('tokens', {
token: { token: {
type: String, type: DataTypes.STRING,
default: () => crypto.randomBytes(48).toString('hex') defaultValue: () => crypto.randomBytes(48).toString('hex'),
primaryKey: true
}, },
clientId: { clientId: {
type: Number, type: DataTypes.BIGINT,
required: true allowNull: false
}, },
type: { type: {
type: Number, type: DataTypes.STRING,
enum: TokenType, defaultValue: TokenType.API
default: TokenType.SESSION
}, },
userAgent: { userAgent: {
type: String, type: DataTypes.STRING,
required: false allowNull: true
}, },
created: { created: {
type: Date, type: DataTypes.DATE,
default: Date.now defaultValue: Date.now
} }
}); });
export const Token = mongoose.model('tokens', TokenSchema); export const Token = TokenSchema;