aboutsummaryrefslogtreecommitdiff
path: root/src/lib/models/__helpers.ts
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-10-27 19:26:52 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-10-27 19:26:52 -0400
commit38f488528ba96e8445f29c9fe24131930f03a697 (patch)
treefd87eb69a0f785739e51077aa3b94921fa9be8ee /src/lib/models/__helpers.ts
parent43dadce8b744a43b86cc3febba1046d714cdafcb (diff)
downloadtanzanite-38f488528ba96e8445f29c9fe24131930f03a697.tar.gz
tanzanite-38f488528ba96e8445f29c9fe24131930f03a697.tar.bz2
tanzanite-38f488528ba96e8445f29c9fe24131930f03a697.zip
use declaration merging for models and clean them up
Diffstat (limited to 'src/lib/models/__helpers.ts')
-rw-r--r--src/lib/models/__helpers.ts45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/lib/models/__helpers.ts b/src/lib/models/__helpers.ts
index 243b274..dafbd84 100644
--- a/src/lib/models/__helpers.ts
+++ b/src/lib/models/__helpers.ts
@@ -1,6 +1,5 @@
import { DataTypes, Model } from 'sequelize';
-export const NEVER_USED = 'This should never be executed';
export function jsonParseGet(this: Model, key: string): any {
return JSON.parse(this.getDataValue(key));
}
@@ -8,7 +7,21 @@ export function jsonParseSet(this: Model, key: string, value: any): any {
return this.setDataValue(key, JSON.stringify(value));
}
-export function jsonArrayInit(key: string): any {
+export function jsonObject(key: string): any {
+ return {
+ type: DataTypes.TEXT,
+ get: function (): Record<string, unknown> {
+ return jsonParseGet.call(this, key);
+ },
+ set: function (val: Record<string, unknown>) {
+ return jsonParseSet.call(this, key, val);
+ },
+ allowNull: false,
+ defaultValue: '{}'
+ };
+}
+
+export function jsonArray(key: string): any {
return {
type: DataTypes.TEXT,
get: function (): string[] {
@@ -21,3 +34,31 @@ export function jsonArrayInit(key: string): any {
defaultValue: '[]'
};
}
+
+export function jsonBoolean(key: string, defaultVal = false): any {
+ return {
+ type: DataTypes.STRING,
+ get: function (): boolean {
+ return jsonParseGet.call(this, key);
+ },
+ set: function (val: boolean) {
+ return jsonParseSet.call(this, key, val);
+ },
+ allowNull: false,
+ defaultValue: `${defaultVal}`
+ };
+}
+
+export function jsonBigint(key: string, defaultVal = 0n): any {
+ return {
+ type: DataTypes.TEXT,
+ get: function (): bigint {
+ return BigInt(this.getDataValue(key));
+ },
+ set: function (val: bigint) {
+ return this.setDataValue(key, `${val}`);
+ },
+ allowNull: false,
+ defaultValue: `${defaultVal}`
+ };
+}