/
database.service.ts
58 lines (51 loc) · 1.56 KB
/
database.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {Injectable} from '@angular/core';
import * as localForage from 'localforage';
import {SnackService} from '../snack/snack.service';
import {T} from '../../t.const';
@Injectable({
providedIn: 'root',
})
export class DatabaseService {
constructor(
private _snackService: SnackService,
) {
this._init();
}
async load(key: string): Promise<any> {
try {
return localForage.ready().then(() => localForage.getItem(key));
} catch (e) {
this._snackService.open({type: 'ERROR', msg: T.GLOBAL_SNACK.ERR_DB_LOAD});
}
}
async save(key: string, data: any): Promise<any> {
try {
return localForage.ready().then(() => localForage.setItem(key, data));
} catch (e) {
this._snackService.open({type: 'ERROR', msg: T.GLOBAL_SNACK.ERR_DB_SAVE});
}
}
async remove(key: string): Promise<any> {
try {
return localForage.ready().then(() => localForage.removeItem(key));
} catch (e) {
this._snackService.open({type: 'ERROR', msg: T.GLOBAL_SNACK.ERR_DB_DELETE});
}
}
async clearDatabase(): Promise<any> {
try {
return localForage.ready().then(() => localForage.clear());
} catch (e) {
this._snackService.open({type: 'ERROR', msg: T.GLOBAL_SNACK.ERR_DB_CLEAR});
}
}
private _init() {
localForage.config({
driver: [localForage.INDEXEDDB, localForage.WEBSQL, localForage.LOCALSTORAGE],
name: 'SUP',
// version: 1.0,
storeName: 'SUP_STORE', // Should be alphanumeric, with underscores.
description: 'All data for Super Productivity 2'
});
}
}