@lunora/d1 1.0.0-alpha.239 → 1.0.0-alpha.240
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{exportGlobalRows as e,importGlobalRows as t}from"./packem_shared/exportGlobalRows-IncHgv-p.mjs";import{D1Client as l,D1Session as i}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as s,createD1CtxDb as p,readD1CdcChanges as g,runD1AggregateMigrations as m,runD1CdcMigration as D,runD1GlobalTableMigrations as b,runD1RankMigrations as x,runD1SearchMigrations as f,sweepD1CdcRetention as c}from"./packem_shared/backfillD1SearchIndexes-BHgn1SNb.mjs";import{GlobalImportStagingError as d,abortStagedGlobalRows as u,commitStagedGlobalRows as R,stageGlobalRows as G}from"./packem_shared/GlobalImportStagingError-
|
|
1
|
+
import{exportGlobalRows as e,importGlobalRows as t}from"./packem_shared/exportGlobalRows-IncHgv-p.mjs";import{D1Client as l,D1Session as i}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as s,createD1CtxDb as p,readD1CdcChanges as g,runD1AggregateMigrations as m,runD1CdcMigration as D,runD1GlobalTableMigrations as b,runD1RankMigrations as x,runD1SearchMigrations as f,sweepD1CdcRetention as c}from"./packem_shared/backfillD1SearchIndexes-BHgn1SNb.mjs";import{GlobalImportStagingError as d,abortStagedGlobalRows as u,commitStagedGlobalRows as R,stageGlobalRows as G}from"./packem_shared/GlobalImportStagingError-d4vGUGYx.mjs";import{facetGlobalColumn as T,listGlobalTables as M,readGlobalTablePage as w}from"./packem_shared/facetGlobalColumn-BqK1_P3S.mjs";import{MigrationRunner as E}from"./packem_shared/MigrationRunner-CD5zcqp6.mjs";import{d1QueryTag as q,emitD1QueryCost as A,readD1QueryCost as I}from"./packem_shared/d1QueryTag-BcEGW4q1.mjs";import{D1TimeoutError as k,isTransientD1Error as L,retryingExec as _,withD1Retry as F}from"./packem_shared/D1TimeoutError-D4YUqZPk.mjs";import{applySqlMigrationFiles as O}from"./packem_shared/applySqlMigrationFiles-CUiRKrxP.mjs";import{default as U}from"./packem_shared/sqliteDialect-B_3JNtdq.mjs";import{applyCdcChanges as W}from"@lunora/shard-engine";import{ALLOW_DESTRUCTIVE_MARKER as v}from"./packem_shared/ALLOW_DESTRUCTIVE_MARKER-CwYNPwK3.mjs";import{createSqlCtxDb as B,selectGlobalTables as H}from"@lunora/sql-store";export{v as ALLOW_DESTRUCTIVE_MARKER,l as D1Client,i as D1Session,k as D1TimeoutError,d as GlobalImportStagingError,E as MigrationRunner,u as abortStagedGlobalRows,W as applyCdcChanges,O as applySqlMigrationFiles,s as backfillD1SearchIndexes,R as commitStagedGlobalRows,p as createD1CtxDb,B as createSqlCtxDb,q as d1QueryTag,A as emitD1QueryCost,e as exportGlobalRows,T as facetGlobalColumn,t as importGlobalRows,L as isTransientD1Error,M as listGlobalTables,g as readD1CdcChanges,I as readD1QueryCost,w as readGlobalTablePage,_ as retryingExec,m as runD1AggregateMigrations,D as runD1CdcMigration,b as runD1GlobalTableMigrations,x as runD1RankMigrations,f as runD1SearchMigrations,H as selectGlobalTables,U as sqliteDialect,G as stageGlobalRows,c as sweepD1CdcRetention,F as withD1Retry};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{toErrorBody as g}from"@lunora/errors";import{prepareGlobalImportRow as h}from"@lunora/sql-store";import{encodeWire as A,decodeWire as D}from"@lunora/wire";import{quoteIdentifier as f}from"./quoteIdentifier-CObIFRhb.mjs";const E="__lunora_import_stage__",c="__lunora_import_session__",u=1440*60*1e3,p=200,M=(o,s,t)=>{const e=[],i=[];let l=(s.startLine??1)-1;for(const a of s.rows){l+=1;const r=a.line??l;if(!t.has(a.table)){e.push({code:"BAD_ROW",line:r,message:`table "${a.table}" is not in this replace import's tables`,table:a.table});continue}const n=h(o,a,r);n.kind==="settled"?n.outcome.kind==="error"&&e.push(n.outcome.error):n.kind==="delete"?e.push({code:"BAD_ROW",line:r,message:'a replace import does not take `op: "delete"` rows',table:a.table}):n.explicitId===void 0?e.push({code:"BAD_ROW",line:r,message:"a replace import row must carry its `_id`",table:n.table}):i.push({decoded:n.decoded,id:n.explicitId,line:r,table:n.table})}return{errors:e,ready:i}},$=async(o,s,t)=>(await o.all(`SELECT 1 FROM ${f(s)} WHERE id = ? LIMIT 1`,[t])).length>0,y=async(o,s,t)=>{try{if(await $(s,t.table,t.id)){const e={...t.decoded};delete e._id,await o.replace(t.table,t.id,e,{allowExplicitId:!0})}else await o.insert(t.table,t.decoded,{allowExplicitId:!0});return}catch(e){const{body:i}=g(e,{fallbackCode:"INSERT_FAILED"});return{code:i.code,line:t.line,message:i.message,table:t.table}}};class R extends Error{code;constructor(s,t){super(t),this.name="GlobalImportStagingError",this.code=s}}const L=async o=>{await o.run(`CREATE TABLE IF NOT EXISTS "${E}" (seq INTEGER PRIMARY KEY AUTOINCREMENT, session TEXT NOT NULL, tbl TEXT NOT NULL, id TEXT NOT NULL, line INTEGER NOT NULL, doc TEXT NOT NULL)`,[]),await o.run(`CREATE INDEX IF NOT EXISTS "${E}_by_session" ON "${E}" (session, tbl, id)`,[]),await o.run(`CREATE TABLE IF NOT EXISTS "${c}" (session TEXT PRIMARY KEY, state TEXT NOT NULL, generation TEXT NOT NULL, expires_at INTEGER NOT NULL, result TEXT)`,[])},N=async(o,s)=>{if(o.batch){await o.batch(s);return}for(const{params:t,sql:e}of s)await o.run(e,t)},w=async(o,s)=>{await L(o),await N(o,[{params:[s],sql:`DELETE FROM "${E}" WHERE session IN (SELECT session FROM "${c}" WHERE expires_at < ? AND state IN ('staging', 'committed'))`},{params:[s],sql:`DELETE FROM "${c}" WHERE expires_at < ? AND state IN ('staging', 'committed')`}])},b=`INSERT INTO "${c}" (session, state, generation, expires_at) VALUES (?, 'staging', ?, ?) ON CONFLICT (session) DO UPDATE SET expires_at = excluded.expires_at`,_=async(o,s,t)=>{const[e]=await o.all(`SELECT state, generation, result FROM "${c}" WHERE session = ?`,[s]);if(e===void 0)return;const i=String(e.state);if(i!=="staging"&&i!=="committed")throw new R("IMPORT_SESSION_CORRUPT",`import session "${s}" has an unreadable state in D1`);if(String(e.generation)!==t)throw new R("IMPORT_SESSION_STALE",`import session "${s}" in D1 belongs to an earlier session of the same id`);return typeof e.result=="string"?{generation:t,result:JSON.parse(e.result),state:i}:{generation:t,state:i}},U=async(o,s,t,e=Date.now())=>{if(await w(o,e),(await _(o,t.session,t.generation))?.state==="committed")throw new R("IMPORT_SESSION_COMMITTED",`import session "${t.session}" is already committed`);const l=new Set(t.tables.filter(d=>s.tables[d]?.shardMode?.kind==="global")),{errors:a,ready:r}=M(s,t,l);if(a.length>0)return{errors:a,staged:{}};const n={};return await N(o,[{params:[t.session,t.generation,e+u],sql:b},...r.map(({decoded:d,id:T,line:S,table:m})=>(n[m]=(n[m]??0)+1,{params:[t.session,m,T,S,JSON.stringify(A(d))],sql:`INSERT INTO "${E}" (session, tbl, id, line, doc) VALUES (?, ?, ?, ?, ?)`}))]),{errors:[],staged:n}},C=async(o,s,t)=>{const e={};let i=0;for(;;){const l=await s.all(`SELECT seq, tbl, id, line, doc FROM "${E}" WHERE session = ? AND seq > ? ORDER BY seq LIMIT ?`,[t,i,p]),a=[];for(const r of l){const n=String(r.tbl),d=String(r.id),T=D(JSON.parse(String(r.doc))),S=await y(o,s,{decoded:T,id:d,line:Number(r.line),table:n});S===void 0?e[n]=(e[n]??0)+1:a.push(S)}if(a.length>0||l.length<p)return{errors:a,inserted:e};i=Number(l.at(-1)?.seq)}},O=o=>new R("IMPORT_SESSION_EXPIRED",`import session "${o}" has no staged .global() rows (expired, or aborted)`),I=async(o,s)=>(await o.all(`SELECT 1 FROM "${E}" WHERE session = ? LIMIT 1`,[s])).length>0,F=async(o,s,t,e,i)=>{if((await s.all("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?",[e])).length===0)return 0;let a=0;for(;;){const r=await s.all(`SELECT "id" FROM ${f(e)} WHERE "id" NOT IN (SELECT id FROM "${E}" WHERE session = ? AND tbl = ?)${i?` AND EXISTS (SELECT 1 FROM "${E}" WHERE session = ?)`:""} LIMIT ?`,i?[t,e,t,p]:[t,e,p]);for(const{id:n}of r)await o.delete(e,String(n),{hard:!0});if(a+=r.length,r.length<p){if(i&&!await I(s,t))throw O(t);return a}}},G=async(o,s,t,e,i=Date.now())=>{await w(s,i);const l=await _(s,e.session,e.generation);if(l?.state==="committed"&&l.result!==void 0)return l.result;if(e.staged&&(l===void 0||!await I(s,e.session)))throw O(e.session);await s.run(b,[e.session,e.generation,i+u]);const{errors:a,inserted:r}=await C(o,s,e.session);if(a.length>0)return{conflicts:0,deleted:{},errors:a,inserted:r};const n={};for(const T of e.tables.filter(S=>t.tables[S]?.shardMode?.kind==="global")){if(e.staged&&!await I(s,e.session))throw O(e.session);n[T]=await F(o,s,e.session,T,e.staged)}const d={conflicts:0,deleted:n,errors:[],inserted:r};return await N(s,[{params:[e.session,e.generation,i+u,JSON.stringify(d)],sql:`INSERT INTO "${c}" (session, state, generation, expires_at, result) VALUES (?, 'committed', ?, ?, ?) ON CONFLICT (session) DO UPDATE SET state = 'committed', expires_at = excluded.expires_at, result = excluded.result`},{params:[e.session],sql:`DELETE FROM "${E}" WHERE session = ?`}]),d},P=async(o,s,t)=>{await L(o),await N(o,[{params:[s,s,t],sql:`DELETE FROM "${E}" WHERE session = ? AND EXISTS (SELECT 1 FROM "${c}" WHERE session = ? AND generation = ?)`},{params:[s,t],sql:`DELETE FROM "${c}" WHERE session = ? AND generation = ?`}])};export{R as GlobalImportStagingError,P as abortStagedGlobalRows,G as commitStagedGlobalRows,U as stageGlobalRows};
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{toErrorBody as b}from"@lunora/errors";import{prepareGlobalImportRow as w}from"@lunora/sql-store";import{encodeWire as g,decodeWire as _}from"@lunora/wire";import{quoteIdentifier as u}from"./quoteIdentifier-CObIFRhb.mjs";const d="__lunora_import_stage__",c="__lunora_import_session__",O=1440*60*1e3,S=200,h=(o,t,s)=>{const e=[],n=[];let l=(t.startLine??1)-1;for(const i of t.rows){l+=1;const r=i.line??l;if(!s.has(i.table)){e.push({code:"BAD_ROW",line:r,message:`table "${i.table}" is not in this replace import's tables`,table:i.table});continue}const a=w(o,i,r);a.kind==="settled"?a.outcome.kind==="error"&&e.push(a.outcome.error):a.kind==="delete"?e.push({code:"BAD_ROW",line:r,message:'a replace import does not take `op: "delete"` rows',table:i.table}):a.explicitId===void 0?e.push({code:"BAD_ROW",line:r,message:"a replace import row must carry its `_id`",table:a.table}):n.push({decoded:a.decoded,id:a.explicitId,line:r,table:a.table})}return{errors:e,ready:n}},A=async(o,t,s)=>(await o.all(`SELECT 1 FROM ${u(t)} WHERE id = ? LIMIT 1`,[s])).length>0,M=async(o,t,s)=>{try{if(await A(t,s.table,s.id)){const e={...s.decoded};delete e._id,await o.replace(s.table,s.id,e,{allowExplicitId:!0})}else await o.insert(s.table,s.decoded,{allowExplicitId:!0});return}catch(e){const{body:n}=b(e,{fallbackCode:"INSERT_FAILED"});return{code:n.code,line:s.line,message:n.message,table:s.table}}};class R extends Error{code;constructor(t,s){super(s),this.name="GlobalImportStagingError",this.code=t}}const I=async o=>{await o.run(`CREATE TABLE IF NOT EXISTS "${d}" (seq INTEGER PRIMARY KEY AUTOINCREMENT, session TEXT NOT NULL, tbl TEXT NOT NULL, id TEXT NOT NULL, line INTEGER NOT NULL, doc TEXT NOT NULL)`,[]),await o.run(`CREATE INDEX IF NOT EXISTS "${d}_by_session" ON "${d}" (session, tbl, id)`,[]),await o.run(`CREATE TABLE IF NOT EXISTS "${c}" (session TEXT PRIMARY KEY, state TEXT NOT NULL, generation TEXT NOT NULL, expires_at INTEGER NOT NULL, result TEXT)`,[])},m=async(o,t)=>{if(o.batch){await o.batch(t);return}for(const{params:s,sql:e}of t)await o.run(e,s)},f=async(o,t)=>{await I(o),await m(o,[{params:[t],sql:`DELETE FROM "${d}" WHERE session IN (SELECT session FROM "${c}" WHERE expires_at < ? AND state IN ('staging', 'committed'))`},{params:[t],sql:`DELETE FROM "${c}" WHERE expires_at < ? AND state IN ('staging', 'committed')`}])},L=async(o,t,s)=>{const[e]=await o.all(`SELECT state, generation, result FROM "${c}" WHERE session = ?`,[t]);if(e===void 0)return;const n=String(e.state);if(n!=="staging"&&n!=="committed")throw new R("IMPORT_SESSION_CORRUPT",`import session "${t}" has an unreadable state in D1`);if(String(e.generation)!==s)throw new R("IMPORT_SESSION_STALE",`import session "${t}" in D1 belongs to an earlier session of the same id`);return typeof e.result=="string"?{generation:s,result:JSON.parse(e.result),state:n}:{generation:s,state:n}},H=async(o,t,s,e=Date.now())=>{if(await f(o,e),(await L(o,s.session,s.generation))?.state==="committed")throw new R("IMPORT_SESSION_COMMITTED",`import session "${s.session}" is already committed`);const l=new Set(s.tables.filter(E=>t.tables[E]?.shardMode?.kind==="global")),{errors:i,ready:r}=h(t,s,l);if(i.length>0)return{errors:i,staged:{}};const a={};return await m(o,[{params:[s.session,s.generation,e+O],sql:`INSERT INTO "${c}" (session, state, generation, expires_at) VALUES (?, 'staging', ?, ?) ON CONFLICT (session) DO UPDATE SET expires_at = excluded.expires_at`},...r.map(({decoded:E,id:T,line:p,table:N})=>(a[N]=(a[N]??0)+1,{params:[s.session,N,T,p,JSON.stringify(g(E))],sql:`INSERT INTO "${d}" (session, tbl, id, line, doc) VALUES (?, ?, ?, ?, ?)`}))]),{errors:[],staged:a}},D=async(o,t,s)=>{const e={};let n=0;for(;;){const l=await t.all(`SELECT seq, tbl, id, line, doc FROM "${d}" WHERE session = ? AND seq > ? ORDER BY seq LIMIT ?`,[s,n,S]),i=[];for(const r of l){const a=String(r.tbl),E=String(r.id),T=_(JSON.parse(String(r.doc))),p=await M(o,t,{decoded:T,id:E,line:Number(r.line),table:a});p===void 0?e[a]=(e[a]??0)+1:i.push(p)}if(i.length>0||l.length<S)return{errors:i,inserted:e};n=Number(l.at(-1)?.seq)}},$=async(o,t,s,e)=>{if((await t.all("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?",[e])).length===0)return 0;let l=0;for(;;){const i=await t.all(`SELECT "id" FROM ${u(e)} WHERE "id" NOT IN (SELECT id FROM "${d}" WHERE session = ? AND tbl = ?) LIMIT ?`,[s,e,S]);for(const{id:r}of i)await o.delete(e,String(r),{hard:!0});if(l+=i.length,i.length<S)return l}},y=async(o,t)=>(await o.all(`SELECT 1 FROM "${d}" WHERE session = ? LIMIT 1`,[t])).length>0,U=async(o,t,s,e,n=Date.now())=>{await f(t,n);const l=await L(t,e.session,e.generation);if(l?.state==="committed"&&l.result!==void 0)return l.result;if(e.staged&&(l===void 0||!await y(t,e.session)))throw new R("IMPORT_SESSION_EXPIRED",`import session "${e.session}" has no staged .global() rows (expired, or aborted)`);const{errors:i,inserted:r}=await D(o,t,e.session);if(i.length>0)return{conflicts:0,deleted:{},errors:i,inserted:r};const a={};for(const T of e.tables.filter(p=>s.tables[p]?.shardMode?.kind==="global"))a[T]=await $(o,t,e.session,T);const E={conflicts:0,deleted:a,errors:[],inserted:r};return await m(t,[{params:[e.session,e.generation,n+O,JSON.stringify(E)],sql:`INSERT INTO "${c}" (session, state, generation, expires_at, result) VALUES (?, 'committed', ?, ?, ?) ON CONFLICT (session) DO UPDATE SET state = 'committed', expires_at = excluded.expires_at, result = excluded.result`},{params:[e.session],sql:`DELETE FROM "${d}" WHERE session = ?`}]),E},X=async(o,t,s)=>{await I(o),await m(o,[{params:[t,t,s],sql:`DELETE FROM "${d}" WHERE session = ? AND EXISTS (SELECT 1 FROM "${c}" WHERE session = ? AND generation = ?)`},{params:[t,s],sql:`DELETE FROM "${c}" WHERE session = ? AND generation = ?`}])};export{R as GlobalImportStagingError,X as abortStagedGlobalRows,U as commitStagedGlobalRows,H as stageGlobalRows};
|