@bobfrankston/mailx-store 0.1.34 → 0.1.36

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 (3) hide show
  1. package/db.d.ts +21 -0
  2. package/db.js +52 -3
  3. package/package.json +1 -1
package/db.d.ts CHANGED
@@ -371,6 +371,27 @@ export declare class MailxDB {
371
371
  * here but NOT on the server gets its membership dropped (which
372
372
  * may then GC the messages row if it has no other folders). */
373
373
  getUidsForFolder(_accountId: string, folderId: number): number[];
374
+ /** List recent rows in a folder for a Sent-sweep / reconciliation pass.
375
+ * Returns only the columns the sweeper needs (uid, message_id, date,
376
+ * subject, size, has_attachments, preview, body_path) for every row in
377
+ * the given folder cached after `sinceMs`. Empty-message-id rows are
378
+ * excluded because the sweep keys on Message-ID. */
379
+ getRecentMessagesByCachedAt(accountId: string, folderId: number, sinceMs: number): {
380
+ uid: number;
381
+ message_id: string;
382
+ date: number;
383
+ subject: string;
384
+ size: number;
385
+ has_attachments: number;
386
+ preview: string;
387
+ body_path: string;
388
+ }[];
389
+ /** Rebind a local row to a different server UID without re-upserting.
390
+ * Used by the Sent-sweep to repair optimistic-insert mispredictions
391
+ * (local row was inserted at the predicted UIDNEXT but the server
392
+ * appended at a different real UID). Idempotent: if no row matches
393
+ * (already rebound by an earlier sync), it's a no-op. */
394
+ updateMessageUid(accountId: string, folderId: number, oldUid: number, newUid: number): void;
374
395
  /** Delete a message by account + UID. Reason is propagated into the
375
396
  * audit_log so the DB carries an authoritative trail of every removal. */
376
397
  deleteMessage(accountId: string, uid: number, reason?: string, source?: string): void;
package/db.js CHANGED
@@ -1977,6 +1977,41 @@ export class MailxDB {
1977
1977
  const rows = this.db.prepare("SELECT uid FROM message_folders WHERE folder_id = ?").all(folderId);
1978
1978
  return rows.map(r => r.uid);
1979
1979
  }
1980
+ /** List recent rows in a folder for a Sent-sweep / reconciliation pass.
1981
+ * Returns only the columns the sweeper needs (uid, message_id, date,
1982
+ * subject, size, has_attachments, preview, body_path) for every row in
1983
+ * the given folder cached after `sinceMs`. Empty-message-id rows are
1984
+ * excluded because the sweep keys on Message-ID. */
1985
+ getRecentMessagesByCachedAt(accountId, folderId, sinceMs) {
1986
+ return this.db.prepare(`SELECT uid, message_id, date, subject, size, has_attachments, preview, body_path
1987
+ FROM messages
1988
+ WHERE account_id = ? AND folder_id = ? AND cached_at >= ?
1989
+ AND message_id IS NOT NULL AND message_id <> ''`).all(accountId, folderId, sinceMs);
1990
+ }
1991
+ /** Rebind a local row to a different server UID without re-upserting.
1992
+ * Used by the Sent-sweep to repair optimistic-insert mispredictions
1993
+ * (local row was inserted at the predicted UIDNEXT but the server
1994
+ * appended at a different real UID). Idempotent: if no row matches
1995
+ * (already rebound by an earlier sync), it's a no-op. */
1996
+ updateMessageUid(accountId, folderId, oldUid, newUid) {
1997
+ if (oldUid === newUid)
1998
+ return;
1999
+ try {
2000
+ this.db.prepare(`UPDATE messages SET uid = ?, cached_at = ?
2001
+ WHERE account_id = ? AND folder_id = ? AND uid = ?`).run(newUid, Date.now(), accountId, folderId, oldUid);
2002
+ }
2003
+ catch (e) {
2004
+ // UNIQUE constraint can fire when both old and new UIDs already
2005
+ // exist in the row set (newer sync re-fetched the real UID).
2006
+ // Drop the stale predicted row in that case.
2007
+ if (/UNIQUE/i.test(e?.message || "")) {
2008
+ this.db.prepare(`DELETE FROM messages WHERE account_id = ? AND folder_id = ? AND uid = ?`).run(accountId, folderId, oldUid);
2009
+ }
2010
+ else {
2011
+ throw e;
2012
+ }
2013
+ }
2014
+ }
1980
2015
  /** Delete a message by account + UID. Reason is propagated into the
1981
2016
  * audit_log so the DB carries an authoritative trail of every removal. */
1982
2017
  deleteMessage(accountId, uid, reason, source) {
@@ -2733,9 +2768,23 @@ export class MailxDB {
2733
2768
  ftsQuery += `(${alts}) `;
2734
2769
  }
2735
2770
  else {
2736
- term = ftsClean(term);
2737
- if (term)
2738
- ftsQuery += `${term}* `;
2771
+ // Match the FTS5 unicode61 tokenizer: split on any
2772
+ // non-(letter|number|underscore) so the user's term
2773
+ // shapes the same way the index did. Without this,
2774
+ // typing "192." built "192.*" and matched nothing —
2775
+ // FTS5 had indexed "192.55.226" as the three tokens
2776
+ // 192/55/226 (Bob 2026-05-22 "type 192. it no longer
2777
+ // matches"). AND the sub-tokens (FTS5 implicit AND
2778
+ // between space-separated terms); only the LAST gets
2779
+ // a `*` so incremental typing keeps narrowing.
2780
+ const sub = ftsClean(term).split(/[^\p{L}\p{N}_]+/u).filter(Boolean);
2781
+ if (sub.length === 1) {
2782
+ ftsQuery += `${sub[0]}* `;
2783
+ }
2784
+ else if (sub.length > 1) {
2785
+ const last = sub.pop();
2786
+ ftsQuery += `${sub.join(" ")} ${last}* `;
2787
+ }
2739
2788
  }
2740
2789
  }
2741
2790
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-store",
3
- "version": "0.1.34",
3
+ "version": "0.1.36",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",