@lunora/sql-store 1.0.0-alpha.130 → 1.0.0-alpha.131
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 +1 -1
- package/dist/packem_shared/OCC_VERSION_COLUMN-ByC64Jgx.mjs +1 -0
- package/dist/packem_shared/{SEARCH_STATE_TABLE-DvsS7EJ8.mjs → SEARCH_STATE_TABLE-BvrfudML.mjs} +1 -1
- package/dist/packem_shared/backfillSqlSearchIndexes-DpKDY6wV.mjs +1 -0
- package/dist/packem_shared/{createSqlCtxDb-BBn2L287.mjs → createSqlCtxDb-vhTttOe-.mjs} +2 -2
- package/dist/packem_shared/{ctx-db-search-CkWBiP2e.mjs → ctx-db-search-Cus9tweO.mjs} +1 -1
- package/dist/packem_shared/{ctx-db-search-state-Xa6UmA3s.mjs → ctx-db-search-state-DHkLBltM.mjs} +1 -1
- package/dist/packem_shared/decodeGlobalRow-DtDkyN_O.mjs +1 -0
- package/dist/packem_shared/runSqlAggregateMigrations-wYVDSorY.mjs +1 -0
- package/package.json +3 -3
- package/dist/packem_shared/backfillSqlSearchIndexes-DlBChpGZ.mjs +0 -1
- package/dist/packem_shared/decodeGlobalRow-D0xGl2NU.mjs +0 -1
- package/dist/packem_shared/runSqlAggregateMigrations-D_MIiVIp.mjs +0 -1
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-
|
|
1
|
+
import{createSqlCtxDb as a,readSqlCdcChangedTables as o,readSqlCdcChanges as t,readSqlCdcFloor as l,runSqlCdcMigration as s,sweepSqlCdcRetention as n}from"./packem_shared/createSqlCtxDb-vhTttOe-.mjs";import{runSqlAggregateMigrations as d,runSqlGlobalTableMigrations as S,runSqlRankMigrations as c}from"./packem_shared/runSqlAggregateMigrations-wYVDSorY.mjs";import{d as g,r as f}from"./packem_shared/ctx-db-search-Cus9tweO.mjs";import{S as m,m as x}from"./packem_shared/ctx-db-search-state-DHkLBltM.mjs";import{f as p,e as u,b as h,s as M,t as T}from"./packem_shared/value-codec-DC_dHxUY.mjs";import{decodeGlobalRow as E}from"./packem_shared/decodeGlobalRow-DtDkyN_O.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 @@
|
|
|
1
|
+
const o="_version";export{o as OCC_VERSION_COLUMN};
|
package/dist/packem_shared/{SEARCH_STATE_TABLE-DvsS7EJ8.mjs → SEARCH_STATE_TABLE-BvrfudML.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{S as t,a as S,m as c,r as l,b as i,w as s}from"./ctx-db-search-state-
|
|
1
|
+
import{S as t,a as S,m as c,r as l,b as i,w as s}from"./ctx-db-search-state-DHkLBltM.mjs";import"./decodeGlobalRow-DtDkyN_O.mjs";export{t as SEARCH_STATE_TABLE,S as clearSearchBackfillState,c as migrateSearchState,l as readSearchBackfillState,i as readSearchIndexCoverage,s as writeSearchBackfillState};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./ctx-db-search-state-DHkLBltM.mjs";import{d as S,c as s,a as i,r as l}from"./ctx-db-search-Cus9tweO.mjs";import"./decodeGlobalRow-DtDkyN_O.mjs";export{S as backfillSqlSearchIndexes,s as createSearchSync,i as runSqlSearch,l as runSqlSearchMigrations};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import{LunoraError as T}from"@lunora/errors";import{m as Wt,D as Ut}from"./ctx-db-search-state-
|
|
1
|
+
import{LunoraError as T}from"@lunora/errors";import{m as Wt,D as Ut}from"./ctx-db-search-state-DHkLBltM.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-Cus9tweO.mjs";import{d as Si}from"./ctx-db-search-Cus9tweO.mjs";import{literalInList as zt,throwingScheduler as Qt,CDC_LOG_TABLE as P,cdcForkedError as Jt,cursorBelowRetainedFloor as Zt,cdcTrimmedError as en,assertNoExplicitUndefined as et,hasTrigger as tn,runRowValidators as Ne,assertFlatPredicate as tt,mergeWhere as B,rankTableName as we,sortColumnName as Ee,resolveRankPartition as nt,encodePartitionKey as Ce,resolveRankSeekTuple as nn,RANK_TIEBREAK as ye,CountRlsUnsupportedError as it,stripReservedPatchFields as on,normalizeIdStructurally as rn,assertValidClientId as sn,aggregateSqlFunction as $e,softDeleteScope as _e,compileWhereSql as he,normalizeOrderKeys as an,uniqueIndexFields as cn,equalityPinnedFields as dn,buildSeekWhere as ln,decodeCursor as un,resolveRelationPredicates as ot,resolveWith as rt,relationHooks as st,applySelect as at,encodeCursor as fn,NotFoundError as wn,applyOnDelete as $n,ConflictError as Ae,normalizeCountArgument as _n,selectIndexForCount as hn,encodeAggregateKey as Y,aggregateTableName as Q,selectIndexForAggregate as En,readAggregateValue as qe,runTriggers as yn,matchesRankStaticWhere as ct,rankPivotConditionSql as pt,CURSOR_PREFIX as pn,selectIndexForGroupBy as Rn,tiebreakDirectionFor as Sn,fanOutScalarCounts as gn,matchesStaticWhere as De,foldAggregateTally as dt,coerceAggregateNumber as Be,CDC_LOG_TABLE_SEQ_INDEX as Tn,NotUniqueError as vn}from"@lunora/shard-engine";import{sql as e}from"drizzle-orm";import{d as Rt,b as mn,e as St,n as In,c as bn}from"./value-codec-DC_dHxUY.mjs";import{runSqlGlobalTableMigrations as Cn,runSqlAggregateMigrations as An,runSqlRankMigrations as Ln}from"./runSqlAggregateMigrations-wYVDSorY.mjs";import{queryAll as m,queryRun as M,createIndexIfNotExists as kn,decodeRow as ee,serializeDocumentColumn as J,columnRefSql as O,decodeRows as lt,tableColumns as gt,serializeColumnValue as Oe,forEachRowPaged as ut,BACKFILL_BATCH_SIZE as Fe,queryBatch as ft}from"./decodeGlobalRow-DtDkyN_O.mjs";import{decodeGlobalRow as Ti}from"./decodeGlobalRow-DtDkyN_O.mjs";import{OCC_VERSION_COLUMN as wt}from"./OCC_VERSION_COLUMN-ByC64Jgx.mjs";const Mn=(a,c)=>{if(a.size<c)return;const h=a.keys().next().value;h!==void 0&&a.delete(h)},$t=50,Nn=(a,c={})=>{const h=c.maxBatch??$t,r=Number.isInteger(h)&&h>0?h:$t,p=new Map;let w=!1;const d=async(g,v)=>{const I=[...v.keys()];for(let C=0;C<I.length;C+=r){const N=I.slice(C,C+r);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)}}},L=()=>{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,C)=>{let N=p.get(g);N||(N=new Map,p.set(g,N));const W=N.get(v);W?W.push({reject:C,resolve:I}):N.set(v,[{reject:C,resolve:I}]),w||(w=!0,queueMicrotask(L))})}}},qn=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",Dn=(a,c)=>{const h=a.map(r=>e`${O(r.field)} ${e.raw(`${r.direction==="desc"?"DESC":"ASC"}${Tt(c,r)}`)}`);if(!a.some(r=>qn.has(r.field))){const r=Sn(a);h.push(e`${O("id")} ${e.raw(r==="desc"?"DESC":"ASC")}`)}return e.join(h,e`, `)},_t=50,Bn=(a,c,h)=>{const r={...c};for(const[p,w]of gt(a)){if(w.serverDefault){r[p]=w.serverDefault({auth:h});continue}r[p]===void 0&&(w.defaultFn?r[p]=w.defaultFn():"defaultValue"in w&&(r[p]=w.defaultValue))}return r},ht=(a,c,h,r)=>{for(const[p,w]of gt(a)){if(w.serverDefault){p in c&&(h[p]=w.serverDefault({auth:r}));continue}w.onUpdateFn&&!(p in c)&&(h[p]=w.onUpdateFn())}},On=128,Fn=()=>{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):Mn(a,On),a.set(c,h)}}},Wn=async(a,c,h,r,p)=>{const w=p.get(r);if(w!==void 0)return w;const d=Object.entries(h.tables).filter(([,g])=>g.shardMode===void 0||g.shardMode.kind==="global").map(([g])=>g),L=await Promise.all(d.map(async g=>({found:(await m(a,c,e`SELECT 1 FROM ${e.identifier(g)} WHERE ${e.identifier("id")} = ${r} LIMIT 1`)).length>0,tableName:g})));for(const g of L)if(g.found)return p.set(r,g.tableName),g.tableName},te=a=>a==null?null:Number(a),Un=(a,c,h)=>h.map(r=>{const p={};for(const w of c){const d=a.shape[w];p[w]=mn(r[w]??null,d&&St(d))}return{key:p,value:te(r.value)}}),xn=(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`, `),Pn=(a,c,h,r,p)=>{const w=[];for(let d=0;d<h.length+1;d+=1){const L=h[d],g=c.sortBy[d],v=d<h.length&&L!==void 0&&g!==void 0,I=g?.direction==="desc"?"desc":"asc",C=v?pt(L,r[L],I,!1):e`${e.identifier(ye)} < ${p}`;if(C===void 0)continue;const N=h.slice(0,d).map(W=>Pe(a,e`${e.identifier(W)}`,r[W]));N.push(C),w.push(vt(N))}return w.length>0?e.join(w,e` OR `):e`1 = 0`},Hn=(a,c,h)=>{if(h.length!==c.length)return;const r=[];for(const[p,w]of c.entries()){const d=pt(w.column,h[p],w.direction,!0);if(d===void 0)continue;const L=[];for(let g=0;g<p;g+=1){const v=c[g];v!==void 0&&L.push(Pe(a,e`${e.identifier(v.column)}`,h[g]))}L.push(d),r.push(vt(L))}return r.length===0?e`(1 = 0)`:e`(${e.join(r,e` OR `)})`},Kn=(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[r,p]of a.sortBy.entries())h.push({column:c[r]??Ee(r),direction:p.direction,nullable:!0});return h.push({column:ye,direction:"asc",nullable:!1}),h},jn=async(a,c,h,r,p)=>{const d=[];for(let I=0;I<p.length;I+=50)d.push(p.slice(I,I+50));const L=await Promise.all(d.map(async I=>m(a,c,e`SELECT * FROM ${e.identifier(r)} WHERE ${e.identifier("id")} IN (${ne(I)})`))),g=new Map;for(const I of L)for(const C of I)g.set(C.id,C);const v=[];for(const I of p){const C=ee(h,g.get(I));C&&v.push(C)}return v},Gn=a=>{const c=JSON.stringify(a),h=new TextEncoder().encode(c);let r="";for(const p of h)r+=String.fromCodePoint(p);return pn+btoa(r)},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)`)},Vn=(a,c,h,r)=>{for(const p of h)Ue(a,p,`groupBy(${c}, { by: [..."${p}"] })`);r.field!==void 0&&Ue(a,r.field,`groupBy(${c}, { agg: { op: "${r.op}", field: "${r.field}" } })`)},xe="__cdc_sweep",It="global",Yn=6e4,Xn=1e4,zn=3e4;let Et=0;const Qn=async(a,c)=>{const{autoincrementPrimaryKey:h,key:r,real:p,text:w}=c.companionTypes;await M(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(P)} (${e.identifier("seq")} ${e.raw(h)}, ${e.identifier("ts")} ${e.raw(p)} NOT NULL, ${e.identifier("table")} ${e.raw(r)} NOT NULL, ${e.identifier("id")} ${e.raw(r)} NOT NULL, ${e.identifier("op")} ${e.raw(r)} NOT NULL, ${e.identifier("doc")} ${e.raw(w)})`);const d=c.indexKeyPrefix?.("string");await kn(a,c,{columns:e`${d===void 0?e`${e.identifier("table")}`:e`${e.identifier("table")}(${e.raw(String(d))})`}, ${e.identifier("seq")}`,name:Tn,table:P,unique:!1}),await M(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(xe)} (${e.identifier("id")} ${e.raw(r)} 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`}`)},Jn=async(a,c,h)=>{const r=e`UPDATE ${e.identifier(xe)} SET ${e.identifier("lease_until")} = ${h+Yn} WHERE ${e.identifier("id")} = ${It} AND ${e.identifier("lease_until")} <= ${h}`;if(c.supportsReturning)return(await m(a,c,e`${r} RETURNING ${e.identifier("id")}`)).length>0;const p=await M(a,c,r);return(c.affectedRows?c.affectedRows(p??{rowsAffected:0}):0)>0},Zn=async(a,c,h,r)=>{if(!await Jn(a,c,r))return;const p=r-h;await M(a,c,e`
|
|
2
2
|
DELETE FROM ${e.identifier(P)} WHERE ${e.identifier("seq")} IN (
|
|
3
3
|
SELECT ${e.identifier("seq")} FROM (
|
|
4
4
|
SELECT ${e.identifier("seq")} FROM ${e.identifier(P)} WHERE ${e.identifier("ts")} <= ${p} ORDER BY ${e.identifier("seq")} ASC LIMIT ${e.raw(String(Xn))}
|
|
5
5
|
) AS ${e.identifier("expired")}
|
|
6
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(P)}`),r=Number(h[0]?.seq??Number.NaN);return Number.isFinite(r)?r:void 0},ei=async(a,c)=>{if(c.name!=="sqlite")return;const h=await m(a,c,e`SELECT ${e.identifier("seq")} AS ${e.identifier("seq")} FROM ${e.identifier("sqlite_sequence")} WHERE ${e.identifier("name")} = ${P}`),r=Number(h[0]?.seq??Number.NaN);return Number.isFinite(r)?r:0},ti=a=>JSON.stringify(In(a)?bn(a):a),ni=async(a,c,h,r,p,w,d)=>{await M(a,d,e`INSERT INTO ${e.identifier(P)} (${e.identifier("ts")}, ${e.identifier("table")}, ${e.identifier("id")}, ${e.identifier("op")}, ${e.identifier("doc")}) VALUES (${c}, ${h}, ${r}, ${p}, ${w===void 0?null:ti(w)})`)},hi=async(a,c,h)=>{const r=c.sinceSeq??0,p=Math.max(1,Math.min(c.limit??1e3,1e4)),w=await bt(a,h),d=await ei(a,h);if(d!==void 0&&r>d)throw Jt(d,r,"global");if(w!==void 0&&Zt(w,r))throw en(w,r,"global");const g=(await m(a,h,e`SELECT seq, ts, ${e.identifier("table")}, id, op, doc FROM ${e.identifier(P)} WHERE seq > ${r} ORDER BY seq ASC LIMIT ${e.raw(String(p))}`)).map(v=>{const{doc:I}=v,C={id:String(v.id),op:String(v.op),seq:Number(v.seq),table:String(v.table),ts:Number(v.ts)};return typeof I=="string"?{...C,doc:Rt(JSON.parse(I))}:C});return{changes:g,cursor:g.at(-1)?.seq??r}},ii=async(a,c,h,r={})=>{if(r.cursorOnly){const g=await m(a,h,e`SELECT MAX(seq) AS seq FROM ${e.identifier(P)}`),v=Number(g[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(P)} 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 L=r.retained===!0?await bt(a,h):void 0;return{cursor:w,tables:d,...L===void 0?{}:{floor:L}}},oi=/string or blob too big/iu,ri=/row is too big/iu,si=(a,c)=>{const{code:h,errno:r}=c,p=c instanceof Error?c.message:"";switch(a.name){case"mysql":return r===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 ri.test(p)?"the 8 KB heap page a tuple must fit once its wide columns have been TOASTed out":void 0;default:return oi.test(p)?"the storage engine's per-row ceiling (2 MB on D1)":void 0}},ai=(a,c,h)=>{const r=si(a,c);if(r!==void 0)throw new T("PAYLOAD_TOO_LARGE",`document is too large to store in "${h}": a single row cannot exceed ${r}. 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"):(ai(a,c,h),c)},We=new WeakMap,Ei=a=>{const{crossShardCounter:c,crossShardReader:h,exec:r,maxRelationKeys:p,schema:w}=a,{dialect:d}=a,L={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={...L,serialize:(o,s)=>J(t,s,o)};return g.set(t,i),i},I=(t,n)=>Pe(d.name,t,n),C=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)}`,s=u=>d.name==="postgres"?e`${e.identifier(t.table)}.${e.identifier(u)}`:e`${e.identifier(u)}`,l=e.join(Object.entries(t.set(o,s)).map(([u,y])=>e`${e.identifier(u)} = ${y}`),e`, `),E=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}) ${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 ni(r,N(),t,n,i,o,d),V===void 0))return;const s=N();if(!(s-Et<zn)){Et=s;try{await Zn(r,d,V,s)}catch{}}},Ct=a.scheduler??Qt,Le=Fn();let pe=0;const ke=a.provisionScope??{},U=async()=>{const t=We.get(ke);if(t)return t;const n=(async()=>{await Cn(r,w,d),await An(r,w,d),await Ln(r,w,d),await Wt(r,d),await Pt(r,w,d),G&&await Qn(r,d)})().catch(i=>{throw We.delete(ke),i});return We.set(ke,n),n},re=async(t,n)=>{await U();const i=await Wn(r,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(r,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 E=n.by??[],u=new Map;await ft(r,d,l,t,$=>{if(n.where&&!De($,n.where))return;const _=Y(E,$);dt(u,_,n,$)});const y=Q(t,n.name);await M(r,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(r,d,f),se.set(i,!0),!0},At=async(t,n,i,o)=>{const s=new Map;let l,E=!0;for(;E;){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(r,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(s,"",i,$);l=f.at(-1)?.id,E=f.length===Fe}return s.get("")?.value??null},Lt=async(t,n,i)=>{const o=n.field??"",s=[],l=w.tables[t],E=($,_)=>l?J(l,$,_):Oe(_);for(const $ of n.by??[]){const _=E($,i[$]??null);s.push(_===null?e`${O($)} IS NULL`:e`${O($)} = ${_}`)}for(const[$,_]of Object.entries(n.where??{})){const R=_!==null&&typeof _=="object"&&!Array.isArray(_)?_.eq:_,S=E($,R);s.push(S===null?e`${O($)} IS NULL`:e`${O($)} = ${S}`)}const u=s.length>0?e` WHERE ${e.join(s,e` AND `)}`:e``;if(l&&mt(l.shape[o]))return At(l,t,n,s);const y=$e(n.op),f=await m(r,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(r,d,e`DELETE FROM ${e.identifier(t)} WHERE ${e.identifier("__key__")} = ${n} AND ${e.identifier("__count__")} <= 0`)},Mt=async(t,n,i,o)=>{const s=Q(t,n.name),{op:l}=n,E=n.field??"",u=i&&(!n.where||De(i,n.where))?i:void 0,y=o&&(!n.where||De(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:Be($[E]);if(R===void 0)continue;const S=Y(n.by??[],$);f.add(S),await M(r,d,C({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(b,A)=>({__count__:e`${A("__count__")} + ${b("__count__")}`,__value__:l==="count"?e`${A("__value__")} + ${b("__value__")}`:e`COALESCE(${A("__value__")}, 0) + ${b("__value__")}`}),table:s,values:[S,_*R,_]}))}for(const $ of f)await kt(s,$);return}if(u){const f=Y(n.by??[],u),$=Be(u[E]),R=(await m(r,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${f}`))[0],S=te(R?.value),b=(R?.count??0)-1;if(b<=0)await M(r,d,e`DELETE FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${f}`);else if(R&&$!==void 0&&S!==null&&$===S){const A=await Lt(t,n,u);await M(r,d,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__value__")} = ${A}, ${e.identifier("__count__")} = ${b} WHERE ${e.identifier("__key__")} = ${f}`)}else await M(r,d,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__count__")} = ${e.identifier("__count__")} - 1 WHERE ${e.identifier("__key__")} = ${f}`)}if(y){const f=Y(n.by??[],y),$=Be(y[E]);if($===void 0)await M(r,d,C({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(_,R)=>({__count__:e`${R("__count__")} + 1`}),table:s,values:[f,null,1]}));else{const _=l==="min"?"MIN":"MAX";await M(r,d,C({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:s,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 s of o){const l=`${t}::${s.name}`;(se.get(l)??await He(t,s.name))&&await Mt(t,s,n,i)}},ce=new Map,Ke=async(t,n)=>{const i=we(t,n);return(await m(r,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 E=we(t,n.name);await M(r,d,e`DELETE FROM ${e.identifier(E)}`);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(r,d,_)};return await ft(r,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(E)} (${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 s=w.tables[t],l=s?.rankIndexes;if(!l||l.length===0)return;const E=s?.softDeleteMode?.field,u=E===void 0||o===void 0?void 0:o[E],y=u!=null;for(const f of l){if(i!==void 0&&o!==void 0&&xn(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(r,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((D,j)=>Ee(j)),A=Z(["__id__","__partition__",...b]),F=Ce(f.partitionBy??[],o),k=f.sortBy.map(D=>Oe(o[D.field]??null));await M(r,d,e`INSERT INTO ${e.identifier(S)} (${A}) VALUES (${ne([n,F,...k])})`)}}},de=xt({dialect:d,exec:r,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 H=(t,n,i)=>je.has(`${t} ${n} ${i}`);let Ge;const K=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 yn({ctx:Ge,event:i,op:n,schema:w,tableName:i.table,timing:t})}finally{pe-=1}},Nt=async(t,n)=>{try{await M(r,d,n)}catch(i){yt(d,i,t)}},Te=async(t,n)=>(await m(r,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 s=e`${e.identifier(ut)}`,l=e.join([I(e`${e.identifier("id")}`,o.id),I(s,o[ut]??null)],e` AND `),E=n==="UPDATE"?e`UPDATE ${e.identifier(t)} SET ${i}, ${s} = COALESCE(${s}, 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(r,d,e`${E} RETURNING ${e.identifier("id")}`)).length===0&&u();else{const y=await M(r,d,E);(d.affectedRows?d.affectedRows(y??{rowsAffected:0}):0)===0&&u()}}catch(y){yt(d,y,t)}},qt=(t,n,i,o)=>{const s=Object.keys(t.shape);return{columns:["id","_creationTime",...s],values:[n,i,...s.map(l=>J(t,l,o[l]??null))]}},Dt=async(t,n,i,o)=>{const s=Rn(n,i.op,i.field,o.by,o.where);if(!s||!await Re(t,s.index))return;const E=Q(t,s.index.name),u=Object.keys(s.partial);if(u.length===(s.index.by??[]).length&&u.length>0){const f=Y(s.index.by??[],s.partial),$=await m(r,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__")} = ${f}`);if($.length===0)return[];const _=$[0];return[{key:{...s.partial},value:qe(i.op,{count:_.count,value:te(_.value)})}]}return u.length>0?void 0:(await m(r,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(f=>{const $=f;return{key:Rt(JSON.parse($.key)),value:qe(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`)},Bt=(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 s=n.relationBaseWhere(o);s!==void 0&&(i[o]=s)}return{orderBy:n?.orderBy,relationPolicies:i,where:B(n?.baseWhere,n?.where),with:n?.with}},Xe=(t,n)=>Ve(t)?h?h(t,Bt(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=Nn(async(t,n)=>{const i=await m(r,d,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} IN (${ne(n)})`),o=new Map;for(const s of i){const{id:l}=s;typeof l=="string"&&o.set(l,s)}return o}),x={async cdcChangedTables(t,n){if(G)try{return await U(),await ii(r,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),s=B(B(n.baseWhere,n.where),o),l=await me(s,t,n.relationBaseWhere),E=l!==s;if(i.aggregateIndexes&&!n.baseWhere&&!E&&!o){const R=En(i.aggregateIndexes,n.op,n.field,n.where);if(R&&await Re(t,R.index)){const b=Y(R.index.by??[],R.key),A=Q(t,R.index.name),k=(await m(r,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(A)} WHERE ${e.identifier("__key__")} = ${b}`))[0];return qe(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(r,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 s=_e(i.softDeleteMode,void 0),l=B(B(o.baseWhere,o.where),s),E=await me(l,t,o.relationBaseWhere),u=E!==l;if(i.aggregateIndexes&&!o.baseWhere&&!u&&!s){const _=hn(i.aggregateIndexes,o.where);if(_&&await Re(t,_.index)){const S=Y(_.index.by??[],_.key),b=Q(t,_.index.name),A=await m(r,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")} FROM ${e.identifier(b)} WHERE ${e.identifier("__key__")} = ${S}`);return Number(A[0]?.value??0)}}const y=he(E,v(i)),f=e`SELECT COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(t)}`,$=await m(r,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 s=w.tables[o];if(!s)return;const l=await Te(o,t),E=ee(s,l),u=i?.hard===!0,y=!u&&s.softDeleteMode?s.softDeleteMode.field:void 0;if(!(y&&(!E||E[y]!==null&&E[y]!==void 0))){if(H(o,"before","delete")&&await K("before","delete",{id:t,op:"delete",previous:E??void 0,table:o}),await $n({deletedId:t,deletedReference:f=>E?.[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&&E){const f={...E,[y]:N()},$=e.join(Object.keys(s.shape).map(_=>e`${e.identifier(_)} = ${J(s,_,f[_]??null)}`),e`, `);await ve(o,"UPDATE",$,l),await ae(o,E,f),await X(o,t,E,void 0),await de(o,t,f,E),await oe(o,t,"update",f),H(o,"after","delete")&&await K("after","delete",{id:t,op:"delete",previous:E,table:o});return}await ve(o,"DELETE",void 0,l),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"),H(o,"after","delete")&&await K("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 wn(`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=an(n.orderBy,i.shape,{pinned:dn(n.where),uniqueBy:cn(i.indexes,i.shape)}),s=n.cursor?ln(o,un(n.cursor)):void 0,l=Xe,E=async(k,D,j,Ie)=>{if(Ve(k))return c?gn(c,k,D,j,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={[D]:{in:j}},q=B(B(z,Ie),be),ue=await me(q,k,void 0),ze=he(ue,v(le)),Qe=O(D);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(r,d,fe),Je=new Map;for(const Ze of Ft)Je.set(Ze.__fk__,Number(Ze.count??0));return Je};let u=B(n.baseWhere,n.where);u=B(u,_e(i.softDeleteMode,n.includeDeleted)),u=await ot(u,{fetcher:l,maxRelationKeys:p,relationBaseWhere:n.relationBaseWhere,schema:w,tableName:t}),s&&(u=u?{AND:[u,s]}:s);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(r,d,$),S=lt(i,R);if(_===void 0)return n.with&&await rt({fetcher:l,groupedCounter:E,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>_,A=b?S.slice(0,_):S,F=A.at(-1);return n.with&&await rt({fetcher:l,groupedCounter:E,parents:A,...st(n),schema:w,tableName:t,with:n.with}),{continueCursor:b&&F?fn(F,o):null,isDone:!b,page:at(A,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 s=_e(i.softDeleteMode,void 0),l=B(B(n.baseWhere,n.where),s),E=await me(l,t,n.relationBaseWhere),u=E!==l;if(i.aggregateIndexes&&!n.baseWhere&&!u&&!s){const S=await Dt(t,i.aggregateIndexes,o,n);if(S!==void 0)return S}Vn(i,t,n.by,o);const y=he(E,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(r,d,_);return Un(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 s=Bn(o,n,ie);Ne(o,s);let l,E=!0;i?.clientId!==void 0?(sn(i.clientId),l=i.clientId):i?.allowExplicitId&&typeof s._id=="string"?l=s._id:(l=W(),E=!1);const u=i?.allowExplicitId&&typeof s._creationTime=="number"?s._creationTime:N(),y={...s,_creationTime:u,_id:l};H(t,"before","insert")&&await K("before","insert",{doc:{...y},id:l,op:"insert",table:t}),await Se(t),await ge(t);const{columns:f,values:$}=qt(o,l,u,s);return await Nt(t,e`INSERT INTO ${e.identifier(t)} (${Z(f)}) VALUES (${ne($)})`),E&&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),H(t,"after","insert")&&await K("after","insert",{doc:y,id:l,op:"insert",table:t}),l},normalizeId(t,n){return rn(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 s=w.tables[o];if(!s)throw new T("INTERNAL",`document not found: ${t}`);const l=await Te(o,t),E=ee(s,l);if(!E)throw new T("INTERNAL",`document not found: ${t}`);const u={...E,...on(n),_id:t};ht(s,n,u,ie),Ne(s,u,!0),H(o,"before","update")&&await K("before","update",{doc:{...u},id:t,op:"update",previous:E,table:o}),await Se(o),await ge(o);const y=Object.keys(s.shape),f=e.join(y.map($=>e`${e.identifier($)} = ${J(s,$,u[$]??null)}`),e`, `);await ve(o,"UPDATE",f,l),await ae(o,E,u),await X(o,t,E,u),await de(o,t,u,E),await oe(o,t,"update",u),H(o,"after","update")&&await K("after","update",{doc:u,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=w.tables[i],s=o?.softDeleteMode?.field;if(!o||!s)throw new T("INTERNAL",`ctx.db.restore: table "${i}" is not a .softDelete() table`);const l=await Te(i,t),E=l?.[s]!==null&&l?.[s]!==void 0;if(await x.patch(t,{[s]:null},n),E){const u=ee(o,l);u!==null&&await X(i,t,void 0,{...u,[s]: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=[],s=u=>o.every(y=>y(u)),l=async(u,y)=>{await U();const f=o.length>0,$=await Gt(r,d,n,t,u,Vt(f?void 0:y));if(!f)return y===void 0&&Yt($),$;const _=$.filter(R=>s(R));return y===void 0?_:_.slice(0,y)},E=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 vn(`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 E(R)}};return y};return E(void 0)},async rank(t,n,i){const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(D=>D.name===n);if(!s)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(i.restrictsCounts)throw new it(t);if(await U(),!await Me(t,s))return null;const E=typeof i.row=="string"?i.row:i.row._id;if(!E)return null;const u=we(t,s.name),y=s.sortBy.map((D,j)=>Ee(j)),f=Z(["__partition__",...y]),_=(await m(r,d,e`SELECT ${f} FROM ${e.identifier(u)} WHERE ${e.identifier("__id__")} = ${E}`))[0];if(!_)return null;const R=_.__partition__,S=B(i.baseWhere,i.where);tt(S,w,t,"rank");const b=nt(s,S);if(b&&Ce(s.partitionBy??[],b)!==R)return null;const A=Pn(d.name,s,y,_,E),F=await m(r,d,e`SELECT COUNT(*) AS c FROM ${e.identifier(u)} WHERE ${e.identifier("__partition__")} = ${R} AND (${A})`),k=await m(r,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(B(i.baseWhere,i.where),w,t,"rankPage");const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(q=>q.name===n);if(!s)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(await U(),!await Me(t,s))return{continueCursor:null,isDone:!0,page:[]};const E=we(t,s.name),u=s.sortBy.map((q,ue)=>Ee(ue)),y=Math.max(1,Math.min(1e3,Math.floor(i.take??100))),f=B(i.baseWhere,i.where),$=nt(s,f),_=Kn(s,u),R=e.join(_.map(q=>e`${e.identifier(q.column)} ${e.raw(`${q.direction==="desc"?"DESC":"ASC"}${Tt(d,{direction:q.direction,nullable:q.nullable})}`)}`),e`, `),S=[];typeof i.partitionKey=="string"?S.push(e`${e.identifier("__partition__")} = ${i.partitionKey}`):$&&S.push(e`${e.identifier("__partition__")} = ${Ce(s.partitionBy??[],$)}`);const b=nn(i);if(b!==void 0){const q=Hn(d.name,_,b);q!==void 0&&S.push(q)}const A=Z([ye,"__partition__",...u]);let F=e`SELECT ${A} FROM ${e.identifier(E)}`;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(r,d,F),D=k.length>y,j=D?k.slice(0,y):k,Ie=j.map(q=>q[ye]),le=await jn(r,d,o,t,Ie);let be=null;const z=j.at(-1);if(D&&z!==void 0){const q=[z.__partition__,...u.map(ue=>z[ue]),z[ye]];be=Gn(q)}return{continueCursor:be,isDone:!D,page:le}},async replace(t,n,i,o){et("replace",n);const s=await re(t,i);if(!s)throw new T("INTERNAL",`document not found: ${t}`);const l=w.tables[s];if(!l)throw new T("INTERNAL",`document not found: ${t}`);const E=await Te(s,t);if(E===void 0)throw new T("INTERNAL",`document not found: ${t}`);const y=tn(w,s,"update")||(l.aggregateIndexes??[]).length>0||(l.rankIndexes??[]).length>0?ee(l,E)??void 0:void 0,f=o?.allowExplicitId&&typeof n._creationTime=="number"?n._creationTime:void 0,$=typeof E._creationTime=="number"?E._creationTime:void 0,_=f??$??N(),R={...n,_creationTime:_,_id:t};ht(l,n,R,ie),Ne(l,R),H(s,"before","update")&&await K("before","update",{doc:{...R},id:t,op:"update",previous:y,table:s}),await Se(s),await ge(s);const S=Object.keys(l.shape),b=e.join([e`${e.identifier("_creationTime")} = ${_}`,...S.map(A=>e`${e.identifier(A)} = ${J(l,A,R[A]??null)}`)],e`, `);await ve(s,"UPDATE",b,E),await ae(s,y,R),await X(s,t,y,R),await de(s,t,R,y),await oe(s,t,"update",R),H(s,"after","update")&&await K("after","update",{doc:R,id:t,op:"update",previous:y,table:s})}};return Ge={db:x,scheduler:Ct},x};export{Ri as backfillSqlSearchIndexes,Ei as createSqlCtxDb,gi as decodeGlobalRow,ii as readSqlCdcChangedTables,hi as readSqlCdcChanges,bt as readSqlCdcFloor,Qn as runSqlCdcMigration,Pt as runSqlSearchMigrations,Zn as sweepSqlCdcRetention};
|
|
7
|
+
`)},bt=async(a,c)=>{const h=await m(a,c,e`SELECT MIN(${e.identifier("seq")}) AS ${e.identifier("seq")} FROM ${e.identifier(P)}`),r=Number(h[0]?.seq??Number.NaN);return Number.isFinite(r)?r:void 0},ei=async(a,c)=>{if(c.name!=="sqlite")return;const h=await m(a,c,e`SELECT ${e.identifier("seq")} AS ${e.identifier("seq")} FROM ${e.identifier("sqlite_sequence")} WHERE ${e.identifier("name")} = ${P}`),r=Number(h[0]?.seq??Number.NaN);return Number.isFinite(r)?r:0},ti=a=>JSON.stringify(In(a)?bn(a):a),ni=async(a,c,h,r,p,w,d)=>{await M(a,d,e`INSERT INTO ${e.identifier(P)} (${e.identifier("ts")}, ${e.identifier("table")}, ${e.identifier("id")}, ${e.identifier("op")}, ${e.identifier("doc")}) VALUES (${c}, ${h}, ${r}, ${p}, ${w===void 0?null:ti(w)})`)},Ei=async(a,c,h)=>{const r=c.sinceSeq??0,p=Math.max(1,Math.min(c.limit??1e3,1e4)),w=await bt(a,h),d=await ei(a,h);if(d!==void 0&&r>d)throw Jt(d,r,"global");if(w!==void 0&&Zt(w,r))throw en(w,r,"global");const g=(await m(a,h,e`SELECT seq, ts, ${e.identifier("table")}, id, op, doc FROM ${e.identifier(P)} WHERE seq > ${r} ORDER BY seq ASC LIMIT ${e.raw(String(p))}`)).map(v=>{const{doc:I}=v,C={id:String(v.id),op:String(v.op),seq:Number(v.seq),table:String(v.table),ts:Number(v.ts)};return typeof I=="string"?{...C,doc:Rt(JSON.parse(I))}:C});return{changes:g,cursor:g.at(-1)?.seq??r}},ii=async(a,c,h,r={})=>{if(r.cursorOnly){const g=await m(a,h,e`SELECT MAX(seq) AS seq FROM ${e.identifier(P)}`),v=Number(g[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(P)} 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 L=r.retained===!0?await bt(a,h):void 0;return{cursor:w,tables:d,...L===void 0?{}:{floor:L}}},oi=/string or blob too big/iu,ri=/row is too big/iu,si=(a,c)=>{const{code:h,errno:r}=c,p=c instanceof Error?c.message:"";switch(a.name){case"mysql":return r===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 ri.test(p)?"the 8 KB heap page a tuple must fit once its wide columns have been TOASTed out":void 0;default:return oi.test(p)?"the storage engine's per-row ceiling (2 MB on D1)":void 0}},ai=(a,c,h)=>{const r=si(a,c);if(r!==void 0)throw new T("PAYLOAD_TOO_LARGE",`document is too large to store in "${h}": a single row cannot exceed ${r}. 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"):(ai(a,c,h),c)},We=new WeakMap,yi=a=>{const{crossShardCounter:c,crossShardReader:h,exec:r,maxRelationKeys:p,schema:w}=a,{dialect:d}=a,L={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={...L,serialize:(o,s)=>J(t,s,o)};return g.set(t,i),i},I=(t,n)=>Pe(d.name,t,n),C=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)}`,s=u=>d.name==="postgres"?e`${e.identifier(t.table)}.${e.identifier(u)}`:e`${e.identifier(u)}`,l=e.join(Object.entries(t.set(o,s)).map(([u,y])=>e`${e.identifier(u)} = ${y}`),e`, `),E=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}) ${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 ni(r,N(),t,n,i,o,d),V===void 0))return;const s=N();if(!(s-Et<zn)){Et=s;try{await Zn(r,d,V,s)}catch{}}},Ct=a.scheduler??Qt,Le=Fn();let pe=0;const ke=a.provisionScope??{},U=async()=>{const t=We.get(ke);if(t)return t;const n=(async()=>{await Cn(r,w,d),await An(r,w,d),await Ln(r,w,d),await Wt(r,d),await Pt(r,w,d),G&&await Qn(r,d)})().catch(i=>{throw We.delete(ke),i});return We.set(ke,n),n},re=async(t,n)=>{await U();const i=await Wn(r,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(r,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 E=n.by??[],u=new Map;await ut(r,d,l,t,$=>{if(n.where&&!De($,n.where))return;const _=Y(E,$);dt(u,_,n,$)});const y=Q(t,n.name);await M(r,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 ft(r,d,f),se.set(i,!0),!0},At=async(t,n,i,o)=>{const s=new Map;let l,E=!0;for(;E;){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(r,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(s,"",i,$);l=f.at(-1)?.id,E=f.length===Fe}return s.get("")?.value??null},Lt=async(t,n,i)=>{const o=n.field??"",s=[],l=w.tables[t],E=($,_)=>l?J(l,$,_):Oe(_);for(const $ of n.by??[]){const _=E($,i[$]??null);s.push(_===null?e`${O($)} IS NULL`:e`${O($)} = ${_}`)}for(const[$,_]of Object.entries(n.where??{})){const R=_!==null&&typeof _=="object"&&!Array.isArray(_)?_.eq:_,S=E($,R);s.push(S===null?e`${O($)} IS NULL`:e`${O($)} = ${S}`)}const u=s.length>0?e` WHERE ${e.join(s,e` AND `)}`:e``;if(l&&mt(l.shape[o]))return At(l,t,n,s);const y=$e(n.op),f=await m(r,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(r,d,e`DELETE FROM ${e.identifier(t)} WHERE ${e.identifier("__key__")} = ${n} AND ${e.identifier("__count__")} <= 0`)},Mt=async(t,n,i,o)=>{const s=Q(t,n.name),{op:l}=n,E=n.field??"",u=i&&(!n.where||De(i,n.where))?i:void 0,y=o&&(!n.where||De(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:Be($[E]);if(R===void 0)continue;const S=Y(n.by??[],$);f.add(S),await M(r,d,C({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(b,A)=>({__count__:e`${A("__count__")} + ${b("__count__")}`,__value__:l==="count"?e`${A("__value__")} + ${b("__value__")}`:e`COALESCE(${A("__value__")}, 0) + ${b("__value__")}`}),table:s,values:[S,_*R,_]}))}for(const $ of f)await kt(s,$);return}if(u){const f=Y(n.by??[],u),$=Be(u[E]),R=(await m(r,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${f}`))[0],S=te(R?.value),b=(R?.count??0)-1;if(b<=0)await M(r,d,e`DELETE FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${f}`);else if(R&&$!==void 0&&S!==null&&$===S){const A=await Lt(t,n,u);await M(r,d,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__value__")} = ${A}, ${e.identifier("__count__")} = ${b} WHERE ${e.identifier("__key__")} = ${f}`)}else await M(r,d,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__count__")} = ${e.identifier("__count__")} - 1 WHERE ${e.identifier("__key__")} = ${f}`)}if(y){const f=Y(n.by??[],y),$=Be(y[E]);if($===void 0)await M(r,d,C({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(_,R)=>({__count__:e`${R("__count__")} + 1`}),table:s,values:[f,null,1]}));else{const _=l==="min"?"MIN":"MAX";await M(r,d,C({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:s,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 s of o){const l=`${t}::${s.name}`;(se.get(l)??await He(t,s.name))&&await Mt(t,s,n,i)}},ce=new Map,Ke=async(t,n)=>{const i=we(t,n);return(await m(r,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 E=we(t,n.name);await M(r,d,e`DELETE FROM ${e.identifier(E)}`);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 ft(r,d,_)};return await ut(r,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(E)} (${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 s=w.tables[t],l=s?.rankIndexes;if(!l||l.length===0)return;const E=s?.softDeleteMode?.field,u=E===void 0||o===void 0?void 0:o[E],y=u!=null;for(const f of l){if(i!==void 0&&o!==void 0&&xn(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(r,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((D,j)=>Ee(j)),A=Z(["__id__","__partition__",...b]),F=Ce(f.partitionBy??[],o),k=f.sortBy.map(D=>Oe(o[D.field]??null));await M(r,d,e`INSERT INTO ${e.identifier(S)} (${A}) VALUES (${ne([n,F,...k])})`)}}},de=xt({dialect:d,exec:r,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 H=(t,n,i)=>je.has(`${t} ${n} ${i}`);let Ge;const K=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 yn({ctx:Ge,event:i,op:n,schema:w,tableName:i.table,timing:t})}finally{pe-=1}},Nt=async(t,n)=>{try{await M(r,d,n)}catch(i){yt(d,i,t)}},Te=async(t,n)=>(await m(r,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 s=e`${e.identifier(wt)}`,l=e.join([I(e`${e.identifier("id")}`,o.id),I(s,o[wt]??null)],e` AND `),E=n==="UPDATE"?e`UPDATE ${e.identifier(t)} SET ${i}, ${s} = COALESCE(${s}, 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(r,d,e`${E} RETURNING ${e.identifier("id")}`)).length===0&&u();else{const y=await M(r,d,E);(d.affectedRows?d.affectedRows(y??{rowsAffected:0}):0)===0&&u()}}catch(y){yt(d,y,t)}},qt=(t,n,i,o)=>{const s=Object.keys(t.shape);return{columns:["id","_creationTime",...s],values:[n,i,...s.map(l=>J(t,l,o[l]??null))]}},Dt=async(t,n,i,o)=>{const s=Rn(n,i.op,i.field,o.by,o.where);if(!s||!await Re(t,s.index))return;const E=Q(t,s.index.name),u=Object.keys(s.partial);if(u.length===(s.index.by??[]).length&&u.length>0){const f=Y(s.index.by??[],s.partial),$=await m(r,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__")} = ${f}`);if($.length===0)return[];const _=$[0];return[{key:{...s.partial},value:qe(i.op,{count:_.count,value:te(_.value)})}]}return u.length>0?void 0:(await m(r,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(f=>{const $=f;return{key:Rt(JSON.parse($.key)),value:qe(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`)},Bt=(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 s=n.relationBaseWhere(o);s!==void 0&&(i[o]=s)}return{orderBy:n?.orderBy,relationPolicies:i,where:B(n?.baseWhere,n?.where),with:n?.with}},Xe=(t,n)=>Ve(t)?h?h(t,Bt(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=Nn(async(t,n)=>{const i=await m(r,d,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} IN (${ne(n)})`),o=new Map;for(const s of i){const{id:l}=s;typeof l=="string"&&o.set(l,s)}return o}),x={async cdcChangedTables(t,n){if(G)try{return await U(),await ii(r,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),s=B(B(n.baseWhere,n.where),o),l=await me(s,t,n.relationBaseWhere),E=l!==s;if(i.aggregateIndexes&&!n.baseWhere&&!E&&!o){const R=En(i.aggregateIndexes,n.op,n.field,n.where);if(R&&await Re(t,R.index)){const b=Y(R.index.by??[],R.key),A=Q(t,R.index.name),k=(await m(r,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(A)} WHERE ${e.identifier("__key__")} = ${b}`))[0];return qe(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(r,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 s=_e(i.softDeleteMode,void 0),l=B(B(o.baseWhere,o.where),s),E=await me(l,t,o.relationBaseWhere),u=E!==l;if(i.aggregateIndexes&&!o.baseWhere&&!u&&!s){const _=hn(i.aggregateIndexes,o.where);if(_&&await Re(t,_.index)){const S=Y(_.index.by??[],_.key),b=Q(t,_.index.name),A=await m(r,d,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")} FROM ${e.identifier(b)} WHERE ${e.identifier("__key__")} = ${S}`);return Number(A[0]?.value??0)}}const y=he(E,v(i)),f=e`SELECT COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(t)}`,$=await m(r,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 s=w.tables[o];if(!s)return;const l=await Te(o,t),E=ee(s,l),u=i?.hard===!0,y=!u&&s.softDeleteMode?s.softDeleteMode.field:void 0;if(!(y&&(!E||E[y]!==null&&E[y]!==void 0))){if(H(o,"before","delete")&&await K("before","delete",{id:t,op:"delete",previous:E??void 0,table:o}),await $n({deletedId:t,deletedReference:f=>E?.[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&&E){const f={...E,[y]:N()},$=e.join(Object.keys(s.shape).map(_=>e`${e.identifier(_)} = ${J(s,_,f[_]??null)}`),e`, `);await ve(o,"UPDATE",$,l),await ae(o,E,f),await X(o,t,E,void 0),await de(o,t,f,E),await oe(o,t,"update",f),H(o,"after","delete")&&await K("after","delete",{id:t,op:"delete",previous:E,table:o});return}await ve(o,"DELETE",void 0,l),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"),H(o,"after","delete")&&await K("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 wn(`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=an(n.orderBy,i.shape,{pinned:dn(n.where),uniqueBy:cn(i.indexes,i.shape)}),s=n.cursor?ln(o,un(n.cursor)):void 0,l=Xe,E=async(k,D,j,Ie)=>{if(Ve(k))return c?gn(c,k,D,j,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={[D]:{in:j}},q=B(B(z,Ie),be),ue=await me(q,k,void 0),ze=he(ue,v(le)),Qe=O(D);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(r,d,fe),Je=new Map;for(const Ze of Ft)Je.set(Ze.__fk__,Number(Ze.count??0));return Je};let u=B(n.baseWhere,n.where);u=B(u,_e(i.softDeleteMode,n.includeDeleted)),u=await ot(u,{fetcher:l,maxRelationKeys:p,relationBaseWhere:n.relationBaseWhere,schema:w,tableName:t}),s&&(u=u?{AND:[u,s]}:s);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(r,d,$),S=lt(i,R);if(_===void 0)return n.with&&await rt({fetcher:l,groupedCounter:E,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>_,A=b?S.slice(0,_):S,F=A.at(-1);return n.with&&await rt({fetcher:l,groupedCounter:E,parents:A,...st(n),schema:w,tableName:t,with:n.with}),{continueCursor:b&&F?fn(F,o):null,isDone:!b,page:at(A,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 s=_e(i.softDeleteMode,void 0),l=B(B(n.baseWhere,n.where),s),E=await me(l,t,n.relationBaseWhere),u=E!==l;if(i.aggregateIndexes&&!n.baseWhere&&!u&&!s){const S=await Dt(t,i.aggregateIndexes,o,n);if(S!==void 0)return S}Vn(i,t,n.by,o);const y=he(E,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(r,d,_);return Un(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 s=Bn(o,n,ie);Ne(o,s);let l,E=!0;i?.clientId!==void 0?(sn(i.clientId),l=i.clientId):i?.allowExplicitId&&typeof s._id=="string"?l=s._id:(l=W(),E=!1);const u=i?.allowExplicitId&&typeof s._creationTime=="number"?s._creationTime:N(),y={...s,_creationTime:u,_id:l};H(t,"before","insert")&&await K("before","insert",{doc:{...y},id:l,op:"insert",table:t}),await Se(t),await ge(t);const{columns:f,values:$}=qt(o,l,u,s);return await Nt(t,e`INSERT INTO ${e.identifier(t)} (${Z(f)}) VALUES (${ne($)})`),E&&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),H(t,"after","insert")&&await K("after","insert",{doc:y,id:l,op:"insert",table:t}),l},normalizeId(t,n){return rn(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 s=w.tables[o];if(!s)throw new T("INTERNAL",`document not found: ${t}`);const l=await Te(o,t),E=ee(s,l);if(!E)throw new T("INTERNAL",`document not found: ${t}`);const u={...E,...on(n),_id:t};ht(s,n,u,ie),Ne(s,u,!0),H(o,"before","update")&&await K("before","update",{doc:{...u},id:t,op:"update",previous:E,table:o}),await Se(o),await ge(o);const y=Object.keys(s.shape),f=e.join(y.map($=>e`${e.identifier($)} = ${J(s,$,u[$]??null)}`),e`, `);await ve(o,"UPDATE",f,l),await ae(o,E,u),await X(o,t,E,u),await de(o,t,u,E),await oe(o,t,"update",u),H(o,"after","update")&&await K("after","update",{doc:u,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=w.tables[i],s=o?.softDeleteMode?.field;if(!o||!s)throw new T("INTERNAL",`ctx.db.restore: table "${i}" is not a .softDelete() table`);const l=await Te(i,t),E=l?.[s]!==null&&l?.[s]!==void 0;if(await x.patch(t,{[s]:null},n),E){const u=ee(o,l);u!==null&&await X(i,t,void 0,{...u,[s]: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=[],s=u=>o.every(y=>y(u)),l=async(u,y)=>{await U();const f=o.length>0,$=await Gt(r,d,n,t,u,Vt(f?void 0:y));if(!f)return y===void 0&&Yt($),$;const _=$.filter(R=>s(R));return y===void 0?_:_.slice(0,y)},E=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 vn(`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 E(R)}};return y};return E(void 0)},async rank(t,n,i){const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(D=>D.name===n);if(!s)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(i.restrictsCounts)throw new it(t);if(await U(),!await Me(t,s))return null;const E=typeof i.row=="string"?i.row:i.row._id;if(!E)return null;const u=we(t,s.name),y=s.sortBy.map((D,j)=>Ee(j)),f=Z(["__partition__",...y]),_=(await m(r,d,e`SELECT ${f} FROM ${e.identifier(u)} WHERE ${e.identifier("__id__")} = ${E}`))[0];if(!_)return null;const R=_.__partition__,S=B(i.baseWhere,i.where);tt(S,w,t,"rank");const b=nt(s,S);if(b&&Ce(s.partitionBy??[],b)!==R)return null;const A=Pn(d.name,s,y,_,E),F=await m(r,d,e`SELECT COUNT(*) AS c FROM ${e.identifier(u)} WHERE ${e.identifier("__partition__")} = ${R} AND (${A})`),k=await m(r,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(B(i.baseWhere,i.where),w,t,"rankPage");const o=w.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(q=>q.name===n);if(!s)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(await U(),!await Me(t,s))return{continueCursor:null,isDone:!0,page:[]};const E=we(t,s.name),u=s.sortBy.map((q,ue)=>Ee(ue)),y=Math.max(1,Math.min(1e3,Math.floor(i.take??100))),f=B(i.baseWhere,i.where),$=nt(s,f),_=Kn(s,u),R=e.join(_.map(q=>e`${e.identifier(q.column)} ${e.raw(`${q.direction==="desc"?"DESC":"ASC"}${Tt(d,{direction:q.direction,nullable:q.nullable})}`)}`),e`, `),S=[];typeof i.partitionKey=="string"?S.push(e`${e.identifier("__partition__")} = ${i.partitionKey}`):$&&S.push(e`${e.identifier("__partition__")} = ${Ce(s.partitionBy??[],$)}`);const b=nn(i);if(b!==void 0){const q=Hn(d.name,_,b);q!==void 0&&S.push(q)}const A=Z([ye,"__partition__",...u]);let F=e`SELECT ${A} FROM ${e.identifier(E)}`;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(r,d,F),D=k.length>y,j=D?k.slice(0,y):k,Ie=j.map(q=>q[ye]),le=await jn(r,d,o,t,Ie);let be=null;const z=j.at(-1);if(D&&z!==void 0){const q=[z.__partition__,...u.map(ue=>z[ue]),z[ye]];be=Gn(q)}return{continueCursor:be,isDone:!D,page:le}},async replace(t,n,i,o){et("replace",n);const s=await re(t,i);if(!s)throw new T("INTERNAL",`document not found: ${t}`);const l=w.tables[s];if(!l)throw new T("INTERNAL",`document not found: ${t}`);const E=await Te(s,t);if(E===void 0)throw new T("INTERNAL",`document not found: ${t}`);const y=tn(w,s,"update")||(l.aggregateIndexes??[]).length>0||(l.rankIndexes??[]).length>0?ee(l,E)??void 0:void 0,f=o?.allowExplicitId&&typeof n._creationTime=="number"?n._creationTime:void 0,$=typeof E._creationTime=="number"?E._creationTime:void 0,_=f??$??N(),R={...n,_creationTime:_,_id:t};ht(l,n,R,ie),Ne(l,R),H(s,"before","update")&&await K("before","update",{doc:{...R},id:t,op:"update",previous:y,table:s}),await Se(s),await ge(s);const S=Object.keys(l.shape),b=e.join([e`${e.identifier("_creationTime")} = ${_}`,...S.map(A=>e`${e.identifier(A)} = ${J(l,A,R[A]??null)}`)],e`, `);await ve(s,"UPDATE",b,E),await ae(s,y,R),await X(s,t,y,R),await de(s,t,R,y),await oe(s,t,"update",R),H(s,"after","update")&&await K("after","update",{doc:R,id:t,op:"update",previous:y,table:s})}};return Ge={db:x,scheduler:Ct},x};export{Si as backfillSqlSearchIndexes,yi as createSqlCtxDb,Ti as decodeGlobalRow,ii as readSqlCdcChangedTables,Ei as readSqlCdcChanges,bt as readSqlCdcFloor,Qn as runSqlCdcMigration,Pt as runSqlSearchMigrations,Zn as sweepSqlCdcRetention};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as l}from"@lunora/errors";import{D as y,c as Z,m as tt,r as R,a as nt,l as W,w as N,b as et}from"./ctx-db-search-state-Xa6UmA3s.mjs";import{sql as e}from"drizzle-orm";import{unionAll as it}from"@lunora/shard-engine";import{queryRun as E,decodeRows as Q,queryAll as A,queryBatch as rt,createIndexIfNotExists as q,columnRefSql as M,serializeColumnValue as ot,forEachRowPaged as G}from"./decodeGlobalRow-D0xGl2NU.mjs";const st=t=>{const n=atob(t),i=Uint8Array.from(n,r=>r.codePointAt(0)??0);return new TextDecoder().decode(i)},at=()=>new l("BAD_REQUEST","invalid cursor"),F=16,k=8,h=1024,v=(t,n)=>n.query(t),ft=(t,n)=>{if(!n)return{exact:!0,lower:t,upper:t};const i=[...t].at(-1)??"",r=(i.codePointAt(0)??0)+1;if(r>=55296&&r<=57343||r>1114111)return{exact:!0,lower:t,upper:t};const o=t.slice(0,t.length-i.length);return{exact:!1,lower:t,upper:o+String.fromCodePoint(r)}},Lt=(t,n,i)=>{const r={eq:(o,s)=>{if(!t.definition.filterFields?.includes(o))throw new l("INTERNAL",`field "${o}" is not a filter field of search index "${t.indexName}" on table "${n}"`);if(t.filters.length>=k)throw new l("BAD_REQUEST",`search index "${t.indexName}" on table "${n}": at most ${String(k)} .eq() filters are supported per search query`);return t.filters.push({field:o,value:s}),r},search:(o,s)=>{const a=t;if(o!==a.definition.field)throw new l("INTERNAL",`search index "${a.indexName}" on table "${n}" indexes "${a.definition.field}", not "${o}"`);const f=v(s,i).length;if(f>F)throw new l("BAD_REQUEST",`search index "${a.indexName}" on table "${n}": at most ${String(F)} search terms are supported (got ${String(f)})`);return a.field=o,a.query=s,a.hasQuery=!0,r}};return r},Ot=t=>{if(t.length>h)throw new l("BAD_REQUEST",`more than ${String(h)} documents match this search — narrow it with filters or read it a page at a time with .paginate()`)},xt=t=>Math.min(t.offset+t.numItems+1,h),ct=t=>btoa(`search:${String(t)}`),ut=t=>{let n;try{n=st(t)}catch{return}if(!n.startsWith("search:"))return;const i=Number(n.slice(7));return Number.isInteger(i)&&i>=0?i:void 0},Ut=t=>{if(typeof t.endCursor=="string")throw new l("BAD_REQUEST","bounded (endCursor) pagination is not supported on search queries — relevance order has no stable range boundary");if(!Number.isFinite(t.numItems))throw new l("BAD_REQUEST",`search pagination needs a finite numItems, got ${String(t.numItems)}`);const n=Math.max(0,Math.floor(t.numItems)),i=t.cursor?ut(t.cursor):0;if(i===void 0)throw at();if(i+n>=h)throw new l("BAD_REQUEST",`search pagination reaches the ${String(h)}-document limit (offset ${String(i)} + ${String(n)} requested) — a page must end below the cap so the probe row that answers \`hasMore\` still fits: retry with numItems ${String(Math.max(1,h-i-1))} or fewer, or narrow the query or the filters instead`);return{numItems:n,offset:i}},Bt=(t,n)=>{const i=n.offset+n.numItems,r=n.numItems>0&&t.length>i;return{continueCursor:r?ct(i):null,isDone:!r,page:t.slice(n.offset,i)}},qt=t=>{if(t===void 0)return h+1;if(!Number.isFinite(t))return h;const n=Math.max(0,Math.floor(t));if(n>h)throw new l("BAD_REQUEST",`search returns at most ${String(h)} documents (asked for ${String(n)}) — narrow the query or paginate instead`);return n},dt=(t,n)=>{if(!n.includes("."))return t[n];let i=t;for(const r of n.split(".")){if(i===null||typeof i!="object"||Array.isArray(i))return;i=i[r]}return i},b=(t,n)=>`${t}__fts_${n}`,$t="__text__",$="__id__",g="__token__",O="__n__",mt=1e3,V=(t,n)=>n.document(t),x=(t,n)=>dt(t,n),lt=t=>typeof t=="string"?t:t==null?"":typeof t=="number"||typeof t=="bigint"||typeof t=="boolean"?String(t):JSON.stringify(t)??"",St=(t,n,i)=>t!==void 0&&n!==void 0&&x(t,i.field)===x(n,i.field),ht=(t,n)=>V(lt(x(t,n.field)),y(n.language)).slice(0,mt),U=(t,n)=>ht(t,n).join(" "),wt=(t,n)=>{const i=new Map;for(const r of V(t,n))i.set(r,(i.get(r)??0)+1);return i},C=async(t,n,i,r)=>{await E(t,n,e`DELETE FROM ${e.identifier(i)} WHERE ${e.identifier($)} = ${r}`)},L=(t,n)=>t.name==="mysql"?e`${e.identifier(n)}(191)`:t.textPatternOperatorClass===void 0?e`${e.identifier(n)}`:e`${e.identifier(n)} ${e.raw(t.textPatternOperatorClass)}`,H=50,Et=(t,n)=>n?e`${e.identifier(g)} LIKE ${`${t}%`}`:e`${e.identifier(g)} = ${t}`,X=(t,n)=>{const i=n.filters.map(r=>e`m.${M(r.field)} = ${ot(r.value)}`);return t.softDeleteMode&&i.push(e`m.${M(t.softDeleteMode.field)} IS NULL`),i},J=async(t,n,i,r,o,s,a)=>{const f=X(i,o);let c=e`SELECT m.* FROM (${a}) s JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = s.${e.identifier($)}`;return f.length>0&&(c=e`${c} WHERE ${e.join(f,e` AND `)}`),c=e`${c} ORDER BY s.${e.identifier("__score__")} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`,Q(i,await A(t,n,c))},pt=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=a.length-1,u=a.map((d,_)=>Et(d,_===c)),m=e.join(u,e` OR `),S=u.map(d=>e`SUM(CASE WHEN ${d} THEN ${e.identifier(O)} ELSE 0 END)`),p=e.join(S.map(d=>e`${d} > 0`),e` AND `),w=e`SELECT ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM ${e.identifier(f)} WHERE ${m} GROUP BY ${e.identifier($)} HAVING ${p}`;return J(t,n,i,r,o,s,w)},_t=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=`${f}__vocab`,u=a.length-1,m=a.map((w,d)=>{const _=ft(w,d===u),z=_.exact?e`${e.identifier("term")} = ${_.lower}`:e`${e.identifier("term")} >= ${_.lower} AND ${e.identifier("term")} < ${_.upper}`;return e`SELECT ${e.identifier("doc")}, ${e.raw(String(d))} AS ${e.identifier("__term__")}, COUNT(*) AS ${e.identifier("__n__")} FROM ${e.identifier(c)} WHERE ${z} GROUP BY ${e.identifier("doc")}`}),S=a.map((w,d)=>e`SUM(CASE WHEN u.${e.identifier("__term__")} = ${e.raw(String(d))} THEN u.${e.identifier("__n__")} ELSE 0 END)`),p=e`SELECT f.${e.identifier($)} AS ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM (${it(m)}) u JOIN ${e.identifier(f)} f ON f.rowid = u.${e.identifier("doc")} GROUP BY f.${e.identifier($)} HAVING ${e.join(S.map(w=>e`${w} > 0`),e` AND `)}`;return J(t,n,i,r,o,s,p)},gt=async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch,f=v(o.query,y(o.definition.language));if(!a||f.length===0)return[];const c=b(r,o.indexName),u=[a.matches(c,f),...X(i,o)],m=e`SELECT m.* FROM ${e.identifier(c)} JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = ${e.identifier(c)}.${e.identifier($)} WHERE ${e.join(u,e` AND `)} ORDER BY ${a.rank(c,f)} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`;return Q(i,await A(t,n,m))},yt={ensureCompanion:async(t,n,i)=>{const{integer:r,key:o}=n.companionTypes;await E(t,n,e`CREATE TABLE IF NOT EXISTS ${e.identifier(i)} (${e.identifier(g)} ${e.raw(o)} NOT NULL, ${e.identifier($)} ${e.raw(o)} NOT NULL, ${e.identifier(O)} ${e.raw(r)} NOT NULL)`),await q(t,n,{columns:e`${L(n,g)}, ${L(n,$)}`,name:`${i}__btree`,table:i,unique:!1}),await q(t,n,{columns:L(n,$),name:`${i}__by_id`,table:i,unique:!1})},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r);const a=[...wt(U(o,s),y(s.language))],f=e.join([g,$,O].map(u=>e.identifier(u)),e`, `),c=[];for(let u=0;u<a.length;u+=H){const m=e.join(a.slice(u,u+H).map(([S,p])=>e`(${S}, ${r}, ${p})`),e`, `);c.push(e`INSERT INTO ${e.identifier(i)} (${f}) VALUES ${m}`)}await rt(t,n,c)},name:"inverted",runSearch:pt},Tt={ensureCompanion:async(t,n,i)=>{await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(i)} USING fts5(${e.identifier($t)}, ${e.identifier($)} UNINDEXED)`),await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(`${i}__vocab`)} USING fts5vocab(${e.identifier(i)}, ${e.raw("instance")})`)},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r),await E(t,n,e`INSERT INTO ${e.identifier(i)} (${e.identifier("__text__")}, ${e.identifier($)}) VALUES (${U(o,s)}, ${r})`)},name:"fts5",runSearch:_t},It={ensureCompanion:async(t,n,i)=>{const r=n.nativeTextSearch;if(r){await E(t,n,r.createCompanion(i,n.companionTypes.key));for(const o of r.createIndexes(i))await E(t,n,o)}},indexDocument:async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch;a&&(await C(t,n,i,r),await E(t,n,a.indexDocument(i,r,U(o,s))))},name:"native",runSearch:gt},T=(t,n)=>t.strategy==="native"&&n.nativeTextSearch!==void 0?It:n.supportsFts5?Tt:yt,D=(t,n)=>`${Z(t)}/${T(t,n).name}`,B=function*(t){for(const[n,i]of Object.entries(t.tables)){const r=i.searchIndexes;if(!(i.shardMode!==void 0&&i.shardMode.kind!=="global"||!r))for(const o of r)yield[n,i,o]}},I=(t,n)=>b(t,n.name),Nt=async(t,n,i,r)=>{const o=I(i,r);return W(await R(t,n,o),D(r,n)).finished?!0:et(t,n,o)},Mt=async(t,n,i,r,o,s)=>{if(!await Nt(t,n,r,o.definition))throw new l("SEARCH_INDEX_BUILDING",`search index "${o.indexName}" on table "${r}" is still backfilling and currently covers only part of the table — retry once it finishes, or run the backfillSearch admin operation to complete it now`);return T(o.definition,n).runSearch(t,n,i,r,o,s)},P=t=>t.slice(t.lastIndexOf("/")+1),j=200,Y=async(t,n,i,r,o)=>{const s=I(r,o),a=T(o,n),f=D(o,n),c=W(await R(t,n,s),f);if(c.finished)return!0;if(c.wipe&&await N(t,n,s,void 0,!1,f),(await A(t,n,n.tableExists(r))).length===0)return await N(t,n,s,void 0,!0,f),!0;let m=0,S=c.cursor;await G(t,n,i,r,async w=>{m+=1;const d=w._id;typeof d=="string"&&(S=d,await a.indexDocument(t,n,s,d,w,o))},{after:c.cursor,limit:j});const p=m<j;return await N(t,n,s,S,p,f),p},K=async(t,n,i)=>{await tt(t,i);for(const[r,,o]of B(n)){const s=I(r,o),a=D(o,i),f=await R(t,i,s);f.profile!==void 0&&P(f.profile)!==P(a)&&(await E(t,i,e`DROP TABLE IF EXISTS ${e.identifier(s)}`),await nt(t,i,s)),await T(o,i).ensureCompanion(t,i,s)}},Rt=async(t,n,i,r,o)=>{const s=I(r,o);if((await R(t,n,s)).profile!==void 0)return;let f=0;(await A(t,n,n.tableExists(r))).length>0&&await G(t,n,i,r,()=>{f+=1},{limit:1}),await N(t,n,s,void 0,f===0,D(o,n))},Ft=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){if(s.staged){await Rt(t,i,o,r,s);continue}await Y(t,i,o,r,s)}},kt=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){let a=!1;for(;!a;)a=await Y(t,i,o,r,s)}},Ht=t=>{const{dialect:n,exec:i,schema:r}=t;return async(o,s,a,f)=>{const c=r.tables[o]?.searchIndexes;if(!(!c||c.length===0))for(const u of c){if(St(f,a,u))continue;const m=I(o,u);if(a){await T(u,n).indexDocument(i,n,m,s,a,u);continue}await C(i,n,m,s)}}};export{Lt as A,Ut as C,Bt as R,qt as T,Mt as a,xt as b,Ht as c,kt as d,Ft as r,Ot as x};
|
|
1
|
+
import{LunoraError as l}from"@lunora/errors";import{D as y,c as Z,m as tt,r as R,a as nt,l as W,w as N,b as et}from"./ctx-db-search-state-DHkLBltM.mjs";import{sql as e}from"drizzle-orm";import{unionAll as it}from"@lunora/shard-engine";import{queryRun as E,decodeRows as Q,queryAll as A,queryBatch as rt,createIndexIfNotExists as q,columnRefSql as M,serializeColumnValue as ot,forEachRowPaged as G}from"./decodeGlobalRow-DtDkyN_O.mjs";const st=t=>{const n=atob(t),i=Uint8Array.from(n,r=>r.codePointAt(0)??0);return new TextDecoder().decode(i)},at=()=>new l("BAD_REQUEST","invalid cursor"),F=16,k=8,h=1024,v=(t,n)=>n.query(t),ft=(t,n)=>{if(!n)return{exact:!0,lower:t,upper:t};const i=[...t].at(-1)??"",r=(i.codePointAt(0)??0)+1;if(r>=55296&&r<=57343||r>1114111)return{exact:!0,lower:t,upper:t};const o=t.slice(0,t.length-i.length);return{exact:!1,lower:t,upper:o+String.fromCodePoint(r)}},Lt=(t,n,i)=>{const r={eq:(o,s)=>{if(!t.definition.filterFields?.includes(o))throw new l("INTERNAL",`field "${o}" is not a filter field of search index "${t.indexName}" on table "${n}"`);if(t.filters.length>=k)throw new l("BAD_REQUEST",`search index "${t.indexName}" on table "${n}": at most ${String(k)} .eq() filters are supported per search query`);return t.filters.push({field:o,value:s}),r},search:(o,s)=>{const a=t;if(o!==a.definition.field)throw new l("INTERNAL",`search index "${a.indexName}" on table "${n}" indexes "${a.definition.field}", not "${o}"`);const f=v(s,i).length;if(f>F)throw new l("BAD_REQUEST",`search index "${a.indexName}" on table "${n}": at most ${String(F)} search terms are supported (got ${String(f)})`);return a.field=o,a.query=s,a.hasQuery=!0,r}};return r},Ot=t=>{if(t.length>h)throw new l("BAD_REQUEST",`more than ${String(h)} documents match this search — narrow it with filters or read it a page at a time with .paginate()`)},xt=t=>Math.min(t.offset+t.numItems+1,h),ct=t=>btoa(`search:${String(t)}`),ut=t=>{let n;try{n=st(t)}catch{return}if(!n.startsWith("search:"))return;const i=Number(n.slice(7));return Number.isInteger(i)&&i>=0?i:void 0},Ut=t=>{if(typeof t.endCursor=="string")throw new l("BAD_REQUEST","bounded (endCursor) pagination is not supported on search queries — relevance order has no stable range boundary");if(!Number.isFinite(t.numItems))throw new l("BAD_REQUEST",`search pagination needs a finite numItems, got ${String(t.numItems)}`);const n=Math.max(0,Math.floor(t.numItems)),i=t.cursor?ut(t.cursor):0;if(i===void 0)throw at();if(i+n>=h)throw new l("BAD_REQUEST",`search pagination reaches the ${String(h)}-document limit (offset ${String(i)} + ${String(n)} requested) — a page must end below the cap so the probe row that answers \`hasMore\` still fits: retry with numItems ${String(Math.max(1,h-i-1))} or fewer, or narrow the query or the filters instead`);return{numItems:n,offset:i}},Bt=(t,n)=>{const i=n.offset+n.numItems,r=n.numItems>0&&t.length>i;return{continueCursor:r?ct(i):null,isDone:!r,page:t.slice(n.offset,i)}},qt=t=>{if(t===void 0)return h+1;if(!Number.isFinite(t))return h;const n=Math.max(0,Math.floor(t));if(n>h)throw new l("BAD_REQUEST",`search returns at most ${String(h)} documents (asked for ${String(n)}) — narrow the query or paginate instead`);return n},dt=(t,n)=>{if(!n.includes("."))return t[n];let i=t;for(const r of n.split(".")){if(i===null||typeof i!="object"||Array.isArray(i))return;i=i[r]}return i},b=(t,n)=>`${t}__fts_${n}`,$t="__text__",$="__id__",g="__token__",O="__n__",mt=1e3,V=(t,n)=>n.document(t),x=(t,n)=>dt(t,n),lt=t=>typeof t=="string"?t:t==null?"":typeof t=="number"||typeof t=="bigint"||typeof t=="boolean"?String(t):JSON.stringify(t)??"",St=(t,n,i)=>t!==void 0&&n!==void 0&&x(t,i.field)===x(n,i.field),ht=(t,n)=>V(lt(x(t,n.field)),y(n.language)).slice(0,mt),U=(t,n)=>ht(t,n).join(" "),wt=(t,n)=>{const i=new Map;for(const r of V(t,n))i.set(r,(i.get(r)??0)+1);return i},C=async(t,n,i,r)=>{await E(t,n,e`DELETE FROM ${e.identifier(i)} WHERE ${e.identifier($)} = ${r}`)},L=(t,n)=>t.name==="mysql"?e`${e.identifier(n)}(191)`:t.textPatternOperatorClass===void 0?e`${e.identifier(n)}`:e`${e.identifier(n)} ${e.raw(t.textPatternOperatorClass)}`,H=50,Et=(t,n)=>n?e`${e.identifier(g)} LIKE ${`${t}%`}`:e`${e.identifier(g)} = ${t}`,X=(t,n)=>{const i=n.filters.map(r=>e`m.${M(r.field)} = ${ot(r.value)}`);return t.softDeleteMode&&i.push(e`m.${M(t.softDeleteMode.field)} IS NULL`),i},J=async(t,n,i,r,o,s,a)=>{const f=X(i,o);let c=e`SELECT m.* FROM (${a}) s JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = s.${e.identifier($)}`;return f.length>0&&(c=e`${c} WHERE ${e.join(f,e` AND `)}`),c=e`${c} ORDER BY s.${e.identifier("__score__")} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`,Q(i,await A(t,n,c))},pt=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=a.length-1,u=a.map((d,_)=>Et(d,_===c)),m=e.join(u,e` OR `),S=u.map(d=>e`SUM(CASE WHEN ${d} THEN ${e.identifier(O)} ELSE 0 END)`),p=e.join(S.map(d=>e`${d} > 0`),e` AND `),w=e`SELECT ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM ${e.identifier(f)} WHERE ${m} GROUP BY ${e.identifier($)} HAVING ${p}`;return J(t,n,i,r,o,s,w)},_t=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=`${f}__vocab`,u=a.length-1,m=a.map((w,d)=>{const _=ft(w,d===u),z=_.exact?e`${e.identifier("term")} = ${_.lower}`:e`${e.identifier("term")} >= ${_.lower} AND ${e.identifier("term")} < ${_.upper}`;return e`SELECT ${e.identifier("doc")}, ${e.raw(String(d))} AS ${e.identifier("__term__")}, COUNT(*) AS ${e.identifier("__n__")} FROM ${e.identifier(c)} WHERE ${z} GROUP BY ${e.identifier("doc")}`}),S=a.map((w,d)=>e`SUM(CASE WHEN u.${e.identifier("__term__")} = ${e.raw(String(d))} THEN u.${e.identifier("__n__")} ELSE 0 END)`),p=e`SELECT f.${e.identifier($)} AS ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM (${it(m)}) u JOIN ${e.identifier(f)} f ON f.rowid = u.${e.identifier("doc")} GROUP BY f.${e.identifier($)} HAVING ${e.join(S.map(w=>e`${w} > 0`),e` AND `)}`;return J(t,n,i,r,o,s,p)},gt=async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch,f=v(o.query,y(o.definition.language));if(!a||f.length===0)return[];const c=b(r,o.indexName),u=[a.matches(c,f),...X(i,o)],m=e`SELECT m.* FROM ${e.identifier(c)} JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = ${e.identifier(c)}.${e.identifier($)} WHERE ${e.join(u,e` AND `)} ORDER BY ${a.rank(c,f)} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`;return Q(i,await A(t,n,m))},yt={ensureCompanion:async(t,n,i)=>{const{integer:r,key:o}=n.companionTypes;await E(t,n,e`CREATE TABLE IF NOT EXISTS ${e.identifier(i)} (${e.identifier(g)} ${e.raw(o)} NOT NULL, ${e.identifier($)} ${e.raw(o)} NOT NULL, ${e.identifier(O)} ${e.raw(r)} NOT NULL)`),await q(t,n,{columns:e`${L(n,g)}, ${L(n,$)}`,name:`${i}__btree`,table:i,unique:!1}),await q(t,n,{columns:L(n,$),name:`${i}__by_id`,table:i,unique:!1})},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r);const a=[...wt(U(o,s),y(s.language))],f=e.join([g,$,O].map(u=>e.identifier(u)),e`, `),c=[];for(let u=0;u<a.length;u+=H){const m=e.join(a.slice(u,u+H).map(([S,p])=>e`(${S}, ${r}, ${p})`),e`, `);c.push(e`INSERT INTO ${e.identifier(i)} (${f}) VALUES ${m}`)}await rt(t,n,c)},name:"inverted",runSearch:pt},Tt={ensureCompanion:async(t,n,i)=>{await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(i)} USING fts5(${e.identifier($t)}, ${e.identifier($)} UNINDEXED)`),await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(`${i}__vocab`)} USING fts5vocab(${e.identifier(i)}, ${e.raw("instance")})`)},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r),await E(t,n,e`INSERT INTO ${e.identifier(i)} (${e.identifier("__text__")}, ${e.identifier($)}) VALUES (${U(o,s)}, ${r})`)},name:"fts5",runSearch:_t},It={ensureCompanion:async(t,n,i)=>{const r=n.nativeTextSearch;if(r){await E(t,n,r.createCompanion(i,n.companionTypes.key));for(const o of r.createIndexes(i))await E(t,n,o)}},indexDocument:async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch;a&&(await C(t,n,i,r),await E(t,n,a.indexDocument(i,r,U(o,s))))},name:"native",runSearch:gt},T=(t,n)=>t.strategy==="native"&&n.nativeTextSearch!==void 0?It:n.supportsFts5?Tt:yt,D=(t,n)=>`${Z(t)}/${T(t,n).name}`,B=function*(t){for(const[n,i]of Object.entries(t.tables)){const r=i.searchIndexes;if(!(i.shardMode!==void 0&&i.shardMode.kind!=="global"||!r))for(const o of r)yield[n,i,o]}},I=(t,n)=>b(t,n.name),Nt=async(t,n,i,r)=>{const o=I(i,r);return W(await R(t,n,o),D(r,n)).finished?!0:et(t,n,o)},Mt=async(t,n,i,r,o,s)=>{if(!await Nt(t,n,r,o.definition))throw new l("SEARCH_INDEX_BUILDING",`search index "${o.indexName}" on table "${r}" is still backfilling and currently covers only part of the table — retry once it finishes, or run the backfillSearch admin operation to complete it now`);return T(o.definition,n).runSearch(t,n,i,r,o,s)},P=t=>t.slice(t.lastIndexOf("/")+1),j=200,Y=async(t,n,i,r,o)=>{const s=I(r,o),a=T(o,n),f=D(o,n),c=W(await R(t,n,s),f);if(c.finished)return!0;if(c.wipe&&await N(t,n,s,void 0,!1,f),(await A(t,n,n.tableExists(r))).length===0)return await N(t,n,s,void 0,!0,f),!0;let m=0,S=c.cursor;await G(t,n,i,r,async w=>{m+=1;const d=w._id;typeof d=="string"&&(S=d,await a.indexDocument(t,n,s,d,w,o))},{after:c.cursor,limit:j});const p=m<j;return await N(t,n,s,S,p,f),p},K=async(t,n,i)=>{await tt(t,i);for(const[r,,o]of B(n)){const s=I(r,o),a=D(o,i),f=await R(t,i,s);f.profile!==void 0&&P(f.profile)!==P(a)&&(await E(t,i,e`DROP TABLE IF EXISTS ${e.identifier(s)}`),await nt(t,i,s)),await T(o,i).ensureCompanion(t,i,s)}},Rt=async(t,n,i,r,o)=>{const s=I(r,o);if((await R(t,n,s)).profile!==void 0)return;let f=0;(await A(t,n,n.tableExists(r))).length>0&&await G(t,n,i,r,()=>{f+=1},{limit:1}),await N(t,n,s,void 0,f===0,D(o,n))},Ft=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){if(s.staged){await Rt(t,i,o,r,s);continue}await Y(t,i,o,r,s)}},kt=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){let a=!1;for(;!a;)a=await Y(t,i,o,r,s)}},Ht=t=>{const{dialect:n,exec:i,schema:r}=t;return async(o,s,a,f)=>{const c=r.tables[o]?.searchIndexes;if(!(!c||c.length===0))for(const u of c){if(St(f,a,u))continue;const m=I(o,u);if(a){await T(u,n).indexDocument(i,n,m,s,a,u);continue}await C(i,n,m,s)}}};export{Lt as A,Ut as C,Bt as R,qt as T,Mt as a,xt as b,Ht as c,kt as d,Ft as r,Ot as x};
|
package/dist/packem_shared/{ctx-db-search-state-Xa6UmA3s.mjs → ctx-db-search-state-DHkLBltM.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{sql as e}from"drizzle-orm";import{queryRun as p,queryAll as m}from"./decodeGlobalRow-
|
|
1
|
+
import{sql as e}from"drizzle-orm";import{queryRun as p,queryAll as m}from"./decodeGlobalRow-DtDkyN_O.mjs";const y=["de","en","es","fr","it","nl","none","pt"],L=i=>y.includes(i),A="a an and are as at be but by for if in into is it no not of on or such that the their then there these they this to was will with",R="aber als am an auch auf aus bei bin bis bist da dass der den des dem die das denn dir du ein eine für hat ich im in ist mit nicht noch nur oder sich sie sind über und von vor war wie wir zu zum zur",F="a al como con de del el en es la las lo los mas no o para pero por que se su sus un una uno y ya",N="au aux avec ce ces dans de des du elle en et eux il je la le les leur lui ma mais me même mes moi mon ne nos notre nous on ou par pas pour qu que qui sa se ses son sur ta te tes toi ton tu un une vos votre vous y",_="a ai al alla anche che chi ci coi col come con da dal degli dei del della di do e ed gli ha hai hanno i il in la le lo ma mi ne nei nel non o per più quale quanto se si sono su sul tra un una uno vi",q="aan al als bij dan dat de der deze die dit door een en er het hij ij in is je kan me men met mij na naar niet nog nu of om ons ook op over te tot uit van voor was wat we wij zij zijn zo",x="a ao aos as até com como da das de do dos e em entre era essa esse esta este eu foi há isso já mais mas me mesmo meu na nas no nos num numa o os ou para pela pelo por qual que quem se sem seu só sua também te tem um uma você",C=new RegExp("(?<=\\p{Script=Cyrillic})[\\u0300-\\u0305\\u0307\\u0309-\\u036F]|(?<!\\p{Script=Cyrillic})[\\u0300-\\u036F]","gu"),O=/[\u0080-\u{10FFFF}]/u,b=/[\p{L}\p{N}]+/gu,H=/[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\p{Script_Extensions=Hangul}]+/gu,g=/[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\p{Script_Extensions=Hangul}]/u,D=i=>{if(!g.test(i))return[i];const n=[];let t=0;for(const r of i.matchAll(H)){const o=r.index,s=[...r[0]];if(o>t&&n.push(i.slice(t,o)),t=o+r[0].length,s.length===1){n.push(r[0]);continue}for(let a=0;a+1<s.length;a+=1)n.push(`${String(s[a])}${String(s[a+1])}`)}return t<i.length&&n.push(i.slice(t)),n},v=i=>O.test(i)?i.normalize("NFD").replaceAll(C,"").normalize("NFC").toLowerCase():i.toLowerCase(),f=i=>new Set(v(i).split(" ")),j={de:f(R),en:f(A),es:f(F),fr:f(N),it:f(_),nl:f(q),none:new Set,pt:f(x)},z=3,U=256,w=new Map,k=i=>{const n=i!==void 0&&L(i)?i:"none",t=w.get(n);if(t)return t;const r=j[n],o=a=>{const d=v(a),$=d.match(b)??[],u=(g.test(d)?$.flatMap(l=>D(l)):$).filter(l=>l.length<=U);return r.size===0?u:u.filter(l=>!r.has(l))},s={document:o,profile:`${n}-v${String(z)}`,query:a=>{const d=o(a);return d.filter(($,u)=>d.lastIndexOf($)===u)}};return w.set(n,s),s},W=i=>`${k(i.language).profile}:${i.field}`,S=i=>{const n=i.slice(i.indexOf(":")+1),t=n.lastIndexOf("/");return t===-1?n:n.slice(0,t)},V=(i,n)=>i.profile!==n?{cursor:void 0,finished:!1,wipe:i.cursor!==void 0||i.done}:{cursor:i.cursor,finished:i.done,wipe:!1},I=(i,n)=>typeof i=="string"&&S(i)===S(n),c="__lunora_search_state",K=async(i,n)=>{const{integer:t,key:r}=n.companionTypes,o=async(s,a)=>{try{await p(i,n,e`ALTER TABLE ${e.identifier(c)} ADD COLUMN ${e.identifier(s)} ${e.raw(a)}`)}catch{}};await p(i,n,e`CREATE TABLE IF NOT EXISTS ${e.identifier(c)} (${e.identifier("companion")} ${e.raw(r)} PRIMARY KEY, ${e.identifier("cursor")} ${e.raw(r)}, ${e.identifier("done")} ${e.raw(t)} NOT NULL DEFAULT 0, ${e.identifier("profile")} ${e.raw(r)}, ${e.identifier("covered")} ${e.raw(t)} NOT NULL DEFAULT 0)`),await o("profile",r),await o("covered",`${t} NOT NULL DEFAULT 0`),await p(i,n,e`UPDATE ${e.identifier(c)} SET ${e.identifier("covered")} = 1 WHERE ${e.identifier("done")} = 1 AND ${e.identifier("covered")} = 0`)},T=i=>i===1||i===!0||i==="1",P=async(i,n,t)=>{const r=await m(i,n,e`SELECT ${e.identifier("covered")} FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`);return T(r[0]?.covered)},Y=async(i,n,t)=>{await p(i,n,e`DELETE FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`)},X=async(i,n,t)=>{const o=(await m(i,n,e`SELECT ${e.identifier("cursor")}, ${e.identifier("done")}, ${e.identifier("profile")} FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`))[0];if(!o)return{cursor:void 0,done:!1,profile:void 0};const{cursor:s,profile:a}=o;return{cursor:typeof s=="string"?s:void 0,done:T(o.done),profile:typeof a=="string"?a:void 0}},G=async(i,n,t,r,o,s)=>{const a=r??null,d=await m(i,n,e`SELECT ${e.identifier("profile")} FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`),$=I(d[0]?.profile,s),u=o?1:0,l=o||!$?e`${e.identifier("covered")} = ${u}`:e`${e.identifier("covered")} = ${e.identifier("covered")}`,h=e`UPDATE ${e.identifier(c)} SET ${e.identifier("cursor")} = ${a}, ${e.identifier("done")} = ${u}, ${e.identifier("profile")} = ${s}, ${l} WHERE ${e.identifier("companion")} = ${t}`;if(d.length>0){await p(i,n,h);return}try{await p(i,n,e`INSERT INTO ${e.identifier(c)} (${e.identifier("companion")}, ${e.identifier("cursor")}, ${e.identifier("done")}, ${e.identifier("profile")}, ${e.identifier("covered")}) VALUES (${t}, ${a}, ${o?1:0}, ${s}, ${o?1:0})`)}catch(E){if(!n.isUniqueViolation(E))throw E;await p(i,n,h)}};export{k as D,c as S,Y as a,P as b,W as c,V as l,K as m,X as r,G as w};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{renderSql as m}from"@lunora/shard-engine";import{sql as r}from"drizzle-orm";import{s as $,b as I,e as N}from"./value-codec-DC_dHxUY.mjs";import{OCC_VERSION_COLUMN as j}from"./OCC_VERSION_COLUMN-ByC64Jgx.mjs";const R=e=>e==="_id"||e==="id"?"id":e,K=e=>r`${r.identifier(R(e))}`,D=(e,n)=>r`${r.identifier(e)}.${r.identifier(R(n))}`,v=e=>{const n=[];for(const[t,o]of Object.entries(e.shape)){const i=o._meta?.column;i&&n.push([t,i])}return n},O=(e,n,t)=>{const{params:o,sql:i}=m(n.name,t);return e.all(i,o)},u=(e,n,t)=>{const{params:o,sql:i}=m(n.name,t);return e.run(i,o)},B=async(e,n,t)=>{if(t.length!==0){if(!e.batch){for(const o of t)await u(e,n,o);return}await e.batch(t.map(o=>m(n.name,o)))}},F=async(e,n,t)=>{const o=t.unique?r`UNIQUE `:r``;if(n.name==="mysql"){try{await u(e,n,r`CREATE ${o}INDEX ${r.identifier(t.name)} ON ${r.identifier(t.table)} (${t.columns})`)}catch(i){const s=i;if(s.errno!==1061&&s.code!=="ER_DUP_KEYNAME"&&s.code!==1061)throw i}return}await u(e,n,r`CREATE ${o}INDEX IF NOT EXISTS ${r.identifier(t.name)} ON ${r.identifier(t.table)} (${t.columns})`)},L=$,d=e=>{if(e===void 0)return!1;if(e._meta?.column?.notNull===!1)return!0;switch(e.kind){case"any":case"null":return!0;case"literal":return e._meta?.value===null;case"optional":return d(e._meta?.inner);case"union":return e._meta?.members?.some(n=>d(n))===!0;default:return!1}},g=e=>e.kind==="optional"&&!d(e),w=new WeakMap,_=e=>{let n=w.get(e);return n===void 0&&(n=Object.entries(e.shape).map(([t,o])=>[t,N(o),g(o)]),w.set(e,n)),n},p=new WeakMap,S=(e,n)=>{let t=p.get(e);return t===void 0&&(t=new Map(_(e).map(([o,i])=>[o,i])),p.set(e,t)),t.get(n)},U=(e,n,t)=>$(t,S(e,n)),M=(e,n)=>{const t={};for(const[o,i,s]of _(e)){const c=n[o];c===void 0||s&&c===null||(t[o]=I(c,i))}return t._id=n.id,t._creationTime=n._creationTime,t},q=(e,n)=>n?M(e,n):null,x=(e,n)=>{const t=[];for(const o of n){const i=q(e,o);i&&t.push(i)}return t},C=500,z=async(e,n,t,o,i,s={})=>{let c=s.after,a=s.limit,f=!0;for(;f&&(a===void 0||a>0);){const E=a===void 0?C:Math.min(C,a),b=c===void 0?r``:r` WHERE ${r.identifier("id")} > ${c}`,l=await O(e,n,r`SELECT * FROM ${r.identifier(o)}${b} ORDER BY ${r.identifier("id")} ASC LIMIT ${r.raw(String(E))}`);for(const y of l){const h=q(t,y);h&&await i(h)}c=l.at(-1)?.id,f=l.length===E,a=a===void 0?void 0:a-l.length}};export{C as BACKFILL_BATCH_SIZE,j as OCC_VERSION_COLUMN,K as columnRefSql,F as createIndexIfNotExists,M as decodeGlobalRow,q as decodeRow,x as decodeRows,z as forEachRowPaged,R as physicalColumn,D as qualifiedColumnRefSql,O as queryAll,B as queryBatch,u as queryRun,L as serializeColumnValue,U as serializeDocumentColumn,v as tableColumns};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as N}from"@lunora/errors";import{aggregateTableName as x,rankTableName as D,sortColumnName as A,renderSql as U,bigintSqlKey as M}from"@lunora/shard-engine";import{sql as n}from"drizzle-orm";import{queryRun as g,queryAll as w,createIndexIfNotExists as E,tableColumns as k,qualifiedColumnRefSql as B,columnRefSql as d,queryBatch as P}from"./decodeGlobalRow-DtDkyN_O.mjs";import{e as p,i as j,B as b,r as F,U as K,a as G}from"./value-codec-DC_dHxUY.mjs";import{OCC_VERSION_COLUMN as h}from"./OCC_VERSION_COLUMN-ByC64Jgx.mjs";const L=(i,t,e=!1)=>t.columnType(p(i),{unique:e||i._meta?.column?.unique===!0}),S=i=>{const t=new Set;for(const e of i.indexes)e.unique===!0&&e.fields.length===1&&t.add(e.fields[0]);return t},Y=(i,t,e)=>{if(e.indexKeyPrefix!==void 0)for(const o of t.indexes){if(o.unique!==!0||o.fields.length<=1)continue;const r=o.fields.filter(u=>{const l=t.shape[u];return l!==void 0&&e.indexKeyPrefix?.(p(l))!==void 0});if(r.length>0)throw new Error(`${i}.${o.name}: a composite unique index cannot cover ${r.join(", ")} in full on this engine (its key limit forces a prefix, which would constrain the prefix rather than the value). Split it into single-column unique indexes, or store those fields as a bounded type.`)}},V=(i,t,e)=>{const o=[],r=S(t);for(const[a,f]of Object.entries(t.shape)){if(!f._meta?.column)continue;const c=f._meta.column.notNull&&f.kind!=="optional"?" NOT NULL":"";o.push(n`${n.identifier(a)} ${n.raw(`${L(f,e,r.has(a))}${c}`)}`)}const u=[...e.frameworkColumns().map(a=>n`${n.identifier(a.name)} ${n.raw(a.type)}`),n`${n.identifier(h)} ${n.raw(e.companionTypes.integer)}`],l=u.length+o.length,s=e.maxTableColumns;if(s!==void 0&&l>s){const a=l-1===s,f="Move one or more fields into a single object field, or split the table — either way the existing rows need a data migration (`defineMigration` + `lunora migrate up`); there is no in-place path.";throw new N("VALIDATION_ERROR",`@lunora/sql-store: global table "${i}" needs ${String(l)} columns, over this engine's ${String(s)}-column limit. ${a?`One of them is "${h}", the row version every guarded write reads, which caps declared fields at ${String(s-u.length)}. A table provisioned at ${String(s)} columns before that column existed cannot be widened to hold it. `:""}${f}`)}return n.join([...u,...o],n`, `)},H=i=>{if(i.indexKeyPrefix?.("string")===void 0)return n`${d("_creationTime")}, ${d("id")}`},q=async(i,t,e,o)=>{const r=n.join(e.refs.map(l=>n`${l} IS NOT NULL`),n` AND `);if((await w(i,t,n`SELECT ${e.columns} FROM ${n.identifier(e.table)} WHERE ${r} GROUP BY ${e.columns} HAVING COUNT(*) > 1 LIMIT 1`)).length>0)throw new N("INTERNAL",`unique index "${e.name}" on "${e.table}" ${o}: existing rows are duplicates under it. De-duplicate the table with a data migration first.`)},I=async(i,t,e)=>{if(!e.unique){await E(i,t,e);return}try{await E(i,t,e)}catch(o){throw t.isUniqueViolation(o)&&await q(i,t,e,"cannot be created"),o}},X=async(i,t,e)=>{if(t.name!=="sqlite")return;const r=(await w(i,t,n`SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ${e.name} AND tbl_name = ${e.table}`))[0]?.sql;if(typeof r!="string")return;const u=a=>{const f=a.indexOf("(");return f===-1?void 0:a.slice(f,a.lastIndexOf(")")+1)},l=e.unique?n`UNIQUE `:n``,s=u(U(t.name,n`CREATE ${l}INDEX ${n.identifier(e.name)} ON ${n.identifier(e.table)} (${e.columns})`).sql);s===void 0||s===u(r)||(e.unique&&await q(i,t,e,"cannot be re-created with its new column list; the previous index is left in place"),await g(i,t,n`DROP INDEX IF EXISTS ${n.identifier(e.name)}`))},W=async(i,t,e,o)=>{Y(t,e,o);const r=S(e),u=s=>{const a=d(s),f=e.shape[s],c=f&&o.indexKeyPrefix&&f._meta?.column?.unique!==!0&&!r.has(s)?o.indexKeyPrefix(p(f)):void 0;return c===void 0?a:n`${a}(${n.raw(String(c))})`},l=H(o);for(const s of e.indexes){const a=s.fields.map($=>u($)),f=n.join(a,n`, `),c=s.unique??!1,m={columns:c||l===void 0?f:n`${f}, ${l}`,name:`${t}_${s.name}`,refs:a,table:t,unique:c};await X(i,o,m),await I(i,o,m)}l!==void 0&&await E(i,o,{columns:l,name:`${t}__by_creation`,table:t,unique:!1});for(const[s,a]of k(e)){if(!a.unique)continue;const f=u(s);await I(i,o,{columns:f,name:`${t}_unique_${s}`,refs:[f],table:t,unique:!0})}},_="__lunora_migration_state",Q=async(i,t)=>{await g(i,t,n`CREATE TABLE IF NOT EXISTS ${n.identifier(_)} (${n.identifier("name")} ${n.raw(t.companionTypes.key)} PRIMARY KEY)`)},z=async(i,t,e)=>(await w(i,t,n`SELECT ${n.identifier("name")} FROM ${n.identifier(_)} WHERE ${n.identifier("name")} = ${e}`)).length>0,J=async(i,t,e,o,r,u)=>{r>0&&u!==void 0&&console.warn(`[@lunora/sql-store] storage migration "${e}" on "${o}": ${String(r)} row(s) ${u} and were left as stored — range filters and ORDER BY over them stay wrong until they are corrected by hand.`);try{await g(i,t,n`INSERT INTO ${n.identifier(_)} (${n.identifier("name")}) VALUES (${e})`)}catch(l){if(!t.isUniqueViolation(l))throw l}},Z=100,nn=(i,t)=>{const e=[],o=[];for(const r of t.fields){const u=i[r],l=t.convert(u);l!==void 0&&(e.push(n`${d(r)} = ${l}`),o.push(n`${d(r)} = ${u}`))}return{assignments:e,guards:o}},O=async(i,t,e,o)=>{if(o.fields.length===0||(await Q(i,e),await z(i,e,o.marker)))return;const r=n.join(o.fields.map(a=>o.probe(a)),n` OR `),u=n.join([d("id"),...o.fields.map(a=>d(a))],n`, `);let l="",s=0;for(;;){const a=await w(i,e,n`SELECT ${u} FROM ${n.identifier(t)} WHERE (${r}) AND ${d("id")} > ${l} ORDER BY ${d("id")} LIMIT ${n.raw(String(Z))}`);if(a.length===0){await J(i,e,o.marker,t,s,o.describeUnconvertible);return}const f=[];for(const m of a){const{id:$}=m,{assignments:T,guards:C}=typeof $=="string"?nn(m,o):{assignments:[],guards:[]};if(T.length===0){s+=1;continue}f.push(n`UPDATE ${n.identifier(t)} SET ${n.join(T,n`, `)} WHERE ${d("id")} = ${$} AND ${n.join(C,n` AND `)}`)}await P(i,e,f);const c=a.at(-1)?.id;if(typeof c!="string")return;l=c}},R=i=>`'${i}%'`,en=i=>Object.entries(i.shape).filter(([,t])=>t._meta?.column!==void 0&&p(t)==="bigint").map(([t])=>t),tn=i=>Object.entries(i.shape).filter(([,t])=>t._meta?.column!==void 0&&j(p(t))).map(([t])=>t),on=async(i,t,e,o)=>O(i,t,o,{convert:r=>{if(!(typeof r!="string"||r.length===b))try{return M(BigInt(r))}catch{return}},describeUnconvertible:"hold a value the order-preserving key cannot represent (non-numeric text, or a magnitude past 39 digits)",fields:en(e),marker:`bigint-key:${t}`,probe:r=>n`(${d(r)} IS NOT NULL AND LENGTH(${d(r)}) <> ${b})`}),rn=async(i,t,e,o)=>O(i,t,o,{convert:F,fields:tn(e),marker:`untyped-number-key:${t}`,probe:r=>n`(${d(r)} LIKE ${n.raw(R(K))} AND ${d(r)} NOT LIKE ${n.raw(R(G))})`}),sn=async(i,t,e,o)=>{const r=[[h,o.companionTypes.integer],...Object.entries(e.shape).filter(([,s])=>s._meta?.column!==void 0).map(([s,a])=>[s,L(a,o)])],u="t",l=async s=>{try{return await w(i,o,n`SELECT ${n.join(s.map(a=>B(u,a)),n`, `)} FROM ${n.identifier(t)} AS ${n.identifier(u)} LIMIT 0`),!0}catch{return!1}};if(!await l(r.map(([s])=>s)))for(const[s,a]of r)await l([s])||await g(i,o,n`ALTER TABLE ${n.identifier(t)} ADD COLUMN ${n.identifier(s)} ${n.raw(a)}`)},gn=async(i,t,e)=>{for(const[o,r]of Object.entries(t.tables)){if(r.shardMode?.kind!=="global")continue;const u=V(o,r,e);await g(i,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(o)} (${u})`),await sn(i,o,r,e),await W(i,o,r,e),await on(i,o,r,e),await rn(i,o,r,e)}},v=i=>i.shardMode===void 0||i.shardMode.kind==="global",pn=async(i,t,e)=>{const{integer:o,key:r,real:u}=e.companionTypes;for(const[l,s]of Object.entries(t.tables)){const a=s.aggregateIndexes;if(!(!a||a.length===0||!v(s)))for(const f of a){const c=x(l,f.name);await g(i,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(c)} (${n.identifier("__key__")} ${n.raw(r)} PRIMARY KEY, ${n.identifier("__value__")} ${n.raw(u)}, ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0)`),e.name==="sqlite"&&((await w(i,e,n`PRAGMA table_info(${n.identifier(c)})`)).some($=>$.name==="__count__")||await g(i,e,n`ALTER TABLE ${n.identifier(c)} ADD COLUMN ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0`))}}},y=(i,t,e,o)=>{const r=i.name==="mysql"&&o?n`${n.identifier(t)}(191)`:n`${n.identifier(t)}`;return n`${r} ${n.raw(e)}`},an=(i,t,e)=>{const o=[y(i,"__partition__","ASC",!0)];for(const[r,u]of t.sortBy.entries()){const l=e.shape[u.field],s=l!==void 0&&i.indexKeyPrefix?.(p(l))!==void 0;o.push(y(i,A(r),u.direction==="desc"?"DESC":"ASC",s))}return o.push(y(i,"__id__","ASC",!0)),o},un=(i,t,e)=>t.sortBy.map((o,r)=>{const u=e.shape[o.field],l=i.columnType(u?p(u):void 0);return n`${n.identifier(A(r))} ${n.raw(l)}`}),wn=async(i,t,e)=>{const{key:o}=e.companionTypes;for(const[r,u]of Object.entries(t.tables)){const l=u.rankIndexes;if(!(!l||l.length===0||!v(u)))for(const s of l){const a=D(r,s.name),f=un(e,s,u),c=f.length>0?n`, ${n.join(f,n`, `)}`:n``;await g(i,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(a)} (${n.identifier("__id__")} ${n.raw(o)} PRIMARY KEY, ${n.identifier("__partition__")} ${n.raw(o)} NOT NULL${c})`);const m=an(e,s,u),$=`${r}__rank_${s.name}__btree`;await E(i,e,{columns:n.join(m,n`, `),name:$,table:a,unique:!1})}}};export{pn as runSqlAggregateMigrations,gn as runSqlGlobalTableMigrations,wn as runSqlRankMigrations};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/sql-store",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.131",
|
|
4
4
|
"description": "Internal dialect-parameterized SQL store core for Lunora .global() backends (D1, PlanetScale)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
54
|
-
"@lunora/shard-engine": "1.0.0-alpha.
|
|
53
|
+
"@lunora/errors": "1.0.0-alpha.41",
|
|
54
|
+
"@lunora/shard-engine": "1.0.0-alpha.79",
|
|
55
55
|
"drizzle-orm": "^0.45.2"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"./ctx-db-search-state-Xa6UmA3s.mjs";import{d as S,c as s,a as i,r as l}from"./ctx-db-search-CkWBiP2e.mjs";import"./decodeGlobalRow-D0xGl2NU.mjs";export{S as backfillSqlSearchIndexes,s as createSearchSync,i as runSqlSearch,l as runSqlSearchMigrations};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{renderSql as m}from"@lunora/shard-engine";import{sql as i}from"drizzle-orm";import{s as p,b as I,e as N}from"./value-codec-DC_dHxUY.mjs";const K="_version",R=e=>e==="_id"||e==="id"?"id":e,v=e=>i`${i.identifier(R(e))}`,D=(e,n)=>i`${i.identifier(e)}.${i.identifier(R(n))}`,B=e=>{const n=[];for(const[t,o]of Object.entries(e.shape)){const r=o._meta?.column;r&&n.push([t,r])}return n},O=(e,n,t)=>{const{params:o,sql:r}=m(n.name,t);return e.all(r,o)},u=(e,n,t)=>{const{params:o,sql:r}=m(n.name,t);return e.run(r,o)},F=async(e,n,t)=>{if(t.length!==0){if(!e.batch){for(const o of t)await u(e,n,o);return}await e.batch(t.map(o=>m(n.name,o)))}},L=async(e,n,t)=>{const o=t.unique?i`UNIQUE `:i``;if(n.name==="mysql"){try{await u(e,n,i`CREATE ${o}INDEX ${i.identifier(t.name)} ON ${i.identifier(t.table)} (${t.columns})`)}catch(r){const s=r;if(s.errno!==1061&&s.code!=="ER_DUP_KEYNAME"&&s.code!==1061)throw r}return}await u(e,n,i`CREATE ${o}INDEX IF NOT EXISTS ${i.identifier(t.name)} ON ${i.identifier(t.table)} (${t.columns})`)},U=p,d=e=>{if(e===void 0)return!1;if(e._meta?.column?.notNull===!1)return!0;switch(e.kind){case"any":case"null":return!0;case"literal":return e._meta?.value===null;case"optional":return d(e._meta?.inner);case"union":return e._meta?.members?.some(n=>d(n))===!0;default:return!1}},g=e=>e.kind==="optional"&&!d(e),w=new WeakMap,_=e=>{let n=w.get(e);return n===void 0&&(n=Object.entries(e.shape).map(([t,o])=>[t,N(o),g(o)]),w.set(e,n)),n},C=new WeakMap,S=(e,n)=>{let t=C.get(e);return t===void 0&&(t=new Map(_(e).map(([o,r])=>[o,r])),C.set(e,t)),t.get(n)},z=(e,n,t)=>p(t,S(e,n)),M=(e,n)=>{const t={};for(const[o,r,s]of _(e)){const c=n[o];c===void 0||s&&c===null||(t[o]=I(c,r))}return t._id=n.id,t._creationTime=n._creationTime,t},q=(e,n)=>n?M(e,n):null,W=(e,n)=>{const t=[];for(const o of n){const r=q(e,o);r&&t.push(r)}return t},$=500,X=async(e,n,t,o,r,s={})=>{let c=s.after,a=s.limit,f=!0;for(;f&&(a===void 0||a>0);){const E=a===void 0?$:Math.min($,a),b=c===void 0?i``:i` WHERE ${i.identifier("id")} > ${c}`,l=await O(e,n,i`SELECT * FROM ${i.identifier(o)}${b} ORDER BY ${i.identifier("id")} ASC LIMIT ${i.raw(String(E))}`);for(const y of l){const h=q(t,y);h&&await r(h)}c=l.at(-1)?.id,f=l.length===E,a=a===void 0?void 0:a-l.length}};export{$ as BACKFILL_BATCH_SIZE,K as OCC_VERSION_COLUMN,v as columnRefSql,L as createIndexIfNotExists,M as decodeGlobalRow,q as decodeRow,W as decodeRows,X as forEachRowPaged,R as physicalColumn,D as qualifiedColumnRefSql,O as queryAll,F as queryBatch,u as queryRun,U as serializeColumnValue,z as serializeDocumentColumn,B as tableColumns};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as N}from"@lunora/errors";import{aggregateTableName as x,rankTableName as D,sortColumnName as A,renderSql as U,bigintSqlKey as M}from"@lunora/shard-engine";import{sql as n}from"drizzle-orm";import{queryRun as g,queryAll as E,createIndexIfNotExists as p,OCC_VERSION_COLUMN as h,tableColumns as k,qualifiedColumnRefSql as B,columnRefSql as d,queryBatch as P}from"./decodeGlobalRow-D0xGl2NU.mjs";import{e as w,i as j,B as b,r as F,U as K,a as G}from"./value-codec-DC_dHxUY.mjs";const L=(i,t,e=!1)=>t.columnType(w(i),{unique:e||i._meta?.column?.unique===!0}),S=i=>{const t=new Set;for(const e of i.indexes)e.unique===!0&&e.fields.length===1&&t.add(e.fields[0]);return t},Y=(i,t,e)=>{if(e.indexKeyPrefix!==void 0)for(const o of t.indexes){if(o.unique!==!0||o.fields.length<=1)continue;const r=o.fields.filter(u=>{const l=t.shape[u];return l!==void 0&&e.indexKeyPrefix?.(w(l))!==void 0});if(r.length>0)throw new Error(`${i}.${o.name}: a composite unique index cannot cover ${r.join(", ")} in full on this engine (its key limit forces a prefix, which would constrain the prefix rather than the value). Split it into single-column unique indexes, or store those fields as a bounded type.`)}},V=(i,t,e)=>{const o=[],r=S(t);for(const[a,f]of Object.entries(t.shape)){if(!f._meta?.column)continue;const c=f._meta.column.notNull&&f.kind!=="optional"?" NOT NULL":"";o.push(n`${n.identifier(a)} ${n.raw(`${L(f,e,r.has(a))}${c}`)}`)}const u=[...e.frameworkColumns().map(a=>n`${n.identifier(a.name)} ${n.raw(a.type)}`),n`${n.identifier(h)} ${n.raw(e.companionTypes.integer)}`],l=u.length+o.length,s=e.maxTableColumns;if(s!==void 0&&l>s){const a=l-1===s,f="Move one or more fields into a single object field, or split the table — either way the existing rows need a data migration (`defineMigration` + `lunora migrate up`); there is no in-place path.";throw new N("VALIDATION_ERROR",`@lunora/sql-store: global table "${i}" needs ${String(l)} columns, over this engine's ${String(s)}-column limit. ${a?`One of them is "${h}", the row version every guarded write reads, which caps declared fields at ${String(s-u.length)}. A table provisioned at ${String(s)} columns before that column existed cannot be widened to hold it. `:""}${f}`)}return n.join([...u,...o],n`, `)},H=i=>{if(i.indexKeyPrefix?.("string")===void 0)return n`${d("_creationTime")}, ${d("id")}`},q=async(i,t,e,o)=>{const r=n.join(e.refs.map(l=>n`${l} IS NOT NULL`),n` AND `);if((await E(i,t,n`SELECT ${e.columns} FROM ${n.identifier(e.table)} WHERE ${r} GROUP BY ${e.columns} HAVING COUNT(*) > 1 LIMIT 1`)).length>0)throw new N("INTERNAL",`unique index "${e.name}" on "${e.table}" ${o}: existing rows are duplicates under it. De-duplicate the table with a data migration first.`)},I=async(i,t,e)=>{if(!e.unique){await p(i,t,e);return}try{await p(i,t,e)}catch(o){throw t.isUniqueViolation(o)&&await q(i,t,e,"cannot be created"),o}},X=async(i,t,e)=>{if(t.name!=="sqlite")return;const r=(await E(i,t,n`SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ${e.name} AND tbl_name = ${e.table}`))[0]?.sql;if(typeof r!="string")return;const u=a=>{const f=a.indexOf("(");return f===-1?void 0:a.slice(f,a.lastIndexOf(")")+1)},l=e.unique?n`UNIQUE `:n``,s=u(U(t.name,n`CREATE ${l}INDEX ${n.identifier(e.name)} ON ${n.identifier(e.table)} (${e.columns})`).sql);s===void 0||s===u(r)||(e.unique&&await q(i,t,e,"cannot be re-created with its new column list; the previous index is left in place"),await g(i,t,n`DROP INDEX IF EXISTS ${n.identifier(e.name)}`))},W=async(i,t,e,o)=>{Y(t,e,o);const r=S(e),u=s=>{const a=d(s),f=e.shape[s],c=f&&o.indexKeyPrefix&&f._meta?.column?.unique!==!0&&!r.has(s)?o.indexKeyPrefix(w(f)):void 0;return c===void 0?a:n`${a}(${n.raw(String(c))})`},l=H(o);for(const s of e.indexes){const a=s.fields.map($=>u($)),f=n.join(a,n`, `),c=s.unique??!1,m={columns:c||l===void 0?f:n`${f}, ${l}`,name:`${t}_${s.name}`,refs:a,table:t,unique:c};await X(i,o,m),await I(i,o,m)}l!==void 0&&await p(i,o,{columns:l,name:`${t}__by_creation`,table:t,unique:!1});for(const[s,a]of k(e)){if(!a.unique)continue;const f=u(s);await I(i,o,{columns:f,name:`${t}_unique_${s}`,refs:[f],table:t,unique:!0})}},_="__lunora_migration_state",Q=async(i,t)=>{await g(i,t,n`CREATE TABLE IF NOT EXISTS ${n.identifier(_)} (${n.identifier("name")} ${n.raw(t.companionTypes.key)} PRIMARY KEY)`)},z=async(i,t,e)=>(await E(i,t,n`SELECT ${n.identifier("name")} FROM ${n.identifier(_)} WHERE ${n.identifier("name")} = ${e}`)).length>0,J=async(i,t,e,o,r,u)=>{r>0&&u!==void 0&&console.warn(`[@lunora/sql-store] storage migration "${e}" on "${o}": ${String(r)} row(s) ${u} and were left as stored — range filters and ORDER BY over them stay wrong until they are corrected by hand.`);try{await g(i,t,n`INSERT INTO ${n.identifier(_)} (${n.identifier("name")}) VALUES (${e})`)}catch(l){if(!t.isUniqueViolation(l))throw l}},Z=100,nn=(i,t)=>{const e=[],o=[];for(const r of t.fields){const u=i[r],l=t.convert(u);l!==void 0&&(e.push(n`${d(r)} = ${l}`),o.push(n`${d(r)} = ${u}`))}return{assignments:e,guards:o}},O=async(i,t,e,o)=>{if(o.fields.length===0||(await Q(i,e),await z(i,e,o.marker)))return;const r=n.join(o.fields.map(a=>o.probe(a)),n` OR `),u=n.join([d("id"),...o.fields.map(a=>d(a))],n`, `);let l="",s=0;for(;;){const a=await E(i,e,n`SELECT ${u} FROM ${n.identifier(t)} WHERE (${r}) AND ${d("id")} > ${l} ORDER BY ${d("id")} LIMIT ${n.raw(String(Z))}`);if(a.length===0){await J(i,e,o.marker,t,s,o.describeUnconvertible);return}const f=[];for(const m of a){const{id:$}=m,{assignments:T,guards:C}=typeof $=="string"?nn(m,o):{assignments:[],guards:[]};if(T.length===0){s+=1;continue}f.push(n`UPDATE ${n.identifier(t)} SET ${n.join(T,n`, `)} WHERE ${d("id")} = ${$} AND ${n.join(C,n` AND `)}`)}await P(i,e,f);const c=a.at(-1)?.id;if(typeof c!="string")return;l=c}},R=i=>`'${i}%'`,en=i=>Object.entries(i.shape).filter(([,t])=>t._meta?.column!==void 0&&w(t)==="bigint").map(([t])=>t),tn=i=>Object.entries(i.shape).filter(([,t])=>t._meta?.column!==void 0&&j(w(t))).map(([t])=>t),on=async(i,t,e,o)=>O(i,t,o,{convert:r=>{if(!(typeof r!="string"||r.length===b))try{return M(BigInt(r))}catch{return}},describeUnconvertible:"hold a value the order-preserving key cannot represent (non-numeric text, or a magnitude past 39 digits)",fields:en(e),marker:`bigint-key:${t}`,probe:r=>n`(${d(r)} IS NOT NULL AND LENGTH(${d(r)}) <> ${b})`}),rn=async(i,t,e,o)=>O(i,t,o,{convert:F,fields:tn(e),marker:`untyped-number-key:${t}`,probe:r=>n`(${d(r)} LIKE ${n.raw(R(K))} AND ${d(r)} NOT LIKE ${n.raw(R(G))})`}),sn=async(i,t,e,o)=>{const r=[[h,o.companionTypes.integer],...Object.entries(e.shape).filter(([,s])=>s._meta?.column!==void 0).map(([s,a])=>[s,L(a,o)])],u="t",l=async s=>{try{return await E(i,o,n`SELECT ${n.join(s.map(a=>B(u,a)),n`, `)} FROM ${n.identifier(t)} AS ${n.identifier(u)} LIMIT 0`),!0}catch{return!1}};if(!await l(r.map(([s])=>s)))for(const[s,a]of r)await l([s])||await g(i,o,n`ALTER TABLE ${n.identifier(t)} ADD COLUMN ${n.identifier(s)} ${n.raw(a)}`)},$n=async(i,t,e)=>{for(const[o,r]of Object.entries(t.tables)){if(r.shardMode?.kind!=="global")continue;const u=V(o,r,e);await g(i,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(o)} (${u})`),await sn(i,o,r,e),await W(i,o,r,e),await on(i,o,r,e),await rn(i,o,r,e)}},v=i=>i.shardMode===void 0||i.shardMode.kind==="global",gn=async(i,t,e)=>{const{integer:o,key:r,real:u}=e.companionTypes;for(const[l,s]of Object.entries(t.tables)){const a=s.aggregateIndexes;if(!(!a||a.length===0||!v(s)))for(const f of a){const c=x(l,f.name);await g(i,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(c)} (${n.identifier("__key__")} ${n.raw(r)} PRIMARY KEY, ${n.identifier("__value__")} ${n.raw(u)}, ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0)`),e.name==="sqlite"&&((await E(i,e,n`PRAGMA table_info(${n.identifier(c)})`)).some($=>$.name==="__count__")||await g(i,e,n`ALTER TABLE ${n.identifier(c)} ADD COLUMN ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0`))}}},y=(i,t,e,o)=>{const r=i.name==="mysql"&&o?n`${n.identifier(t)}(191)`:n`${n.identifier(t)}`;return n`${r} ${n.raw(e)}`},an=(i,t,e)=>{const o=[y(i,"__partition__","ASC",!0)];for(const[r,u]of t.sortBy.entries()){const l=e.shape[u.field],s=l!==void 0&&i.indexKeyPrefix?.(w(l))!==void 0;o.push(y(i,A(r),u.direction==="desc"?"DESC":"ASC",s))}return o.push(y(i,"__id__","ASC",!0)),o},un=(i,t,e)=>t.sortBy.map((o,r)=>{const u=e.shape[o.field],l=i.columnType(u?w(u):void 0);return n`${n.identifier(A(r))} ${n.raw(l)}`}),wn=async(i,t,e)=>{const{key:o}=e.companionTypes;for(const[r,u]of Object.entries(t.tables)){const l=u.rankIndexes;if(!(!l||l.length===0||!v(u)))for(const s of l){const a=D(r,s.name),f=un(e,s,u),c=f.length>0?n`, ${n.join(f,n`, `)}`:n``;await g(i,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(a)} (${n.identifier("__id__")} ${n.raw(o)} PRIMARY KEY, ${n.identifier("__partition__")} ${n.raw(o)} NOT NULL${c})`);const m=an(e,s,u),$=`${r}__rank_${s.name}__btree`;await p(i,e,{columns:n.join(m,n`, `),name:$,table:a,unique:!1})}}};export{gn as runSqlAggregateMigrations,$n as runSqlGlobalTableMigrations,wn as runSqlRankMigrations};
|