From 38f488528ba96e8445f29c9fe24131930f03a697 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:26:52 -0400 Subject: use declaration merging for models and clean them up --- src/lib/models/Stat.ts | 52 +++++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 37 deletions(-) (limited to 'src/lib/models/Stat.ts') diff --git a/src/lib/models/Stat.ts b/src/lib/models/Stat.ts index 0059898..89fc00f 100644 --- a/src/lib/models/Stat.ts +++ b/src/lib/models/Stat.ts @@ -1,56 +1,34 @@ import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { NEVER_USED } from './__helpers'; +import { jsonBigint } from './__helpers'; + +type Environment = 'production' | 'development' | 'beta'; export interface StatModel { - environment: 'production' | 'development' | 'beta'; + environment: Environment; commandsUsed: bigint; } export interface StatModelCreationAttributes { - environment: 'production' | 'development' | 'beta'; + environment: Environment; commandsUsed?: bigint; } -export class Stat extends BaseModel implements StatModel { - /** - * The bot's environment. - */ - public get environment(): 'production' | 'development' | 'beta' { - throw new Error(NEVER_USED); - } - public set environment(_: 'production' | 'development' | 'beta') { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface Stat { + /** The bot's environment. */ + environment: Environment; - /** - * The number of commands used - */ - public get commandsUsed(): bigint { - throw new Error(NEVER_USED); - } - public set commandsUsed(_: bigint) { - throw new Error(NEVER_USED); - } + /** The number of commands used */ + commandsUsed: bigint; +} +export class Stat extends BaseModel implements StatModel { public static initModel(sequelize: Sequelize): void { Stat.init( { - environment: { - type: DataTypes.STRING, - primaryKey: true - }, - commandsUsed: { - type: DataTypes.TEXT, - allowNull: false, - get: function (): bigint { - return BigInt(this.getDataValue('commandsUsed') as unknown as string); - }, - set: function (val: bigint) { - return this.setDataValue('commandsUsed', `${val}` as any); - }, - defaultValue: '0' - } + environment: { type: DataTypes.STRING, primaryKey: true }, + commandsUsed: jsonBigint('commandsUsed') }, { sequelize } ); -- cgit