@lunora/db 1.0.0-alpha.59 → 1.0.0-alpha.60

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.
@@ -1,5 +1,5 @@
1
- export { type b as CheckpointRegistry, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, i as createCheckpointRegistry, m as lunoraCollectionOptions } from "../packem_shared/collection-options.d-CspA2N8I.mjs";
2
- export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/index.d-BepAls8u.mjs";
1
+ export { type b as CheckpointRegistry, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, i as createCheckpointRegistry, m as lunoraCollectionOptions } from "../packem_shared/collection-options.d-DWNFnQ70.mjs";
2
+ export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/index.d-DreeESSV.mjs";
3
3
  import '@lunora/client';
4
4
  import '@tanstack/db';
5
5
  import '@tanstack/offline-transactions';
@@ -1,5 +1,5 @@
1
- export { type b as CheckpointRegistry, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, i as createCheckpointRegistry, m as lunoraCollectionOptions } from "../packem_shared/collection-options.d-CspA2N8I.js";
2
- export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/index.d-B5epwy7P.js";
1
+ export { type b as CheckpointRegistry, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, i as createCheckpointRegistry, m as lunoraCollectionOptions } from "../packem_shared/collection-options.d-DWNFnQ70.js";
2
+ export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/index.d-5tpy1rI6.js";
3
3
  import '@lunora/client';
4
4
  import '@tanstack/db';
5
5
  import '@tanstack/offline-transactions';
