-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdexie.service.ts
105 lines (83 loc) · 2.85 KB
/
dexie.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {Injectable} from '@angular/core';
import {DexieDatabase} from './dexie.database';
@Injectable()
export class DexieService {
constructor(private db: DexieDatabase) {}
addOne(table: string,object: Object) {
return this.db.table(table).add(object);
}
addMultiple(table: string,objects: Object[]) {
return this.db.table(table).bulkAdd(objects);
}
count(table: string) {
return this.db.table(table).count();
}
addOrUpdateOne(table: string,object: Object,key?: any) {
if(key) {
return this.db.table(table).put(object,key);
} else {
return this.db.table(table).put(object);
}
}
addOrUpdateMultiple(table: string,objects: Object[],keys?: any) {
if(keys) {
return this.db.table(table).bulkPut(objects,keys);
} else {
return this.db.table(table).bulkPut(objects);
}
}
deleteOne(table: string,primaryKey: any) {
return this.db.table(table).delete(primaryKey);
}
deleteMultiple(table: string,primaryKeys: any[]) {
return this.db.table(table).bulkDelete(primaryKeys);
}
clearAll(table: string) {
return this.db.table(table).clear();
}
operateOnEach(table: string,callback: (item: any,idbCursor: any) => any) {
return this.db.table(table).each(callback);
}
filter(table: string,filterFunction: (value: any) => boolean) {
return this.db.table(table).filter(filterFunction);
}
getByPrimaryKey(table: string,primaryKey: any,callback?: (item: Object) => any) {
if(callback) {
return this.db.table(table).get(primaryKey,callback);
} else {
return this.db.table(table).get(primaryKey);
}
}
getByKeyToValueMap(table: string,keyValueMap: Object,callback?: (item: Object) => any) {
if(callback) {
return this.db.table(table).get(keyValueMap,callback);
} else {
return this.db.table(table).get(keyValueMap);
}
}
getFirstNItemsOfTable(table: string,num: number) {
return this.db.table(table).limit(num);
}
orderBy(table: string,index: string) {
return this.db.table(table).orderBy(index);
}
offset(table: string,ignoreUpto: number) {
return this.db.table(table).offset(ignoreUpto);
}
reverse(table: string) {
return this.db.table(table).reverse();
}
toArray(table: string,callback?: (objects: Object[]) => any) {
if(callback) {
return this.db.table(table).toArray(callback);
} else {
return this.db.table(table).toArray();
}
}
toCollection(table: string) {
return this.db.table(table).toCollection();
}
update(table: string,key: any,changes: Object) {
return this.db.table(table).update(key,changes);
}
}