You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
773 B
32 lines
773 B
import Dexie from 'dexie';
|
|
import {DatabaseService} from './database.service';
|
|
|
|
export abstract class Model<InterfaceType> {
|
|
public static dbService?: DatabaseService;
|
|
|
|
public id?: number;
|
|
|
|
public static getSchema(): string {
|
|
throw new TypeError('Child class must implement.');
|
|
}
|
|
|
|
public static getTableName(): string {
|
|
throw new TypeError('Child class must implement.');
|
|
}
|
|
|
|
public abstract getDatabase(): Dexie.Table<InterfaceType, number>;
|
|
public abstract getSaveRecord(): any;
|
|
|
|
public staticClass() {
|
|
return (this.constructor as typeof Model);
|
|
}
|
|
|
|
public exists() {
|
|
return !!this.id;
|
|
}
|
|
|
|
public async save() {
|
|
this.id = await this.getDatabase().put(this.getSaveRecord());
|
|
}
|
|
}
|