Fix broken db operation for raw bytes

This commit is contained in:
Sosthene 2025-09-03 15:08:54 +02:00
parent c3455ac888
commit a2ae855c10

View File

@ -73,10 +73,14 @@ export default class Database {
* Get a single object from a store * Get a single object from a store
* O(log n) operation - only reads specific key * O(log n) operation - only reads specific key
*/ */
public async getObject(storeName: string, key: string): Promise<any | null> { public async getObject(storeName: string, key: string, isBuffer: boolean = false): Promise<any | null> {
try { try {
const fullKey = this.getKey(storeName, key); const fullKey = this.getKey(storeName, key);
return await this.db.get(fullKey); if (isBuffer) {
return await this.db.get(fullKey, { valueEncoding: 'buffer' });
} else {
return await this.db.get(fullKey);
}
} catch (error) { } catch (error) {
if ((error as any).code === 'LEVEL_NOT_FOUND') { if ((error as any).code === 'LEVEL_NOT_FOUND') {
return null; return null;
@ -89,12 +93,16 @@ export default class Database {
* Add or update an object in a store * Add or update an object in a store
* O(log n) operation - only writes specific key-value pair * O(log n) operation - only writes specific key-value pair
*/ */
public async addObject(operation: DatabaseObject): Promise<void> { public async addObject(operation: DatabaseObject, isBuffer: boolean = false): Promise<void> {
const { storeName, object, key } = operation; const { storeName, object, key } = operation;
if (key) { if (key) {
const fullKey = this.getKey(storeName, key); const fullKey = this.getKey(storeName, key);
await this.db.put(fullKey, object); if (isBuffer) {
await this.db.put(fullKey, object, { valueEncoding: 'buffer' });
} else {
await this.db.put(fullKey, object);
}
} else { } else {
// Auto-generate key if none provided // Auto-generate key if none provided
const autoKey = Date.now().toString() + Math.random().toString(36).substr(2, 9); const autoKey = Date.now().toString() + Math.random().toString(36).substr(2, 9);