Add business methods

This commit is contained in:
omaroughriss 2025-11-27 16:50:03 +01:00
parent 8a356fe2fd
commit 93a4fd17c7

View File

@ -366,99 +366,92 @@ export class Database {
}); });
} }
public async getObject(storeName: string, key: string): Promise<any | null> { public async getProcess(processId: string): Promise<any | null> {
const db = await this.getDb(); return this.getObject('processes', processId);
const tx = db.transaction(storeName, 'readonly'); }
const store = tx.objectStore(storeName);
const result = await new Promise((resolve, reject) => { public async getAllProcesses(): Promise<Record<string, any>> {
const getRequest = store.get(key); return this.dumpStore('processes');
getRequest.onsuccess = () => resolve(getRequest.result); }
getRequest.onerror = () => reject(getRequest.error);
// ============================================
// 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>> { public async getBlob(hash: string): Promise<Blob | null> {
const db = await this.getDb(); return this.getObject('data', hash);
const tx = db.transaction(storeName, 'readonly'); }
const store = tx.objectStore(storeName);
try { // ============================================
return new Promise((resolve, reject) => { // BUSINESS METHODS - DIFFS
const result: Record<string, any> = {}; // ============================================
const cursor = store.openCursor();
cursor.onsuccess = (event) => { public async saveDiffs(diffs: any[]): Promise<void> {
const request = event.target as IDBRequest<IDBCursorWithValue | null>; if (diffs.length === 0) return;
const cursor = request.result;
if (cursor) {
result[cursor.key as string] = cursor.value;
cursor.continue();
} else {
resolve(result);
}
};
cursor.onerror = () => { for (const diff of diffs) {
reject(cursor.error); 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> { public async getDiff(hash: string): Promise<any | null> {
const db = await this.getDb(); return this.getObject('diffs', hash);
const tx = db.transaction(storeName, 'readwrite'); }
const store = tx.objectStore(storeName);
try { public async getAllDiffs(): Promise<Record<string, any>> {
await new Promise((resolve, reject) => { return this.dumpStore('diffs');
const getRequest = store.delete(key); }
getRequest.onsuccess = () => resolve(getRequest.result);
getRequest.onerror = () => reject(getRequest.error); // ============================================
}); // BUSINESS METHODS - SECRETS
} catch (e) { // ============================================
throw e;
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> { public async getAllSecrets(): Promise<{ shared_secrets: Record<string, any>; unconfirmed_secrets: any[] }> {
const db = await this.getDb(); const sharedSecrets = await this.dumpStore('shared_secrets');
const tx = db.transaction(storeName, 'readwrite'); const unconfirmedSecrets = await this.dumpStore('unconfirmed_secrets');
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 return {
public async requestStoreByIndex(storeName: string, indexName: string, request: string): Promise<any[]> { shared_secrets: sharedSecrets,
const db = await this.getDb(); unconfirmed_secrets: Object.values(unconfirmedSecrets),
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;
}
} }
} }