@vue-skuilder/db 0.1.14-2 → 0.1.15

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.
@@ -299,6 +299,33 @@ var init_updateQueue = __esm({
299
299
  // Database for read operations
300
300
  writeDB;
301
301
  // Database for write operations (local-first)
302
+ /**
303
+ * Queues an update for a document and applies it with conflict resolution.
304
+ *
305
+ * @param id - Document ID to update
306
+ * @param update - Partial object or function that transforms the document
307
+ * @returns Promise resolving to the updated document
308
+ *
309
+ * @throws {PouchError} with status 404 if document doesn't exist
310
+ *
311
+ * @remarks
312
+ * **Error Handling Pattern:**
313
+ * - This method does NOT create documents if they don't exist
314
+ * - Callers are responsible for handling 404 errors and creating documents
315
+ * - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * try {
320
+ * await updateQueue.update(docId, (doc) => ({ ...doc, field: newValue }));
321
+ * } catch (e) {
322
+ * if ((e as PouchError).status === 404) {
323
+ * // Create the document with initial values
324
+ * await db.put({ _id: docId, field: newValue, ...initialFields });
325
+ * }
326
+ * }
327
+ * ```
328
+ */
302
329
  update(id, update) {
303
330
  logger.debug(`Update requested on doc: ${id}`);
304
331
  if (this.pendingUpdates[id]) {
@@ -331,7 +358,6 @@ var init_updateQueue = __esm({
331
358
  for (let i = 0; i < MAX_RETRIES; i++) {
332
359
  try {
333
360
  const doc = await this.readDB.get(id);
334
- logger.debug(`Retrieved doc: ${id}`);
335
361
  let updatedDoc = { ...doc };
336
362
  const updatesToApply = [...this.pendingUpdates[id]];
337
363
  for (const update of updatesToApply) {
@@ -345,7 +371,6 @@ var init_updateQueue = __esm({
345
371
  }
346
372
  }
347
373
  await this.writeDB.put(updatedDoc);
348
- logger.debug(`Put doc: ${id}`);
349
374
  this.pendingUpdates[id].splice(0, updatesToApply.length);
350
375
  if (this.pendingUpdates[id].length === 0) {
351
376
  this.inprogressUpdates[id] = false;
@@ -360,6 +385,7 @@ var init_updateQueue = __esm({
360
385
  await new Promise((res) => setTimeout(res, 50 * Math.random()));
361
386
  } else if (e.name === "not_found" && i === 0) {
362
387
  logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
388
+ delete this.inprogressUpdates[id];
363
389
  throw e;
364
390
  } else {
365
391
  delete this.inprogressUpdates[id];
@@ -2391,12 +2417,22 @@ Currently logged-in as ${this._username}.`
2391
2417
  }
2392
2418
  /**
2393
2419
  * Logs a record of the user's interaction with the card and returns the card's
2394
- * up-to-date history
2420
+ * up-to-date history.
2421
+ *
2422
+ * **Automatic Initialization:**
2423
+ * If this is the user's first interaction with the card (CardHistory doesn't exist),
2424
+ * this method automatically creates the CardHistory document with initial values
2425
+ * (lapses: 0, streak: 0, bestInterval: 0).
2426
+ *
2427
+ * **Error Handling:**
2428
+ * - Handles 404 errors by creating initial CardHistory document
2429
+ * - Re-throws all other errors from UpdateQueue
2395
2430
  *
2396
2431
  * // [ ] #db-refactor extract to a smaller scope - eg, UserStudySession
2397
2432
  *
2398
- * @param record the recent recorded interaction between user and card
2433
+ * @param record - The recent recorded interaction between user and card
2399
2434
  * @returns The updated state of the card's CardHistory data
2435
+ * @throws Error if document creation fails or non-404 database error occurs
2400
2436
  */
2401
2437
  async putCardRecord(record) {
2402
2438
  const cardHistoryID = getCardHistoryID(record.courseID, record.cardID);