@lunora/sql-store 1.0.0-alpha.119 → 1.0.0-alpha.120

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.
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{createSqlCtxDb as a,readSqlCdcChangedTables as o,readSqlCdcChanges as t,readSqlCdcFloor as l,runSqlCdcMigration as s,sweepSqlCdcRetention as n}from"./packem_shared/createSqlCtxDb-D1EQA8lK.mjs";import{runSqlAggregateMigrations as d,runSqlGlobalTableMigrations as S,runSqlRankMigrations as c}from"./packem_shared/runSqlAggregateMigrations-Da82_-Ux.mjs";import{d as g,r as f}from"./packem_shared/ctx-db-search-CHYvzJMF.mjs";import{S as m,m as x}from"./packem_shared/ctx-db-search-state-CsWTh-xc.mjs";import{f as p,e as u,b as h,s as M,t as T}from"./packem_shared/value-codec-Dhem8GM5.mjs";import{decodeGlobalRow as E}from"./packem_shared/decodeGlobalRow-ohh6RNIm.mjs";export{m as SEARCH_STATE_TABLE,g as backfillSqlSearchIndexes,a as createSqlCtxDb,p as decodeBigint,E as decodeGlobalRow,u as effectiveColumnKind,x as migrateSearchState,o as readSqlCdcChangedTables,t as readSqlCdcChanges,l as readSqlCdcFloor,d as runSqlAggregateMigrations,s as runSqlCdcMigration,S as runSqlGlobalTableMigrations,c as runSqlRankMigrations,f as runSqlSearchMigrations,h as sqliteDecode,M as sqliteEncode,n as sweepSqlCdcRetention,T as tryJsonParse};
1
+ import{createSqlCtxDb as a,readSqlCdcChangedTables as o,readSqlCdcChanges as t,readSqlCdcFloor as l,runSqlCdcMigration as s,sweepSqlCdcRetention as n}from"./packem_shared/createSqlCtxDb-Dpgqv8z5.mjs";import{runSqlAggregateMigrations as d,runSqlGlobalTableMigrations as S,runSqlRankMigrations as c}from"./packem_shared/runSqlAggregateMigrations-Da82_-Ux.mjs";import{d as g,r as f}from"./packem_shared/ctx-db-search-CHYvzJMF.mjs";import{S as m,m as x}from"./packem_shared/ctx-db-search-state-CsWTh-xc.mjs";import{f as p,e as u,b as h,s as M,t as T}from"./packem_shared/value-codec-Dhem8GM5.mjs";import{decodeGlobalRow as E}from"./packem_shared/decodeGlobalRow-ohh6RNIm.mjs";export{m as SEARCH_STATE_TABLE,g as backfillSqlSearchIndexes,a as createSqlCtxDb,p as decodeBigint,E as decodeGlobalRow,u as effectiveColumnKind,x as migrateSearchState,o as readSqlCdcChangedTables,t as readSqlCdcChanges,l as readSqlCdcFloor,d as runSqlAggregateMigrations,s as runSqlCdcMigration,S as runSqlGlobalTableMigrations,c as runSqlRankMigrations,f as runSqlSearchMigrations,h as sqliteDecode,M as sqliteEncode,n as sweepSqlCdcRetention,T as tryJsonParse};
@@ -0,0 +1,7 @@
1
+ import{LunoraError as T}from"@lunora/errors";import{m as Wt,D as Ut}from"./ctx-db-search-state-CsWTh-xc.mjs";import{c as xt,r as Pt,A as Ht,C as Kt,R as jt,a as Gt,T as Vt,x as Yt,b as Xt}from"./ctx-db-search-CHYvzJMF.mjs";import{d as yi}from"./ctx-db-search-CHYvzJMF.mjs";import{literalInList as zt,throwingScheduler as Qt,CDC_LOG_TABLE as j,cursorBelowRetainedFloor as Jt,cdcTrimmedError as Zt,assertNoExplicitUndefined as et,hasTrigger as en,runRowValidators as Ne,assertFlatPredicate as tt,mergeWhere as q,rankTableName as we,sortColumnName as Ee,resolveRankPartition as nt,encodePartitionKey as Ce,resolveRankSeekTuple as tn,RANK_TIEBREAK as ye,CountRlsUnsupportedError as it,stripReservedPatchFields as nn,normalizeIdStructurally as on,assertValidClientId as rn,aggregateSqlFunction as $e,softDeleteScope as _e,compileWhereSql as he,normalizeOrderKeys as sn,uniqueIndexFields as an,equalityPinnedFields as cn,buildSeekWhere as dn,decodeCursor as ln,resolveRelationPredicates as ot,resolveWith as rt,relationHooks as st,applySelect as at,encodeCursor as un,NotFoundError as fn,applyOnDelete as wn,ConflictError as Ae,normalizeCountArgument as $n,selectIndexForCount as _n,encodeAggregateKey as Y,aggregateTableName as Q,selectIndexForAggregate as hn,readAggregateValue as De,runTriggers as En,matchesRankStaticWhere as ct,rankPivotConditionSql as pt,CURSOR_PREFIX as yn,selectIndexForGroupBy as pn,tiebreakDirectionFor as Rn,fanOutScalarCounts as Sn,matchesStaticWhere as Be,foldAggregateTally as dt,coerceAggregateNumber as qe,CDC_LOG_TABLE_SEQ_INDEX as gn,NotUniqueError as Tn}from"@lunora/shard-engine";import{sql as e}from"drizzle-orm";import{d as Rt,b as vn,e as St,n as mn,c as In}from"./value-codec-Dhem8GM5.mjs";import{runSqlGlobalTableMigrations as bn,runSqlAggregateMigrations as Cn,runSqlRankMigrations as An}from"./runSqlAggregateMigrations-Da82_-Ux.mjs";import{queryAll as m,queryRun as M,createIndexIfNotExists as Ln,decodeRow as ee,serializeDocumentColumn as J,columnRefSql as O,decodeRows as lt,tableColumns as gt,OCC_VERSION_COLUMN as ut,serializeColumnValue as Oe,forEachRowPaged as ft,BACKFILL_BATCH_SIZE as Fe,queryBatch as wt}from"./decodeGlobalRow-ohh6RNIm.mjs";import{decodeGlobalRow as Ri}from"./decodeGlobalRow-ohh6RNIm.mjs";const kn=(a,c)=>{if(a.size<c)return;const E=a.keys().next().value;E!==void 0&&a.delete(E)},$t=50,Mn=(a,c={})=>{const E=c.maxBatch??$t,s=Number.isInteger(E)&&E>0?E:$t,p=new Map;let w=!1;const d=async(g,v)=>{const I=[...v.keys()];for(let L=0;L<I.length;L+=s){const N=I.slice(L,L+s);try{const W=await a(g,N);for(const G of N){const V=W.get(G);for(const ie of v.get(G)??[])ie.resolve(V)}}catch(W){for(const G of N)for(const V of v.get(G)??[])V.reject(W)}}},A=()=>{w=!1;const g=[...p.entries()];p.clear();for(const[v,I]of g)d(v,I).catch(()=>{})};return{async load(g,v){return new Promise((I,L)=>{let N=p.get(g);N||(N=new Map,p.set(g,N));const W=N.get(v);W?W.push({reject:L,resolve:I}):N.set(v,[{reject:L,resolve:I}]),w||(w=!0,queueMicrotask(A))})}}},Nn=new Set(["_id","id"]),Pe=(a,c,E)=>a==="postgres"?e`${c} IS NOT DISTINCT FROM ${E}`:a==="mysql"?e`${c} <=> ${E}`:e`${c} IS ${E}`,Tt=(a,c)=>a.name!=="postgres"||c.nullable!==!0?"":c.direction==="desc"?" NULLS LAST":" NULLS FIRST",Dn=(a,c)=>{const E=a.map(s=>e`${O(s.field)} ${e.raw(`${s.direction==="desc"?"DESC":"ASC"}${Tt(c,s)}`)}`);if(!a.some(s=>Nn.has(s.field))){const s=Rn(a);E.push(e`${O("id")} ${e.raw(s==="desc"?"DESC":"ASC")}`)}return e.join(E,e`, `)},_t=50,Bn=(a,c,E)=>{const s={...c};for(const[p,w]of gt(a)){if(w.serverDefault){s[p]=w.serverDefault({auth:E});continue}s[p]===void 0&&(w.defaultFn?s[p]=w.defaultFn():"defaultValue"in w&&(s[p]=w.defaultValue))}return s},ht=(a,c,E,s)=>{for(const[p,w]of gt(a)){if(w.serverDefault){p in c&&(E[p]=w.serverDefault({auth:s}));continue}w.onUpdateFn&&!(p in c)&&(E[p]=w.onUpdateFn())}},qn=128,On=()=>{const a=new Map;return{delete:c=>{a.delete(c)},get:c=>{const E=a.get(c);if(E!==void 0)return a.delete(c),a.set(c,E),E},set:(c,E)=>{a.has(c)?a.delete(c):kn(a,qn),a.set(c,E)}}},Fn=async(a,c,E,s,p)=>{const w=p.get(s);if(w!==void 0)return w;const d=Object.entries(E.tables).filter(([,g])=>g.shardMode===void 0||g.shardMode.kind==="global").map(([g])=>g),A=await Promise.all(d.map(async g=>({found:(await m(a,c,e`SELECT 1 FROM ${e.identifier(g)} WHERE ${e.identifier("id")} = ${s} LIMIT 1`)).length>0,tableName:g})));for(const g of A)if(g.found)return p.set(s,g.tableName),g.tableName},te=a=>a==null?null:Number(a),Wn=(a,c,E)=>E.map(s=>{const p={};for(const w of c){const d=a.shape[w];p[w]=vn(s[w]??null,d&&St(d))}return{key:p,value:te(s.value)}}),Un=(a,c,E)=>[...a.partitionBy??[],...a.sortBy.map(p=>p.field),...a.where?Object.keys(a.where):[]].every(p=>c[p]===E[p]),vt=a=>a.length===1?a[0]:e`(${e.join(a,e` AND `)})`,Z=a=>e.join(a.map(c=>e`${e.identifier(c)}`),e`, `),ne=a=>e.join(a.map(c=>e`${c}`),e`, `),xn=(a,c,E,s,p)=>{const w=[];for(let d=0;d<E.length+1;d+=1){const A=E[d],g=c.sortBy[d],v=d<E.length&&A!==void 0&&g!==void 0,I=g?.direction==="desc"?"desc":"asc",L=v?pt(A,s[A],I,!1):e`${e.identifier(ye)} < ${p}`;if(L===void 0)continue;const N=E.slice(0,d).map(W=>Pe(a,e`${e.identifier(W)}`,s[W]));N.push(L),w.push(vt(N))}return w.length>0?e.join(w,e` OR `):e`1 = 0`},Pn=(a,c,E)=>{if(E.length!==c.length)return;const s=[];for(const[p,w]of c.entries()){const d=pt(w.column,E[p],w.direction,!0);if(d===void 0)continue;const A=[];for(let g=0;g<p;g+=1){const v=c[g];v!==void 0&&A.push(Pe(a,e`${e.identifier(v.column)}`,E[g]))}A.push(d),s.push(vt(A))}return s.length===0?e`(1 = 0)`:e`(${e.join(s,e` OR `)})`},Hn=(a,c)=>{if(a.sortBy.length===0)throw new T("INTERNAL",`rankIndex "${a.name}" requires at least one "sortBy" column for stable pagination`);const E=[{column:"__partition__",direction:"asc",nullable:!1}];for(const[s,p]of a.sortBy.entries())E.push({column:c[s]??Ee(s),direction:p.direction,nullable:!0});return E.push({column:ye,direction:"asc",nullable:!1}),E},Kn=async(a,c,E,s,p)=>{const d=[];for(let I=0;I<p.length;I+=50)d.push(p.slice(I,I+50));const A=await Promise.all(d.map(async I=>m(a,c,e`SELECT * FROM ${e.identifier(s)} WHERE ${e.identifier("id")} IN (${ne(I)})`))),g=new Map;for(const I of A)for(const L of I)g.set(L.id,L);const v=[];for(const I of p){const L=ee(E,g.get(I));L&&v.push(L)}return v},jn=a=>{const c=JSON.stringify(a),E=new TextEncoder().encode(c);let s="";for(const p of E)s+=String.fromCodePoint(p);return yn+btoa(s)},mt=a=>{if(a===void 0)return!1;const c=St(a);return c==="any"||c==="bigint"||c==="from"||c==="union"},Ue=(a,c,E)=>{if(mt(a.shape[c]))throw new T("BAD_REQUEST",`${E}: "${c}" may hold an order-preserving key rather than a value SQL can reduce or group — declare an aggregateIndex covering this (by, field, op) so the maintained companion answers it instead (its running total is a REAL, so it stays exact only while the total is inside 2^53)`)},Gn=(a,c,E,s)=>{for(const p of E)Ue(a,p,`groupBy(${c}, { by: [..."${p}"] })`);s.field!==void 0&&Ue(a,s.field,`groupBy(${c}, { agg: { op: "${s.op}", field: "${s.field}" } })`)},xe="__cdc_sweep",It="global",Vn=6e4,Yn=1e4,Xn=3e4;let Et=0;const zn=async(a,c)=>{const{autoincrementPrimaryKey:E,key:s,real:p,text:w}=c.companionTypes;await M(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(j)} (${e.identifier("seq")} ${e.raw(E)}, ${e.identifier("ts")} ${e.raw(p)} NOT NULL, ${e.identifier("table")} ${e.raw(s)} NOT NULL, ${e.identifier("id")} ${e.raw(s)} NOT NULL, ${e.identifier("op")} ${e.raw(s)} NOT NULL, ${e.identifier("doc")} ${e.raw(w)})`);const d=c.indexKeyPrefix?.("string");await Ln(a,c,{columns:e`${d===void 0?e`${e.identifier("table")}`:e`${e.identifier("table")}(${e.raw(String(d))})`}, ${e.identifier("seq")}`,name:gn,table:j,unique:!1}),await M(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(xe)} (${e.identifier("id")} ${e.raw(s)} NOT NULL PRIMARY KEY, ${e.identifier("lease_until")} ${e.raw(p)} NOT NULL)`),await M(a,c,e`INSERT INTO ${e.identifier(xe)} (${e.identifier("id")}, ${e.identifier("lease_until")}) VALUES (${It}, ${0}) ${c.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${e.identifier("id")} = ${e.identifier("id")}`:e`ON CONFLICT(${e.identifier("id")}) DO NOTHING`}`)},Qn=async(a,c,E)=>{const s=e`UPDATE ${e.identifier(xe)} SET ${e.identifier("lease_until")} = ${E+Vn} WHERE ${e.identifier("id")} = ${It} AND ${e.identifier("lease_until")} <= ${E}`;if(c.supportsReturning)return(await m(a,c,e`${s} RETURNING ${e.identifier("id")}`)).length>0;const p=await M(a,c,s);return(c.affectedRows?c.affectedRows(p??{rowsAffected:0}):0)>0},Jn=async(a,c,E,s)=>{if(!await Qn(a,c,s))return;const p=s-E;await M(a,c,e`
2
+ DELETE FROM ${e.identifier(j)} WHERE ${e.identifier("seq")} IN (
3
+ SELECT ${e.identifier("seq")} FROM (
4
+ SELECT ${e.identifier("seq")} FROM ${e.identifier(j)} WHERE ${e.identifier("ts")} <= ${p} ORDER BY ${e.identifier("seq")} ASC LIMIT ${e.raw(String(Yn))}
5
+ ) AS ${e.identifier("expired")}
6
+ )
7
+ `)},bt=async(a,c)=>{const E=await m(a,c,e`SELECT MIN(${e.identifier("seq")}) AS ${e.identifier("seq")} FROM ${e.identifier(j)}`),s=Number(E[0]?.seq??Number.NaN);return Number.isFinite(s)?s:void 0},Zn=a=>JSON.stringify(mn(a)?In(a):a),ei=async(a,c,E,s,p,w,d)=>{await M(a,d,e`INSERT INTO ${e.identifier(j)} (${e.identifier("ts")}, ${e.identifier("table")}, ${e.identifier("id")}, ${e.identifier("op")}, ${e.identifier("doc")}) VALUES (${c}, ${E}, ${s}, ${p}, ${w===void 0?null:Zn(w)})`)},$i=async(a,c,E)=>{const s=c.sinceSeq??0,p=Math.max(1,Math.min(c.limit??1e3,1e4)),w=await bt(a,E);if(w!==void 0&&Jt(w,s))throw Zt(w,s,"global");const A=(await m(a,E,e`SELECT seq, ts, ${e.identifier("table")}, id, op, doc FROM ${e.identifier(j)} WHERE seq > ${s} ORDER BY seq ASC LIMIT ${e.raw(String(p))}`)).map(g=>{const{doc:v}=g,I={id:String(g.id),op:String(g.op),seq:Number(g.seq),table:String(g.table),ts:Number(g.ts)};return typeof v=="string"?{...I,doc:Rt(JSON.parse(v))}:I});return{changes:A,cursor:A.at(-1)?.seq??s}},ti=async(a,c,E,s={})=>{if(s.cursorOnly){const g=await m(a,E,e`SELECT MAX(seq) AS seq FROM ${e.identifier(j)}`),v=Number(g[0]?.seq??c);return{cursor:Number.isFinite(v)?v:c,tables:[]}}const p=await m(a,E,e`SELECT ${e.identifier("table")} AS ${e.identifier("table")}, MAX(seq) AS seq FROM ${e.identifier(j)} WHERE seq > ${c} GROUP BY ${e.identifier("table")}`);let w=c;const d=[];for(const g of p){const v=Number(g.seq);Number.isFinite(v)&&v>w&&(w=v),d.push(String(g.table))}const A=s.retained===!0?await bt(a,E):void 0;return{cursor:w,tables:d,...A===void 0?{}:{floor:A}}},ni=/string or blob too big/iu,ii=/row is too big/iu,oi=(a,c)=>{const{code:E,errno:s}=c,p=c instanceof Error?c.message:"";switch(a.name){case"mysql":return s===1118||E==="ER_TOO_BIG_ROWSIZE"?"InnoDB's per-row ceiling — roughly 8 KB, half a 16 KB page, for the part of the row stored inline":void 0;case"postgres":return ii.test(p)?"the 8 KB heap page a tuple must fit once its wide columns have been TOASTed out":void 0;default:return ni.test(p)?"the storage engine's per-row ceiling (2 MB on D1)":void 0}},ri=(a,c,E)=>{const s=oi(a,c);if(s!==void 0)throw new T("PAYLOAD_TOO_LARGE",`document is too large to store in "${E}": a single row cannot exceed ${s}. The limit is on the STORED bytes, which are UTF-8. Keep the payload in R2 (ctx.storage) and store a reference on the row.`)},yt=(a,c,E)=>{throw a.isUniqueViolation(c)?new Ae(`unique constraint violation on "${E}"`,"unique"):(ri(a,c,E),c)},We=new WeakMap,_i=a=>{const{crossShardCounter:c,crossShardReader:E,exec:s,maxRelationKeys:p,schema:w}=a,{dialect:d}=a,A={fieldRef:O,...d.name==="mysql"?{containsExpr:(t,n)=>e`LOCATE(${n}, ${t}) > 0`}:{},...d.name==="postgres"?{containsExpr:(t,n)=>e`strpos(${t}, ${n}) > 0`}:{},...d.name==="sqlite"?{}:{inList:zt}},g=new WeakMap,v=t=>{const n=g.get(t);if(n!==void 0)return n;const i={...A,serialize:(o,r)=>J(t,r,o)};return g.set(t,i),i},I=(t,n)=>Pe(d.name,t,n),L=t=>{const n=Z(t.columns),i=ne(t.values),o=u=>d.name==="mysql"?e`VALUES(${e.identifier(u)})`:e`excluded.${e.identifier(u)}`,r=u=>d.name==="postgres"?e`${e.identifier(t.table)}.${e.identifier(u)}`:e`${e.identifier(u)}`,l=e.join(Object.entries(t.set(o,r)).map(([u,y])=>e`${e.identifier(u)} = ${y}`),e`, `),h=d.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${l}`:e`ON CONFLICT(${e.identifier(t.conflictKey)}) DO UPDATE SET ${l}`;return e`INSERT INTO ${e.identifier(t.table)} (${n}) VALUES (${i}) ${h}`},N=a.clock??(()=>Date.now()),W=a.idGenerator??(()=>crypto.randomUUID()),G=a.cdc??!1,V=typeof a.cdcRetentionMs=="number"&&Number.isFinite(a.cdcRetentionMs)&&a.cdcRetentionMs>0?a.cdcRetentionMs:void 0,ie=a.auth??{identity:null,userId:null},oe=async(t,n,i,o)=>{if(!G||(await ei(s,N(),t,n,i,o,d),V===void 0))return;const r=N();if(!(r-Et<Xn)){Et=r;try{await Jn(s,d,V,r)}catch{}}},Ct=a.scheduler??Qt,Le=On();let pe=0;const ke=a.provisionScope??{},U=async()=>{const t=We.get(ke);if(t)return t;const n=(async()=>{await bn(s,w,d),await Cn(s,w,d),await An(s,w,d),await Wt(s,d),await Pt(s,w,d),G&&await zn(s,d)})().catch(i=>{throw We.delete(ke),i});return We.set(ke,n),n},re=async(t,n)=>{await U();const i=await Fn(s,d,w,t,Le);if(!(n!==void 0&&i!==n))return i},se=new Map,He=async(t,n)=>{const i=Q(t,n);return(await m(s,d,d.tableExists(i))).length>0},Re=async(t,n)=>{const i=`${t}::${n.name}`,o=se.get(i);if(o!==void 0)return o;if(!await He(t,n.name))return se.set(i,!1),!1;const l=w.tables[t];if(!l)return se.set(i,!1),!1;const h=n.by??[],u=new Map;await ft(s,d,l,t,$=>{if(n.where&&!Be($,n.where))return;const _=Y(h,$);dt(u,_,n,$)});const y=Q(t,n.name);await M(s,d,e`DELETE FROM ${e.identifier(y)}`);const f=[];for(const[$,_]of u)f.push(e`INSERT INTO ${e.identifier(y)} (${e.identifier("__key__")}, ${e.identifier("__value__")}, ${e.identifier("__count__")}) VALUES (${$}, ${_.value}, ${_.count})`);return await wt(s,d,f),se.set(i,!0),!0},At=async(t,n,i,o)=>{const r=new Map;let l,h=!0;for(;h;){const u=l===void 0?o:[...o,e`${e.identifier("id")} > ${l}`],y=u.length>0?e` WHERE ${e.join(u,e` AND `)}`:e``,f=await m(s,d,e`SELECT ${e.identifier("id")}, ${O(i.field??"")} FROM ${e.identifier(n)}${y} ORDER BY ${e.identifier("id")} ASC LIMIT ${e.raw(String(Fe))}`);for(const $ of lt(t,f))dt(r,"",i,$);l=f.at(-1)?.id,h=f.length===Fe}return r.get("")?.value??null},Lt=async(t,n,i)=>{const o=n.field??"",r=[],l=w.tables[t],h=($,_)=>l?J(l,$,_):Oe(_);for(const $ of n.by??[]){const _=h($,i[$]??null);r.push(_===null?e`${O($)} IS NULL`:e`${O($)} = ${_}`)}for(const[$,_]of Object.entries(n.where??{})){const R=_!==null&&typeof _=="object"&&!Array.isArray(_)?_.eq:_,S=h($,R);r.push(S===null?e`${O($)} IS NULL`:e`${O($)} = ${S}`)}const u=r.length>0?e` WHERE ${e.join(r,e` AND `)}`:e``;if(l&&mt(l.shape[o]))return At(l,t,n,r);const y=$e(n.op),f=await m(s,d,e`SELECT ${e.raw(y)}(${O(o)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}${u}`);return te(f[0]?.value)},kt=async(t,n)=>{await M(s,d,e`DELETE FROM ${e.identifier(t)} WHERE ${e.identifier("__key__")} = ${n} AND ${e.identifier("__count__")} <= 0`)},Mt=async(t,n,i,o)=>{const r=Q(t,n.name),{op:l}=n,h=n.field??"",u=i&&(!n.where||Be(i,n.where))?i:void 0,y=o&&(!n.where||Be(o,n.where))?o:void 0;if(!(!u&&!y)){if(l==="count"||l==="sum"||l==="avg"){const f=new Set;for(const[$,_]of[[u,-1],[y,1]]){if(!$)continue;const R=l==="count"?1:qe($[h]);if(R===void 0)continue;const S=Y(n.by??[],$);f.add(S),await M(s,d,L({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(b,C)=>({__count__:e`${C("__count__")} + ${b("__count__")}`,__value__:l==="count"?e`${C("__value__")} + ${b("__value__")}`:e`COALESCE(${C("__value__")}, 0) + ${b("__value__")}`}),table:r,values:[S,_*R,_]}))}for(const $ of f)await kt(r,$);return}if(u){const f=Y(n.by??[],u),$=qe(u[h]),R=(await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(r)} WHERE ${e.identifier("__key__")} = ${f}`))[0],S=te(R?.value),b=(R?.count??0)-1;if(b<=0)await M(s,d,e`DELETE FROM ${e.identifier(r)} WHERE ${e.identifier("__key__")} = ${f}`);else if(R&&$!==void 0&&S!==null&&$===S){const C=await Lt(t,n,u);await M(s,d,e`UPDATE ${e.identifier(r)} SET ${e.identifier("__value__")} = ${C}, ${e.identifier("__count__")} = ${b} WHERE ${e.identifier("__key__")} = ${f}`)}else await M(s,d,e`UPDATE ${e.identifier(r)} SET ${e.identifier("__count__")} = ${e.identifier("__count__")} - 1 WHERE ${e.identifier("__key__")} = ${f}`)}if(y){const f=Y(n.by??[],y),$=qe(y[h]);if($===void 0)await M(s,d,L({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(_,R)=>({__count__:e`${R("__count__")} + 1`}),table:r,values:[f,null,1]}));else{const _=l==="min"?"MIN":"MAX";await M(s,d,L({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(R,S)=>({__count__:e`${S("__count__")} + 1`,__value__:e`${e.raw(_)}(COALESCE(${S("__value__")}, ${R("__value__")}), ${R("__value__")})`}),table:r,values:[f,$,1]}))}}}},Se=async t=>{const n=w.tables[t]?.aggregateIndexes;if(!(!n||n.length===0))for(const i of n)await Re(t,i)},ae=async(t,n,i)=>{const o=w.tables[t]?.aggregateIndexes;if(!(!o||o.length===0))for(const r of o){const l=`${t}::${r.name}`;(se.get(l)??await He(t,r.name))&&await Mt(t,r,n,i)}},ce=new Map,Ke=async(t,n)=>{const i=we(t,n);return(await m(s,d,d.tableExists(i))).length>0},Me=async(t,n)=>{const i=`${t}::rank::${n.name}`,o=ce.get(i);if(o!==void 0)return o;if(!await Ke(t,n.name))return ce.set(i,!1),!1;const l=w.tables[t];if(!l)return ce.set(i,!1),!1;const h=we(t,n.name);await M(s,d,e`DELETE FROM ${e.identifier(h)}`);const u=n.sortBy.map((_,R)=>Ee(R)),y=Z(["__id__","__partition__",...u]);let f=[];const $=async()=>{if(f.length===0)return;const _=f;f=[],await wt(s,d,_)};return await ft(s,d,l,t,async _=>{if(n.where&&!ct(_,n.where))return;const R=Ce(n.partitionBy??[],_),S=n.sortBy.map(b=>Oe(_[b.field]??null));f.push(e`INSERT INTO ${e.identifier(h)} (${y}) VALUES (${ne([_._id,R,...S])})`),f.length>=Fe&&await $()}),await $(),ce.set(i,!0),!0},ge=async t=>{const n=w.tables[t]?.rankIndexes;if(!(!n||n.length===0))for(const i of n)await Me(t,i)},X=async(t,n,i,o)=>{const r=w.tables[t],l=r?.rankIndexes;if(!l||l.length===0)return;const h=r?.softDeleteMode?.field,u=h===void 0||o===void 0?void 0:o[h],y=u!=null;for(const f of l){if(i!==void 0&&o!==void 0&&Un(f,i,o))continue;const $=`${t}::rank::${f.name}`;if(!(ce.get($)??await Ke(t,f.name)))continue;const S=we(t,f.name);if(i&&await M(s,d,e`DELETE FROM ${e.identifier(S)} WHERE ${e.identifier("__id__")} = ${n}`),o&&!y){if(f.where&&!ct(o,f.where))continue;const b=f.sortBy.map((B,K)=>Ee(K)),C=Z(["__id__","__partition__",...b]),F=Ce(f.partitionBy??[],o),k=f.sortBy.map(B=>Oe(o[B.field]??null));await M(s,d,e`INSERT INTO ${e.identifier(S)} (${C}) VALUES (${ne([n,F,...k])})`)}}},de=xt({dialect:d,exec:s,schema:w}),je=new Set;for(const[t,n]of Object.entries(w.tables))for(const i of Object.values(n.triggerMap??{}))je.add(`${t} ${i.timing} ${i.op}`);const P=(t,n,i)=>je.has(`${t} ${n} ${i}`);let Ge;const H=async(t,n,i)=>{if(pe+=1,pe>_t)throw pe-=1,new Ae(`trigger recursion exceeded ${String(_t)} levels on "${i.table}" — check for a self-triggering write`,"trigger");try{await En({ctx:Ge,event:i,op:n,schema:w,tableName:i.table,timing:t})}finally{pe-=1}},Nt=async(t,n)=>{try{await M(s,d,n)}catch(i){yt(d,i,t)}},Te=async(t,n)=>(await m(s,d,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} = ${n}`))[0],ve=async(t,n,i,o)=>{if(o===void 0)return;const r=e`${e.identifier(ut)}`,l=e.join([I(e`${e.identifier("id")}`,o.id),I(r,o[ut]??null)],e` AND `),h=n==="UPDATE"?e`UPDATE ${e.identifier(t)} SET ${i}, ${r} = COALESCE(${r}, 0) + 1 WHERE ${l}`:e`DELETE FROM ${e.identifier(t)} WHERE ${l}`,u=()=>{throw new Ae(`optimistic concurrency conflict on "${t}" — the row changed during this mutation; refetch and retry`,"occ")};try{if(d.supportsReturning)(await m(s,d,e`${h} RETURNING ${e.identifier("id")}`)).length===0&&u();else{const y=await M(s,d,h);(d.affectedRows?d.affectedRows(y??{rowsAffected:0}):0)===0&&u()}}catch(y){yt(d,y,t)}},Dt=(t,n,i,o)=>{const r=Object.keys(t.shape);return{columns:["id","_creationTime",...r],values:[n,i,...r.map(l=>J(t,l,o[l]??null))]}},Bt=async(t,n,i,o)=>{const r=pn(n,i.op,i.field,o.by,o.where);if(!r||!await Re(t,r.index))return;const h=Q(t,r.index.name),u=Object.keys(r.partial);if(u.length===(r.index.by??[]).length&&u.length>0){const f=Y(r.index.by??[],r.partial),$=await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(h)} WHERE ${e.identifier("__key__")} = ${f}`);if($.length===0)return[];const _=$[0];return[{key:{...r.partial},value:De(i.op,{count:_.count,value:te(_.value)})}]}return u.length>0?void 0:(await m(s,d,e`SELECT ${e.identifier("__key__")} AS ${e.identifier("key")}, ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(h)}`)).map(f=>{const $=f;return{key:Rt(JSON.parse($.key)),value:De(i.op,{count:$.count,value:te($.value)})}})},Ve=t=>{const n=w.tables[t]?.shardMode?.kind;return n==="shardBy"||n==="root"},Ye=t=>{throw new T("INTERNAL",`cross-backend relation: a global table cannot load the shard-local relation '${t}' (it spans every shard) — wire a cross-shard reader to support it`)},qt=(t,n)=>{if(n?.relationMask!==void 0&&n.with!==void 0&&Object.keys(n.with).length>0)throw new T("MASK_UNSUPPORTED",`masking cannot follow a nested \`with\` across the cross-shard relation hop to "${t}" — the nested rows are hydrated on the serving shard, where the mask policy does not exist. Read the nested relation at its own (masked) call site instead.`);const i={};if(n?.relationBaseWhere)for(const o of Object.keys(w.tables)){const r=n.relationBaseWhere(o);r!==void 0&&(i[o]=r)}return{orderBy:n?.orderBy,relationPolicies:i,where:q(n?.baseWhere,n?.where),with:n?.with}},Xe=(t,n)=>Ve(t)?E?E(t,qt(t,n)):Ye(t):x.findMany(t,n),me=(t,n,i)=>ot(t,{fetcher:Xe,maxRelationKeys:p,relationBaseWhere:i,schema:w,tableName:n}),Ot=Mn(async(t,n)=>{const i=await m(s,d,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} IN (${ne(n)})`),o=new Map;for(const r of i){const{id:l}=r;typeof l=="string"&&o.set(l,r)}return o}),x={async cdcChangedTables(t,n){if(G)try{return await U(),await ti(s,t,d,{...n,retained:V!==void 0})}catch{return}},async aggregate(t,n){const i=w.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);if(await U(),$e(n.op),n.op==="count")return x.count(t,{baseWhere:n.baseWhere,relationBaseWhere:n.relationBaseWhere,restrictsCounts:n.restrictsCounts,where:n.where});if(!n.field)throw new T("INTERNAL",`aggregate(${t}, { op: "${n.op}" }): "field" is required for non-count reducers`);const o=_e(i.softDeleteMode,void 0),r=q(q(n.baseWhere,n.where),o),l=await me(r,t,n.relationBaseWhere),h=l!==r;if(i.aggregateIndexes&&!n.baseWhere&&!h&&!o){const R=hn(i.aggregateIndexes,n.op,n.field,n.where);if(R&&await Re(t,R.index)){const b=Y(R.index.by??[],R.key),C=Q(t,R.index.name),k=(await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(C)} WHERE ${e.identifier("__key__")} = ${b}`))[0];return De(n.op,k===void 0?void 0:{count:k.count,value:te(k.value)})}}Ue(i,n.field,`aggregate(${t}, { op: "${n.op}", field: "${n.field}" })`);const u=he(l,v(i)),y=e.raw($e(n.op)),f=e`SELECT ${y}(${O(n.field)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}`,_=(await m(s,d,u?e`${f} WHERE ${u}`:f))[0]?.value;return _==null?null:Number(_)},async count(t,n){const i=w.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=$n(n);if(o.restrictsCounts)throw new it(t);const r=_e(i.softDeleteMode,void 0),l=q(q(o.baseWhere,o.where),r),h=await me(l,t,o.relationBaseWhere),u=h!==l;if(i.aggregateIndexes&&!o.baseWhere&&!u&&!r){const _=_n(i.aggregateIndexes,o.where);if(_&&await Re(t,_.index)){const S=Y(_.index.by??[],_.key),b=Q(t,_.index.name),C=await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")} FROM ${e.identifier(b)} WHERE ${e.identifier("__key__")} = ${S}`);return Number(C[0]?.value??0)}}const y=he(h,v(i)),f=e`SELECT COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(t)}`,$=await m(s,d,y?e`${f} WHERE ${y}`:f);return Number($[0]?.count??0)},async delete(t,n,i){const o=await re(t,n);if(!o)return;const r=w.tables[o];if(!r)return;const l=await Te(o,t),h=ee(r,l),u=i?.hard===!0,y=!u&&r.softDeleteMode?r.softDeleteMode.field:void 0;if(!(y&&(!h||h[y]!==null&&h[y]!==void 0))){if(P(o,"before","delete")&&await H("before","delete",{id:t,op:"delete",previous:h??void 0,table:o}),await wn({deletedId:t,deletedReference:f=>h?.[f],findHolders:async(f,$,_)=>{if(w.tables[f]?.shardMode?.kind==="shardBy")throw new T("INTERNAL",`cross-backend cascade from global '${o}' into shardBy '${f}' is not supported — would require Query Coordinator fan-out across shards`);return(await x.findMany(f,{includeDeleted:u,where:{[$]:_}})).page},onCascade:(f,$)=>x.delete($,void 0,i),onRestrict:f=>{throw new Ae(f,"restrict")},onSetNull:(f,$,_)=>x.patch($,{[_]:null}),schema:w,tableName:o}),await Se(o),await ge(o),y&&h){const f={...h,[y]:N()},$=e.join(Object.keys(r.shape).map(_=>e`${e.identifier(_)} = ${J(r,_,f[_]??null)}`),e`, `);await ve(o,"UPDATE",$,l),await ae(o,h,f),await X(o,t,h,void 0),await de(o,t,f,h),await oe(o,t,"update",f),P(o,"after","delete")&&await H("after","delete",{id:t,op:"delete",previous:h,table:o});return}await ve(o,"DELETE",void 0,l),Le.delete(t),await ae(o,h??void 0,void 0),await X(o,t,h??void 0,void 0),await de(o,t,void 0),await oe(o,t,"delete"),P(o,"after","delete")&&await H("after","delete",{id:t,op:"delete",previous:h??void 0,table:o})}},async findFirst(t,n={}){return(await x.findMany(t,{...n,limit:1})).page[0]??null},async findFirstOrThrow(t,n={}){const i=await x.findFirst(t,n);if(i===null)throw new fn(`findFirstOrThrow: no "${t}" document matched`);return i},async findMany(t,n={}){const i=w.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=sn(n.orderBy,i.shape,{pinned:cn(n.where),uniqueBy:an(i.indexes,i.shape)}),r=n.cursor?dn(o,ln(n.cursor)):void 0,l=Xe,h=async(k,B,K,Ie)=>{if(Ve(k))return c?Sn(c,k,B,K,Ie):Ye(k);const le=w.tables[k];if(!le)throw new T("INTERNAL",`unknown table: ${k}`);const be=_e(le.softDeleteMode,void 0),z={[B]:{in:K}},D=q(q(z,Ie),be),ue=await me(D,k,void 0),ze=he(ue,v(le)),Qe=O(B);let fe=e`SELECT ${Qe} AS __fk__, COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(k)}`;ze&&(fe=e`${fe} WHERE ${ze}`),fe=e`${fe} GROUP BY ${Qe}`;const Ft=await m(s,d,fe),Je=new Map;for(const Ze of Ft)Je.set(Ze.__fk__,Number(Ze.count??0));return Je};let u=q(n.baseWhere,n.where);u=q(u,_e(i.softDeleteMode,n.includeDeleted)),u=await ot(u,{fetcher:l,maxRelationKeys:p,relationBaseWhere:n.relationBaseWhere,schema:w,tableName:t}),r&&(u=u?{AND:[u,r]}:r);const y=he(u,v(i)),f=Dn(o,d);let $=e`SELECT * FROM ${e.identifier(t)}`;y&&($=e`${$} WHERE ${y}`),$=e`${$} ORDER BY ${f}`;const _=typeof n.limit=="number"?Math.max(0,Math.floor(n.limit)):void 0;_!==void 0&&($=e`${$} LIMIT ${e.raw(String(_+1))}`);const R=await m(s,d,$),S=lt(i,R);if(_===void 0)return n.with&&await rt({fetcher:l,groupedCounter:h,parents:S,...st(n),schema:w,tableName:t,with:n.with}),{continueCursor:null,isDone:!0,page:at(S,n.select,n.with)};const b=S.length>_,C=b?S.slice(0,_):S,F=C.at(-1);return n.with&&await rt({fetcher:l,groupedCounter:h,parents:C,...st(n),schema:w,tableName:t,with:n.with}),{continueCursor:b&&F?un(F,o):null,isDone:!b,page:at(C,n.select,n.with)}},async get(t,n){const i=await re(t,n);if(!i)return null;const o=w.tables[i];return o?ee(o,await Ot.load(i,t)):null},async groupBy(t,n){const i=w.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=n.agg??{op:"count"};if($e(o.op),o.op!=="count"&&!o.field)throw new T("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);const r=_e(i.softDeleteMode,void 0),l=q(q(n.baseWhere,n.where),r),h=await me(l,t,n.relationBaseWhere),u=h!==l;if(i.aggregateIndexes&&!n.baseWhere&&!u&&!r){const S=await Bt(t,i.aggregateIndexes,o,n);if(S!==void 0)return S}Gn(i,t,n.by,o);const y=he(h,v(i)),f=n.by.map(S=>e`${O(S)} AS ${e.identifier(S)}`);if(o.op==="count")f.push(e`COUNT(*) AS ${e.identifier("value")}`);else{if(!o.field)throw new T("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);f.push(e`${e.raw($e(o.op))}(${O(o.field)}) AS ${e.identifier("value")}`)}const $=e.join(n.by.map(S=>O(S)),e`, `);let _=e`SELECT ${e.join(f,e`, `)} FROM ${e.identifier(t)}`;y&&(_=e`${_} WHERE ${y}`),_=e`${_} GROUP BY ${$}`;const R=await m(s,d,_);return Wn(i,n.by,R)},async insert(t,n,i){const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);await U();const r=Bn(o,n,ie);Ne(o,r);let l,h=!0;i?.clientId!==void 0?(rn(i.clientId),l=i.clientId):i?.allowExplicitId&&typeof r._id=="string"?l=r._id:(l=W(),h=!1);const u=i?.allowExplicitId&&typeof r._creationTime=="number"?r._creationTime:N(),y={...r,_creationTime:u,_id:l};P(t,"before","insert")&&await H("before","insert",{doc:{...y},id:l,op:"insert",table:t}),await Se(t),await ge(t);const{columns:f,values:$}=Dt(o,l,u,r);return await Nt(t,e`INSERT INTO ${e.identifier(t)} (${Z(f)}) VALUES (${ne($)})`),h&&Le.set(l,t),await ae(t,void 0,y),await X(t,l,void 0,y),await de(t,l,y),await oe(t,l,"insert",y),P(t,"after","insert")&&await H("after","insert",{doc:y,id:l,op:"insert",table:t}),l},normalizeId(t,n){return on(w,t,n)},async patch(t,n,i){et("patch",n);const o=await re(t,i);if(!o)throw new T("INTERNAL",`document not found: ${t}`);const r=w.tables[o];if(!r)throw new T("INTERNAL",`document not found: ${t}`);const l=await Te(o,t),h=ee(r,l);if(!h)throw new T("INTERNAL",`document not found: ${t}`);const u={...h,...nn(n),_id:t};ht(r,n,u,ie),Ne(r,u,!0),P(o,"before","update")&&await H("before","update",{doc:{...u},id:t,op:"update",previous:h,table:o}),await Se(o),await ge(o);const y=Object.keys(r.shape),f=e.join(y.map($=>e`${e.identifier($)} = ${J(r,$,u[$]??null)}`),e`, `);await ve(o,"UPDATE",f,l),await ae(o,h,u),await X(o,t,h,u),await de(o,t,u,h),await oe(o,t,"update",u),P(o,"after","update")&&await H("after","update",{doc:u,id:t,op:"update",previous:h,table:o})},async restore(t,n){const i=await re(t,n);if(!i)throw new T("INTERNAL",`document not found: ${t}`);const o=w.tables[i],r=o?.softDeleteMode?.field;if(!o||!r)throw new T("INTERNAL",`ctx.db.restore: table "${i}" is not a .softDelete() table`);const l=await Te(i,t),h=l?.[r]!==null&&l?.[r]!==void 0;if(await x.patch(t,{[r]:null},n),h){const u=ee(o,l);u!==null&&await X(i,t,void 0,{...u,[r]:null})}},query(t){const n=w.tables[t];if(!n)throw new T("INTERNAL",`unknown table: ${t}`);const i="the legacy query()/withIndex() reader is not available on the D1 (global) backend; use findMany",o=[],r=u=>o.every(y=>y(u)),l=async(u,y)=>{await U();const f=o.length>0,$=await Gt(s,d,n,t,u,Vt(f?void 0:y));if(!f)return y===void 0&&Yt($),$;const _=$.filter(R=>r(R));return y===void 0?_:_.slice(0,y)},h=u=>{const y={async*[Symbol.asyncIterator](){if(!u)throw new T("INTERNAL",i);yield*await l(u,void 0)},async collect(){if(!u)throw new T("INTERNAL",i);return l(u,void 0)},async collectWithScores(){throw new T("GLOBAL_SEARCH_SCORES_UNSUPPORTED",`collectWithScores() is not supported on \`.global()\` tables (table "${t}") — relevance scores are not yet surfaced on this backend; use .collect() instead`)},filter(f){return o.push(f),y},async first(){if(!u)throw new T("INTERNAL",i);return(await l(u,1))[0]??null},order(){return y},async paginate(f){if(!u)throw new T("INTERNAL",i);const $=Kt(f);return jt(await l(u,Xt($)),$)},async take(f){if(!u)throw new T("INTERNAL",i);return l(u,f)},async unique(){if(!u)throw new T("INTERNAL",i);const f=await l(u,2);if(f.length>1)throw new Tn(`unique() on table "${t}" matched ${String(f.length)} documents; expected at most one`);return f[0]??null},withGeoIndex(){throw new T("INTERNAL",`geo indexes are not supported on \`.global()\` tables (table "${t}") — geospatial queries run only on sharded tables`)},withIndex(){throw new T("INTERNAL",i)},withSearchIndex(f,$){const _=(n.searchIndexes??[]).find(S=>S.name===f);if(!_)throw new T("INTERNAL",`unknown search index "${f}" on table "${t}"`);const R={definition:_,field:_.field,filters:[],hasQuery:!1,indexName:f,query:""};if($(Ht(R,t,Ut(_.language))),!R.hasQuery)throw new T("INTERNAL",`search index "${f}" on table "${t}" requires a .search(field, query) call`);return h(R)}};return y};return h(void 0)},async rank(t,n,i){const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const r=o.rankIndexes?.find(B=>B.name===n);if(!r)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(i.restrictsCounts)throw new it(t);if(await U(),!await Me(t,r))return null;const h=typeof i.row=="string"?i.row:i.row._id;if(!h)return null;const u=we(t,r.name),y=r.sortBy.map((B,K)=>Ee(K)),f=Z(["__partition__",...y]),_=(await m(s,d,e`SELECT ${f} FROM ${e.identifier(u)} WHERE ${e.identifier("__id__")} = ${h}`))[0];if(!_)return null;const R=_.__partition__,S=q(i.baseWhere,i.where);tt(S,w,t,"rank");const b=nt(r,S);if(b&&Ce(r.partitionBy??[],b)!==R)return null;const C=xn(d.name,r,y,_,h),F=await m(s,d,e`SELECT COUNT(*) AS c FROM ${e.identifier(u)} WHERE ${e.identifier("__partition__")} = ${R} AND (${C})`),k=await m(s,d,e`SELECT COUNT(*) AS c FROM ${e.identifier(u)} WHERE ${e.identifier("__partition__")} = ${R}`);return{position:Number(F[0]?.c??0)+1,total:Number(k[0]?.c??0)}},async rankPage(t,n,i={}){tt(q(i.baseWhere,i.where),w,t,"rankPage");const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const r=o.rankIndexes?.find(D=>D.name===n);if(!r)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(await U(),!await Me(t,r))return{continueCursor:null,isDone:!0,page:[]};const h=we(t,r.name),u=r.sortBy.map((D,ue)=>Ee(ue)),y=Math.max(1,Math.min(1e3,Math.floor(i.take??100))),f=q(i.baseWhere,i.where),$=nt(r,f),_=Hn(r,u),R=e.join(_.map(D=>e`${e.identifier(D.column)} ${e.raw(`${D.direction==="desc"?"DESC":"ASC"}${Tt(d,{direction:D.direction,nullable:D.nullable})}`)}`),e`, `),S=[];typeof i.partitionKey=="string"?S.push(e`${e.identifier("__partition__")} = ${i.partitionKey}`):$&&S.push(e`${e.identifier("__partition__")} = ${Ce(r.partitionBy??[],$)}`);const b=tn(i);if(b!==void 0){const D=Pn(d.name,_,b);D!==void 0&&S.push(D)}const C=Z([ye,"__partition__",...u]);let F=e`SELECT ${C} FROM ${e.identifier(h)}`;S.length>0&&(F=e`${F} WHERE ${e.join(S,e` AND `)}`),F=e`${F} ORDER BY ${R} LIMIT ${e.raw(String(y+1))}`;const k=await m(s,d,F),B=k.length>y,K=B?k.slice(0,y):k,Ie=K.map(D=>D[ye]),le=await Kn(s,d,o,t,Ie);let be=null;const z=K.at(-1);if(B&&z!==void 0){const D=[z.__partition__,...u.map(ue=>z[ue]),z[ye]];be=jn(D)}return{continueCursor:be,isDone:!B,page:le}},async replace(t,n,i,o){et("replace",n);const r=await re(t,i);if(!r)throw new T("INTERNAL",`document not found: ${t}`);const l=w.tables[r];if(!l)throw new T("INTERNAL",`document not found: ${t}`);const h=await Te(r,t);if(h===void 0)throw new T("INTERNAL",`document not found: ${t}`);const y=en(w,r,"update")||(l.aggregateIndexes??[]).length>0||(l.rankIndexes??[]).length>0?ee(l,h)??void 0:void 0,f=o?.allowExplicitId&&typeof n._creationTime=="number"?n._creationTime:void 0,$=typeof h._creationTime=="number"?h._creationTime:void 0,_=f??$??N(),R={...n,_creationTime:_,_id:t};ht(l,n,R,ie),Ne(l,R),P(r,"before","update")&&await H("before","update",{doc:{...R},id:t,op:"update",previous:y,table:r}),await Se(r),await ge(r);const S=Object.keys(l.shape),b=e.join([e`${e.identifier("_creationTime")} = ${_}`,...S.map(C=>e`${e.identifier(C)} = ${J(l,C,R[C]??null)}`)],e`, `);await ve(r,"UPDATE",b,h),await ae(r,y,R),await X(r,t,y,R),await de(r,t,R,y),await oe(r,t,"update",R),P(r,"after","update")&&await H("after","update",{doc:R,id:t,op:"update",previous:y,table:r})}};return Ge={db:x,scheduler:Ct},x};export{yi as backfillSqlSearchIndexes,_i as createSqlCtxDb,Ri as decodeGlobalRow,ti as readSqlCdcChangedTables,$i as readSqlCdcChanges,bt as readSqlCdcFloor,zn as runSqlCdcMigration,Pt as runSqlSearchMigrations,Jn as sweepSqlCdcRetention};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/sql-store",
3
- "version": "1.0.0-alpha.119",
3
+ "version": "1.0.0-alpha.120",
4
4
  "description": "Internal dialect-parameterized SQL store core for Lunora .global() backends (D1, PlanetScale)",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "@lunora/errors": "1.0.0-alpha.36",
54
- "@lunora/shard-engine": "1.0.0-alpha.68",
54
+ "@lunora/shard-engine": "1.0.0-alpha.69",
55
55
  "drizzle-orm": "^0.45.2"
56
56
  },
57
57
  "engines": {
@@ -1,7 +0,0 @@
1
- import{LunoraError as T}from"@lunora/errors";import{m as Wt,D as Ut}from"./ctx-db-search-state-CsWTh-xc.mjs";import{c as xt,r as Pt,A as Ht,C as Kt,R as jt,a as Gt,T as Vt,x as Yt,b as Xt}from"./ctx-db-search-CHYvzJMF.mjs";import{d as Ei}from"./ctx-db-search-CHYvzJMF.mjs";import{literalInList as zt,throwingScheduler as Qt,CDC_LOG_TABLE as j,cursorBelowRetainedFloor as Jt,cdcTrimmedError as Zt,assertNoExplicitUndefined as et,hasTrigger as en,runRowValidators as Ne,assertFlatPredicate as tt,mergeWhere as q,rankTableName as we,sortColumnName as Ee,resolveRankPartition as nt,encodePartitionKey as Ce,resolveRankSeekTuple as tn,RANK_TIEBREAK as ye,CountRlsUnsupportedError as it,normalizeIdStructurally as nn,assertValidClientId as on,aggregateSqlFunction as $e,softDeleteScope as _e,compileWhereSql as he,normalizeOrderKeys as rn,uniqueIndexFields as sn,equalityPinnedFields as an,buildSeekWhere as cn,decodeCursor as dn,resolveRelationPredicates as ot,resolveWith as rt,relationHooks as st,applySelect as at,encodeCursor as ln,NotFoundError as un,applyOnDelete as fn,ConflictError as Ae,normalizeCountArgument as wn,selectIndexForCount as $n,encodeAggregateKey as Y,aggregateTableName as Q,selectIndexForAggregate as _n,readAggregateValue as De,runTriggers as hn,matchesRankStaticWhere as ct,rankPivotConditionSql as pt,CURSOR_PREFIX as En,selectIndexForGroupBy as yn,tiebreakDirectionFor as pn,fanOutScalarCounts as Rn,matchesStaticWhere as Be,foldAggregateTally as dt,coerceAggregateNumber as qe,CDC_LOG_TABLE_SEQ_INDEX as Sn,NotUniqueError as gn}from"@lunora/shard-engine";import{sql as e}from"drizzle-orm";import{d as Rt,b as Tn,e as St,n as vn,c as mn}from"./value-codec-Dhem8GM5.mjs";import{runSqlGlobalTableMigrations as In,runSqlAggregateMigrations as bn,runSqlRankMigrations as Cn}from"./runSqlAggregateMigrations-Da82_-Ux.mjs";import{queryAll as m,queryRun as M,createIndexIfNotExists as An,decodeRow as ee,serializeDocumentColumn as J,columnRefSql as O,decodeRows as lt,tableColumns as gt,OCC_VERSION_COLUMN as ut,serializeColumnValue as Oe,forEachRowPaged as ft,BACKFILL_BATCH_SIZE as Fe,queryBatch as wt}from"./decodeGlobalRow-ohh6RNIm.mjs";import{decodeGlobalRow as pi}from"./decodeGlobalRow-ohh6RNIm.mjs";const Ln=(a,c)=>{if(a.size<c)return;const h=a.keys().next().value;h!==void 0&&a.delete(h)},$t=50,kn=(a,c={})=>{const h=c.maxBatch??$t,s=Number.isInteger(h)&&h>0?h:$t,p=new Map;let $=!1;const d=async(S,v)=>{const I=[...v.keys()];for(let A=0;A<I.length;A+=s){const N=I.slice(A,A+s);try{const W=await a(S,N);for(const G of N){const V=W.get(G);for(const ie of v.get(G)??[])ie.resolve(V)}}catch(W){for(const G of N)for(const V of v.get(G)??[])V.reject(W)}}},C=()=>{$=!1;const S=[...p.entries()];p.clear();for(const[v,I]of S)d(v,I).catch(()=>{})};return{async load(S,v){return new Promise((I,A)=>{let N=p.get(S);N||(N=new Map,p.set(S,N));const W=N.get(v);W?W.push({reject:A,resolve:I}):N.set(v,[{reject:A,resolve:I}]),$||($=!0,queueMicrotask(C))})}}},Mn=new Set(["_id","id"]),Pe=(a,c,h)=>a==="postgres"?e`${c} IS NOT DISTINCT FROM ${h}`:a==="mysql"?e`${c} <=> ${h}`:e`${c} IS ${h}`,Tt=(a,c)=>a.name!=="postgres"||c.nullable!==!0?"":c.direction==="desc"?" NULLS LAST":" NULLS FIRST",Nn=(a,c)=>{const h=a.map(s=>e`${O(s.field)} ${e.raw(`${s.direction==="desc"?"DESC":"ASC"}${Tt(c,s)}`)}`);if(!a.some(s=>Mn.has(s.field))){const s=pn(a);h.push(e`${O("id")} ${e.raw(s==="desc"?"DESC":"ASC")}`)}return e.join(h,e`, `)},_t=50,Dn=(a,c,h)=>{const s={...c};for(const[p,$]of gt(a)){if($.serverDefault){s[p]=$.serverDefault({auth:h});continue}s[p]===void 0&&($.defaultFn?s[p]=$.defaultFn():"defaultValue"in $&&(s[p]=$.defaultValue))}return s},ht=(a,c,h,s)=>{for(const[p,$]of gt(a)){if($.serverDefault){p in c&&(h[p]=$.serverDefault({auth:s}));continue}$.onUpdateFn&&!(p in c)&&(h[p]=$.onUpdateFn())}},Bn=128,qn=()=>{const a=new Map;return{delete:c=>{a.delete(c)},get:c=>{const h=a.get(c);if(h!==void 0)return a.delete(c),a.set(c,h),h},set:(c,h)=>{a.has(c)?a.delete(c):Ln(a,Bn),a.set(c,h)}}},On=async(a,c,h,s,p)=>{const $=p.get(s);if($!==void 0)return $;const d=Object.entries(h.tables).filter(([,S])=>S.shardMode===void 0||S.shardMode.kind==="global").map(([S])=>S),C=await Promise.all(d.map(async S=>({found:(await m(a,c,e`SELECT 1 FROM ${e.identifier(S)} WHERE ${e.identifier("id")} = ${s} LIMIT 1`)).length>0,tableName:S})));for(const S of C)if(S.found)return p.set(s,S.tableName),S.tableName},te=a=>a==null?null:Number(a),Fn=(a,c,h)=>h.map(s=>{const p={};for(const $ of c){const d=a.shape[$];p[$]=Tn(s[$]??null,d&&St(d))}return{key:p,value:te(s.value)}}),Wn=(a,c,h)=>[...a.partitionBy??[],...a.sortBy.map(p=>p.field),...a.where?Object.keys(a.where):[]].every(p=>c[p]===h[p]),vt=a=>a.length===1?a[0]:e`(${e.join(a,e` AND `)})`,Z=a=>e.join(a.map(c=>e`${e.identifier(c)}`),e`, `),ne=a=>e.join(a.map(c=>e`${c}`),e`, `),Un=(a,c,h,s,p)=>{const $=[];for(let d=0;d<h.length+1;d+=1){const C=h[d],S=c.sortBy[d],v=d<h.length&&C!==void 0&&S!==void 0,I=S?.direction==="desc"?"desc":"asc",A=v?pt(C,s[C],I,!1):e`${e.identifier(ye)} < ${p}`;if(A===void 0)continue;const N=h.slice(0,d).map(W=>Pe(a,e`${e.identifier(W)}`,s[W]));N.push(A),$.push(vt(N))}return $.length>0?e.join($,e` OR `):e`1 = 0`},xn=(a,c,h)=>{if(h.length!==c.length)return;const s=[];for(const[p,$]of c.entries()){const d=pt($.column,h[p],$.direction,!0);if(d===void 0)continue;const C=[];for(let S=0;S<p;S+=1){const v=c[S];v!==void 0&&C.push(Pe(a,e`${e.identifier(v.column)}`,h[S]))}C.push(d),s.push(vt(C))}return s.length===0?e`(1 = 0)`:e`(${e.join(s,e` OR `)})`},Pn=(a,c)=>{if(a.sortBy.length===0)throw new T("INTERNAL",`rankIndex "${a.name}" requires at least one "sortBy" column for stable pagination`);const h=[{column:"__partition__",direction:"asc",nullable:!1}];for(const[s,p]of a.sortBy.entries())h.push({column:c[s]??Ee(s),direction:p.direction,nullable:!0});return h.push({column:ye,direction:"asc",nullable:!1}),h},Hn=async(a,c,h,s,p)=>{const d=[];for(let I=0;I<p.length;I+=50)d.push(p.slice(I,I+50));const C=await Promise.all(d.map(async I=>m(a,c,e`SELECT * FROM ${e.identifier(s)} WHERE ${e.identifier("id")} IN (${ne(I)})`))),S=new Map;for(const I of C)for(const A of I)S.set(A.id,A);const v=[];for(const I of p){const A=ee(h,S.get(I));A&&v.push(A)}return v},Kn=a=>{const c=JSON.stringify(a),h=new TextEncoder().encode(c);let s="";for(const p of h)s+=String.fromCodePoint(p);return En+btoa(s)},mt=a=>{if(a===void 0)return!1;const c=St(a);return c==="any"||c==="bigint"||c==="from"||c==="union"},Ue=(a,c,h)=>{if(mt(a.shape[c]))throw new T("BAD_REQUEST",`${h}: "${c}" may hold an order-preserving key rather than a value SQL can reduce or group — declare an aggregateIndex covering this (by, field, op) so the maintained companion answers it instead (its running total is a REAL, so it stays exact only while the total is inside 2^53)`)},jn=(a,c,h,s)=>{for(const p of h)Ue(a,p,`groupBy(${c}, { by: [..."${p}"] })`);s.field!==void 0&&Ue(a,s.field,`groupBy(${c}, { agg: { op: "${s.op}", field: "${s.field}" } })`)},xe="__cdc_sweep",It="global",Gn=6e4,Vn=1e4,Yn=3e4;let Et=0;const Xn=async(a,c)=>{const{autoincrementPrimaryKey:h,key:s,real:p,text:$}=c.companionTypes;await M(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(j)} (${e.identifier("seq")} ${e.raw(h)}, ${e.identifier("ts")} ${e.raw(p)} NOT NULL, ${e.identifier("table")} ${e.raw(s)} NOT NULL, ${e.identifier("id")} ${e.raw(s)} NOT NULL, ${e.identifier("op")} ${e.raw(s)} NOT NULL, ${e.identifier("doc")} ${e.raw($)})`);const d=c.indexKeyPrefix?.("string");await An(a,c,{columns:e`${d===void 0?e`${e.identifier("table")}`:e`${e.identifier("table")}(${e.raw(String(d))})`}, ${e.identifier("seq")}`,name:Sn,table:j,unique:!1}),await M(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(xe)} (${e.identifier("id")} ${e.raw(s)} NOT NULL PRIMARY KEY, ${e.identifier("lease_until")} ${e.raw(p)} NOT NULL)`),await M(a,c,e`INSERT INTO ${e.identifier(xe)} (${e.identifier("id")}, ${e.identifier("lease_until")}) VALUES (${It}, ${0}) ${c.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${e.identifier("id")} = ${e.identifier("id")}`:e`ON CONFLICT(${e.identifier("id")}) DO NOTHING`}`)},zn=async(a,c,h)=>{const s=e`UPDATE ${e.identifier(xe)} SET ${e.identifier("lease_until")} = ${h+Gn} WHERE ${e.identifier("id")} = ${It} AND ${e.identifier("lease_until")} <= ${h}`;if(c.supportsReturning)return(await m(a,c,e`${s} RETURNING ${e.identifier("id")}`)).length>0;const p=await M(a,c,s);return(c.affectedRows?c.affectedRows(p??{rowsAffected:0}):0)>0},Qn=async(a,c,h,s)=>{if(!await zn(a,c,s))return;const p=s-h;await M(a,c,e`
2
- DELETE FROM ${e.identifier(j)} WHERE ${e.identifier("seq")} IN (
3
- SELECT ${e.identifier("seq")} FROM (
4
- SELECT ${e.identifier("seq")} FROM ${e.identifier(j)} WHERE ${e.identifier("ts")} <= ${p} ORDER BY ${e.identifier("seq")} ASC LIMIT ${e.raw(String(Vn))}
5
- ) AS ${e.identifier("expired")}
6
- )
7
- `)},bt=async(a,c)=>{const h=await m(a,c,e`SELECT MIN(${e.identifier("seq")}) AS ${e.identifier("seq")} FROM ${e.identifier(j)}`),s=Number(h[0]?.seq??Number.NaN);return Number.isFinite(s)?s:void 0},Jn=a=>JSON.stringify(vn(a)?mn(a):a),Zn=async(a,c,h,s,p,$,d)=>{await M(a,d,e`INSERT INTO ${e.identifier(j)} (${e.identifier("ts")}, ${e.identifier("table")}, ${e.identifier("id")}, ${e.identifier("op")}, ${e.identifier("doc")}) VALUES (${c}, ${h}, ${s}, ${p}, ${$===void 0?null:Jn($)})`)},wi=async(a,c,h)=>{const s=c.sinceSeq??0,p=Math.max(1,Math.min(c.limit??1e3,1e4)),$=await bt(a,h);if($!==void 0&&Jt($,s))throw Zt($,s,"global");const C=(await m(a,h,e`SELECT seq, ts, ${e.identifier("table")}, id, op, doc FROM ${e.identifier(j)} WHERE seq > ${s} ORDER BY seq ASC LIMIT ${e.raw(String(p))}`)).map(S=>{const{doc:v}=S,I={id:String(S.id),op:String(S.op),seq:Number(S.seq),table:String(S.table),ts:Number(S.ts)};return typeof v=="string"?{...I,doc:Rt(JSON.parse(v))}:I});return{changes:C,cursor:C.at(-1)?.seq??s}},ei=async(a,c,h,s={})=>{if(s.cursorOnly){const S=await m(a,h,e`SELECT MAX(seq) AS seq FROM ${e.identifier(j)}`),v=Number(S[0]?.seq??c);return{cursor:Number.isFinite(v)?v:c,tables:[]}}const p=await m(a,h,e`SELECT ${e.identifier("table")} AS ${e.identifier("table")}, MAX(seq) AS seq FROM ${e.identifier(j)} WHERE seq > ${c} GROUP BY ${e.identifier("table")}`);let $=c;const d=[];for(const S of p){const v=Number(S.seq);Number.isFinite(v)&&v>$&&($=v),d.push(String(S.table))}const C=s.retained===!0?await bt(a,h):void 0;return{cursor:$,tables:d,...C===void 0?{}:{floor:C}}},ti=/string or blob too big/iu,ni=/row is too big/iu,ii=(a,c)=>{const{code:h,errno:s}=c,p=c instanceof Error?c.message:"";switch(a.name){case"mysql":return s===1118||h==="ER_TOO_BIG_ROWSIZE"?"InnoDB's per-row ceiling — roughly 8 KB, half a 16 KB page, for the part of the row stored inline":void 0;case"postgres":return ni.test(p)?"the 8 KB heap page a tuple must fit once its wide columns have been TOASTed out":void 0;default:return ti.test(p)?"the storage engine's per-row ceiling (2 MB on D1)":void 0}},oi=(a,c,h)=>{const s=ii(a,c);if(s!==void 0)throw new T("PAYLOAD_TOO_LARGE",`document is too large to store in "${h}": a single row cannot exceed ${s}. The limit is on the STORED bytes, which are UTF-8. Keep the payload in R2 (ctx.storage) and store a reference on the row.`)},yt=(a,c,h)=>{throw a.isUniqueViolation(c)?new Ae(`unique constraint violation on "${h}"`,"unique"):(oi(a,c,h),c)},We=new WeakMap,$i=a=>{const{crossShardCounter:c,crossShardReader:h,exec:s,maxRelationKeys:p,schema:$}=a,{dialect:d}=a,C={fieldRef:O,...d.name==="mysql"?{containsExpr:(t,n)=>e`LOCATE(${n}, ${t}) > 0`}:{},...d.name==="postgres"?{containsExpr:(t,n)=>e`strpos(${t}, ${n}) > 0`}:{},...d.name==="sqlite"?{}:{inList:zt}},S=new WeakMap,v=t=>{const n=S.get(t);if(n!==void 0)return n;const i={...C,serialize:(o,r)=>J(t,r,o)};return S.set(t,i),i},I=(t,n)=>Pe(d.name,t,n),A=t=>{const n=Z(t.columns),i=ne(t.values),o=f=>d.name==="mysql"?e`VALUES(${e.identifier(f)})`:e`excluded.${e.identifier(f)}`,r=f=>d.name==="postgres"?e`${e.identifier(t.table)}.${e.identifier(f)}`:e`${e.identifier(f)}`,u=e.join(Object.entries(t.set(o,r)).map(([f,y])=>e`${e.identifier(f)} = ${y}`),e`, `),E=d.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${u}`:e`ON CONFLICT(${e.identifier(t.conflictKey)}) DO UPDATE SET ${u}`;return e`INSERT INTO ${e.identifier(t.table)} (${n}) VALUES (${i}) ${E}`},N=a.clock??(()=>Date.now()),W=a.idGenerator??(()=>crypto.randomUUID()),G=a.cdc??!1,V=typeof a.cdcRetentionMs=="number"&&Number.isFinite(a.cdcRetentionMs)&&a.cdcRetentionMs>0?a.cdcRetentionMs:void 0,ie=a.auth??{identity:null,userId:null},oe=async(t,n,i,o)=>{if(!G||(await Zn(s,N(),t,n,i,o,d),V===void 0))return;const r=N();if(!(r-Et<Yn)){Et=r;try{await Qn(s,d,V,r)}catch{}}},Ct=a.scheduler??Qt,Le=qn();let pe=0;const ke=a.provisionScope??{},U=async()=>{const t=We.get(ke);if(t)return t;const n=(async()=>{await In(s,$,d),await bn(s,$,d),await Cn(s,$,d),await Wt(s,d),await Pt(s,$,d),G&&await Xn(s,d)})().catch(i=>{throw We.delete(ke),i});return We.set(ke,n),n},re=async(t,n)=>{await U();const i=await On(s,d,$,t,Le);if(!(n!==void 0&&i!==n))return i},se=new Map,He=async(t,n)=>{const i=Q(t,n);return(await m(s,d,d.tableExists(i))).length>0},Re=async(t,n)=>{const i=`${t}::${n.name}`,o=se.get(i);if(o!==void 0)return o;if(!await He(t,n.name))return se.set(i,!1),!1;const u=$.tables[t];if(!u)return se.set(i,!1),!1;const E=n.by??[],f=new Map;await ft(s,d,u,t,w=>{if(n.where&&!Be(w,n.where))return;const _=Y(E,w);dt(f,_,n,w)});const y=Q(t,n.name);await M(s,d,e`DELETE FROM ${e.identifier(y)}`);const l=[];for(const[w,_]of f)l.push(e`INSERT INTO ${e.identifier(y)} (${e.identifier("__key__")}, ${e.identifier("__value__")}, ${e.identifier("__count__")}) VALUES (${w}, ${_.value}, ${_.count})`);return await wt(s,d,l),se.set(i,!0),!0},At=async(t,n,i,o)=>{const r=new Map;let u,E=!0;for(;E;){const f=u===void 0?o:[...o,e`${e.identifier("id")} > ${u}`],y=f.length>0?e` WHERE ${e.join(f,e` AND `)}`:e``,l=await m(s,d,e`SELECT ${e.identifier("id")}, ${O(i.field??"")} FROM ${e.identifier(n)}${y} ORDER BY ${e.identifier("id")} ASC LIMIT ${e.raw(String(Fe))}`);for(const w of lt(t,l))dt(r,"",i,w);u=l.at(-1)?.id,E=l.length===Fe}return r.get("")?.value??null},Lt=async(t,n,i)=>{const o=n.field??"",r=[],u=$.tables[t],E=(w,_)=>u?J(u,w,_):Oe(_);for(const w of n.by??[]){const _=E(w,i[w]??null);r.push(_===null?e`${O(w)} IS NULL`:e`${O(w)} = ${_}`)}for(const[w,_]of Object.entries(n.where??{})){const g=_!==null&&typeof _=="object"&&!Array.isArray(_)?_.eq:_,R=E(w,g);r.push(R===null?e`${O(w)} IS NULL`:e`${O(w)} = ${R}`)}const f=r.length>0?e` WHERE ${e.join(r,e` AND `)}`:e``;if(u&&mt(u.shape[o]))return At(u,t,n,r);const y=$e(n.op),l=await m(s,d,e`SELECT ${e.raw(y)}(${O(o)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}${f}`);return te(l[0]?.value)},kt=async(t,n)=>{await M(s,d,e`DELETE FROM ${e.identifier(t)} WHERE ${e.identifier("__key__")} = ${n} AND ${e.identifier("__count__")} <= 0`)},Mt=async(t,n,i,o)=>{const r=Q(t,n.name),{op:u}=n,E=n.field??"",f=i&&(!n.where||Be(i,n.where))?i:void 0,y=o&&(!n.where||Be(o,n.where))?o:void 0;if(!(!f&&!y)){if(u==="count"||u==="sum"||u==="avg"){const l=new Set;for(const[w,_]of[[f,-1],[y,1]]){if(!w)continue;const g=u==="count"?1:qe(w[E]);if(g===void 0)continue;const R=Y(n.by??[],w);l.add(R),await M(s,d,A({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(b,L)=>({__count__:e`${L("__count__")} + ${b("__count__")}`,__value__:u==="count"?e`${L("__value__")} + ${b("__value__")}`:e`COALESCE(${L("__value__")}, 0) + ${b("__value__")}`}),table:r,values:[R,_*g,_]}))}for(const w of l)await kt(r,w);return}if(f){const l=Y(n.by??[],f),w=qe(f[E]),g=(await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(r)} WHERE ${e.identifier("__key__")} = ${l}`))[0],R=te(g?.value),b=(g?.count??0)-1;if(b<=0)await M(s,d,e`DELETE FROM ${e.identifier(r)} WHERE ${e.identifier("__key__")} = ${l}`);else if(g&&w!==void 0&&R!==null&&w===R){const L=await Lt(t,n,f);await M(s,d,e`UPDATE ${e.identifier(r)} SET ${e.identifier("__value__")} = ${L}, ${e.identifier("__count__")} = ${b} WHERE ${e.identifier("__key__")} = ${l}`)}else await M(s,d,e`UPDATE ${e.identifier(r)} SET ${e.identifier("__count__")} = ${e.identifier("__count__")} - 1 WHERE ${e.identifier("__key__")} = ${l}`)}if(y){const l=Y(n.by??[],y),w=qe(y[E]);if(w===void 0)await M(s,d,A({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(_,g)=>({__count__:e`${g("__count__")} + 1`}),table:r,values:[l,null,1]}));else{const _=u==="min"?"MIN":"MAX";await M(s,d,A({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(g,R)=>({__count__:e`${R("__count__")} + 1`,__value__:e`${e.raw(_)}(COALESCE(${R("__value__")}, ${g("__value__")}), ${g("__value__")})`}),table:r,values:[l,w,1]}))}}}},Se=async t=>{const n=$.tables[t]?.aggregateIndexes;if(!(!n||n.length===0))for(const i of n)await Re(t,i)},ae=async(t,n,i)=>{const o=$.tables[t]?.aggregateIndexes;if(!(!o||o.length===0))for(const r of o){const u=`${t}::${r.name}`;(se.get(u)??await He(t,r.name))&&await Mt(t,r,n,i)}},ce=new Map,Ke=async(t,n)=>{const i=we(t,n);return(await m(s,d,d.tableExists(i))).length>0},Me=async(t,n)=>{const i=`${t}::rank::${n.name}`,o=ce.get(i);if(o!==void 0)return o;if(!await Ke(t,n.name))return ce.set(i,!1),!1;const u=$.tables[t];if(!u)return ce.set(i,!1),!1;const E=we(t,n.name);await M(s,d,e`DELETE FROM ${e.identifier(E)}`);const f=n.sortBy.map((_,g)=>Ee(g)),y=Z(["__id__","__partition__",...f]);let l=[];const w=async()=>{if(l.length===0)return;const _=l;l=[],await wt(s,d,_)};return await ft(s,d,u,t,async _=>{if(n.where&&!ct(_,n.where))return;const g=Ce(n.partitionBy??[],_),R=n.sortBy.map(b=>Oe(_[b.field]??null));l.push(e`INSERT INTO ${e.identifier(E)} (${y}) VALUES (${ne([_._id,g,...R])})`),l.length>=Fe&&await w()}),await w(),ce.set(i,!0),!0},ge=async t=>{const n=$.tables[t]?.rankIndexes;if(!(!n||n.length===0))for(const i of n)await Me(t,i)},X=async(t,n,i,o)=>{const r=$.tables[t],u=r?.rankIndexes;if(!u||u.length===0)return;const E=r?.softDeleteMode?.field,f=E===void 0||o===void 0?void 0:o[E],y=f!=null;for(const l of u){if(i!==void 0&&o!==void 0&&Wn(l,i,o))continue;const w=`${t}::rank::${l.name}`;if(!(ce.get(w)??await Ke(t,l.name)))continue;const R=we(t,l.name);if(i&&await M(s,d,e`DELETE FROM ${e.identifier(R)} WHERE ${e.identifier("__id__")} = ${n}`),o&&!y){if(l.where&&!ct(o,l.where))continue;const b=l.sortBy.map((B,K)=>Ee(K)),L=Z(["__id__","__partition__",...b]),F=Ce(l.partitionBy??[],o),k=l.sortBy.map(B=>Oe(o[B.field]??null));await M(s,d,e`INSERT INTO ${e.identifier(R)} (${L}) VALUES (${ne([n,F,...k])})`)}}},de=xt({dialect:d,exec:s,schema:$}),je=new Set;for(const[t,n]of Object.entries($.tables))for(const i of Object.values(n.triggerMap??{}))je.add(`${t} ${i.timing} ${i.op}`);const P=(t,n,i)=>je.has(`${t} ${n} ${i}`);let Ge;const H=async(t,n,i)=>{if(pe+=1,pe>_t)throw pe-=1,new Ae(`trigger recursion exceeded ${String(_t)} levels on "${i.table}" — check for a self-triggering write`,"trigger");try{await hn({ctx:Ge,event:i,op:n,schema:$,tableName:i.table,timing:t})}finally{pe-=1}},Nt=async(t,n)=>{try{await M(s,d,n)}catch(i){yt(d,i,t)}},Te=async(t,n)=>(await m(s,d,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} = ${n}`))[0],ve=async(t,n,i,o)=>{if(o===void 0)return;const r=e`${e.identifier(ut)}`,u=e.join([I(e`${e.identifier("id")}`,o.id),I(r,o[ut]??null)],e` AND `),E=n==="UPDATE"?e`UPDATE ${e.identifier(t)} SET ${i}, ${r} = COALESCE(${r}, 0) + 1 WHERE ${u}`:e`DELETE FROM ${e.identifier(t)} WHERE ${u}`,f=()=>{throw new Ae(`optimistic concurrency conflict on "${t}" — the row changed during this mutation; refetch and retry`,"occ")};try{if(d.supportsReturning)(await m(s,d,e`${E} RETURNING ${e.identifier("id")}`)).length===0&&f();else{const y=await M(s,d,E);(d.affectedRows?d.affectedRows(y??{rowsAffected:0}):0)===0&&f()}}catch(y){yt(d,y,t)}},Dt=(t,n,i,o)=>{const r=Object.keys(t.shape);return{columns:["id","_creationTime",...r],values:[n,i,...r.map(u=>J(t,u,o[u]??null))]}},Bt=async(t,n,i,o)=>{const r=yn(n,i.op,i.field,o.by,o.where);if(!r||!await Re(t,r.index))return;const E=Q(t,r.index.name),f=Object.keys(r.partial);if(f.length===(r.index.by??[]).length&&f.length>0){const l=Y(r.index.by??[],r.partial),w=await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(E)} WHERE ${e.identifier("__key__")} = ${l}`);if(w.length===0)return[];const _=w[0];return[{key:{...r.partial},value:De(i.op,{count:_.count,value:te(_.value)})}]}return f.length>0?void 0:(await m(s,d,e`SELECT ${e.identifier("__key__")} AS ${e.identifier("key")}, ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(E)}`)).map(l=>{const w=l;return{key:Rt(JSON.parse(w.key)),value:De(i.op,{count:w.count,value:te(w.value)})}})},Ve=t=>{const n=$.tables[t]?.shardMode?.kind;return n==="shardBy"||n==="root"},Ye=t=>{throw new T("INTERNAL",`cross-backend relation: a global table cannot load the shard-local relation '${t}' (it spans every shard) — wire a cross-shard reader to support it`)},qt=(t,n)=>{if(n?.relationMask!==void 0&&n.with!==void 0&&Object.keys(n.with).length>0)throw new T("MASK_UNSUPPORTED",`masking cannot follow a nested \`with\` across the cross-shard relation hop to "${t}" — the nested rows are hydrated on the serving shard, where the mask policy does not exist. Read the nested relation at its own (masked) call site instead.`);const i={};if(n?.relationBaseWhere)for(const o of Object.keys($.tables)){const r=n.relationBaseWhere(o);r!==void 0&&(i[o]=r)}return{orderBy:n?.orderBy,relationPolicies:i,where:q(n?.baseWhere,n?.where),with:n?.with}},Xe=(t,n)=>Ve(t)?h?h(t,qt(t,n)):Ye(t):x.findMany(t,n),me=(t,n,i)=>ot(t,{fetcher:Xe,maxRelationKeys:p,relationBaseWhere:i,schema:$,tableName:n}),Ot=kn(async(t,n)=>{const i=await m(s,d,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} IN (${ne(n)})`),o=new Map;for(const r of i){const{id:u}=r;typeof u=="string"&&o.set(u,r)}return o}),x={async cdcChangedTables(t,n){if(G)try{return await U(),await ei(s,t,d,{...n,retained:V!==void 0})}catch{return}},async aggregate(t,n){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);if(await U(),$e(n.op),n.op==="count")return x.count(t,{baseWhere:n.baseWhere,relationBaseWhere:n.relationBaseWhere,restrictsCounts:n.restrictsCounts,where:n.where});if(!n.field)throw new T("INTERNAL",`aggregate(${t}, { op: "${n.op}" }): "field" is required for non-count reducers`);const o=_e(i.softDeleteMode,void 0),r=q(q(n.baseWhere,n.where),o),u=await me(r,t,n.relationBaseWhere),E=u!==r;if(i.aggregateIndexes&&!n.baseWhere&&!E&&!o){const g=_n(i.aggregateIndexes,n.op,n.field,n.where);if(g&&await Re(t,g.index)){const b=Y(g.index.by??[],g.key),L=Q(t,g.index.name),k=(await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(L)} WHERE ${e.identifier("__key__")} = ${b}`))[0];return De(n.op,k===void 0?void 0:{count:k.count,value:te(k.value)})}}Ue(i,n.field,`aggregate(${t}, { op: "${n.op}", field: "${n.field}" })`);const f=he(u,v(i)),y=e.raw($e(n.op)),l=e`SELECT ${y}(${O(n.field)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}`,_=(await m(s,d,f?e`${l} WHERE ${f}`:l))[0]?.value;return _==null?null:Number(_)},async count(t,n){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=wn(n);if(o.restrictsCounts)throw new it(t);const r=_e(i.softDeleteMode,void 0),u=q(q(o.baseWhere,o.where),r),E=await me(u,t,o.relationBaseWhere),f=E!==u;if(i.aggregateIndexes&&!o.baseWhere&&!f&&!r){const _=$n(i.aggregateIndexes,o.where);if(_&&await Re(t,_.index)){const R=Y(_.index.by??[],_.key),b=Q(t,_.index.name),L=await m(s,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")} FROM ${e.identifier(b)} WHERE ${e.identifier("__key__")} = ${R}`);return Number(L[0]?.value??0)}}const y=he(E,v(i)),l=e`SELECT COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(t)}`,w=await m(s,d,y?e`${l} WHERE ${y}`:l);return Number(w[0]?.count??0)},async delete(t,n,i){const o=await re(t,n);if(!o)return;const r=$.tables[o];if(!r)return;const u=await Te(o,t),E=ee(r,u),f=i?.hard===!0,y=!f&&r.softDeleteMode?r.softDeleteMode.field:void 0;if(!(y&&(!E||E[y]!==null&&E[y]!==void 0))){if(P(o,"before","delete")&&await H("before","delete",{id:t,op:"delete",previous:E??void 0,table:o}),await fn({deletedId:t,deletedReference:l=>E?.[l],findHolders:async(l,w,_)=>{if($.tables[l]?.shardMode?.kind==="shardBy")throw new T("INTERNAL",`cross-backend cascade from global '${o}' into shardBy '${l}' is not supported — would require Query Coordinator fan-out across shards`);return(await x.findMany(l,{includeDeleted:f,where:{[w]:_}})).page},onCascade:(l,w)=>x.delete(w,void 0,i),onRestrict:l=>{throw new Ae(l,"restrict")},onSetNull:(l,w,_)=>x.patch(w,{[_]:null}),schema:$,tableName:o}),await Se(o),await ge(o),y&&E){const l={...E,[y]:N()},w=e.join(Object.keys(r.shape).map(_=>e`${e.identifier(_)} = ${J(r,_,l[_]??null)}`),e`, `);await ve(o,"UPDATE",w,u),await ae(o,E,l),await X(o,t,E,void 0),await de(o,t,l,E),await oe(o,t,"update",l),P(o,"after","delete")&&await H("after","delete",{id:t,op:"delete",previous:E,table:o});return}await ve(o,"DELETE",void 0,u),Le.delete(t),await ae(o,E??void 0,void 0),await X(o,t,E??void 0,void 0),await de(o,t,void 0),await oe(o,t,"delete"),P(o,"after","delete")&&await H("after","delete",{id:t,op:"delete",previous:E??void 0,table:o})}},async findFirst(t,n={}){return(await x.findMany(t,{...n,limit:1})).page[0]??null},async findFirstOrThrow(t,n={}){const i=await x.findFirst(t,n);if(i===null)throw new un(`findFirstOrThrow: no "${t}" document matched`);return i},async findMany(t,n={}){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=rn(n.orderBy,i.shape,{pinned:an(n.where),uniqueBy:sn(i.indexes,i.shape)}),r=n.cursor?cn(o,dn(n.cursor)):void 0,u=Xe,E=async(k,B,K,Ie)=>{if(Ve(k))return c?Rn(c,k,B,K,Ie):Ye(k);const le=$.tables[k];if(!le)throw new T("INTERNAL",`unknown table: ${k}`);const be=_e(le.softDeleteMode,void 0),z={[B]:{in:K}},D=q(q(z,Ie),be),ue=await me(D,k,void 0),ze=he(ue,v(le)),Qe=O(B);let fe=e`SELECT ${Qe} AS __fk__, COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(k)}`;ze&&(fe=e`${fe} WHERE ${ze}`),fe=e`${fe} GROUP BY ${Qe}`;const Ft=await m(s,d,fe),Je=new Map;for(const Ze of Ft)Je.set(Ze.__fk__,Number(Ze.count??0));return Je};let f=q(n.baseWhere,n.where);f=q(f,_e(i.softDeleteMode,n.includeDeleted)),f=await ot(f,{fetcher:u,maxRelationKeys:p,relationBaseWhere:n.relationBaseWhere,schema:$,tableName:t}),r&&(f=f?{AND:[f,r]}:r);const y=he(f,v(i)),l=Nn(o,d);let w=e`SELECT * FROM ${e.identifier(t)}`;y&&(w=e`${w} WHERE ${y}`),w=e`${w} ORDER BY ${l}`;const _=typeof n.limit=="number"?Math.max(0,Math.floor(n.limit)):void 0;_!==void 0&&(w=e`${w} LIMIT ${e.raw(String(_+1))}`);const g=await m(s,d,w),R=lt(i,g);if(_===void 0)return n.with&&await rt({fetcher:u,groupedCounter:E,parents:R,...st(n),schema:$,tableName:t,with:n.with}),{continueCursor:null,isDone:!0,page:at(R,n.select,n.with)};const b=R.length>_,L=b?R.slice(0,_):R,F=L.at(-1);return n.with&&await rt({fetcher:u,groupedCounter:E,parents:L,...st(n),schema:$,tableName:t,with:n.with}),{continueCursor:b&&F?ln(F,o):null,isDone:!b,page:at(L,n.select,n.with)}},async get(t,n){const i=await re(t,n);if(!i)return null;const o=$.tables[i];return o?ee(o,await Ot.load(i,t)):null},async groupBy(t,n){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=n.agg??{op:"count"};if($e(o.op),o.op!=="count"&&!o.field)throw new T("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);const r=_e(i.softDeleteMode,void 0),u=q(q(n.baseWhere,n.where),r),E=await me(u,t,n.relationBaseWhere),f=E!==u;if(i.aggregateIndexes&&!n.baseWhere&&!f&&!r){const R=await Bt(t,i.aggregateIndexes,o,n);if(R!==void 0)return R}jn(i,t,n.by,o);const y=he(E,v(i)),l=n.by.map(R=>e`${O(R)} AS ${e.identifier(R)}`);if(o.op==="count")l.push(e`COUNT(*) AS ${e.identifier("value")}`);else{if(!o.field)throw new T("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);l.push(e`${e.raw($e(o.op))}(${O(o.field)}) AS ${e.identifier("value")}`)}const w=e.join(n.by.map(R=>O(R)),e`, `);let _=e`SELECT ${e.join(l,e`, `)} FROM ${e.identifier(t)}`;y&&(_=e`${_} WHERE ${y}`),_=e`${_} GROUP BY ${w}`;const g=await m(s,d,_);return Fn(i,n.by,g)},async insert(t,n,i){const o=$.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);await U();const r=Dn(o,n,ie);Ne(o,r);let u,E=!0;i?.clientId!==void 0?(on(i.clientId),u=i.clientId):i?.allowExplicitId&&typeof r._id=="string"?u=r._id:(u=W(),E=!1);const f=i?.allowExplicitId&&typeof r._creationTime=="number"?r._creationTime:N(),y={...r,_creationTime:f,_id:u};P(t,"before","insert")&&await H("before","insert",{doc:{...y},id:u,op:"insert",table:t}),await Se(t),await ge(t);const{columns:l,values:w}=Dt(o,u,f,r);return await Nt(t,e`INSERT INTO ${e.identifier(t)} (${Z(l)}) VALUES (${ne(w)})`),E&&Le.set(u,t),await ae(t,void 0,y),await X(t,u,void 0,y),await de(t,u,y),await oe(t,u,"insert",y),P(t,"after","insert")&&await H("after","insert",{doc:y,id:u,op:"insert",table:t}),u},normalizeId(t,n){return nn($,t,n)},async patch(t,n,i){et("patch",n);const o=await re(t,i);if(!o)throw new T("INTERNAL",`document not found: ${t}`);const r=$.tables[o];if(!r)throw new T("INTERNAL",`document not found: ${t}`);const u=await Te(o,t),E=ee(r,u);if(!E)throw new T("INTERNAL",`document not found: ${t}`);const f={...E,...n,_id:t};ht(r,n,f,ie),Ne(r,f,!0),P(o,"before","update")&&await H("before","update",{doc:{...f},id:t,op:"update",previous:E,table:o}),await Se(o),await ge(o);const y=Object.keys(r.shape),l=e.join(y.map(w=>e`${e.identifier(w)} = ${J(r,w,f[w]??null)}`),e`, `);await ve(o,"UPDATE",l,u),await ae(o,E,f),await X(o,t,E,f),await de(o,t,f,E),await oe(o,t,"update",f),P(o,"after","update")&&await H("after","update",{doc:f,id:t,op:"update",previous:E,table:o})},async restore(t,n){const i=await re(t,n);if(!i)throw new T("INTERNAL",`document not found: ${t}`);const o=$.tables[i],r=o?.softDeleteMode?.field;if(!o||!r)throw new T("INTERNAL",`ctx.db.restore: table "${i}" is not a .softDelete() table`);const u=await Te(i,t),E=u?.[r]!==null&&u?.[r]!==void 0;if(await x.patch(t,{[r]:null},n),E){const f=ee(o,u);f!==null&&await X(i,t,void 0,{...f,[r]:null})}},query(t){const n=$.tables[t];if(!n)throw new T("INTERNAL",`unknown table: ${t}`);const i="the legacy query()/withIndex() reader is not available on the D1 (global) backend; use findMany",o=[],r=f=>o.every(y=>y(f)),u=async(f,y)=>{await U();const l=o.length>0,w=await Gt(s,d,n,t,f,Vt(l?void 0:y));if(!l)return y===void 0&&Yt(w),w;const _=w.filter(g=>r(g));return y===void 0?_:_.slice(0,y)},E=f=>{const y={async*[Symbol.asyncIterator](){if(!f)throw new T("INTERNAL",i);yield*await u(f,void 0)},async collect(){if(!f)throw new T("INTERNAL",i);return u(f,void 0)},async collectWithScores(){throw new T("GLOBAL_SEARCH_SCORES_UNSUPPORTED",`collectWithScores() is not supported on \`.global()\` tables (table "${t}") — relevance scores are not yet surfaced on this backend; use .collect() instead`)},filter(l){return o.push(l),y},async first(){if(!f)throw new T("INTERNAL",i);return(await u(f,1))[0]??null},order(){return y},async paginate(l){if(!f)throw new T("INTERNAL",i);const w=Kt(l);return jt(await u(f,Xt(w)),w)},async take(l){if(!f)throw new T("INTERNAL",i);return u(f,l)},async unique(){if(!f)throw new T("INTERNAL",i);const l=await u(f,2);if(l.length>1)throw new gn(`unique() on table "${t}" matched ${String(l.length)} documents; expected at most one`);return l[0]??null},withGeoIndex(){throw new T("INTERNAL",`geo indexes are not supported on \`.global()\` tables (table "${t}") — geospatial queries run only on sharded tables`)},withIndex(){throw new T("INTERNAL",i)},withSearchIndex(l,w){const _=(n.searchIndexes??[]).find(R=>R.name===l);if(!_)throw new T("INTERNAL",`unknown search index "${l}" on table "${t}"`);const g={definition:_,field:_.field,filters:[],hasQuery:!1,indexName:l,query:""};if(w(Ht(g,t,Ut(_.language))),!g.hasQuery)throw new T("INTERNAL",`search index "${l}" on table "${t}" requires a .search(field, query) call`);return E(g)}};return y};return E(void 0)},async rank(t,n,i){const o=$.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const r=o.rankIndexes?.find(B=>B.name===n);if(!r)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(i.restrictsCounts)throw new it(t);if(await U(),!await Me(t,r))return null;const E=typeof i.row=="string"?i.row:i.row._id;if(!E)return null;const f=we(t,r.name),y=r.sortBy.map((B,K)=>Ee(K)),l=Z(["__partition__",...y]),_=(await m(s,d,e`SELECT ${l} FROM ${e.identifier(f)} WHERE ${e.identifier("__id__")} = ${E}`))[0];if(!_)return null;const g=_.__partition__,R=q(i.baseWhere,i.where);tt(R,$,t,"rank");const b=nt(r,R);if(b&&Ce(r.partitionBy??[],b)!==g)return null;const L=Un(d.name,r,y,_,E),F=await m(s,d,e`SELECT COUNT(*) AS c FROM ${e.identifier(f)} WHERE ${e.identifier("__partition__")} = ${g} AND (${L})`),k=await m(s,d,e`SELECT COUNT(*) AS c FROM ${e.identifier(f)} WHERE ${e.identifier("__partition__")} = ${g}`);return{position:Number(F[0]?.c??0)+1,total:Number(k[0]?.c??0)}},async rankPage(t,n,i={}){tt(q(i.baseWhere,i.where),$,t,"rankPage");const o=$.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const r=o.rankIndexes?.find(D=>D.name===n);if(!r)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(await U(),!await Me(t,r))return{continueCursor:null,isDone:!0,page:[]};const E=we(t,r.name),f=r.sortBy.map((D,ue)=>Ee(ue)),y=Math.max(1,Math.min(1e3,Math.floor(i.take??100))),l=q(i.baseWhere,i.where),w=nt(r,l),_=Pn(r,f),g=e.join(_.map(D=>e`${e.identifier(D.column)} ${e.raw(`${D.direction==="desc"?"DESC":"ASC"}${Tt(d,{direction:D.direction,nullable:D.nullable})}`)}`),e`, `),R=[];typeof i.partitionKey=="string"?R.push(e`${e.identifier("__partition__")} = ${i.partitionKey}`):w&&R.push(e`${e.identifier("__partition__")} = ${Ce(r.partitionBy??[],w)}`);const b=tn(i);if(b!==void 0){const D=xn(d.name,_,b);D!==void 0&&R.push(D)}const L=Z([ye,"__partition__",...f]);let F=e`SELECT ${L} FROM ${e.identifier(E)}`;R.length>0&&(F=e`${F} WHERE ${e.join(R,e` AND `)}`),F=e`${F} ORDER BY ${g} LIMIT ${e.raw(String(y+1))}`;const k=await m(s,d,F),B=k.length>y,K=B?k.slice(0,y):k,Ie=K.map(D=>D[ye]),le=await Hn(s,d,o,t,Ie);let be=null;const z=K.at(-1);if(B&&z!==void 0){const D=[z.__partition__,...f.map(ue=>z[ue]),z[ye]];be=Kn(D)}return{continueCursor:be,isDone:!B,page:le}},async replace(t,n,i,o){et("replace",n);const r=await re(t,i);if(!r)throw new T("INTERNAL",`document not found: ${t}`);const u=$.tables[r];if(!u)throw new T("INTERNAL",`document not found: ${t}`);const E=await Te(r,t);if(E===void 0)throw new T("INTERNAL",`document not found: ${t}`);const y=en($,r,"update")||(u.aggregateIndexes??[]).length>0||(u.rankIndexes??[]).length>0?ee(u,E)??void 0:void 0,l=o?.allowExplicitId&&typeof n._creationTime=="number"?n._creationTime:N(),w={...n,_creationTime:l,_id:t};ht(u,n,w,ie),Ne(u,w),P(r,"before","update")&&await H("before","update",{doc:{...w},id:t,op:"update",previous:y,table:r}),await Se(r),await ge(r);const _=Object.keys(u.shape),g=e.join([e`${e.identifier("_creationTime")} = ${l}`,..._.map(R=>e`${e.identifier(R)} = ${J(u,R,w[R]??null)}`)],e`, `);await ve(r,"UPDATE",g,E),await ae(r,y,w),await X(r,t,y,w),await de(r,t,w,y),await oe(r,t,"update",w),P(r,"after","update")&&await H("after","update",{doc:w,id:t,op:"update",previous:y,table:r})}};return Ge={db:x,scheduler:Ct},x};export{Ei as backfillSqlSearchIndexes,$i as createSqlCtxDb,pi as decodeGlobalRow,ei as readSqlCdcChangedTables,wi as readSqlCdcChanges,bt as readSqlCdcFloor,Xn as runSqlCdcMigration,Pt as runSqlSearchMigrations,Qn as sweepSqlCdcRetention};