@pixagram/lacerta-db 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +26 -17
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -287,34 +287,41 @@ class IndexedDBUtility {
287
287
  });
288
288
  }
289
289
 
290
- // FIXED: Improved transaction retry logic with idempotency check
290
+ // Improved transaction handling with better retry logic
291
291
  static async performTransaction(db, storeNames, mode, callback, retries = 3, operationId = null) {
292
292
  let lastError = null;
293
-
293
+
294
294
  for (let attempt = 0; attempt <= retries; attempt++) {
295
295
  try {
296
- if (!db) {
297
- throw new Error('Database connection is not available.');
296
+ if (!db || db.readyState !== 'done') {
297
+ throw new Error('Database connection is not available or ready.');
298
298
  }
299
-
299
+
300
300
  const tx = db.transaction(Array.isArray(storeNames) ? storeNames : [storeNames], mode);
301
- const stores = Array.isArray(storeNames)
302
- ? storeNames.map(name => tx.objectStore(name))
301
+ const stores = Array.isArray(storeNames)
302
+ ? storeNames.map(name => tx.objectStore(name))
303
303
  : [tx.objectStore(storeNames)];
304
-
305
- // Track if operation has been applied
306
- const operationKey = operationId || `${Date.now()}_${Math.random()}`;
307
-
308
- const result = await callback(...stores, operationKey);
304
+
305
+ // Add transaction timeout
306
+ const timeoutId = setTimeout(() => {
307
+ tx.abort();
308
+ }, 30000); // 30 second timeout
309
+
310
+ const result = await callback(...stores, operationId);
309
311
 
310
312
  return new Promise((resolve, reject) => {
311
- tx.oncomplete = () => resolve(result);
313
+ tx.oncomplete = () => {
314
+ clearTimeout(timeoutId);
315
+ resolve(result);
316
+ };
312
317
  tx.onerror = () => {
318
+ clearTimeout(timeoutId);
313
319
  lastError = new Error(`Transaction failed: ${tx.error ? tx.error.message : 'unknown error'}`);
314
320
  reject(lastError);
315
321
  };
316
322
  tx.onabort = () => {
317
- lastError = new Error(`Transaction aborted: ${tx.error ? tx.error.message : 'unknown reason'}`);
323
+ clearTimeout(timeoutId);
324
+ lastError = new Error(`Transaction aborted: ${tx.error ? tx.error.message : 'timeout or abort'}`);
318
325
  reject(lastError);
319
326
  };
320
327
  });
@@ -322,12 +329,13 @@ class IndexedDBUtility {
322
329
  lastError = error;
323
330
  if (attempt < retries) {
324
331
  console.warn(`Transaction failed, retrying... (${retries - attempt} attempts left): ${error.message}`);
325
- // Exponential backoff
326
- await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 100));
332
+ // Exponential backoff with jitter
333
+ const delay = Math.pow(2, attempt) * 100 + Math.random() * 100;
334
+ await new Promise(resolve => setTimeout(resolve, delay));
327
335
  }
328
336
  }
329
337
  }
330
-
338
+
331
339
  throw new Error(`Transaction ultimately failed after ${retries + 1} attempts: ${lastError.message}`);
332
340
  }
333
341
 
@@ -2075,3 +2083,4 @@ export class Collection {
2075
2083
  return `[Collection: ${this.name} | Database: ${this.database.name} | Size: ${this.sizeKB.toFixed(2)}KB | Documents: ${this.length}]`;
2076
2084
  }
2077
2085
  }
2086
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixagram/lacerta-db",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Lacerta-DB is a Javascript IndexedDB Database for Web Browsers. Simple, Fast, Secure.",
5
5
  "devDependencies": {
6
6
  "@babel/core": "^7.23.6",