import {Model} from './Model'; export interface IKeyValue { id?: number; key: string; value: string; json: boolean; offlineUpdatedAt?: string; } export class KeyValue extends Model implements IKeyValue { id?: number; key: string; value: string; json: boolean; offlineUpdatedAt?: string; public static getTableName() { return 'keyValues'; } public static getSchema() { return '++id, key, value, json, offlineUpdatedAt'; } constructor(key: string, value: string, json: boolean, offlineUpdatedAt?: string, id?: number) { super(); this.key = key; this.value = value; this.json = json; if ( typeof offlineUpdatedAt !== 'undefined' ) { this.offlineUpdatedAt = offlineUpdatedAt; } if ( id ) { this.id = id; } } get data(): any { if ( this.json ) { return JSON.parse(this.value); } return this.value; } set data(val: any) { if ( typeof val === 'string' ) { this.json = false; this.value = val; } else { this.json = true; this.value = JSON.stringify(val); } } public getSaveRecord(): any { return { ...(this.id ? { id: this.id } : {}), key: this.key, value: this.value, json: this.json, ...(typeof this.offlineUpdatedAt === 'undefined' ? {} : { offlineUpdatedAt: this.offlineUpdatedAt }) }; } public getDatabase(): Dexie.Table { return this.staticClass().dbService.table('keyValues') as Dexie.Table; } }