Add business methods
This commit is contained in:
parent
8a356fe2fd
commit
93a4fd17c7
@ -366,99 +366,92 @@ export class Database {
|
||||
});
|
||||
}
|
||||
|
||||
public async getObject(storeName: string, key: string): Promise<any | null> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
const getRequest = store.get(key);
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
public async getProcess(processId: string): Promise<any | null> {
|
||||
return this.getObject('processes', processId);
|
||||
}
|
||||
|
||||
public async getAllProcesses(): Promise<Record<string, any>> {
|
||||
return this.dumpStore('processes');
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - BLOBS
|
||||
// ============================================
|
||||
|
||||
public async saveBlob(hash: string, data: Blob): Promise<void> {
|
||||
await this.addObject({
|
||||
storeName: 'data',
|
||||
object: data,
|
||||
key: hash,
|
||||
});
|
||||
return result ?? null; // Convert undefined to null
|
||||
}
|
||||
|
||||
public async dumpStore(storeName: string): Promise<Record<string, any>> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
public async getBlob(hash: string): Promise<Blob | null> {
|
||||
return this.getObject('data', hash);
|
||||
}
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
const result: Record<string, any> = {};
|
||||
const cursor = store.openCursor();
|
||||
// ============================================
|
||||
// BUSINESS METHODS - DIFFS
|
||||
// ============================================
|
||||
|
||||
cursor.onsuccess = (event) => {
|
||||
const request = event.target as IDBRequest<IDBCursorWithValue | null>;
|
||||
const cursor = request.result;
|
||||
if (cursor) {
|
||||
result[cursor.key as string] = cursor.value;
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
};
|
||||
public async saveDiffs(diffs: any[]): Promise<void> {
|
||||
if (diffs.length === 0) return;
|
||||
|
||||
cursor.onerror = () => {
|
||||
reject(cursor.error);
|
||||
};
|
||||
for (const diff of diffs) {
|
||||
await this.addObject({
|
||||
storeName: 'diffs',
|
||||
object: diff,
|
||||
key: null,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching data from IndexedDB:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteObject(storeName: string, key: string): Promise<void> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const getRequest = store.delete(key);
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
public async getDiff(hash: string): Promise<any | null> {
|
||||
return this.getObject('diffs', hash);
|
||||
}
|
||||
|
||||
public async getAllDiffs(): Promise<Record<string, any>> {
|
||||
return this.dumpStore('diffs');
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - SECRETS
|
||||
// ============================================
|
||||
|
||||
public async getSharedSecret(address: string): Promise<string | null> {
|
||||
return this.getObject('shared_secrets', address);
|
||||
}
|
||||
|
||||
public async saveSecretsBatch(unconfirmedSecrets: any[], sharedSecrets: { key: string; value: any }[]): Promise<void> {
|
||||
if (unconfirmedSecrets && unconfirmedSecrets.length > 0) {
|
||||
for (const secret of unconfirmedSecrets) {
|
||||
await this.addObject({
|
||||
storeName: 'unconfirmed_secrets',
|
||||
object: secret,
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (sharedSecrets && sharedSecrets.length > 0) {
|
||||
for (const { key, value } of sharedSecrets) {
|
||||
await this.addObject({
|
||||
storeName: 'shared_secrets',
|
||||
object: value,
|
||||
key: key,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async clearStore(storeName: string): Promise<void> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const clearRequest = store.clear();
|
||||
clearRequest.onsuccess = () => resolve(clearRequest.result);
|
||||
clearRequest.onerror = () => reject(clearRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
public async getAllSecrets(): Promise<{ shared_secrets: Record<string, any>; unconfirmed_secrets: any[] }> {
|
||||
const sharedSecrets = await this.dumpStore('shared_secrets');
|
||||
const unconfirmedSecrets = await this.dumpStore('unconfirmed_secrets');
|
||||
|
||||
// Request a store by index
|
||||
public async requestStoreByIndex(storeName: string, indexName: string, request: string): Promise<any[]> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
const index = store.index(indexName);
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
const getAllRequest = index.getAll(request);
|
||||
getAllRequest.onsuccess = () => {
|
||||
const allItems = getAllRequest.result;
|
||||
const filtered = allItems.filter((item) => item.state_id === request);
|
||||
resolve(filtered);
|
||||
};
|
||||
getAllRequest.onerror = () => reject(getAllRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
return {
|
||||
shared_secrets: sharedSecrets,
|
||||
unconfirmed_secrets: Object.values(unconfirmedSecrets),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user