@lunora/d1 1.0.0-alpha.106 → 1.0.0-alpha.107
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/MigrationRunner-Dm07rpAT.mjs +2 -0
- package/dist/packem_shared/facetGlobalColumn-DRjoggv-.mjs +1 -0
- package/package.json +1 -1
- package/dist/packem_shared/MigrationRunner-SadkIbU5.mjs +0 -2
- package/dist/packem_shared/facetGlobalColumn-EeEbDTka.mjs +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{exportGlobalRows as o,importGlobalRows as t,selectGlobalTables as a}from"./packem_shared/exportGlobalRows-DJHnVfLg.mjs";import{D1Client as i,D1Session as n}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as D,createD1CtxDb as b,readD1CdcChanges as p,runD1AggregateMigrations as x,runD1CdcMigration as c,runD1GlobalTableMigrations as g,runD1RankMigrations as f,runD1SearchMigrations as m,sweepD1CdcRetention as C}from"./packem_shared/backfillD1SearchIndexes-V-i_kEvG.mjs";import{facetGlobalColumn as d,listGlobalTables as G,readGlobalTablePage as M}from"./packem_shared/facetGlobalColumn-
|
|
1
|
+
import{exportGlobalRows as o,importGlobalRows as t,selectGlobalTables as a}from"./packem_shared/exportGlobalRows-DJHnVfLg.mjs";import{D1Client as i,D1Session as n}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as D,createD1CtxDb as b,readD1CdcChanges as p,runD1AggregateMigrations as x,runD1CdcMigration as c,runD1GlobalTableMigrations as g,runD1RankMigrations as f,runD1SearchMigrations as m,sweepD1CdcRetention as C}from"./packem_shared/backfillD1SearchIndexes-V-i_kEvG.mjs";import{facetGlobalColumn as d,listGlobalTables as G,readGlobalTablePage as M}from"./packem_shared/facetGlobalColumn-DRjoggv-.mjs";import{MigrationRunner as T}from"./packem_shared/MigrationRunner-Dm07rpAT.mjs";import{D1TimeoutError as w,isTransientD1Error as S,retryingExec as y,withD1Retry as E}from"./packem_shared/D1TimeoutError-D4YUqZPk.mjs";import{default as q}from"./packem_shared/sqliteDialect-J6_WTDHg.mjs";import{applyCdcChanges as I}from"@lunora/shard-engine";import{createSqlCtxDb as j}from"@lunora/sql-store";export{i as D1Client,n as D1Session,w as D1TimeoutError,T as MigrationRunner,I as applyCdcChanges,D as backfillD1SearchIndexes,b as createD1CtxDb,j as createSqlCtxDb,o as exportGlobalRows,d as facetGlobalColumn,t as importGlobalRows,S as isTransientD1Error,G as listGlobalTables,p as readD1CdcChanges,M as readGlobalTablePage,y as retryingExec,x as runD1AggregateMigrations,c as runD1CdcMigration,g as runD1GlobalTableMigrations,f as runD1RankMigrations,m as runD1SearchMigrations,a as selectGlobalTables,q as sqliteDialect,C as sweepD1CdcRetention,E as withD1Retry};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{LunoraError as d}from"@lunora/errors";import{sql as m}from"drizzle-orm";import{D1Client as S}from"./D1Client-CzS0Qfa6.mjs";const p="__drizzle_migrations",g=`CREATE TABLE IF NOT EXISTS ${p} (id INTEGER PRIMARY KEY AUTOINCREMENT, hash TEXT NOT NULL UNIQUE, created_at NUMERIC)`,T=/\s/u,w=/[$\w]/u,A=/^(?:\s|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*/u,I=/^CREATE\s+(?:TEMP\s+|TEMPORARY\s+)?TRIGGER\b/iu,N=/^[0-9a-f]{64}$/u,v=new RegExp(String.raw`UNIQUE constraint failed:\s*${p}\.hash`,"iu"),_=l=>{const t=l.sql,e=I.test(t.slice(A.exec(t)?.[0].length??0));let n=!1,o=!1,c=!1,r=!1,i,f="",u=!1,E=!1;for(let a=0;a<t.length;a+=1){const s=t[a],h=t[a+1];if(c){s===`
|
|
2
|
+
`&&(c=!1);continue}if(r){s==="*"&&h==="/"&&(r=!1,a+=1);continue}if(n){s==="'"&&(h==="'"?a+=1:n=!1);continue}if(o){s==='"'&&(h==='"'?a+=1:o=!1);continue}if(s==="-"&&h==="-"){c=!0,a+=1;continue}if(s==="/"&&h==="*"){r=!0,a+=1;continue}if(i!==void 0){if(s!==void 0&&!T.test(s))throw new d("INTERNAL",`Migration "${l.name}" (v${String(l.version)}) contains more than one SQL statement. Split it into separate migrations — batch() runs them atomically.`);continue}if(e&&s!==void 0){if(w.test(s)){f+=s;continue}const R=f.toUpperCase();f="",R!==""?(u=E&&R==="END",E=!1):s!==";"&&!T.test(s)&&(u=!1,E=!1)}if(s==="'"){n=!0;continue}if(s==='"'){o=!0;continue}if(s===";"){if(e&&!u){E=!0;continue}i=a}}return i},L=async l=>{const t=new TextEncoder().encode(l),e=await crypto.subtle.digest("SHA-256",t);return[...new Uint8Array(e)].map(n=>n.toString(16).padStart(2,"0")).join("")};class ${client;migrations;constructor(t,e){this.client=t instanceof S?t:new S(t),this.migrations=[...e].toSorted((n,o)=>n.version-o.version),this.assertUniqueVersions(),this.assertUniqueSql()}async run(){await this.client.drizzle.run(m.raw(g));const t=await this.client.drizzle.all(m.raw(`SELECT hash FROM ${p}`)),e=new Set(t.map(r=>r.hash)),n=[],o=[],c=await Promise.all(this.migrations.map(async r=>L(r.sql)));for(const[r,i]of this.migrations.entries()){const f=c[r];if(e.has(f)){o.push({name:i.name,version:i.version});continue}await this.applyOne(i,f)?n.push({name:i.name,version:i.version}):o.push({name:i.name,version:i.version})}return{applied:n,skipped:o}}async applyOne(t,e){const n=_(t),o=(n===void 0?t.sql:t.sql.slice(0,n)).trim();if(!N.test(e))throw new d("INTERNAL",`migration "${t.name}" produced a non-hex hash; refusing to inline into SQL`);const c=`INSERT INTO ${p} (hash, created_at) VALUES ('${e}', ${String(Date.now())})`,r=[this.client.drizzle.run(m.raw(o)),this.client.drizzle.run(m.raw(c))];try{await this.client.batch(r)}catch(i){if(v.test(i instanceof Error?i.message:String(i)))return!1;throw i}return!0}assertUniqueVersions(){const t=new Set;for(const e of this.migrations){if(t.has(e.version))throw new d("INTERNAL",`Duplicate migration version ${String(e.version)}`);t.add(e.version)}}assertUniqueSql(){const t=new Map;for(const e of this.migrations){const n=t.get(e.sql);if(n!==void 0)throw new d("INTERNAL",`Migrations ${String(n)} and ${String(e.version)} have identical SQL — bump the content, not just the version.`);t.set(e.sql,e.version)}}}export{$ as MigrationRunner,p as TRACKING_TABLE_NAME};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as E}from"@lunora/errors";import{sqliteEncode as $,decodeGlobalRow as I}from"@lunora/sql-store";import{n as M,e as A}from"./wire-codec-Cgm2xFo4.mjs";import{runD1GlobalTableMigrations as p}from"./backfillD1SearchIndexes-V-i_kEvG.mjs";import{TRACKING_TABLE_NAME as C}from"./MigrationRunner-Dm07rpAT.mjs";import{quoteIdentifier as i}from"./quoteIdentifier-CObIFRhb.mjs";const g=50,y=500,U=30,F=200,v=(t,o,n)=>Math.min(Math.max(t,o),n),G=new RegExp(`^sqlite_|^_cf_|^d1_|^__cdc|^__lunora_|^${C}$|__agg_|__rank_|__fts_`,"u"),k=t=>G.test(t),T=/password|secret|token|hash|salt|credential/iu,R=async t=>(await t.all("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",[])).map(n=>String(n.name)).filter(n=>!k(n)),L=async(t,o,n)=>{if(await p(t,o),!(await R(t)).includes(n))throw new E("UNKNOWN_TABLE",`unknown table: ${n}`,{status:404})},b=async(t,o,n="",a=[])=>{const e=await t.all(`SELECT COUNT(*) AS c FROM ${o}${n}`,a);return Number(e[0]?.c??0)},S=(t,o,n)=>t.tables[o]!==void 0&&n==="_id"?"id":n,h=(t,o,n,a)=>{const e=[],r=[];for(const s of a??[]){if(!n.includes(s.column))throw new E("UNKNOWN_COLUMN",`unknown column: ${s.column}`,{status:404});if(t.tables[o]===void 0&&T.test(s.column))throw new E("FORBIDDEN",`cannot filter on a redacted column: ${s.column}`,{status:403});const l=i(S(t,o,s.column));s.value===null||s.value===void 0?e.push(`${l} IS NULL`):(e.push(`${l} = ?`),r.push($(s.value)))}return{params:r,where:e.length>0?` WHERE ${e.join(" AND ")}`:""}},q=(t,o,n)=>{const a=t.tables[o];if(a)return I(a,n);const e={};for(const[r,s]of Object.entries(n))e[r]=s!=null&&T.test(r)?"•••":s;return e},O=async(t,o,n)=>{const a=o.tables[n];return a?["_id","_creationTime",...Object.keys(a.shape)]:(await t.all(`PRAGMA table_info(${i(n)})`,[])).map(r=>String(r.name))},P=async(t,o,n)=>{if(o.tables[n])return;const a=await t.all(`PRAGMA foreign_key_list(${i(n)})`,[]);if(a.length===0)return;const e={};for(const r of a){const s=String(r.from),l=String(r.table);e[s]??=l}return e},H=async(t,o)=>{await p(t,o);const n=await R(t);return Promise.all(n.map(async a=>({name:a,rowCount:await b(t,i(a))})))},X=async(t,o,n)=>{const{table:a}=n;await L(t,o,a);const e=v(Math.trunc(n.limit??g),1,y),r=Math.max(0,Math.trunc(n.offset??0)),s=i(a),l=await O(t,o,a),{params:w,where:u}=h(o,a,l,n.filters),m=await b(t,s,u,w),f=(await t.all(`SELECT * FROM ${s}${u} LIMIT ? OFFSET ?`,[...w,e,r])).map(d=>{const c=q(o,a,d);return M(c)?A(c):c}),N=await P(t,o,a);return N===void 0?{columns:l,rows:f,total:m}:{columns:l,refs:N,rows:f,total:m}},Z=async(t,o,n)=>{const{column:a,table:e}=n;await L(t,o,e);const r=await O(t,o,e);if(!r.includes(a))throw new E("UNKNOWN_COLUMN",`unknown column: ${a}`,{status:404});const s=i(e),{params:l,where:w}=h(o,e,r,n.filters);if(o.tables[e]===void 0&&T.test(a)){const c=await b(t,s,w,l);return{truncated:!1,values:c===0?[]:[{count:c,value:"•••"}]}}const u=v(Math.trunc(n.limit??U),1,F),m=i(S(o,e,a)),_=await t.all(`SELECT ${m} AS value, COUNT(*) AS count FROM ${s}${w} GROUP BY ${m} ORDER BY count DESC LIMIT ?`,[...l,u+1]),f=_.length>u,d=(f?_.slice(0,u):_).map(c=>({count:Number(c.count),value:c.value}));return{truncated:f,values:M(d)?A(d):d}};export{Z as facetGlobalColumn,H as listGlobalTables,X as readGlobalTablePage};
|
package/package.json
CHANGED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{LunoraError as f}from"@lunora/errors";import{sql as h}from"drizzle-orm";import{D1Client as m}from"./D1Client-CzS0Qfa6.mjs";const u="__drizzle_migrations",p=`CREATE TABLE IF NOT EXISTS ${u} (id INTEGER PRIMARY KEY AUTOINCREMENT, hash TEXT NOT NULL UNIQUE, created_at NUMERIC)`,d=/\s/u,E=/^[0-9a-f]{64}$/u,S=new RegExp(String.raw`UNIQUE constraint failed:\s*${u}\.hash`,"iu"),w=c=>{const n=c.sql;let e=!1,i=!1,o=!1,a=!1,r;for(let t=0;t<n.length;t+=1){const s=n[t],l=n[t+1];if(o){s===`
|
|
2
|
-
`&&(o=!1);continue}if(a){s==="*"&&l==="/"&&(a=!1,t+=1);continue}if(e){s==="'"&&(l==="'"?t+=1:e=!1);continue}if(i){s==='"'&&(l==='"'?t+=1:i=!1);continue}if(s==="-"&&l==="-"){o=!0,t+=1;continue}if(s==="/"&&l==="*"){a=!0,t+=1;continue}if(r!==void 0){if(s!==void 0&&!d.test(s))throw new f("INTERNAL",`Migration "${c.name}" (v${String(c.version)}) contains more than one SQL statement. Split it into separate migrations — batch() runs them atomically.`);continue}if(s==="'"){e=!0;continue}if(s==='"'){i=!0;continue}s===";"&&(r=t)}return r},g=async c=>{const n=new TextEncoder().encode(c),e=await crypto.subtle.digest("SHA-256",n);return[...new Uint8Array(e)].map(i=>i.toString(16).padStart(2,"0")).join("")};class A{client;migrations;constructor(n,e){this.client=n instanceof m?n:new m(n),this.migrations=[...e].toSorted((i,o)=>i.version-o.version),this.assertUniqueVersions(),this.assertUniqueSql()}async run(){await this.client.drizzle.run(h.raw(p));const n=await this.client.drizzle.all(h.raw(`SELECT hash FROM ${u}`)),e=new Set(n.map(r=>r.hash)),i=[],o=[],a=await Promise.all(this.migrations.map(async r=>g(r.sql)));for(const[r,t]of this.migrations.entries()){const s=a[r];if(e.has(s)){o.push({name:t.name,version:t.version});continue}await this.applyOne(t,s)?i.push({name:t.name,version:t.version}):o.push({name:t.name,version:t.version})}return{applied:i,skipped:o}}async applyOne(n,e){const i=w(n),o=(i===void 0?n.sql:n.sql.slice(0,i)).trim();if(!E.test(e))throw new f("INTERNAL",`migration "${n.name}" produced a non-hex hash; refusing to inline into SQL`);const a=`INSERT INTO ${u} (hash, created_at) VALUES ('${e}', ${String(Date.now())})`,r=[this.client.drizzle.run(h.raw(o)),this.client.drizzle.run(h.raw(a))];try{await this.client.batch(r)}catch(t){if(S.test(t instanceof Error?t.message:String(t)))return!1;throw t}return!0}assertUniqueVersions(){const n=new Set;for(const e of this.migrations){if(n.has(e.version))throw new f("INTERNAL",`Duplicate migration version ${String(e.version)}`);n.add(e.version)}}assertUniqueSql(){const n=new Map;for(const e of this.migrations){const i=n.get(e.sql);if(i!==void 0)throw new f("INTERNAL",`Migrations ${String(i)} and ${String(e.version)} have identical SQL — bump the content, not just the version.`);n.set(e.sql,e.version)}}}export{A as MigrationRunner};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as E}from"@lunora/errors";import{sqliteEncode as I,decodeGlobalRow as $}from"@lunora/sql-store";import{n as M,e as v}from"./wire-codec-Cgm2xFo4.mjs";import{runD1GlobalTableMigrations as S}from"./backfillD1SearchIndexes-V-i_kEvG.mjs";import{quoteIdentifier as i}from"./quoteIdentifier-CObIFRhb.mjs";const C=50,g=500,y=30,U=200,h=(t,o,n)=>Math.min(Math.max(t,o),n),F=/^sqlite_|^_cf_|^d1_|^__cdc|^__lunora_|__agg_|__rank_|__fts_/u,k=t=>F.test(t),N=/password|secret|token|hash|salt|credential/iu,p=async t=>(await t.all("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",[])).map(n=>String(n.name)).filter(n=>!k(n)),A=async(t,o,n)=>{if(await S(t,o),!(await p(t)).includes(n))throw new E("UNKNOWN_TABLE",`unknown table: ${n}`,{status:404})},T=async(t,o,n="",a=[])=>{const e=await t.all(`SELECT COUNT(*) AS c FROM ${o}${n}`,a);return Number(e[0]?.c??0)},L=(t,o,n)=>t.tables[o]!==void 0&&n==="_id"?"id":n,O=(t,o,n,a)=>{const e=[],r=[];for(const s of a??[]){if(!n.includes(s.column))throw new E("UNKNOWN_COLUMN",`unknown column: ${s.column}`,{status:404});if(t.tables[o]===void 0&&N.test(s.column))throw new E("FORBIDDEN",`cannot filter on a redacted column: ${s.column}`,{status:403});const l=i(L(t,o,s.column));s.value===null||s.value===void 0?e.push(`${l} IS NULL`):(e.push(`${l} = ?`),r.push(I(s.value)))}return{params:r,where:e.length>0?` WHERE ${e.join(" AND ")}`:""}},q=(t,o,n)=>{const a=t.tables[o];if(a)return $(a,n);const e={};for(const[r,s]of Object.entries(n))e[r]=s!=null&&N.test(r)?"•••":s;return e},R=async(t,o,n)=>{const a=o.tables[n];return a?["_id","_creationTime",...Object.keys(a.shape)]:(await t.all(`PRAGMA table_info(${i(n)})`,[])).map(r=>String(r.name))},G=async(t,o,n)=>{if(o.tables[n])return;const a=await t.all(`PRAGMA foreign_key_list(${i(n)})`,[]);if(a.length===0)return;const e={};for(const r of a){const s=String(r.from),l=String(r.table);e[s]??=l}return e},K=async(t,o)=>{await S(t,o);const n=await p(t);return Promise.all(n.map(async a=>({name:a,rowCount:await T(t,i(a))})))},Y=async(t,o,n)=>{const{table:a}=n;await A(t,o,a);const e=h(Math.trunc(n.limit??C),1,g),r=Math.max(0,Math.trunc(n.offset??0)),s=i(a),l=await R(t,o,a),{params:w,where:u}=O(o,a,l,n.filters),f=await T(t,s,u,w),m=(await t.all(`SELECT * FROM ${s}${u} LIMIT ? OFFSET ?`,[...w,e,r])).map(d=>{const c=q(o,a,d);return M(c)?v(c):c}),b=await G(t,o,a);return b===void 0?{columns:l,rows:m,total:f}:{columns:l,refs:b,rows:m,total:f}},H=async(t,o,n)=>{const{column:a,table:e}=n;await A(t,o,e);const r=await R(t,o,e);if(!r.includes(a))throw new E("UNKNOWN_COLUMN",`unknown column: ${a}`,{status:404});const s=i(e),{params:l,where:w}=O(o,e,r,n.filters);if(o.tables[e]===void 0&&N.test(a)){const c=await T(t,s,w,l);return{truncated:!1,values:c===0?[]:[{count:c,value:"•••"}]}}const u=h(Math.trunc(n.limit??y),1,U),f=i(L(o,e,a)),_=await t.all(`SELECT ${f} AS value, COUNT(*) AS count FROM ${s}${w} GROUP BY ${f} ORDER BY count DESC LIMIT ?`,[...l,u+1]),m=_.length>u,d=(m?_.slice(0,u):_).map(c=>({count:Number(c.count),value:c.value}));return{truncated:m,values:M(d)?v(d):d}};export{H as facetGlobalColumn,K as listGlobalTables,Y as readGlobalTablePage};
|