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.
37 lines
883 B
37 lines
883 B
import Dexie from 'dexie';
|
|
import {DatabaseService} from './database.service';
|
|
import {uuid_v4} from '../../utility';
|
|
|
|
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 static getUUID(): string {
|
|
return uuid_v4();
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|