This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
indexedDbBlazor.ts
173 lines (124 loc) · 5.57 KB
/
indexedDbBlazor.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
///// <reference path="Microsoft.JSInterop.d.ts"/>
import idb from 'idb';
import { DB, UpgradeDB, ObjectStore, Transaction } from 'idb';
import { IDbStore, IIndexSearch, IIndexSpec, IStoreRecord, IStoreSchema, IDotNetInstanceWrapper } from './interopInterfaces';
export class IndexedDbManager {
private dbInstance:any = null;
constructor() { }
public openDatabase = async (data: IDbStore, instanceWrapper: IDotNetInstanceWrapper): Promise<string> => {
const dbStore = data;
//just a test for the moment
instanceWrapper.instance.invokeMethod(instanceWrapper.methodName, "Hello from the other side");
this.dbInstance = await idb.open(dbStore.dbName, dbStore.version, upgradeDB => {
this.upgradeDatabase(upgradeDB, dbStore);
});
return `IndexedDB ${data.dbName} opened`;
}
public deleteDb = async(dbName: string): Promise<string> => {
this.dbInstance.close();
await idb.delete(dbName);
return `The database ${dbName} has been deleted`;
}
public addRecord = async (record: IStoreRecord): Promise<string> => {
const stName = record.storename;
let itemToSave = record.data;
const tx = this.getTransaction(this.dbInstance, stName, 'readwrite');
const objectStore = tx.objectStore(stName);
itemToSave = this.checkForKeyPath(objectStore, itemToSave);
const result = await objectStore.add(itemToSave, record.key);
return `Added new record with id ${result}`;
}
public updateRecord = async (record: IStoreRecord): Promise<string> => {
const stName = record.storename;
const tx = this.getTransaction(this.dbInstance, stName, 'readwrite');
const result = await tx.objectStore(stName).put(record.data, record.key);
return `updated record with id ${result}`;
}
public getRecords = async (storeName: string): Promise<any> => {
const tx = this.getTransaction(this.dbInstance, storeName, 'readonly');
let results = await tx.objectStore(storeName).getAll();
await tx.complete;
return results;
}
public clearStore = async (storeName: string): Promise<string> => {
const tx = this.getTransaction(this.dbInstance, storeName, 'readwrite');
await tx.objectStore(storeName).clear();
await tx.complete;
return `Store ${storeName} cleared`;
}
public getRecordByIndex = async (searchData: IIndexSearch): Promise<any> => {
const tx = this.getTransaction(this.dbInstance, searchData.storename, 'readonly');
const results = await tx.objectStore(searchData.storename)
.index(searchData.indexName)
.get(searchData.queryValue);
await tx.complete;
return results;
}
public getAllRecordsByIndex = async (searchData: IIndexSearch): Promise<any> => {
const tx = this.getTransaction(this.dbInstance, searchData.storename, 'readonly');
let results: any[] = [];
tx.objectStore(searchData.storename)
.index(searchData.indexName)
.iterateCursor(cursor => {
if (!cursor) {
return;
}
if (cursor.key === searchData.queryValue) {
results.push(cursor.value);
}
cursor.continue();
});
await tx.complete;
return results;
}
public getRecordById = async (storename: string, id: any): Promise<any> => {
const tx = this.getTransaction(this.dbInstance, storename, 'readonly');
let result = await tx.objectStore(storename).get(id);
return result;
}
public deleteRecord = async (storename: string, id: any): Promise<string> => {
const tx = this.getTransaction(this.dbInstance, storename, 'readwrite');
await tx.objectStore(storename).delete(id);
return `Record with id: ${id} deleted`;
}
private getTransaction(dbInstance: DB, stName: string, mode?: 'readonly' | 'readwrite') {
const tx = dbInstance.transaction(stName, mode);
tx.complete.catch(
err => {
console.log((err as Error).message);
});
return tx;
}
// Currently don't support aggregate keys
private checkForKeyPath(objectStore: ObjectStore<any, any>, data: any) {
if (!objectStore.autoIncrement || !objectStore.keyPath) {
return data;
}
if (typeof objectStore.keyPath !== 'string') {
return data;
}
const keyPath = objectStore.keyPath as string;
if (!data[keyPath]) {
delete data[keyPath];
}
return data;
}
private upgradeDatabase(upgradeDB: UpgradeDB, dbStore: IDbStore) {
if (upgradeDB.oldVersion < dbStore.version) {
if (dbStore.stores) {
for (var store of dbStore.stores) {
if (!upgradeDB.objectStoreNames.contains(store.name)) {
let primaryKey = store.primaryKey;
if (!primaryKey) {
primaryKey = { name: 'id', keyPath: 'id', auto: true };
}
const newStore = upgradeDB.createObjectStore(store.name, { keyPath: primaryKey.name, autoIncrement: primaryKey.auto });
for (var index of store.indexes) {
newStore.createIndex(index.name, index.keyPath, { unique: index.unique});
}
}
}
}
}
}
}