@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.
@@ -322,6 +322,33 @@ var init_updateQueue = __esm({
322
322
  // Database for read operations
323
323
  writeDB;
324
324
  // Database for write operations (local-first)
325
+ /**
326
+ * Queues an update for a document and applies it with conflict resolution.
327
+ *
328
+ * @param id - Document ID to update
329
+ * @param update - Partial object or function that transforms the document
330
+ * @returns Promise resolving to the updated document
331
+ *
332
+ * @throws {PouchError} with status 404 if document doesn't exist
333
+ *
334
+ * @remarks
335
+ * **Error Handling Pattern:**
336
+ * - This method does NOT create documents if they don't exist
337
+ * - Callers are responsible for handling 404 errors and creating documents
338
+ * - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * try {
343
+ * await updateQueue.update(docId, (doc) => ({ ...doc, field: newValue }));
344
+ * } catch (e) {
345
+ * if ((e as PouchError).status === 404) {
346
+ * // Create the document with initial values
347
+ * await db.put({ _id: docId, field: newValue, ...initialFields });
348
+ * }
349
+ * }
350
+ * ```
351
+ */
325
352
  update(id, update) {
326
353
  logger.debug(`Update requested on doc: ${id}`);
327
354
  if (this.pendingUpdates[id]) {
@@ -354,7 +381,6 @@ var init_updateQueue = __esm({
354
381
  for (let i = 0; i < MAX_RETRIES; i++) {
355
382
  try {
356
383
  const doc = await this.readDB.get(id);
357
- logger.debug(`Retrieved doc: ${id}`);
358
384
  let updatedDoc = { ...doc };
359
385
  const updatesToApply = [...this.pendingUpdates[id]];
360
386
  for (const update of updatesToApply) {
@@ -368,7 +394,6 @@ var init_updateQueue = __esm({
368
394
  }
369
395
  }
370
396
  await this.writeDB.put(updatedDoc);
371
- logger.debug(`Put doc: ${id}`);
372
397
  this.pendingUpdates[id].splice(0, updatesToApply.length);
373
398
  if (this.pendingUpdates[id].length === 0) {
374
399
  this.inprogressUpdates[id] = false;
@@ -383,6 +408,7 @@ var init_updateQueue = __esm({
383
408
  await new Promise((res) => setTimeout(res, 50 * Math.random()));
384
409
  } else if (e.name === "not_found" && i === 0) {
385
410
  logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
411
+ delete this.inprogressUpdates[id];
386
412
  throw e;
387
413
  } else {
388
414
  delete this.inprogressUpdates[id];
@@ -2411,12 +2437,22 @@ Currently logged-in as ${this._username}.`
2411
2437
  }
2412
2438
  /**
2413
2439
  * Logs a record of the user's interaction with the card and returns the card's
2414
- * up-to-date history
2440
+ * up-to-date history.
2441
+ *
2442
+ * **Automatic Initialization:**
2443
+ * If this is the user's first interaction with the card (CardHistory doesn't exist),
2444
+ * this method automatically creates the CardHistory document with initial values
2445
+ * (lapses: 0, streak: 0, bestInterval: 0).
2446
+ *
2447
+ * **Error Handling:**
2448
+ * - Handles 404 errors by creating initial CardHistory document
2449
+ * - Re-throws all other errors from UpdateQueue
2415
2450
  *
2416
2451
  * // [ ] #db-refactor extract to a smaller scope - eg, UserStudySession
2417
2452
  *
2418
- * @param record the recent recorded interaction between user and card
2453
+ * @param record - The recent recorded interaction between user and card
2419
2454
  * @returns The updated state of the card's CardHistory data
2455
+ * @throws Error if document creation fails or non-404 database error occurs
2420
2456
  */
2421
2457
  async putCardRecord(record) {
2422
2458
  const cardHistoryID = getCardHistoryID(record.courseID, record.cardID);