@@ -1 +1 @@
1
- import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-CX7idSTz.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-DtTv50Sr.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
1
+ import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-DEhlJ5je.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-DBxlxj4t.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Collection } from '@tanstack/db';
2
- import { R as Row } from "./packem_shared/collection-options.d-CspA2N8I.mjs";
3
- export { C as CHECKPOINT_FALLBACK_MS, type a as CheckpointFallbackEvent, type b as CheckpointRegistry, type c as CheckpointRegistryOptions, type d as CheckpointRegistryStats, type e as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type g as OutboxExecutor, type h as OutboxMutationMetadata, type S as SyncWriter, i as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, l as getShardCheckpoints, m as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-CspA2N8I.mjs";
4
- export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/index.d-BepAls8u.mjs";
5
- export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "./packem_shared/index.d-Ye1uUYB0.mjs";
2
+ import { R as Row } from "./packem_shared/collection-options.d-DWNFnQ70.mjs";
3
+ export { C as CHECKPOINT_FALLBACK_MS, type a as CheckpointFallbackEvent, type b as CheckpointRegistry, type c as CheckpointRegistryOptions, type d as CheckpointRegistryStats, type e as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type g as OutboxExecutor, type h as OutboxMutationMetadata, type S as SyncWriter, i as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, l as getShardCheckpoints, m as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-DWNFnQ70.mjs";
4
+ export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/index.d-DreeESSV.mjs";
5
+ export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "./packem_shared/index.d-DgB3nvmL.mjs";
6
6
  import '@lunora/client';
7
7
  import '@tanstack/offline-transactions';
8
8
  /** One row to insert. `_id` may be pre-minted client-side so the optimistic row keys match the persisted one. */
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Collection } from '@tanstack/db';
2
- import { R as Row } from "./packem_shared/collection-options.d-CspA2N8I.js";
3
- export { C as CHECKPOINT_FALLBACK_MS, type a as CheckpointFallbackEvent, type b as CheckpointRegistry, type c as CheckpointRegistryOptions, type d as CheckpointRegistryStats, type e as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type g as OutboxExecutor, type h as OutboxMutationMetadata, type S as SyncWriter, i as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, l as getShardCheckpoints, m as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-CspA2N8I.js";
4
- export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/index.d-B5epwy7P.js";
5
- export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "./packem_shared/index.d-Cq7dDe62.js";
2
+ import { R as Row } from "./packem_shared/collection-options.d-DWNFnQ70.js";
3
+ export { C as CHECKPOINT_FALLBACK_MS, type a as CheckpointFallbackEvent, type b as CheckpointRegistry, type c as CheckpointRegistryOptions, type d as CheckpointRegistryStats, type e as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type f as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type g as OutboxExecutor, type h as OutboxMutationMetadata, type S as SyncWriter, i as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, l as getShardCheckpoints, m as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-DWNFnQ70.js";
4
+ export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/index.d-5tpy1rI6.js";
5
+ export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "./packem_shared/index.d-C4dXRxV5.js";
6
6
  import '@lunora/client';
7
7
  import '@tanstack/offline-transactions';
8
8
  /** One row to insert. `_id` may be pre-minted client-side so the optimistic row keys match the persisted one. */
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{applyPlanToCollections as r,applyPlanToDb as i}from"./packem_shared/applyPlanToCollections-C_eyqNF9.mjs";import{CHECKPOINT_FALLBACK_MS as a,createCheckpointRegistry as p,getShardCheckpoints as c,lunoraCollectionOptions as l,releaseShardCheckpoints as s,shardCheckpointStats as C}from"./packem_shared/CHECKPOINT_FALLBACK_MS-CX7idSTz.mjs";import{defineCollections as T}from"./packem_shared/defineCollections-DtTv50Sr.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as u,bindMutators as x,defineMutator as A,initMutators as M}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-Ca-snw_0.mjs";import{OUTBOX_MUTATION_FN_NAME as E,createExecutorOutboxSink as _,createOptimisticOnlineDetector as h,makeDiffEmit as N,runOutboxMutation as S,toMap as d}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,u as DIRECT_TRANSACTION_METADATA_KEY,E as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,x as bindMutators,p as createCheckpointRegistry,_ as createExecutorOutboxSink,h as createOptimisticOnlineDetector,T as defineCollections,A as defineMutator,c as getShardCheckpoints,M as initMutators,l as lunoraCollectionOptions,N as makeDiffEmit,s as releaseShardCheckpoints,S as runOutboxMutation,C as shardCheckpointStats,d as toMap};
1
+ import{applyPlanToCollections as r,applyPlanToDb as i}from"./packem_shared/applyPlanToCollections-C_eyqNF9.mjs";import{CHECKPOINT_FALLBACK_MS as a,createCheckpointRegistry as p,getShardCheckpoints as c,lunoraCollectionOptions as l,releaseShardCheckpoints as s,shardCheckpointStats as C}from"./packem_shared/CHECKPOINT_FALLBACK_MS-DEhlJ5je.mjs";import{defineCollections as T}from"./packem_shared/defineCollections-DBxlxj4t.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as u,bindMutators as x,defineMutator as A,initMutators as M}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-D2l1cFzz.mjs";import{OUTBOX_MUTATION_FN_NAME as E,createExecutorOutboxSink as _,createOptimisticOnlineDetector as h,makeDiffEmit as N,runOutboxMutation as S,toMap as d}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,u as DIRECT_TRANSACTION_METADATA_KEY,E as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,x as bindMutators,p as createCheckpointRegistry,_ as createExecutorOutboxSink,h as createOptimisticOnlineDetector,T as defineCollections,A as defineMutator,c as getShardCheckpoints,M as initMutators,l as lunoraCollectionOptions,N as makeDiffEmit,s as releaseShardCheckpoints,S as runOutboxMutation,C as shardCheckpointStats,d as toMap};
@@ -1,5 +1,5 @@
1
- export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "../packem_shared/index.d-Ye1uUYB0.mjs";
1
+ export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "../packem_shared/index.d-DgB3nvmL.mjs";
2
2
  import '@lunora/client';
3
3
  import '@tanstack/db';
4
- import "../packem_shared/collection-options.d-CspA2N8I.mjs";
4
+ import "../packem_shared/collection-options.d-DWNFnQ70.mjs";
5
5
  import '@tanstack/offline-transactions';
@@ -1,5 +1,5 @@
1
- export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "../packem_shared/index.d-Cq7dDe62.js";
1
+ export { type B as BindMutatorsContext, type a as BoundMutatorApi, type b as BoundMutators, type C as ClientMutatorContext, type c as ClientMutatorDef, type d as CollectionMap, type e as MutatorRejectedEvent, f as bindMutators, g as defineMutator, i as initMutators } from "../packem_shared/index.d-C4dXRxV5.js";
2
2
  import '@lunora/client';
3
3
  import '@tanstack/db';
4
- import "../packem_shared/collection-options.d-CspA2N8I.js";
4
+ import "../packem_shared/collection-options.d-DWNFnQ70.js";
5
5
  import '@tanstack/offline-transactions';
@@ -1 +1 @@
1
- import{bindMutators as r,defineMutator as i,initMutators as a}from"../packem_shared/DIRECT_TRANSACTION_METADATA_KEY-Ca-snw_0.mjs";export{r as bindMutators,i as defineMutator,a as initMutators};
1
+ import{bindMutators as r,defineMutator as i,initMutators as a}from"../packem_shared/DIRECT_TRANSACTION_METADATA_KEY-D2l1cFzz.mjs";export{r as bindMutators,i as defineMutator,a as initMutators};
@@ -0,0 +1 @@
1
+ import{LunoraError as S}from"@lunora/errors";import{BTreeIndex as M}from"@tanstack/db";import{toMap as T,makeDiffEmit as E}from"./OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const I=()=>{let e=Number.NEGATIVE_INFINITY;const r=[];return{advance:t=>{if(!(t<=e)){e=t;for(let s=r.length-1;s>=0;s-=1){const i=r[s];i&&i.threshold<=e&&(i.resolve(),r.splice(s,1))}}},await:t=>t<=e?Promise.resolve():new Promise(s=>{r.push({resolve:s,threshold:t})}),passed:t=>t<=e,reset:()=>{for(const t of r.splice(0))t.resolve();e=Number.NEGATIVE_INFINITY},waiting:()=>r.length}};let b=!1;const K=e=>{b||(b=!0,console.warn(`[@lunora/db] released an optimistic overlay via the ${String(e.waitedMs)}ms checkpoint fallback: the server confirmed ${e.kind} ${String(e.watermark)} but no sync frame ever echoed it. A dropped shape poke or \`settled\` frame is the usual cause — inspect the subscription rather than raising \`fallbackMs\`. (Reported once per process.)`))},u=new WeakMap,w=new WeakMap,C=new WeakSet,N=new WeakMap,F=3e3,R=(e={})=>{const r=e.fallbackMs??F,t=e.onFallback??K,s=I(),i=I();let d=0;const o=new Set,p=(a,c)=>{for(const l of o)l.kind===a&&l.threshold<=c&&(clearTimeout(l.handle),o.delete(l))},y=(a,c)=>{if(r<=0)return;const l=a==="checkpoint"?s:i;if(l.passed(c))return;for(const m of o)if(m.kind===a&&m.threshold>=c)return;const n={handle:setTimeout(()=>{if(o.delete(n),!l.passed(c)){d+=1,l.advance(c);try{t({kind:a,waitedMs:r,watermark:c})}catch{}}},r),kind:a,threshold:c};o.add(n)},f={acknowledge:({checkpoint:a,mutationId:c})=>{a!==void 0&&y("checkpoint",a),c!==void 0&&y("mutationId",c)},awaitCheckpoint:a=>s.await(a),awaitMutationId:a=>i.await(a),dispose:()=>{for(const a of o)clearTimeout(a.handle);o.clear()},resolve:({checkpoint:a,mutationId:c})=>{a!==void 0&&(s.advance(a),p("checkpoint",a)),c!==void 0&&(i.advance(c),p("mutationId",c))},stats:()=>({fallbacks:d,pendingCheckpointWaiters:s.waiting(),pendingMutationWaiters:i.waiting()})};return N.set(f,()=>{f.dispose(),s.reset(),i.reset()}),f},G=e=>{const r=u.get(e);if(r){for(const t of r.values())t.resolve({checkpoint:Number.POSITIVE_INFINITY,mutationId:Number.POSITIVE_INFINITY}),t.dispose();u.delete(e)}},A=e=>{const r=e.currentIdentity();if(u.has(e)&&w.get(e)!==r)for(const t of u.get(e)?.values()??[])N.get(t)?.();w.set(e,r)},_=(e,r,t)=>{A(e);let s=u.get(e);s||(s=new Map,u.set(e,s));const i=r??"",d=s.get(i);if(d)return d;const o=R(t);return s.set(i,o),o},W=e=>{C.add(e)},P=e=>C.has(e),L=e=>{const r=u.get(e),t={};if(r)for(const[s,i]of r)t[s]=i.stats();return t},V=e=>{if(e.list===void 0==(e.shape===void 0))throw new S("INTERNAL","lunoraCollectionOptions: pass exactly one of `list` or `shape`");const r=e.getKey??(n=>n._id),t=()=>{const n=e.checkpoints??_(e.client,e.shape?.shardKey??e.shardKey);return W(n),n},s=t(),i=new Map;let d,o,p,y,f;const a=(n,m)=>{const k=h=>{d?.(T(h,r)),m?.(),e.shape===void 0&&(t().resolve({mutationId:f??e.client.confirmedMutationWatermark(e.shardKey)}),f=void 0)},g=h=>p?.(h),v=h=>{h.mutationId!==void 0&&(f=h.mutationId),t().resolve(h)};return e.shape!==void 0?e.client.subscribeShape({args:n,name:e.shape.name},k,{onCheckpoint:v,onError:g,shardKey:e.shape.shardKey}):e.client.subscribe(e.list,n,k,{onCheckpoint:v,onError:g,shardKey:e.shardKey})},c={autoIndex:"eager",defaultIndexType:M,getKey:r,id:e.id??e.list?.__lunoraRef??`shape:${e.shape?.name??""}`,...e.load==="eager"?{startSync:!0}:{},sync:{sync:n=>(d=E(i,n),p=k=>{n.markReady(),e.onError?.(k)},e.scopeBy===void 0?o=a(e.shape?.args??{},()=>{n.markReady()}):(n.markReady(),y!==void 0&&(o=a(y,void 0))),()=>{d=void 0,p=void 0,o?.(),o=void 0,i.clear()})}};return{checkpoints:s,config:c,scope:n=>{e.scopeBy!==void 0&&(y=n,o?.(),o=void 0,d?.(new Map),n!==void 0&&d!==void 0&&(o=a(n,void 0)))}}};export{F as CHECKPOINT_FALLBACK_MS,R as createCheckpointRegistry,_ as getShardCheckpoints,P as hasCheckpointsAttached,V as lunoraCollectionOptions,W as markCheckpointsAttached,G as releaseShardCheckpoints,L as shardCheckpointStats,A as syncShardCheckpointIdentity};
@@ -0,0 +1 @@
1
+ import{LunoraError as f}from"@lunora/errors";import{createTransaction as k}from"@tanstack/db";import{getShardCheckpoints as v,hasCheckpointsAttached as w,syncShardCheckpointIdentity as C}from"./CHECKPOINT_FALLBACK_MS-DEhlJ5je.mjs";import{runOutboxMutation as I}from"./OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const E="__tanstack_db_direct",T="defineMutator: `serverRef` must be a generated mutator reference (api.mutators.*) or a 'namespace:fn' string",A=t=>{const e=typeof t=="string"?t:t?.__lunoraRef;if(typeof e!="string"||e.length===0)throw new f("INTERNAL",T);return e},S=t=>({__lunoraClientMutator:!0,apply:t.apply,serverRef:A(t.serverRef)}),g=(t,e,l)=>{let c=0,u=t.currentIdentity();const d=()=>{C(t);const r=t.currentIdentity();r!==u&&(c=0,u=r)},y=()=>(d(),c=Math.max(c,t.confirmedMutationWatermark(e.shardKey))+1,c);let m=Promise.resolve();const R=(r,a)=>{const s=m.then(async()=>{for(let i=0;;i+=1){const n=y();try{const{applied:o}=await t.callMutator(r,a,{clientSeq:n,shardKey:e.shardKey});if(o)return n}catch(o){throw d(),c=t.confirmedMutationWatermark(e.shardKey),o}if(i>=32)throw new f("INTERNAL",`lunora: custom mutator "${r}" could not claim a fresh client sequence after ${String(32)} attempts`)}});return m=s.then(()=>{},()=>{}),s},M=()=>{if(e.checkpoints===!1)return;if(e.checkpoints)return e.checkpoints;const r=v(t,e.shardKey);return w(r)?r:void 0},_=(r,a,s,i)=>{const{onWriteRejected:n}=e;n&&r.isPersisted.promise.catch(o=>{const h=o instanceof Error?o:new Error(String(o));try{n({args:i,code:h.code,error:h,mutator:a,serverRef:s})}catch{}})},p={};for(const[r,a]of Object.entries(l))p[r]=s=>{const i=k({autoCommit:!0,metadata:{[E]:!0,serverRef:a.serverRef},mutationFn:async()=>{let n=0;await I(async()=>{n=await R(a.serverRef,s)});const o=M();o&&(o.acknowledge({mutationId:n}),await o.awaitMutationId(n))}});return i.mutate(()=>{a.apply({collections:e.collections},s)}),_(i,r,a.serverRef,s),i};return p},q=()=>({bindMutators:g,defineMutator:S});export{E as DIRECT_TRANSACTION_METADATA_KEY,g as bindMutators,S as defineMutator,q as initMutators};
@@ -1 +1 @@
1
- import{lunoraCollectionOptions as M}from"./CHECKPOINT_FALLBACK_MS-CX7idSTz.mjs";import{createCollection as v,safeRandomUUID as E}from"@tanstack/db";import{startOfflineExecutor as I,NonRetriableError as O}from"@tanstack/offline-transactions";import{createOutboxCarrier as T,createOptimisticOnlineDetector as U,OUTBOX_MUTATION_FN_NAME as C,registerOutboxCarrier as _,runOutboxMutation as x}from"./OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const N=new WeakMap,B=(n,c)=>{let o=N.get(n);o===void 0&&(o=new Map,N.set(n,o));const i=o.get(c)??0;o.set(c,i+1),i===1&&console.warn(`[@lunora/db] table "${c}" is bound by more than one defineCollections call on this client. Each call creates its own live collection and outbox for the table, so the two can drift and derived indexes built from one can silently read stale rows. Bind every table in a single defineCollections call (see the "One source of truth per table" docs section).`)},D=(n,c,o={})=>{const i={},g={},f={},p=Object.entries(c);for(const[t,e]of p){const r=e.insert;B(n,t);const{config:l,scope:m}=M({client:n,getKey:e.getKey,id:t,list:e.list,...e.load===void 0?{}:{load:e.load},onError:e.onError,scopeBy:e.scopeBy,shardKey:e.shardKey});i[t]=v(l),e.scopeBy!==void 0&&(g[t]=m),r&&(f[t]=async({idempotencyKey:s,transaction:a})=>{for(const[h,F]of a.mutations.entries()){const w=F.modified,K=`${s}:${String(h)}`;try{await x(()=>n.mutation(r.mutation,r.toArgs(w),{mutationId:K}))}catch(u){if(u instanceof O&&o.onWriteRejected)try{o.onWriteRejected({code:u.code,collection:t,error:u,row:w})}catch{}throw u}}})}f[C]=async({transaction:t})=>{const e=t.metadata;if(e){if(e.identity!==n.currentIdentity())throw new O("outbox write dropped: identity changed since it was queued");await x(()=>n.mutation({__lunoraRef:e.functionPath},e.args,{mutationId:e.idempotencyKey,shardKey:e.shardKey}))}};const y=T(),d=I({collections:{...i,[C]:y},mutationFns:f,onlineDetector:U(),...o.onLeadershipChange?{onLeadershipChange:o.onLeadershipChange}:{},...o.onStorageFailure?{onStorageFailure:o.onStorageFailure}:{},onUnknownMutationFn:(t,e)=>{try{o.onWriteRejected?.({code:"UNKNOWN_MUTATION_FN",collection:t,error:new Error(`offline write dropped: mutation "${t}" no longer exists (removed or renamed in a deploy?)`),row:e.mutations[0]?.modified})}catch{}}});_(d,y);const b={};for(const[t,e]of p){const r=e.insert,l=i[t];if(!r||!l)continue;const m=d.createOfflineAction({mutationFnName:t,onMutate:({id:s,input:a})=>{l.insert(r.optimistic(a,s))}});b[t]=s=>{const a=E(),h=m({id:a,input:s});return{id:a,transaction:h}}}return{actions:b,collections:i,executor:d,pendingCount:()=>d.getPendingCount(),scope:g}};export{D as defineCollections};
1
+ import{lunoraCollectionOptions as M}from"./CHECKPOINT_FALLBACK_MS-DEhlJ5je.mjs";import{createCollection as v,safeRandomUUID as E}from"@tanstack/db";import{startOfflineExecutor as I,NonRetriableError as O}from"@tanstack/offline-transactions";import{createOutboxCarrier as T,createOptimisticOnlineDetector as U,OUTBOX_MUTATION_FN_NAME as C,registerOutboxCarrier as _,runOutboxMutation as x}from"./OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const N=new WeakMap,B=(n,c)=>{let o=N.get(n);o===void 0&&(o=new Map,N.set(n,o));const i=o.get(c)??0;o.set(c,i+1),i===1&&console.warn(`[@lunora/db] table "${c}" is bound by more than one defineCollections call on this client. Each call creates its own live collection and outbox for the table, so the two can drift and derived indexes built from one can silently read stale rows. Bind every table in a single defineCollections call (see the "One source of truth per table" docs section).`)},D=(n,c,o={})=>{const i={},g={},f={},p=Object.entries(c);for(const[t,e]of p){const r=e.insert;B(n,t);const{config:l,scope:m}=M({client:n,getKey:e.getKey,id:t,list:e.list,...e.load===void 0?{}:{load:e.load},onError:e.onError,scopeBy:e.scopeBy,shardKey:e.shardKey});i[t]=v(l),e.scopeBy!==void 0&&(g[t]=m),r&&(f[t]=async({idempotencyKey:s,transaction:a})=>{for(const[h,F]of a.mutations.entries()){const w=F.modified,K=`${s}:${String(h)}`;try{await x(()=>n.mutation(r.mutation,r.toArgs(w),{mutationId:K}))}catch(u){if(u instanceof O&&o.onWriteRejected)try{o.onWriteRejected({code:u.code,collection:t,error:u,row:w})}catch{}throw u}}})}f[C]=async({transaction:t})=>{const e=t.metadata;if(e){if(e.identity!==n.currentIdentity())throw new O("outbox write dropped: identity changed since it was queued");await x(()=>n.mutation({__lunoraRef:e.functionPath},e.args,{mutationId:e.idempotencyKey,shardKey:e.shardKey}))}};const y=T(),d=I({collections:{...i,[C]:y},mutationFns:f,onlineDetector:U(),...o.onLeadershipChange?{onLeadershipChange:o.onLeadershipChange}:{},...o.onStorageFailure?{onStorageFailure:o.onStorageFailure}:{},onUnknownMutationFn:(t,e)=>{try{o.onWriteRejected?.({code:"UNKNOWN_MUTATION_FN",collection:t,error:new Error(`offline write dropped: mutation "${t}" no longer exists (removed or renamed in a deploy?)`),row:e.mutations[0]?.modified})}catch{}}});_(d,y);const b={};for(const[t,e]of p){const r=e.insert,l=i[t];if(!r||!l)continue;const m=d.createOfflineAction({mutationFnName:t,onMutate:({id:s,input:a})=>{l.insert(r.optimistic(a,s))}});b[t]=s=>{const a=E(),h=m({id:a,input:s});return{id:a,transaction:h}}}return{actions:b,collections:i,executor:d,pendingCount:()=>d.getPendingCount(),scope:g}};export{D as defineCollections};
@@ -1,4 +1,4 @@
1
- import { R as Row } from "./collection-options.d-CspA2N8I.js";
1
+ import { R as Row } from "./collection-options.d-DWNFnQ70.js";
2
2
  import { FunctionReference, SubscriptionError, LunoraClient } from '@lunora/client';
3
3
  import { Transaction, Collection } from '@tanstack/db';
4
4
  import { OfflineExecutor, StorageDiagnostic } from '@tanstack/offline-transactions';
@@ -1,6 +1,6 @@
1
1
  import { LunoraClient } from '@lunora/client';
2
2
  import { Collection, Transaction } from '@tanstack/db';
3
- import { b as CheckpointRegistry } from "./collection-options.d-CspA2N8I.js";
3
+ import { b as CheckpointRegistry } from "./collection-options.d-DWNFnQ70.js";
4
4
  /**
5
5
  * TanStack DB's "direct transaction" marker.
6
6
  *
@@ -1,6 +1,6 @@
1
1
  import { LunoraClient } from '@lunora/client';
2
2
  import { Collection, Transaction } from '@tanstack/db';
3
- import { b as CheckpointRegistry } from "./collection-options.d-CspA2N8I.mjs";
3
+ import { b as CheckpointRegistry } from "./collection-options.d-DWNFnQ70.mjs";
4
4
  /**
5
5
  * TanStack DB's "direct transaction" marker.
6
6
  *
@@ -1,4 +1,4 @@
1
- import { R as Row } from "./collection-options.d-CspA2N8I.mjs";
1
+ import { R as Row } from "./collection-options.d-DWNFnQ70.mjs";
2
2
  import { FunctionReference, SubscriptionError, LunoraClient } from '@lunora/client';
3
3
  import { Transaction, Collection } from '@tanstack/db';
4
4
  import { OfflineExecutor, StorageDiagnostic } from '@tanstack/offline-transactions';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/db",
3
- "version": "1.0.0-alpha.59",
3
+ "version": "1.0.0-alpha.60",
4
4
  "description": "TanStack DB binding: typed, live-synced collections and a durable offline outbox over the Lunora client",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- import{LunoraError as w}from"@lunora/errors";import{BTreeIndex as C}from"@tanstack/db";import{toMap as S,makeDiffEmit as M}from"./OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const g=()=>{let e=Number.NEGATIVE_INFINITY;const n=[];return{advance:r=>{if(!(r<=e)){e=r;for(let a=n.length-1;a>=0;a-=1){const s=n[a];s&&s.threshold<=e&&(s.resolve(),n.splice(a,1))}}},await:r=>r<=e?Promise.resolve():new Promise(a=>{n.push({resolve:a,threshold:r})}),passed:r=>r<=e,waiting:()=>n.length}};let b=!1;const T=e=>{b||(b=!0,console.warn(`[@lunora/db] released an optimistic overlay via the ${String(e.waitedMs)}ms checkpoint fallback: the server confirmed ${e.kind} ${String(e.watermark)} but no sync frame ever echoed it. A dropped shape poke or \`settled\` frame is the usual cause — inspect the subscription rather than raising \`fallbackMs\`. (Reported once per process.)`))},m=new WeakMap,I=new WeakSet,E=3e3,N=(e={})=>{const n=e.fallbackMs??E,r=e.onFallback??T,a=g(),s=g();let d=0;const c=new Set,h=(t,o)=>{for(const l of c)l.kind===t&&l.threshold<=o&&(clearTimeout(l.handle),c.delete(l))},u=(t,o)=>{if(n<=0)return;const l=t==="checkpoint"?a:s;if(l.passed(o))return;for(const p of c)if(p.kind===t&&p.threshold>=o)return;const i={handle:setTimeout(()=>{if(c.delete(i),!l.passed(o)){d+=1,l.advance(o);try{r({kind:t,waitedMs:n,watermark:o})}catch{}}},n),kind:t,threshold:o};c.add(i)};return{acknowledge:({checkpoint:t,mutationId:o})=>{t!==void 0&&u("checkpoint",t),o!==void 0&&u("mutationId",o)},awaitCheckpoint:t=>a.await(t),awaitMutationId:t=>s.await(t),dispose:()=>{for(const t of c)clearTimeout(t.handle);c.clear()},resolve:({checkpoint:t,mutationId:o})=>{t!==void 0&&(a.advance(t),h("checkpoint",t)),o!==void 0&&(s.advance(o),h("mutationId",o))},stats:()=>({fallbacks:d,pendingCheckpointWaiters:a.waiting(),pendingMutationWaiters:s.waiting()})}},K=(e,n,r)=>{let a=m.get(e);a||(a=new Map,m.set(e,a));const s=n??"",d=a.get(s);if(d)return d;const c=N(r);return a.set(s,c),c},F=e=>{I.add(e)},x=e=>I.has(e),O=e=>{const n=m.get(e);if(n){for(const r of n.values())r.resolve({checkpoint:Number.POSITIVE_INFINITY,mutationId:Number.POSITIVE_INFINITY}),r.dispose();m.delete(e)}},W=e=>{const n=m.get(e),r={};if(n)for(const[a,s]of n)r[a]=s.stats();return r},B=e=>{if(e.list===void 0==(e.shape===void 0))throw new w("INTERNAL","lunoraCollectionOptions: pass exactly one of `list` or `shape`");const n=e.getKey??(i=>i._id),r=e.checkpoints??K(e.client,e.shape?.shardKey??e.shardKey);F(r);const a=new Map;let s,d,c,h,u;const t=(i,p)=>{const y=f=>{s?.(S(f,n)),p?.(),e.shape===void 0&&(r.resolve({mutationId:u??e.client.confirmedMutationWatermark(e.shardKey)}),u=void 0)},k=f=>c?.(f),v=f=>{f.mutationId!==void 0&&(u=f.mutationId),r.resolve(f)};return e.shape!==void 0?e.client.subscribeShape({args:i,name:e.shape.name},y,{onCheckpoint:v,onError:k,shardKey:e.shape.shardKey}):e.client.subscribe(e.list,i,y,{onCheckpoint:v,onError:k,shardKey:e.shardKey})},o={autoIndex:"eager",defaultIndexType:C,getKey:n,id:e.id??e.list?.__lunoraRef??`shape:${e.shape?.name??""}`,...e.load==="eager"?{startSync:!0}:{},sync:{sync:i=>(s=M(a,i),c=y=>{i.markReady(),e.onError?.(y)},e.scopeBy===void 0?d=t(e.shape?.args??{},()=>{i.markReady()}):(i.markReady(),h!==void 0&&(d=t(h,void 0))),()=>{s=void 0,c=void 0,d?.(),d=void 0,a.clear()})}};return{checkpoints:r,config:o,scope:i=>{e.scopeBy!==void 0&&(h=i,d?.(),d=void 0,s?.(new Map),i!==void 0&&s!==void 0&&(d=t(i,void 0)))}}};export{E as CHECKPOINT_FALLBACK_MS,N as createCheckpointRegistry,K as getShardCheckpoints,x as hasCheckpointsAttached,B as lunoraCollectionOptions,F as markCheckpointsAttached,O as releaseShardCheckpoints,W as shardCheckpointStats};
@@ -1 +0,0 @@
1
- import{LunoraError as h}from"@lunora/errors";import{createTransaction as v}from"@tanstack/db";import{getShardCheckpoints as w,hasCheckpointsAttached as k}from"./CHECKPOINT_FALLBACK_MS-CX7idSTz.mjs";import{runOutboxMutation as C}from"./OUTBOX_MUTATION_FN_NAME-BfFCughR.mjs";const E="__tanstack_db_direct",I="defineMutator: `serverRef` must be a generated mutator reference (api.mutators.*) or a 'namespace:fn' string",T=t=>{const e=typeof t=="string"?t:t?.__lunoraRef;if(typeof e!="string"||e.length===0)throw new h("INTERNAL",I);return e},A=t=>({__lunoraClientMutator:!0,apply:t.apply,serverRef:T(t.serverRef)}),S=(t,e,l)=>{let c=0,u=t.currentIdentity();const d=()=>{const r=t.currentIdentity();r!==u&&(c=0,u=r)},y=()=>(d(),c=Math.max(c,t.confirmedMutationWatermark(e.shardKey))+1,c);let m=Promise.resolve();const R=(r,a)=>{const s=m.then(async()=>{for(let i=0;;i+=1){const n=y();try{const{applied:o}=await t.callMutator(r,a,{clientSeq:n,shardKey:e.shardKey});if(o)return n}catch(o){throw d(),c=t.confirmedMutationWatermark(e.shardKey),o}if(i>=32)throw new h("INTERNAL",`lunora: custom mutator "${r}" could not claim a fresh client sequence after ${String(32)} attempts`)}});return m=s.then(()=>{},()=>{}),s},M=()=>{if(e.checkpoints===!1)return;if(e.checkpoints)return e.checkpoints;const r=w(t,e.shardKey);return k(r)?r:void 0},_=(r,a,s,i)=>{const{onWriteRejected:n}=e;n&&r.isPersisted.promise.catch(o=>{const f=o instanceof Error?o:new Error(String(o));try{n({args:i,code:f.code,error:f,mutator:a,serverRef:s})}catch{}})},p={};for(const[r,a]of Object.entries(l))p[r]=s=>{const i=v({autoCommit:!0,metadata:{[E]:!0,serverRef:a.serverRef},mutationFn:async()=>{let n=0;await C(async()=>{n=await R(a.serverRef,s)});const o=M();o&&(o.acknowledge({mutationId:n}),await o.awaitMutationId(n))}});return i.mutate(()=>{a.apply({collections:e.collections},s)}),_(i,r,a.serverRef,s),i};return p},j=()=>({bindMutators:S,defineMutator:A});export{E as DIRECT_TRANSACTION_METADATA_KEY,S as bindMutators,A as defineMutator,j as initMutators};
@@ -229,21 +229,6 @@ declare const CHECKPOINT_FALLBACK_MS = 3e3;
229
229
  * every collection on a shard (see that function for why).
230
230
  */
231
231
  declare const createCheckpointRegistry: (options?: CheckpointRegistryOptions) => CheckpointRegistry;
232
- /**
233
- * The shared checkpoint registry for `client` + `shardKey` — created on first use.
234
- * This is the registry {@link lunoraCollectionOptions} and
235
- * {@link import("./define-mutators").bindMutators} default to, which is what makes
236
- * a multi-collection shard work without the caller relaying pokes between
237
- * registries by hand.
238
- *
239
- * `options` applies **only when the registry is created**. Because the point is that
240
- * every collection and mutator on a shard shares one gate, a later call cannot
241
- * retune an existing registry — it returns the existing one and `options` is ignored.
242
- * To control `fallbackMs` / `onFallback`, build the registry yourself with
243
- * {@link createCheckpointRegistry} and pass it explicitly to every
244
- * `lunoraCollectionOptions` and `bindMutators` call for that shard.
245
- */
246
- declare const getShardCheckpoints: (client: LunoraClient, shardKey?: string, options?: CheckpointRegistryOptions) => CheckpointRegistry;
247
232
  /**
248
233
  * Release every pending overlay gate for `client` and drop its shard registries.
249
234
  *
@@ -264,6 +249,21 @@ declare const getShardCheckpoints: (client: LunoraClient, shardKey?: string, opt
264
249
  * ```
265
250
  */
266
251
  declare const releaseShardCheckpoints: (client: LunoraClient) => void;
252
+ /**
253
+ * The shared checkpoint registry for `client` + `shardKey` — created on first use.
254
+ * This is the registry {@link lunoraCollectionOptions} and
255
+ * {@link import("./define-mutators").bindMutators} default to, which is what makes
256
+ * a multi-collection shard work without the caller relaying pokes between
257
+ * registries by hand.
258
+ *
259
+ * `options` applies **only when the registry is created**. Because the point is that
260
+ * every collection and mutator on a shard shares one gate, a later call cannot
261
+ * retune an existing registry — it returns the existing one and `options` is ignored.
262
+ * To control `fallbackMs` / `onFallback`, build the registry yourself with
263
+ * {@link createCheckpointRegistry} and pass it explicitly to every
264
+ * `lunoraCollectionOptions` and `bindMutators` call for that shard.
265
+ */
266
+ declare const getShardCheckpoints: (client: LunoraClient, shardKey?: string, options?: CheckpointRegistryOptions) => CheckpointRegistry;
267
267
  /** Every live registry for `client`, keyed by shard (`""` = unsharded) — for a debug surface. */
268
268
  declare const shardCheckpointStats: (client: LunoraClient) => Record<string, CheckpointRegistryStats>;
269
269
  /**
@@ -229,21 +229,6 @@ declare const CHECKPOINT_FALLBACK_MS = 3e3;
229
229
  * every collection on a shard (see that function for why).
230
230
  */
231
231
  declare const createCheckpointRegistry: (options?: CheckpointRegistryOptions) => CheckpointRegistry;
232
- /**
233
- * The shared checkpoint registry for `client` + `shardKey` — created on first use.
234
- * This is the registry {@link lunoraCollectionOptions} and
235
- * {@link import("./define-mutators").bindMutators} default to, which is what makes
236
- * a multi-collection shard work without the caller relaying pokes between
237
- * registries by hand.
238
- *
239
- * `options` applies **only when the registry is created**. Because the point is that
240
- * every collection and mutator on a shard shares one gate, a later call cannot
241
- * retune an existing registry — it returns the existing one and `options` is ignored.
242
- * To control `fallbackMs` / `onFallback`, build the registry yourself with
243
- * {@link createCheckpointRegistry} and pass it explicitly to every
244
- * `lunoraCollectionOptions` and `bindMutators` call for that shard.
245
- */
246
- declare const getShardCheckpoints: (client: LunoraClient, shardKey?: string, options?: CheckpointRegistryOptions) => CheckpointRegistry;
247
232
  /**
248
233
  * Release every pending overlay gate for `client` and drop its shard registries.
249
234
  *
@@ -264,6 +249,21 @@ declare const getShardCheckpoints: (client: LunoraClient, shardKey?: string, opt
264
249
  * ```
265
250
  */
266
251
  declare const releaseShardCheckpoints: (client: LunoraClient) => void;
252
+ /**
253
+ * The shared checkpoint registry for `client` + `shardKey` — created on first use.
254
+ * This is the registry {@link lunoraCollectionOptions} and
255
+ * {@link import("./define-mutators").bindMutators} default to, which is what makes
256
+ * a multi-collection shard work without the caller relaying pokes between
257
+ * registries by hand.
258
+ *
259
+ * `options` applies **only when the registry is created**. Because the point is that
260
+ * every collection and mutator on a shard shares one gate, a later call cannot
261
+ * retune an existing registry — it returns the existing one and `options` is ignored.
262
+ * To control `fallbackMs` / `onFallback`, build the registry yourself with
263
+ * {@link createCheckpointRegistry} and pass it explicitly to every
264
+ * `lunoraCollectionOptions` and `bindMutators` call for that shard.
265
+ */
266
+ declare const getShardCheckpoints: (client: LunoraClient, shardKey?: string, options?: CheckpointRegistryOptions) => CheckpointRegistry;
267
267
  /** Every live registry for `client`, keyed by shard (`""` = unsharded) — for a debug surface. */
268
268
  declare const shardCheckpointStats: (client: LunoraClient) => Record<string, CheckpointRegistryStats>;
269
269
  /**