@lunora/db 1.0.0-alpha.159 → 1.0.0-alpha.160
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 +1 @@
|
|
|
1
|
-
import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-Do8X5Q7k.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-
|
|
1
|
+
import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-Do8X5Q7k.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-CuRzO6gd.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
|
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}from"./packem_shared/CHECKPOINT_FALLBACK_MS-Do8X5Q7k.mjs";import{defineCollections as O}from"./packem_shared/defineCollections-
|
|
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}from"./packem_shared/CHECKPOINT_FALLBACK_MS-Do8X5Q7k.mjs";import{defineCollections as O}from"./packem_shared/defineCollections-CuRzO6gd.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as f,bindMutators as u,defineMutator as x,initMutators as A}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-CCBmcauu.mjs";import{OUTBOX_MUTATION_FN_NAME as m,createExecutorOutboxSink as E,createOptimisticOnlineDetector as _,makeDiffEmit as N,runOutboxMutation as S,toMap as d}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-rGyegnLw.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,f as DIRECT_TRANSACTION_METADATA_KEY,m as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,u as bindMutators,p as createCheckpointRegistry,E as createExecutorOutboxSink,_ as createOptimisticOnlineDetector,O as defineCollections,x as defineMutator,c as getShardCheckpoints,A as initMutators,l as lunoraCollectionOptions,N as makeDiffEmit,s as releaseShardCheckpoints,S as runOutboxMutation,d as toMap};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{lunoraCollectionOptions as I,scopedShardKey as R}from"./CHECKPOINT_FALLBACK_MS-Do8X5Q7k.mjs";import{isAuthReplayFailure as M}from"@lunora/client";import{createCollection as D,safeRandomUUID as U}from"@tanstack/db";import{startOfflineExecutor as j,NonRetriableError as q}from"@tanstack/offline-transactions";import{assertSecureRandom as L,createOutboxCarrier as k,createOptimisticOnlineDetector as W,OUTBOX_MUTATION_FN_NAME as T,registerOutboxCarrier as $,takeOutboxRejection as S,toNonRetriable as _,runOutboxMutation as H,terminalVerdict as P}from"./OUTBOX_MUTATION_FN_NAME-rGyegnLw.mjs";const A=new WeakMap,V=(o,a)=>{let r=A.get(o);r===void 0&&(r=new Map,A.set(o,r));const c=r.get(a)??0;r.set(a,c+1),c===1&&console.warn(`[@lunora/db] table "${a}" 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).`)},K=(o,a)=>{try{o.onWriteRejected?.(a)}catch{}},G=(o,a)=>{const r=o.replayIdentityVerdict(a?.identity);if(r.verdict==="mismatch")throw new q("outbox write dropped: identity changed since it was queued");if(r.verdict==="unknown")throw new Error("outbox write deferred: no identity established yet");return r.credential},X=6e4,z=[2e3,5e3,1e4,2e4,4e4],J=(o,a,r)=>{if(a!==o.refused)return"send";const c=r-o.since;if(c>=X)return"expired";const d=o.refused===null?z[o.resends]:void 0;return d!==void 0&&c>=d?"send":"wait"},oe=(o,a,r={})=>{L("defineCollections");const c={},d=new Map,O=async(t,e,i)=>{const n=d.get(t),f=n===void 0?"send":J(n,o.getAuthToken(),Date.now());if(f==="expired")throw d.delete(t),_(n?.error);if(f==="wait")throw new Error("outbox write deferred: waiting for a fresh credential");const l=o.getAuthToken(),u=G(o,e);try{await H(async()=>i(u))}catch(s){if(!M(s))throw s;const y=n?.refused===null&&l===null;throw n!==void 0&&!y?(d.delete(t),_(s)):(d.set(t,{error:s,refused:l,resends:n===void 0?0:n.resends+1,since:n?.since??Date.now()}),new Error("outbox write deferred: its credential was refused",{cause:s}))}d.delete(t)},x=(t,e)=>{const i=P(e);return i!==void 0&&d.delete(t),i},E={},p={},B=Object.entries(a);for(const[t,e]of B){const i=e.insert;V(o,t);const{config:n,scope:f}=I({client:o,getKey:e.getKey,id:t,list:e.list,...e.load===void 0?{}:{load:e.load},onError:e.onError,scopeBy:e.scopeBy,shardKey:e.shardKey});c[t]=D(n),e.scopeBy!==void 0&&(E[t]=f),i&&(p[t]=async({idempotencyKey:l,transaction:u})=>{const s=u.metadata,y=s!==void 0&&s.shardKey===void 0&&e.shardKey===void 0&&e.scopeBy!==void 0;for(const[w,g]of u.mutations.entries()){const b=g.modified,v=`${l}:${String(w)}`;try{await O(v,s,async m=>o.mutation(i.mutation,i.toArgs(b),{mutationId:v,replayBaseline:y?null:s?.baselineSeq??null,replayCredential:m,shardKey:y?R(e.scopeBy,b,"the queued row"):s?.shardKey}))}catch(C){const m=x(v,C);throw m!==void 0?(K(r,{code:m.code,collection:t,error:m,row:b}),m):C}}})}p[T]=async({transaction:t})=>{const e=t.metadata;if(e)try{const i=async n=>o.mutation({__lunoraRef:e.functionPath},e.args,{mutationId:e.idempotencyKey,replayBaseline:e.baselineSeq??null,replayCredential:n,shardKey:e.shardKey});await O(e.idempotencyKey,e,i),S(e.idempotencyKey)}catch(i){const n=x(e.idempotencyKey,i);throw n!==void 0?(S(e.idempotencyKey)?.(),K(r,{code:n.code,collection:e.functionPath,error:n}),n):i}};const F=k(),h=j({collections:{...c,[T]:F},mutationFns:p,onlineDetector:W(),...r.onLeadershipChange?{onLeadershipChange:r.onLeadershipChange}:{},...r.onStorageFailure?{onStorageFailure:r.onStorageFailure}:{},onUnknownMutationFn:(t,e)=>{K(r,{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})}});$(h,F);const N={};for(const[t,e]of B){const i=e.insert,n=c[t];!i||!n||(N[t]=f=>{const l=U(),u=i.optimistic(f,l),s=e.shardKey??(e.scopeBy===void 0?void 0:R(e.scopeBy,u,"the inserted row")),y={baselineSeq:o.currentBaseline(s),identity:o.currentIdentity(),shardKey:s},w=h.createOfflineTransaction({autoCommit:!1,metadata:y,mutationFnName:t}),g=w.mutate(()=>{n.insert(u)});return w.commit().catch(()=>{}),{id:l,transaction:g}})}return{actions:N,collections:c,executor:h,pendingCount:()=>h.getPendingCount(),scope:E}};export{oe as defineCollections};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/db",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.160",
|
|
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",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@lunora/client": "1.0.0-alpha.
|
|
58
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
57
|
+
"@lunora/client": "1.0.0-alpha.158",
|
|
58
|
+
"@lunora/errors": "1.0.0-alpha.47"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
61
|
"@tanstack/db": "^0.6.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{lunoraCollectionOptions as _,scopedShardKey as T}from"./CHECKPOINT_FALLBACK_MS-Do8X5Q7k.mjs";import{isAuthReplayFailure as A}from"@lunora/client";import{createCollection as D,safeRandomUUID as U}from"@tanstack/db";import{startOfflineExecutor as j,NonRetriableError as q}from"@tanstack/offline-transactions";import{assertSecureRandom as L,createOutboxCarrier as W,createOptimisticOnlineDetector as $,OUTBOX_MUTATION_FN_NAME as R,registerOutboxCarrier as k,takeOutboxRejection as S,toNonRetriable as I,runOutboxMutation as H,terminalVerdict as P}from"./OUTBOX_MUTATION_FN_NAME-rGyegnLw.mjs";const M=new WeakMap,V=(n,s)=>{let o=M.get(n);o===void 0&&(o=new Map,M.set(n,o));const d=o.get(s)??0;o.set(s,d+1),d===1&&console.warn(`[@lunora/db] table "${s}" 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).`)},K=(n,s)=>{try{n.onWriteRejected?.(s)}catch{}},X=(n,s)=>{const o=n.replayIdentityVerdict(s?.identity);if(o.verdict==="mismatch")throw new q("outbox write dropped: identity changed since it was queued");if(o.verdict==="unknown")throw new Error("outbox write deferred: no identity established yet");return o.credential},z=6e4,ee=(n,s,o={})=>{L("defineCollections");const d={},l=new Map,O=async(t,e,r)=>{const i=l.get(t);if(n.getAuthToken()===i?.refused)throw Date.now()-i.since<z?new Error("outbox write deferred: waiting for a fresh credential"):(l.delete(t),I(i.error));const y=n.getAuthToken(),u=X(n,e);try{await H(async()=>r(u))}catch(a){throw A(a)?i!==void 0?(l.delete(t),I(a)):(l.set(t,{error:a,refused:y,since:Date.now()}),new Error("outbox write deferred: its credential was refused",{cause:a})):a}l.delete(t)},x=(t,e)=>{const r=P(e);return r!==void 0&&l.delete(t),r},B={},p={},E=Object.entries(s);for(const[t,e]of E){const r=e.insert;V(n,t);const{config:i,scope:y}=_({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});d[t]=D(i),e.scopeBy!==void 0&&(B[t]=y),r&&(p[t]=async({idempotencyKey:u,transaction:a})=>{const c=a.metadata,h=c!==void 0&&c.shardKey===void 0&&e.shardKey===void 0&&e.scopeBy!==void 0;for(const[w,g]of a.mutations.entries()){const b=g.modified,v=`${u}:${String(w)}`;try{await O(v,c,async f=>n.mutation(r.mutation,r.toArgs(b),{mutationId:v,replayBaseline:h?null:c?.baselineSeq??null,replayCredential:f,shardKey:h?T(e.scopeBy,b,"the queued row"):c?.shardKey}))}catch(C){const f=x(v,C);throw f!==void 0?(K(o,{code:f.code,collection:t,error:f,row:b}),f):C}}})}p[R]=async({transaction:t})=>{const e=t.metadata;if(e)try{const r=async i=>n.mutation({__lunoraRef:e.functionPath},e.args,{mutationId:e.idempotencyKey,replayBaseline:e.baselineSeq??null,replayCredential:i,shardKey:e.shardKey});await O(e.idempotencyKey,e,r),S(e.idempotencyKey)}catch(r){const i=x(e.idempotencyKey,r);throw i!==void 0?(S(e.idempotencyKey)?.(),K(o,{code:i.code,collection:e.functionPath,error:i}),i):r}};const F=W(),m=j({collections:{...d,[R]:F},mutationFns:p,onlineDetector:$(),...o.onLeadershipChange?{onLeadershipChange:o.onLeadershipChange}:{},...o.onStorageFailure?{onStorageFailure:o.onStorageFailure}:{},onUnknownMutationFn:(t,e)=>{K(o,{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})}});k(m,F);const N={};for(const[t,e]of E){const r=e.insert,i=d[t];!r||!i||(N[t]=y=>{const u=U(),a=r.optimistic(y,u),c=e.shardKey??(e.scopeBy===void 0?void 0:T(e.scopeBy,a,"the inserted row")),h={baselineSeq:n.currentBaseline(c),identity:n.currentIdentity(),shardKey:c},w=m.createOfflineTransaction({autoCommit:!1,metadata:h,mutationFnName:t}),g=w.mutate(()=>{i.insert(a)});return w.commit().catch(()=>{}),{id:u,transaction:g}})}return{actions:N,collections:d,executor:m,pendingCount:()=>m.getPendingCount(),scope:B}};export{ee as defineCollections};
|