modelling/lib/DatabaseModel.ts
Timo Hocker 433b2b5b27 fix
2020-05-05 11:48:46 +02:00

51 lines
1.4 KiB
TypeScript

/*
* 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
*/
import { Persistent } from './Persistent';
export abstract class DatabaseModel extends Persistent {
public get id (): number {
return this.get ('id') as number;
}
public constructor (id?: number) {
super ();
this.properties.id = 'number';
this.define_properties ();
for (const prop of Object.keys (this.properties)) {
if ([
'string',
'number',
'boolean'
].indexOf (this.properties[prop]) < 0) {
throw new Error (
'property types have to be either string, number or boolean'
);
}
}
if (typeof id !== 'undefined')
this.set ('id', id);
}
public get (key: string): string|number|boolean {
return super.get (key) as string|number|boolean;
}
public set (key: string, value: string|number|boolean): void {
super.set (key, value);
}
public to_object (): Record<string, string|number|boolean> {
return super.to_object () as Record<string, string|number|boolean>;
}
public abstract async read(): Promise<boolean>;
public abstract async write(): Promise<boolean>;
public abstract async delete(): Promise<boolean>;
protected abstract define_properties(): void;
}