modelling/lib/DatabaseModel.ts

35 lines
868 B
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-04-23 17:09:14 +02:00
export abstract class DatabaseModel {
2020-05-02 20:00:41 +02:00
protected data: Record<string, string|number|boolean> = {};
public get id (): number {
return this.data.id;
}
public set id (val: number) {
this.data.id = val;
}
2020-04-23 17:09:14 +02:00
public static async get<T extends DatabaseModel> (
id: number,
constructor: new () => T
): Promise<T> {
const dbm = (new constructor);
dbm.id = id;
await dbm.read ();
return dbm;
}
2020-05-02 20:00:41 +02:00
public abstract get(key: string): string|number|boolean;
public abstract set(key: string, value: string|number|boolean);
2020-04-23 17:09:14 +02:00
public abstract read(): Promise<void>;
public abstract write(): Promise<void>;
}