modelling/lib/DatabaseModel.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-05-02 19:40:53 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
2020-05-05 11:37:48 +02:00
import { Persistent } from './Persistent';
2020-05-02 20:00:41 +02:00
2020-05-05 11:37:48 +02:00
export abstract class DatabaseModel extends Persistent {
2020-05-02 20:00:41 +02:00
public get id (): number {
2020-05-05 11:37:48 +02:00
return this.get ('id') as number;
2020-05-02 20:00:41 +02:00
}
2020-05-05 11:37:48 +02:00
public constructor (id?: number) {
super ();
this.properties.id = 'number';
2020-05-05 11:48:46 +02:00
this.define_properties ();
2020-05-05 11:37:48 +02:00
for (const prop of Object.keys (this.properties)) {
2020-05-03 15:35:29 +02:00
if ([
'string',
'number',
'boolean'
2020-05-05 11:37:48 +02:00
].indexOf (this.properties[prop]) < 0) {
throw new Error (
'property types have to be either string, number or boolean'
);
}
2020-05-03 15:35:29 +02:00
}
2020-05-05 11:37:48 +02:00
if (typeof id !== 'undefined')
this.set ('id', id);
2020-04-23 17:09:14 +02:00
}
2020-05-02 21:07:15 +02:00
public get (key: string): string|number|boolean {
2020-05-05 11:37:48 +02:00
return super.get (key) as string|number|boolean;
2020-05-02 21:07:15 +02:00
}
public set (key: string, value: string|number|boolean): void {
2020-05-05 11:37:48 +02:00
super.set (key, value);
2020-05-02 21:07:15 +02:00
}
2020-04-23 17:09:14 +02:00
2020-05-05 11:37:48 +02:00
public to_object (): Record<string, string|number|boolean> {
return super.to_object () as Record<string, string|number|boolean>;
2020-05-04 20:13:41 +02:00
}
2020-05-02 21:20:01 +02:00
public abstract async read(): Promise<boolean>;
public abstract async write(): Promise<boolean>;
2020-05-02 21:26:51 +02:00
public abstract async delete(): Promise<boolean>;
2020-05-05 11:48:46 +02:00
protected abstract define_properties(): void;
2020-04-23 17:09:14 +02:00
}