From 93a4fd17c7528baea2ff82c3f4eed9d02acd9296 Mon Sep 17 00:00:00 2001 From: omaroughriss Date: Thu, 27 Nov 2025 16:50:03 +0100 Subject: [PATCH] Add business methods --- src/services/database.service.ts | 153 +++++++++++++++---------------- 1 file changed, 73 insertions(+), 80 deletions(-) diff --git a/src/services/database.service.ts b/src/services/database.service.ts index 36b796b..e28e653 100755 --- a/src/services/database.service.ts +++ b/src/services/database.service.ts @@ -366,99 +366,92 @@ export class Database { }); } - public async getObject(storeName: string, key: string): Promise { - 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 { + return this.getObject('processes', processId); + } + + public async getAllProcesses(): Promise> { + return this.dumpStore('processes'); + } + + // ============================================ + // BUSINESS METHODS - BLOBS + // ============================================ + + public async saveBlob(hash: string, data: Blob): Promise { + await this.addObject({ + storeName: 'data', + object: data, + key: hash, }); - return result ?? null; // Convert undefined to null } - public async dumpStore(storeName: string): Promise> { - const db = await this.getDb(); - const tx = db.transaction(storeName, 'readonly'); - const store = tx.objectStore(storeName); + public async getBlob(hash: string): Promise { + return this.getObject('data', hash); + } - try { - return new Promise((resolve, reject) => { - const result: Record = {}; - const cursor = store.openCursor(); + // ============================================ + // BUSINESS METHODS - DIFFS + // ============================================ - cursor.onsuccess = (event) => { - const request = event.target as IDBRequest; - const cursor = request.result; - if (cursor) { - result[cursor.key as string] = cursor.value; - cursor.continue(); - } else { - resolve(result); - } - }; + public async saveDiffs(diffs: any[]): Promise { + 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 { - 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 { + return this.getObject('diffs', hash); + } + + public async getAllDiffs(): Promise> { + return this.dumpStore('diffs'); + } + + // ============================================ + // BUSINESS METHODS - SECRETS + // ============================================ + + public async getSharedSecret(address: string): Promise { + return this.getObject('shared_secrets', address); + } + + public async saveSecretsBatch(unconfirmedSecrets: any[], sharedSecrets: { key: string; value: any }[]): Promise { + 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 { - 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; - } - } - - // Request a store by index - public async requestStoreByIndex(storeName: string, indexName: string, request: string): Promise { - 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; - } + public async getAllSecrets(): Promise<{ shared_secrets: Record; unconfirmed_secrets: any[] }> { + const sharedSecrets = await this.dumpStore('shared_secrets'); + const unconfirmedSecrets = await this.dumpStore('unconfirmed_secrets'); + + return { + shared_secrets: sharedSecrets, + unconfirmed_secrets: Object.values(unconfirmedSecrets), + }; } }