66 lines
1.4 KiB
TypeScript
66 lines
1.4 KiB
TypeScript
|
import {Model} from './Model';
|
||
|
|
||
|
export interface IKeyValue {
|
||
|
id?: number;
|
||
|
key: string;
|
||
|
value: string;
|
||
|
json: boolean;
|
||
|
}
|
||
|
|
||
|
export class KeyValue extends Model<IKeyValue> implements IKeyValue {
|
||
|
id?: number;
|
||
|
key: string;
|
||
|
value: string;
|
||
|
json: boolean;
|
||
|
|
||
|
public static getTableName() {
|
||
|
return 'keyValues';
|
||
|
}
|
||
|
|
||
|
public static getSchema() {
|
||
|
return '++id, key, value, json';
|
||
|
}
|
||
|
|
||
|
constructor(key: string, value: string, json: boolean, id?: number) {
|
||
|
super();
|
||
|
|
||
|
this.key = key;
|
||
|
this.value = value;
|
||
|
this.json = json;
|
||
|
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,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
public getDatabase(): Dexie.Table<IKeyValue, number> {
|
||
|
return this.staticClass().dbService.table('keyValues') as Dexie.Table<IKeyValue, number>;
|
||
|
}
|
||
|
}
|