@lunora/sql-store 1.0.0-alpha.11 → 1.0.0-alpha.111
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/LICENSE.md +6 -0
- package/README.md +9 -8
- package/dist/dialect.d.mts +144 -41
- package/dist/dialect.d.ts +144 -41
- package/dist/dialect.mjs +0 -1
- package/dist/index.d.mts +342 -129
- package/dist/index.d.ts +342 -129
- package/dist/index.mjs +1 -2
- package/dist/packem_shared/SEARCH_STATE_TABLE-BKfOx6Hf.mjs +1 -0
- package/dist/packem_shared/backfillSqlSearchIndexes-BV4myjVO.mjs +1 -0
- package/dist/packem_shared/createSqlCtxDb-BK0nDY1l.mjs +7 -0
- package/dist/packem_shared/ctx-db-search-CdewhEdG.mjs +1 -0
- package/dist/packem_shared/ctx-db-search-state-D_X8wIwk.mjs +1 -0
- package/dist/packem_shared/decodeBigint-CuZskgMu.mjs +1 -0
- package/dist/packem_shared/decodeGlobalRow-BdC0SILc.mjs +1 -0
- package/dist/packem_shared/runSqlAggregateMigrations-DYQCOK8J.mjs +1 -0
- package/dist/packem_shared/value-codec-Dzavzwao.mjs +1 -0
- package/package.json +3 -2
- package/dist/packem_shared/createSqlCtxDb-BlGq23Wp.mjs +0 -1784
- package/dist/packem_shared/decodeBigint-Dedu92k4.mjs +0 -69
|
@@ -0,0 +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-D_X8wIwk.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-BdC0SILc.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};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{sql as e}from"drizzle-orm";import{queryRun as p,queryAll as m}from"./decodeGlobalRow-BdC0SILc.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{bigintSqlKey as o}from"@lunora/shard-engine";import{B as a,c as i,e as n,a as r,s as c,t as d}from"./value-codec-Dzavzwao.mjs";export{a as BIGINT_KEY_LENGTH,o as bigintSqlKey,i as decodeBigint,n as effectiveColumnKind,r as sqliteDecode,c as sqliteEncode,d as tryJsonParse};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{renderSql as d}from"@lunora/shard-engine";import{sql as r}from"drizzle-orm";import{s as R,a as q,e as y}from"./value-codec-Dzavzwao.mjs";const K="_version",O=n=>n==="_id"||n==="id"?"id":n,k=n=>r`${r.identifier(O(n))}`,D=n=>{const t=[];for(const[e,o]of Object.entries(n.shape)){const i=o._meta?.column;i&&t.push([e,i])}return t},b=(n,t,e)=>{const{params:o,sql:i}=d(t.name,e);return n.all(i,o)},u=(n,t,e)=>{const{params:o,sql:i}=d(t.name,e);return n.run(i,o)},B=async(n,t,e)=>{if(e.length!==0){if(!n.batch){for(const o of e)await u(n,t,o);return}await n.batch(e.map(o=>d(t.name,o)))}},F=async(n,t,e)=>{const o=e.unique?r`UNIQUE `:r``;if(t.name==="mysql"){try{await u(n,t,r`CREATE ${o}INDEX ${r.identifier(e.name)} ON ${r.identifier(e.table)} (${e.columns})`)}catch(i){const s=i;if(s.errno!==1061&&s.code!=="ER_DUP_KEYNAME"&&s.code!==1061)throw i}return}await u(n,t,r`CREATE ${o}INDEX IF NOT EXISTS ${r.identifier(e.name)} ON ${r.identifier(e.table)} (${e.columns})`)},L=R,h=n=>n?._meta?.column?.notNull===!1,g=n=>{if(n.kind!=="optional"||h(n))return!1;const t=n._meta?.inner;return!h(t)},w=new WeakMap,$=n=>{let t=w.get(n);return t===void 0&&(t=Object.entries(n.shape).map(([e,o])=>[e,y(o),g(o)]),w.set(n,t)),t},C=new WeakMap,M=(n,t)=>{let e=C.get(n);return e===void 0&&(e=new Map($(n).map(([o,i])=>[o,i])),C.set(n,e)),e.get(t)},U=(n,t,e)=>R(e,M(n,t)),S=(n,t)=>{const e={};for(const[o,i,s]of $(n)){const c=t[o];c===void 0||s&&c===null||(e[o]=q(c,i))}return e._id=t.id,e._creationTime=t._creationTime,e},_=(n,t)=>t?S(n,t):null,z=(n,t)=>{const e=[];for(const o of t){const i=_(n,o);i&&e.push(i)}return e},p=500,W=async(n,t,e,o,i,s={})=>{let c=s.after,a=s.limit,m=!0;for(;m&&(a===void 0||a>0);){const f=a===void 0?p:Math.min(p,a),I=c===void 0?r``:r` WHERE ${r.identifier("id")} > ${c}`,l=await b(n,t,r`SELECT * FROM ${r.identifier(o)}${I} ORDER BY ${r.identifier("id")} ASC LIMIT ${r.raw(String(f))}`);for(const N of l){const E=_(e,N);E&&await i(E)}c=l.at(-1)?.id,m=l.length===f,a=a===void 0?void 0:a-l.length}};export{p as BACKFILL_BATCH_SIZE,K as OCC_VERSION_COLUMN,k as columnRefSql,F as createIndexIfNotExists,S as decodeGlobalRow,_ as decodeRow,z as decodeRows,W as forEachRowPaged,O as physicalColumn,b as queryAll,B as queryBatch,u as queryRun,L as serializeColumnValue,U as serializeDocumentColumn,D as tableColumns};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as R}from"@lunora/errors";import{aggregateTableName as C,rankTableName as x,sortColumnName as A,renderSql as v,bigintSqlKey as D}from"@lunora/shard-engine";import{sql as n}from"drizzle-orm";import{queryRun as g,queryAll as w,createIndexIfNotExists as p,OCC_VERSION_COLUMN as T,tableColumns as M,columnRefSql as d,queryBatch as B}from"./decodeGlobalRow-BdC0SILc.mjs";import{B as S,e as h}from"./value-codec-Dzavzwao.mjs";const N=(t,i,e=!1)=>i.columnType(h(t),{unique:e||t._meta?.column?.unique===!0}),L=t=>{const i=new Set;for(const e of t.indexes)e.unique===!0&&e.fields.length===1&&i.add(e.fields[0]);return i},k=(t,i,e)=>{if(e.indexKeyPrefix!==void 0)for(const o of i.indexes){if(o.unique!==!0||o.fields.length<=1)continue;const r=o.fields.filter(s=>{const a=i.shape[s];return a!==void 0&&e.indexKeyPrefix?.(h(a))!==void 0});if(r.length>0)throw new Error(`${t}.${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.`)}},U=(t,i,e)=>{const o=[],r=L(i);for(const[l,f]of Object.entries(i.shape)){if(!f._meta?.column)continue;const c=f._meta.column.notNull&&f.kind!=="optional"?" NOT NULL":"";o.push(n`${n.identifier(l)} ${n.raw(`${N(f,e,r.has(l))}${c}`)}`)}const s=[...e.frameworkColumns().map(l=>n`${n.identifier(l.name)} ${n.raw(l.type)}`),n`${n.identifier(T)} ${n.raw(e.companionTypes.integer)}`],a=s.length+o.length,u=e.maxTableColumns;if(u!==void 0&&a>u){const l=a-1===u,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 R("VALIDATION_ERROR",`@lunora/sql-store: global table "${t}" needs ${String(a)} columns, over this engine's ${String(u)}-column limit. ${l?`One of them is "${T}", the row version every guarded write reads, which caps declared fields at ${String(u-s.length)}. A table provisioned at ${String(u)} columns before that column existed cannot be widened to hold it. `:""}${f}`)}return n.join([...s,...o],n`, `)},j=t=>{if(t.indexKeyPrefix?.("string")===void 0)return n`${d("_creationTime")}, ${d("id")}`},F=async(t,i,e)=>{if(i.name!=="sqlite")return;const r=(await w(t,i,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 s=l=>{const f=l.indexOf("(");return f===-1?void 0:l.slice(f,l.lastIndexOf(")")+1)},a=e.unique?n`UNIQUE `:n``,u=s(v(i.name,n`CREATE ${a}INDEX ${n.identifier(e.name)} ON ${n.identifier(e.table)} (${e.columns})`).sql);if(!(u===void 0||u===s(r))){if(e.unique){const l=n.join(e.refs.map(c=>n`${c} IS NOT NULL`),n` AND `);if((await w(t,i,n`SELECT ${e.columns} FROM ${n.identifier(e.table)} WHERE ${l} GROUP BY ${e.columns} HAVING COUNT(*) > 1 LIMIT 1`)).length>0)throw new R("INTERNAL",`unique index "${e.name}" on "${e.table}" cannot be re-created with its new column list: existing rows are duplicates under it. De-duplicate the table with a data migration first; the previous index is left in place.`)}await g(t,i,n`DROP INDEX IF EXISTS ${n.identifier(e.name)}`)}},P=async(t,i,e,o)=>{k(i,e,o);const r=L(e),s=u=>{const l=d(u),f=e.shape[u],c=f&&o.indexKeyPrefix&&f._meta?.column?.unique!==!0&&!r.has(u)?o.indexKeyPrefix(h(f)):void 0;return c===void 0?l:n`${l}(${n.raw(String(c))})`},a=j(o);for(const u of e.indexes){const l=u.fields.map($=>s($)),f=n.join(l,n`, `),c=u.unique??!1,m={columns:c||a===void 0?f:n`${f}, ${a}`,name:`${i}_${u.name}`,refs:l,table:i,unique:c};await F(t,o,m),await p(t,o,m)}a!==void 0&&await p(t,o,{columns:a,name:`${i}__by_creation`,table:i,unique:!1});for(const[u,l]of M(e))l.unique&&await p(t,o,{columns:s(u),name:`${i}_unique_${u}`,table:i,unique:!0})},_="__lunora_migration_state",K=async(t,i)=>{await g(t,i,n`CREATE TABLE IF NOT EXISTS ${n.identifier(_)} (${n.identifier("name")} ${n.raw(i.companionTypes.key)} PRIMARY KEY)`)},G=async(t,i,e)=>(await w(t,i,n`SELECT ${n.identifier("name")} FROM ${n.identifier(_)} WHERE ${n.identifier("name")} = ${e}`)).length>0,Y=async(t,i,e,o,r)=>{r>0&&console.warn(`[@lunora/sql-store] bigint storage migration on "${o}": ${String(r)} row(s) hold a value the order-preserving key cannot represent (non-numeric text, or a magnitude past 39 digits) and were left as stored — range filters and ORDER BY over them stay wrong until they are corrected by hand.`);try{await g(t,i,n`INSERT INTO ${n.identifier(_)} (${n.identifier("name")}) VALUES (${e})`)}catch(s){if(!i.isUniqueViolation(s))throw s}},H=100,V=t=>Object.entries(t.shape).filter(([,i])=>i._meta?.column!==void 0&&h(i)==="bigint").map(([i])=>i),X=(t,i)=>{const e=[],o=[];for(const r of i){const s=t[r];if(!(typeof s!="string"||s.length===S))try{const a=D(BigInt(s));e.push(n`${d(r)} = ${a}`),o.push(n`${d(r)} = ${s}`)}catch{}}return{assignments:e,guards:o}},W=async(t,i,e,o)=>{const r=V(e);if(r.length===0)return;await K(t,o);const s=`bigint-key:${i}`;if(await G(t,o,s))return;const a=n.join(r.map(c=>n`(${d(c)} IS NOT NULL AND LENGTH(${d(c)}) <> ${S})`),n` OR `),u=n.join([d("id"),...r.map(c=>d(c))],n`, `);let l="",f=0;for(;;){const c=await w(t,o,n`SELECT ${u} FROM ${n.identifier(i)} WHERE (${a}) AND ${d("id")} > ${l} ORDER BY ${d("id")} LIMIT ${n.raw(String(H))}`);if(c.length===0){await Y(t,o,s,i,f);return}const m=[];for(const y of c){const{id:I}=y,{assignments:b,guards:q}=typeof I=="string"?X(y,r):{assignments:[],guards:[]};if(b.length===0){f+=1;continue}m.push(n`UPDATE ${n.identifier(i)} SET ${n.join(b,n`, `)} WHERE ${d("id")} = ${I} AND ${n.join(q,n` AND `)}`)}await B(t,o,m);const $=c.at(-1)?.id;if(typeof $!="string")return;l=$}},Q=async(t,i,e,o)=>{const r=[[T,o.companionTypes.integer],...Object.entries(e.shape).filter(([,a])=>a._meta?.column!==void 0).map(([a,u])=>[a,N(u,o)])],s=async a=>{try{return await w(t,o,n`SELECT ${n.join(a.map(u=>d(u)),n`, `)} FROM ${n.identifier(i)} LIMIT 0`),!0}catch{return!1}};if(!await s(r.map(([a])=>a)))for(const[a,u]of r)await s([a])||await g(t,o,n`ALTER TABLE ${n.identifier(i)} ADD COLUMN ${n.identifier(a)} ${n.raw(u)}`)},rn=async(t,i,e)=>{for(const[o,r]of Object.entries(i.tables)){if(r.shardMode?.kind!=="global")continue;const s=U(o,r,e);await g(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(o)} (${s})`),await Q(t,o,r,e),await P(t,o,r,e),await W(t,o,r,e)}},O=t=>t.shardMode===void 0||t.shardMode.kind==="global",sn=async(t,i,e)=>{const{integer:o,key:r,real:s}=e.companionTypes;for(const[a,u]of Object.entries(i.tables)){const l=u.aggregateIndexes;if(!(!l||l.length===0||!O(u)))for(const f of l){const c=C(a,f.name);await g(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(c)} (${n.identifier("__key__")} ${n.raw(r)} PRIMARY KEY, ${n.identifier("__value__")} ${n.raw(s)}, ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0)`),e.name==="sqlite"&&((await w(t,e,n`PRAGMA table_info(${n.identifier(c)})`)).some($=>$.name==="__count__")||await g(t,e,n`ALTER TABLE ${n.identifier(c)} ADD COLUMN ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0`))}}},E=(t,i,e,o)=>{const r=t.name==="mysql"&&o?n`${n.identifier(i)}(191)`:n`${n.identifier(i)}`;return n`${r} ${n.raw(e)}`},z=(t,i,e)=>{const o=[E(t,"__partition__","ASC",!0)];for(const[r,s]of i.sortBy.entries()){const a=e.shape[s.field],u=a!==void 0&&t.indexKeyPrefix?.(h(a))!==void 0;o.push(E(t,A(r),s.direction==="desc"?"DESC":"ASC",u))}return o.push(E(t,"__id__","ASC",!0)),o},J=(t,i,e)=>i.sortBy.map((o,r)=>{const s=e.shape[o.field],a=t.columnType(s?h(s):void 0);return n`${n.identifier(A(r))} ${n.raw(a)}`}),an=async(t,i,e)=>{const{key:o}=e.companionTypes;for(const[r,s]of Object.entries(i.tables)){const a=s.rankIndexes;if(!(!a||a.length===0||!O(s)))for(const u of a){const l=x(r,u.name),f=J(e,u,s),c=f.length>0?n`, ${n.join(f,n`, `)}`:n``;await g(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(l)} (${n.identifier("__id__")} ${n.raw(o)} PRIMARY KEY, ${n.identifier("__partition__")} ${n.raw(o)} NOT NULL${c})`);const m=z(e,u,s),$=`${r}__rank_${u.name}__btree`;await p(t,e,{columns:n.join(m,n`, `),name:$,table:l,unique:!1})}}};export{sn as runSqlAggregateMigrations,rn as runSqlGlobalTableMigrations,an as runSqlRankMigrations};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as B}from"@lunora/errors";import{decodeBigintSqlKey as I,bigintSqlKey as N,BIGINT_KEY_DIGITS as U}from"@lunora/shard-engine";const S=r=>{if(r.kind!=="optional")return r.kind;const t=r._meta?.inner;return t?S(t):r.kind},A=r=>{let t="";for(let o=0;o<r.length;o+=32768)t+=String.fromCharCode(...r.subarray(o,o+32768));return btoa(t)},k=r=>{const t=atob(r),i=new Uint8Array(t.length);for(let o=0;o<t.length;o+=1)i[o]=t.codePointAt(o)??0;return i},s="$lunora.wire$",m=64,l=1024,g="__proto__",h={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},w={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},T=r=>{if(r===null||typeof r!="object")return!1;const t=Object.getPrototypeOf(r);return t===null||t===Object.prototype},E=(r,t=0)=>{if(t>m)return!0;if(r===null)return!1;const i=typeof r;if(i==="string"||i==="boolean")return!1;if(i==="number")return!Number.isFinite(r);if(i!=="object")return!0;if(Array.isArray(r))return r.length>0&&r[0]===s?!0:r.some(o=>o===void 0||E(o,t+1));if(!T(r)||Object.hasOwn(r,g))return!0;for(const o of Object.keys(r)){const f=r[o];if(f!==void 0&&E(f,t+1))return!0}return!1},b=(r,t=0)=>{if(t>m)throw new RangeError(`wire-codec: value nesting exceeds the ${m}-level limit`);if(r===void 0)return[s,"undefined"];if(r===null)return null;const i=typeof r;if(i==="bigint")return[s,"bigint",r.toString()];if(i==="number"){const e=r;return Number.isNaN(e)?[s,"nan"]:e===1/0?[s,"inf"]:e===-1/0?[s,"-inf"]:e}if(i!=="object")return r;if(r instanceof Date)return[s,"date",b(r.getTime(),t+1)];if(r instanceof Error){const e=r,n={};for(const c of Object.keys(e)){if(e[c]===void 0)continue;const u=b(e[c],t+1);c===g?Object.defineProperty(n,c,{configurable:!0,enumerable:!0,value:u,writable:!0}):n[c]=u}const y=[s,"error",String(e.name),String(e.message),n];return e.cause!==void 0&&y.push(b(e.cause,t+1)),y}if(r instanceof URL)return[s,"url",r.href];if(r instanceof Map)return[s,"map",[...r.entries()].map(([e,n])=>[b(e,t+1),b(n,t+1)])];if(r instanceof Set)return[s,"set",[...r].map(e=>b(e,t+1))];if(r instanceof ArrayBuffer)return[s,"bytes",A(new Uint8Array(r)),"ArrayBuffer"];if(ArrayBuffer.isView(r)){const e=r,n=e.constructor.name,y=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return n==="Uint8Array"?[s,"bytes",A(y)]:[s,"bytes",A(y),n]}if(Array.isArray(r)){const e=r.map(n=>b(n,t+1));return e.length>0&&e[0]===s?[s,"arr",e]:e}if(!T(r)){const e=r.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${e} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=r,f={};for(const e of Object.keys(o)){const n=o[e];if(n===void 0)continue;const y=b(n,t+1);e===g?Object.defineProperty(f,e,{configurable:!0,enumerable:!0,value:y,writable:!0}):f[e]=y}return f},a=(r,t=0)=>{if(t>m)throw new RangeError(`wire-codec: value nesting exceeds the ${m}-level limit`);if(r===null||typeof r!="object")return r;if(Array.isArray(r)){if(r[0]===s)switch(r[1]){case"-inf":return-1/0;case"arr":return r[2].map(e=>a(e,t+1));case"bigint":{const e=r[2];if(typeof e!="string"||e.length>l||!/^-?\d+$/.test(e))throw new RangeError(`wire-codec: invalid or over-long bigint (max ${l} digits)`);return BigInt(e)}case"date":{const e=a(r[2],t+1);if(typeof e!="number")throw new TypeError("wire-codec: malformed date — epoch must be a number");return new Date(e)}case"map":{const e=r[2];return new Map(e.map(n=>{if(!Array.isArray(n)||n.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[a(n[0],t+1),a(n[1],t+1)]}))}case"set":return new Set(r[2].map(e=>a(e,t+1)));case"url":{const e=r[2];if(typeof e!="string")throw new TypeError("wire-codec: malformed url — href must be a string");return new URL(e)}case"error":{const e=r[2],n=r[3];if(typeof e!="string"||typeof n!="string")throw new TypeError("wire-codec: malformed error — name and message must be strings");const y=(Object.hasOwn(w,e)?w[e]:void 0)??Error,c=new y(n);c.name!==e&&Object.defineProperty(c,"name",{configurable:!0,value:e,writable:!0});const u=a(r[4],t+1);if(u===null||typeof u!="object"||Array.isArray(u))throw new TypeError("wire-codec: malformed error — props must be an object");for(const d of Object.keys(u))d===g?Object.defineProperty(c,d,{configurable:!0,enumerable:!0,value:u[d],writable:!0}):c[d]=u[d];return r.length>5&&Object.defineProperty(c,"cause",{configurable:!0,value:a(r[5],t+1),writable:!0}),c}case"bytes":{const e=r[2];if(typeof e!="string")throw new TypeError("wire-codec: malformed bytes — payload must be a base64 string");const n=k(e);if(A(n)!==e)throw new TypeError("wire-codec: malformed bytes — payload must be canonical padded base64");const y=r[3]??"Uint8Array";if(y==="ArrayBuffer")return n.buffer.byteLength===n.byteLength?n.buffer:n.slice().buffer;const c=Object.hasOwn(h,y)?h[y]:void 0;return c?new c(n.slice().buffer):n}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return r.map(e=>a(e,t+1))}return r.map(f=>a(f,t+1))}const i=r,o={};for(const f of Object.keys(i)){const e=a(i[f],t+1);f===g?Object.defineProperty(o,f,{configurable:!0,enumerable:!0,value:e,writable:!0}):o[f]=e}return o},p=s,O=(r,t)=>r.startsWith(p)?a(t(r.slice(p.length))):t(r),R=new Set(["any","from","union"]),L=(r,t)=>{if(t!==void 0&&(typeof r=="number"||typeof r=="boolean")&&R.has(t))return p+JSON.stringify(r);if(typeof r=="boolean")return r?1:0;if(r===null||typeof r=="string"||typeof r=="number")return r;if(typeof r=="bigint")return N(r);if(r instanceof Uint8Array)return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);try{return E(r)?p+JSON.stringify(b(r)):JSON.stringify(r)}catch(i){throw new B("BAD_REQUEST",`this value cannot be stored: ${i instanceof Error?i.message:String(i)}`)}},j=r=>{try{return JSON.parse(r)}catch{return r}},_=r=>{if(typeof r!="string")return r;const t=I(r);if(t!==void 0)return t;try{return BigInt(r)}catch{return r}},C=r=>S(r),x=(r,t)=>{if(r===null)return r;switch(t){case"any":case"from":case"union":return typeof r!="string"?r:r.startsWith("{")||r.startsWith("[")||r.startsWith(p)?O(r,j):I(r)??r;case"array":case"geoPoint":case"object":case"record":return typeof r=="string"?O(r,j):r;case"bigint":return _(r);case"boolean":return r===0||r===1?r===1:r;case"bytes":return r instanceof ArrayBuffer?r:ArrayBuffer.isView(r)?new Uint8Array(r.buffer,r.byteOffset,r.byteLength).slice().buffer:r;default:return r}},K=U+1;export{K as B,x as a,b,_ as c,a as d,C as e,E as n,L as s,j as t};
|
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.111",
|
|
4
4
|
"description": "Internal dialect-parameterized SQL store core for Lunora .global() backends (D1, PlanetScale)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -50,7 +50,8 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@lunora/
|
|
53
|
+
"@lunora/errors": "1.0.0-alpha.33",
|
|
54
|
+
"@lunora/shard-engine": "1.0.0-alpha.58",
|
|
54
55
|
"drizzle-orm": "^0.45.2"
|
|
55
56
|
},
|
|
56
57
|
"engines": {
|