@prisma/dev 0.24.9 → 0.24.10
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/chunk-2XJK3IOI.js +3 -0
- package/dist/chunk-NR64I267.js +1 -0
- package/dist/chunk-PP43TGA5.js +107 -0
- package/dist/daemon.cjs +31 -24
- package/dist/daemon.d.cts +3 -2
- package/dist/daemon.d.ts +3 -2
- package/dist/daemon.js +1 -1
- package/dist/db-BzbLAxLn.d.cts +126 -0
- package/dist/db-HU7BFiFV.d.ts +126 -0
- package/dist/db.cjs +23 -16
- package/dist/db.d.cts +3 -73
- package/dist/db.d.ts +3 -73
- package/dist/db.js +1 -1
- package/dist/index.cjs +33 -26
- package/dist/index.d.cts +9 -6
- package/dist/index.d.ts +9 -6
- package/dist/index.js +1 -1
- package/dist/{state-CDXGsSbm.d.ts → state-CNKFAMiX.d.cts} +14 -1
- package/dist/{state-CDXGsSbm.d.cts → state-CNKFAMiX.d.ts} +14 -1
- package/dist/state.cjs +3 -3
- package/dist/state.d.cts +1 -1
- package/dist/state.d.ts +1 -1
- package/dist/state.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-7KFGQZD5.js +0 -3
- package/dist/chunk-PGJI3PZW.js +0 -100
- package/dist/chunk-UAYMGXE6.js +0 -1
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import{e as h,f,g as M}from"./chunk-EDFHV3AK.js";import{a as A,c as m,g as V,h as C,i as L,j as x}from"./chunk-DWY47FQV.js";import{writeFile as se}from"fs/promises";import{join as p}from"pathe";import{check as oe,lock as ie,unlock as ne}from"proper-lockfile";import{process as B}from"std-env";import{integer as K,literal as ue,minLength as R,minValue as Q,number as Z,object as P,optional as l,parseJson as de,pipe as c,safeParse as le,string as b,url as ce}from"valibot";import{setTimeout as te}from"timers/promises";import{process as D}from"std-env";function _(r,e){if(r==null)return!1;try{return D.kill?.(r,0)??!0}catch(t){return e&&console.error(`Error checking if process with PID ${r} exists:`,t),!1}}async function J(r,e){if(!D.kill)return!1;try{D.kill(r,"SIGTERM")}catch(a){return e&&console.error(`Error killing process with PID ${r}:`,a),!1}let t=0;do{if(!_(r,e))return!0;await te(100)}while(++t<50);try{return D.kill(r,"SIGKILL")}catch(a){return e&&console.error(`Error forcefully killing process with PID ${r}:`,a),!1}}import{join as j}from"pathe";var re="prisma-wal",xe="prisma-queries";function ae(){return j(A.data,"durable-streams")}function H(r){return j(ae(),r)}function Me(r,e=re){return new URL(`/v1/stream/${encodeURIComponent(e)}`,r).toString()}var S=c(b(),ce()),Y=P({connectionString:S,prismaORMConnectionString:l(S),terminalCommand:l(b())}),F=P({url:S}),G=P({serverUrl:S,sqlitePath:c(b(),R(1)),streamName:c(b(),R(1)),url:S}),me=P({queryInsights:l(G),streams:l(G)}),I=c(Z(),K(),Q(1)),he=P({database:Y,http:F,ppg:F,shadowDatabase:Y}),pe=P({databasePort:I,experimental:l(me),exports:l(he),name:c(b(),R(1)),pid:l(c(Z(),K(),Q(0))),port:I,shadowDatabasePort:I,version:ue("1")}),O=Symbol("initialize"),E="default",be=new Set(["durable-streams"]),u=class{_databasePort;databaseConnectTimeoutMillis;databaseIdleTimeoutMillis;debug;dryRun;name;persistenceMode;pid;shadowDatabaseConnectTimeoutMillis;shadowDatabaseIdleTimeoutMillis;_port;_shadowDatabasePort;_streamsPort;constructor(e){this._databasePort=e.databasePort??f,this.databaseConnectTimeoutMillis=e.databaseConnectTimeoutMillis??6e4,this.databaseIdleTimeoutMillis=e.databaseIdleTimeoutMillis??1/0,this.debug=e.debug??!1,this.dryRun=e.dryRun??!1,this.name=e.name??E,this.persistenceMode=e.persistenceMode,this.pid=e.pid??B.pid,this.shadowDatabaseConnectTimeoutMillis=e.shadowDatabaseConnectTimeoutMillis??this.databaseConnectTimeoutMillis,this.shadowDatabaseIdleTimeoutMillis=e.shadowDatabaseIdleTimeoutMillis??this.databaseIdleTimeoutMillis,this._port=e.port??f,this._shadowDatabasePort=e.shadowDatabasePort??f,this._streamsPort=e.streamsPort??f}static async createExclusively(e){let t=e?.dryRun!==!0&&e?.persistenceMode==="stateful"?new g(e):new k(e);return await t[O](),t}static async fromServerDump(e){let{debug:t,name:a=E}=e??{},o=m(a),i=g.getServerDumpPath(o),s=await V(i);if(s==null)return t&&console.debug(`[State] No server dump file found at: ${i}`),null;t&&(console.debug(`[State] server dump file found at "${i}":`),console.debug(s));let{issues:n,output:d,success:v}=le(c(b(),de(),pe),s);if(!v)throw t&&console.debug(`[State] Invalid server dump file at "${i}":
|
|
2
|
+
${JSON.stringify(n,null,2)}`),new Error(`Invalid Prisma Dev state for "${a}".`);return new g({databasePort:d.databasePort,debug:t,dryRun:!1,name:a,pid:d.pid,port:d.port,serverDump:d,shadowDatabasePort:d.shadowDatabasePort})}static async scan(e){let{debug:t,globs:a}=e??{},o=p(m(E),"..");t&&console.debug(`[State] scanning for server states in: ${o}`);let i=(await L(o,a)).filter(s=>!be.has(s));return t&&console.debug(`[State] found server names: ${JSON.stringify(i)}`),await Promise.all(i.map(s=>z(s,e)))}get databasePort(){return this._databasePort}set databasePort(e){this.#t("databasePort",e)}get port(){return this._port}set port(e){this.#t("port",e)}get shadowDatabasePort(){return this._shadowDatabasePort}get streamsPort(){return this._streamsPort}set shadowDatabasePort(e){this.#t("shadowDatabasePort",e)}#t(e,t){if(t<0||!Number.isInteger(t))throw new Error(`Invalid port number: ${t}`);let a=`_${e}`;if(this[a]!==h&&this[a]!==t)throw new Error(`\`${e}\` is already set to ${this[a]}, cannot change it to ${t}`);this[a]=t}},k=class extends u{constructor(e){super({...e,databasePort:e?.databasePort||h,persistenceMode:"stateless",port:e?.port||h,shadowDatabasePort:e?.shadowDatabasePort||h,streamsPort:e?.streamsPort||h})}get databaseDumpPath(){return"<DUMP_PATH>"}get pgliteDataDirPath(){return"memory://"}async[O](){let e;try{e=await u.scan({debug:this.debug,onlyMetadata:!0})}catch(a){this.debug&&console.warn("[State] failed to scan for existing servers, assuming filesystem does not exist or other reasons.",a),e=[]}let t=await M({debug:this.debug,name:this.dryRun?this.name:"",requestedPorts:{databasePort:this.databasePort,port:this.port,shadowDatabasePort:this.shadowDatabasePort,streamsPort:this.streamsPort},servers:e});this._databasePort=t.databasePort,this._port=t.port,this._shadowDatabasePort=t.shadowDatabasePort,this._streamsPort=t.streamsPort}async close(){}async writeServerDump(){}},g=class r extends u{#t;#e;#a;#o;#i;#s;#r;constructor(e){super({...e,persistenceMode:"stateful"}),this.#s=!1,this.#e=m(this.name),this.#t=p(this.#e,"db_dump.bak"),this.#a=p(this.#e,".lock"),this.#o=p(this.#e,".pglite"),this.#r=e?.serverDump??null,this.#i=r.getServerDumpPath(this.#e)}static getServerDumpPath(e){return p(e,"server.json")}get databaseDumpPath(){return this.#t}get exports(){return this.#r?.exports}get experimental(){return this.#r?.experimental}get pgliteDataDirPath(){return this.#o}async[O](){await C(this.#e),this.debug&&console.debug(`[State] using data directory: ${this.#e}`);try{await ie(this.#e,{lockfilePath:this.#a}),this.debug&&console.debug(`[State] obtained lock on: ${this.#e}`);let e=await u.scan({debug:this.debug,onlyMetadata:!0}),t=await M({debug:this.debug,name:this.name,requestedPorts:{databasePort:this.databasePort,port:this.port,shadowDatabasePort:this.shadowDatabasePort,streamsPort:this.streamsPort},servers:e});this._databasePort=t.databasePort,this._port=t.port,this._shadowDatabasePort=t.shadowDatabasePort,this._streamsPort=t.streamsPort,await this.writeServerDump()}catch(e){throw e instanceof Error&&"code"in e&&e.code==="ELOCKED"?new $(this):e}}async close(){if(!this.#s)try{await ne(this.#e,{lockfilePath:this.#a}),this.#s=!0,this.debug&&console.debug(`[State] released lock on: ${this.#e}`)}catch(e){throw this.debug&&console.error(`[State] failed to release lock on: ${this.#e}`,e),e}}async writeServerDump(e,t){this.#r={name:this.name,version:"1",pid:B.pid,port:this.port,databasePort:this.databasePort,experimental:t,shadowDatabasePort:this.shadowDatabasePort,exports:e},await se(this.#i,`${JSON.stringify(this.#r,null,2)}
|
|
3
|
+
`,{encoding:"utf-8"})}};async function Ce(r,e){await fe(r,e);let t=typeof r=="string"?r:r.name,a=m(t);await x(a),await x(H(t))}async function z(r,e){let{debug:t,onlyMetadata:a}=e||{},o=typeof r=="string"?r:r.name,i=typeof r!="string"?r:void 0,s={databasePort:i?.databasePort??-1,experimental:i?.experimental,exports:i?.exports,name:o,pid:i?.pid,port:i?.port??-1,shadowDatabasePort:i?.shadowDatabasePort??-1,version:"1"};try{let n=i||await u.fromServerDump({debug:t,name:o});if(!n)return t&&console.debug(`[State] no server state found for name: ${o}`),{...s,status:"no_such_server"};s.databasePort=n.databasePort,s.experimental=n.experimental,s.exports=n.exports,s.pid=n.pid,s.port=n.port,s.shadowDatabasePort=n.shadowDatabasePort;let{exports:d,pid:v}=n;if(a)return{...s,status:"unknown"};if(!_(v,t))return t&&console.debug(`[State] server state for "${o}" has no running process with PID: ${v}`),{...s,status:"not_running"};let N=m(o);try{if(!await oe(N,{lockfilePath:p(N,".lock")}))return t&&console.debug(`[State] server state for "${o}" is not locked, indicating it is not running.`),{...s,status:"not_running"}}catch(ee){t&&console.error(`[State] server state for "${o}" failed to check lock:`,ee)}if(!d)return{...s,status:"starting_up"};let{http:W}=d,{hc:X}=await import("hono/client"),y=await X(W.url).health.$get();if(!y.ok)return t&&console.debug(`[State] server state for "${o}" is not live: ${JSON.stringify(y)}`),{...s,status:"not_running"};let w=await y.json();return w.name!==r?(t&&console.debug(`[State] server state for "${o}" has mismatched health response: ${JSON.stringify(w)}`),{...s,status:"unknown"}):(t&&console.debug(`[State] server state for "${r}" is live: ${JSON.stringify(w)}`),{...s,status:"running"})}catch(n){return t&&console.error(`[State] failed to get server status for "${o}":`,n),{...s,status:"error"}}}function Pe(r){let{status:e}=r;return e==="running"||e==="starting_up"}async function fe(r,e){let{pid:t,...a}=typeof r=="string"?await z(r,{debug:e}):r;if(!Pe(a))return!1;let o=await u.fromServerDump({debug:e,name:a.name});if(t==null){e&&console.debug(`[State] No PID found for server "${a.name}" to kill.`);try{await o?.close()}catch{}return!1}let i=await J(t,e);try{await o?.close()}catch{}return i}var T=class extends Error{name="ServerStateAlreadyExistsError";constructor(e){super(`A Prisma Dev server with the name "${e}" is already running.`)}},$=class extends T{#t;name="ServerAlreadyRunningError";constructor(e){super(e.name),this.#t=e}get server(){return u.fromServerDump({debug:this.#t.debug,name:this.#t.name})}};export{re as a,xe as b,ae as c,H as d,Me as e,u as f,Ce as g,z as h,Pe as i,fe as j,T as k,$ as l};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as W,b as O,f as R}from"./chunk-PP43TGA5.js";import{a as C,b as I,c as $,d as T,e as f,f as M}from"./chunk-2XJK3IOI.js";import{h as x}from"./chunk-EDFHV3AK.js";import{a as N}from"./chunk-HQMJSXYT.js";import{d as q,j as g}from"./chunk-DWY47FQV.js";import{a as _}from"./chunk-3UF2MBYQ.js";import{createServer as ee}from"http";import{promisify as re}from"util";async function j(r,e){let{port:t}=e;if(e.dryRun)return U(t,null);let n=await te(r,e),{promise:s,reject:i,resolve:l}=N(),{serve:a}=await import("@hono/node-server"),o=a({createServer:ee,fetch:n.fetch,overrideGlobalObjects:!1,port:t},l);o.on("error",m=>{if(typeof m=="object"&&"code"in m&&m.code==="EADDRINUSE")return i(new x(t));console.error("[Accelerate]",m)});let{port:c}=await s;return e.port=c,U(c,o)}function U(r,e){return{async close(){if(!e)return;let{Engine:t}=await import("./engine-XP6YJ63T.js");await Promise.allSettled([re(e.close.bind(e))(),t.stopAll()])},port:r,url:`http://localhost:${r}`}}async function te(r,e){let{debug:t}=e,[{Hono:n},{accelerateRoute:s},{utilityRoute:i}]=await Promise.all([import("hono/tiny"),import("./accelerate-5FDEK4T6.js"),import("./utility-Q5A254LJ.js")]),l=new n;if(t){let{logger:o}=await import("hono/logger");l.use("*",o((...c)=>console.log("[Accelerate]",...c)))}l.use("*",async(o,c)=>(o.set("databaseDumpPath",e.databaseDumpPath),o.set("db",r),o.set("debug",!!t),o.set("name",e.name),o.set("shadowDBPort",e.shadowDatabasePort),await c()));let a=new n;return a.route("/",s),a.route("/",i),l.route("/",a),l}import{isDeepStrictEqual as F}from"util";var ne="application/json",w={connection:"close","content-type":ne},k={apiVersion:"durable.streams/schema-registry/v1",schema:{additionalProperties:!0,properties:{applicationName:{type:["string","null"]},durationMs:{type:"number"},groupKey:{type:["string","null"]},query:{type:"string"},queryId:{type:"string"},reads:{type:"number"},rowsReturned:{type:"number"},tables:{items:{type:"string"},type:"array"},timestamp:{format:"date-time",type:"string"}},required:["durationMs","query","queryId","reads","rowsReturned","tables","timestamp"],type:"object"},search:{aliases:{applicationName:"applicationName",groupKey:"groupKey",queryId:"queryId"},fields:{applicationName:{bindings:[{jsonPointer:"/applicationName",version:1}],exact:!0,exists:!0,kind:"keyword"},eventTime:{bindings:[{jsonPointer:"/timestamp",version:1}],column:!0,exact:!0,exists:!0,kind:"date",sortable:!0},groupKey:{bindings:[{jsonPointer:"/groupKey",version:1}],exact:!0,exists:!0,kind:"keyword"},queryId:{bindings:[{jsonPointer:"/queryId",version:1}],exact:!0,exists:!0,kind:"keyword"}},primaryTimestampField:"eventTime"}};function H(r){return{...r,snapshot:async e=>await se(r.url,e)}}async function L(r){let e={serverUrl:r.serverUrl,sqlitePath:r.sqlitePath,streamName:I,url:f(r.serverUrl,I)};await oe(e);let t=new A({debug:r.debug,streamUrl:e.url}),n=r.bridge.subscribe(s=>{t.enqueue(s)});return{close:async()=>{n(),await t.close()},experimental:e}}async function se(r,e){if(!r)return W();let t=await ae(r);return O(t,e)}async function ae(r){let e=await fetch(`${r}?offset=-1&format=json`,{headers:{connection:"close"}});if(!e.ok)throw new Error(`Failed to read ${r}: HTTP ${e.status}`);return await e.json()}var A=class{#t;#r;#e;constructor(e){this.#t=e.debug,this.#r=Promise.resolve(),this.#e=e.streamUrl}enqueue(e){e.length!==0&&(this.#r=this.#r.then(async()=>{await ce(this.#e,e),this.#t&&console.debug(`[streams] appended ${e.length} query insight record(s) to ${this.#e}`)}).catch(t=>{console.error("[streams] failed to ingest query insights into prisma-queries",t)}))}async close(){await this.#r}};async function oe(r){let e=await fetch(r.url,{headers:w,method:"PUT"});if(!e.ok)throw await b(e,`Failed to create ${r.streamName}`);let t=await le(r);if(ie(t,r.streamName))return;if(t.currentVersion>0)throw new Error(`Failed to install schema for ${r.streamName}: existing schema registry is incompatible and requires a lens migration`);let n=await fetch(`${r.url}/_schema`,{body:JSON.stringify(k),headers:w,method:"POST"});if(!n.ok)throw await b(n,`Failed to install schema for ${r.streamName}`)}function ie(r,e){if(r.currentVersion<=0||r.schema!==e)return!1;let t=r.schemas[String(r.currentVersion)];return F(t,k.schema)&&F(r.search,k.search)}async function le(r){let e=await fetch(`${r.url}/_schema`,{headers:w,method:"GET"});if(!e.ok)throw await b(e,`Failed to inspect schema for ${r.streamName}`);return await e.json()}async function ce(r,e){let t=await fetch(r,{body:JSON.stringify(e),headers:w,method:"POST"});if(!t.ok)throw await b(t,"Failed to append to prisma-queries")}async function b(r,e){let t=await r.text().catch(()=>"");return new Error(`${e}: HTTP ${r.status}${t?` ${t}`:""}`)}import{randomUUID as me}from"crypto";import{setTimeout as K}from"timers/promises";import{isDeepStrictEqual as V}from"util";import{process as ue}from"std-env";var pe="application/json",y={connection:"close","content-type":pe},de={apiVersion:"durable.streams/profile/v1",profile:{kind:"state-protocol",touch:{enabled:!0,onMissingBefore:"coarse"}}},Q={apiVersion:"durable.streams/schema-registry/v1",schema:{additionalProperties:!0,properties:{headers:{properties:{operation:{type:"string"},timestamp:{format:"date-time",type:"string"}},required:["timestamp","operation"],type:"object"},key:{type:"string"},type:{type:"string"}},required:["type","key","headers"],type:"object"},search:{aliases:{rowKey:"key",table:"type"},fields:{eventTime:{bindings:[{jsonPointer:"/headers/timestamp",version:1}],column:!0,exact:!0,exists:!0,kind:"date",sortable:!0},key:{bindings:[{jsonPointer:"/key",version:1}],exact:!0,exists:!0,kind:"keyword"},operation:{bindings:[{jsonPointer:"/headers/operation",version:1}],exact:!0,exists:!0,kind:"keyword"},type:{bindings:[{jsonPointer:"/type",version:1}],exact:!0,exists:!0,kind:"keyword"}},primaryTimestampField:"eventTime"}};async function G(r){let{dbServer:e,debug:t,name:n,persistenceMode:s,port:i,queryInsightsBridge:l,walBridge:a}=r,o=s==="stateless",c=o?Re(n):n,m=T(c),P=!o&&await q(m);ue.env.DS_LOCAL_DATA_ROOT=$(),o&&await g(m);let u=await ge({debug:t,hadExistingStreamsData:P,name:c,port:i}),d={serverUrl:u.exports.http.url,sqlitePath:u.exports.sqlite.path,streamName:C,url:f(u.exports.http.url)};try{await fe(d);let p=await L({bridge:l,debug:t,serverUrl:u.exports.http.url,sqlitePath:u.exports.sqlite.path}),h=new D({dbServer:e,debug:t,streamUrl:d.url}),X=a.subscribe(Z=>{h.enqueue(Z)});return{close:async()=>{X(),await h.close(),await p.close(),await u.close(),await K(100),o&&await g(m)},experimental:d,experimentalQueryInsights:p.experimental}}catch(p){throw await u.close().catch(()=>{}),await K(100),o&&await g(m).catch(()=>{}),p}}var he=["database disk image is malformed","duplicate column name:","file is not a database","malformed database schema","no such column:","no such table:","schema_version row missing after migration","unexpected schema version:"];async function ge(r){let{debug:e,hadExistingStreamsData:t,name:n,port:s}=r,{startLocalDurableStreamsServer:i}=await import("@prisma/streams-local"),l=()=>i({hostname:"127.0.0.1",name:n,port:s});try{return await l()}catch(a){if(!t||!ye(a))throw a;return console.warn(`[streams] resetting incompatible durable streams data for "${n}"`),e&&console.debug(`[streams] original durable streams startup error for "${n}"`,a),await g(T(n)),await l()}}function ye(r){return Se(r).map(t=>t.toLowerCase()).some(t=>he.some(n=>t.includes(n)))}function Se(r){let e=[],t=[r],n=new Set;for(;t.length>0;){let s=t.shift();if(!(s==null||n.has(s))){if(n.add(s),typeof s=="string"){e.push(s);continue}if(s instanceof AggregateError)for(let i of s.errors)t.push(i);if(s instanceof Error){e.push(s.message);let i=s.cause;i!==void 0&&t.push(i)}}}return e}var D=class{#t;#r;#e;#n;#s;constructor(e){this.#t=e.dbServer,this.#r=e.debug,this.#e=Promise.resolve(),this.#n=e.streamUrl,this.#s=new Set}enqueue(e){e.length!==0&&(this.#e=this.#e.then(async()=>{let t=await this.#o(e);t.length!==0&&(await we(this.#n,t),this.#r&&console.debug(`[streams] appended ${t.length} state-protocol record(s) to ${this.#n}`))}).catch(t=>{console.error("[streams] failed to ingest WAL events into prisma-wal",t)}))}async close(){await this.#e}async#o(e){let t=[],n=new Date().toISOString();for(let s of e)t.push(...await this.#i(s,n));return t}async#i(e,t){let n=`${e.schema}.${e.table}`,s=await this.#t.getPrimaryKeyColumns(e.schema,e.table),i=J(e.record),l=J(e.oldRecord),a=this.#a(n,s,l),o=this.#a(n,s,i),c=e.txid===""?void 0:e.txid;return e.type==="insert"?i&&o?[{headers:{operation:"insert",timestamp:t,txid:c},key:o,old_value:null,type:n,value:i}]:[]:e.type==="delete"?l&&a?[{headers:{operation:"delete",timestamp:t,txid:c},key:a,old_value:l,type:n,value:null}]:[]:!i||!l||!a||!o?[]:a!==o?[{headers:{operation:"delete",timestamp:t,txid:c},key:a,old_value:l,type:n,value:null},{headers:{operation:"insert",timestamp:t,txid:c},key:o,old_value:null,type:n,value:i}]:[{headers:{operation:"update",timestamp:t,txid:c},key:o,old_value:l,type:n,value:i}]}#a(e,t,n){if(!n)return null;let s=be(n,t);return s||(this.#s.has(e)||(this.#s.add(e),console.warn(`[streams] falling back to full-row keys for ${e} because no primary key could be resolved`)),Pe(n))}};async function fe(r){let e=await fetch(r.url,{headers:y,method:"PUT"});if(!e.ok)throw await S(e,`Failed to create ${r.streamName}`);let t=await fetch(`${r.url}/_profile`,{body:JSON.stringify(de),headers:y,method:"POST"});if(!t.ok)throw await S(t,`Failed to configure ${r.streamName}`);let n=await ve(r);if(Ee(n,r.streamName))return;if(n.currentVersion>0)throw new Error(`Failed to install schema for ${r.streamName}: existing schema registry is incompatible and requires a lens migration`);let s=await fetch(`${r.url}/_schema`,{body:JSON.stringify(Q),headers:y,method:"POST"});if(!s.ok)throw await S(s,`Failed to install schema for ${r.streamName}`)}async function ve(r){let e=await fetch(`${r.url}/_schema`,{headers:y,method:"GET"});if(!e.ok)throw await S(e,`Failed to inspect schema for ${r.streamName}`);return await e.json()}function Ee(r,e){if(r.currentVersion<=0||r.schema!==e)return!1;let t=r.schemas[String(r.currentVersion)];return V(t,Q.schema)&&V(r.search,Q.search)}async function we(r,e){let t=await fetch(r,{body:JSON.stringify(e),headers:y,method:"POST"});if(!t.ok)throw await S(t,"Failed to append to prisma-wal")}async function S(r,e){let t=await r.text().catch(()=>"");return new Error(`${e}: HTTP ${r.status}${t?` ${t}`:""}`)}function be(r,e){if(e.length===0)return null;let t=[];for(let n of e){if(!Object.prototype.hasOwnProperty.call(r,n))return null;let s=xe(r[n]);if(s==null)return null;t.push(e.length===1?s:`${n}=${s}`)}return t.join("|")}function Pe(r){return JSON.stringify(B(r))}function B(r){return Array.isArray(r)?r.map(e=>B(e)):r&&typeof r=="object"?Object.fromEntries(Object.entries(r).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,B(t)])):r}function J(r){return r?structuredClone(r):null}function xe(r){if(r===null)return"null";if(r===void 0)return"undefined";if(typeof r=="string")return r;if(typeof r=="number")return Number.isFinite(r)?String(r):null;if(typeof r=="bigint")return r.toString();if(typeof r=="boolean")return r?"true":"false";try{return JSON.stringify(r)}catch{return null}}function Re(r){return`${r}.${me().replaceAll("-","")}`}async function Y(r){let e=await M.createExclusively(r),t=null,n=null,s=null,i=null;try{[t,s]=await Promise.all([R("database",e),R("shadow_database",e)]);let l,a;e.dryRun?(l=Ae(),a=Qe(),i=De()):(l=await t.attachWalEventBridge(),a=await t.attachQueryInsightsBridge(),i=await G({dbServer:t,debug:e.debug,name:e.name,port:e.streamsPort,persistenceMode:e.persistenceMode,queryInsightsBridge:a,walBridge:l})),n=await j(t,e);let o=Ie(t,s,n,e);await e.writeServerDump(o,e.dryRun?{}:{queryInsights:i.experimentalQueryInsights,streams:i.experimental});let c=ke(l),m=H(i.experimentalQueryInsights),P=t,u=s,d=n,p=i,h=async()=>{c.close(),await z(e,[d,p,P,u])};return{close:h,dbServer:t,httpServer:n,server:{...o,close:h,experimental:{queryInsights:m,streams:i.experimental,wal:c.api},name:e.name},serverState:e,shadowDbServer:s,streamsServer:i,queryInsightsBridge:a,walBridge:l}}catch(l){return await Te(e,[n,i,t,s],l)}}function Ie(r,e,t,n){let s=`prisma+postgres://localhost:${t.port}/?${new URLSearchParams({api_key:_({databaseUrl:r.prismaORMConnectionString,name:n.name,shadowDatabaseUrl:e.prismaORMConnectionString})}).toString()}`;return{database:{connectionString:r.connectionString,prismaORMConnectionString:r.prismaORMConnectionString,terminalCommand:r.terminalCommand},http:{url:t.url},ppg:{url:s},shadowDatabase:{connectionString:e.prismaORMConnectionString,prismaORMConnectionString:e.prismaORMConnectionString,terminalCommand:e.terminalCommand}}}async function z(r,e){let n=(await Promise.allSettled(e.map(s=>s.close()))).filter(s=>s.status==="rejected").map(s=>new Error(s.reason));try{await r.close()}catch(s){n.push(s)}if(n.length>0)throw new AggregateError(n,"Failed to close some servers")}async function Te(r,e,t){try{await z(r,e.filter(n=>n!==null))}catch(n){throw new AggregateError([t,n],"Failed to start Prisma Dev server cleanly")}throw t}function ke(r){let e=new Set;return{api:{stream:()=>{let t=()=>{},n=Be(r,()=>{e.delete(t)});return t=()=>n.close(),e.add(t),n.stream},subscribe:t=>r.subscribe(t)},close:()=>{for(let t of[...e])t();e.clear()}}}function Ae(){return{async close(){},async poll(){},subscribe(){return()=>{}}}}function Qe(){return{async close(){},subscribe(){return()=>{}}}}function De(){return{async close(){},experimental:{serverUrl:"",sqlitePath:"",streamName:"",url:""},experimentalQueryInsights:{serverUrl:"",sqlitePath:"",streamName:"",url:""}}}function Be(r,e){let t=[],n=!1,s=null,i=r.subscribe(a=>{if(!n){if(s){let o=s;s=null,o.resolve({done:!1,value:a});return}t.push(a)}}),l=()=>{if(!n&&(n=!0,i(),t.length=0,e(),s)){let a=s;s=null,a.resolve({done:!0,value:void 0})}};return{close:l,stream:{[Symbol.asyncIterator](){return this},next(){return t.length>0?Promise.resolve({done:!1,value:t.shift()}):n?Promise.resolve({done:!0,value:void 0}):new Promise((a,o)=>{s={reject:o,resolve:a}})},return(){return l(),Promise.resolve({done:!0,value:void 0})},throw(a){let o=s;return l(),o&&o.reject(a),Promise.reject(a instanceof Error?a:new Error(String(a)))}}}}async function _e(r){let{server:e}=await Y(r);return e}async function Sr(r){return await _e(r)}export{_e as a,Sr as b};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import{c as I}from"./chunk-KWCQYPJI.js";import{h as T}from"./chunk-EDFHV3AK.js";import{d as L,f as O}from"./chunk-DWY47FQV.js";import{createConnection as ke,createServer as qe}from"net";import{join as Fe}from"path";import{filename as Ue}from"pathe/utils";import{performance as W}from"perf_hooks";import{protocol as we}from"@electric-sql/pglite";import{Buffer as de}from"buffer";var C=500,Q=/(?:"[^"]+"|`[^`]+`|\[[^\]]+\]|\w+)/,me=new RegExp(`(?:${Q.source}\\.)*${Q.source}`),pe=new RegExp(`(?:FROM|JOIN|UPDATE|INTO|TABLE)\\s+(${me.source})`,"gi"),ge=/"([^"]+)"|`([^`]+)`|\[([^\]]+)\]|(\w+)/g,$=/prismaQuery='([^']+)'/,fe=new Set([S(`
|
|
2
|
+
select b.oid, b.typarray
|
|
3
|
+
from pg_catalog.pg_type a
|
|
4
|
+
left join pg_catalog.pg_type b on b.oid = a.typelem
|
|
5
|
+
where a.typcategory = $1
|
|
6
|
+
group by b.oid, b.typarray
|
|
7
|
+
order by b.oid
|
|
8
|
+
`),S('GRANT "pg_write_all_data" TO "prisma_application"'),S("SELECT SUM(reads)::BIGINT AS reads, SUM(writes)::BIGINT AS writes, SUM(COALESCE(extends, $1))::BIGINT AS extends FROM pg_stat_io"),S("SELECT pg_database_size(current_database())::BIGINT, SUM(pg_total_relation_size(relid))::BIGINT AS pg_total_relation_size FROM pg_stat_user_tables"),S("SELECT COUNT(*) AS cnt FROM pg_stat_activity WHERE pid <> pg_backend_pid() and xact_start notnull")]),ye=[/^BEGIN\b/i,/^COMMIT\b/i,/^ROLLBACK\b/i,/^SAVEPOINT\b/i,/^RELEASE\b/i,/^SET\b/i,/^SHOW\b/i,/^DISCARD\b/i,/^DEALLOCATE\b/i,/^CLOSE\b/i,/^CREATE EXTENSION\b/i,/^WITH state_assign\b/i];function Ze(){return{generatedAt:Date.now(),queries:[]}}function S(e){return e.replace(/\s+/g," ").trim()}function G(e){return U(S(e))}function k(e){if(!e?.action||!e.model&&/raw/i.test(e.action))return null;let t=e.payload?U(JSON.stringify(e.payload)):"0";return`${e.model??""}.${e.action}:${t}`}function Ee(e){let t=new Set;for(let r of e.matchAll(pe)){let n=r[1];if(!n)continue;let a;for(let s of n.matchAll(ge))a=s[1]??s[2]??s[3]??s[4];a&&!a.startsWith("pg_")&&t.add(a)}return Array.from(t)}function Se(e){return fe.has(e)?!0:ye.some(t=>t.test(e))}function q(e){if(Se(S(e)))return null;let t=Ee(e);return t.length===0&&/\bpg_\w+/i.test(e)?null:t}function N(e){if(Array.isArray(e))return e.map(N);if(e!==null&&typeof e=="object"){let t=e;return t.$type==="Param"?"<<redacted>>":Object.fromEntries(Object.entries(t).map(([r,n])=>[r,N(n)]))}return e}function F(e){let t=e.match($);if(!t?.[1])return{cleanedSql:S(e),prismaQueryInfo:null};let r=e.replace($,"");r=r.replace(/,\s*,/g,",").replace(/\/\*\s*,/g,"/*").replace(/,\s*\*\//g,"*/").replace(/\/\*\s*\*\//g,"").replace(/\s+/g," ").trim();let n=decodeURIComponent(t[1]),a=n.indexOf(":");if(a===-1)return{cleanedSql:r,prismaQueryInfo:{action:n,isRaw:!0}};let s=n.slice(0,a),d=n.slice(a+1),o=s.indexOf("."),u=o===-1?void 0:s.slice(0,o),c=o===-1?s:s.slice(o+1);if(!c)return{cleanedSql:r,prismaQueryInfo:null};let l;try{l=N(JSON.parse(de.from(d,"base64url").toString("utf8")))}catch{l=void 0}return{cleanedSql:r,prismaQueryInfo:{action:c,isRaw:!1,model:u,payload:l}}}function et(e,t={}){let r=new Set(t.excludeApplications??[]),n=be(t.limit),a=typeof t.since=="number"&&Number.isFinite(t.since)?t.since:null,s=new Map;for(let o of e){if(o.applicationName&&r.has(o.applicationName))continue;let u=Date.parse(o.timestamp);if(!Number.isFinite(u))continue;let c=`${o.queryId}:${o.groupKey??""}`,l=s.get(c);if(!l){s.set(c,{count:1,duration:o.durationMs,groupKey:o.groupKey,id:c,lastSeen:u,maxDurationMs:o.durationMs,minDurationMs:o.durationMs,prismaQueryInfo:o.prismaQueryInfo,query:o.query,queryId:o.queryId,reads:o.reads,rowsReturned:o.rowsReturned,tables:o.tables,totalDurationMs:o.durationMs});continue}l.count+=1,l.lastSeen=Math.max(l.lastSeen,u),l.maxDurationMs=Math.max(l.maxDurationMs??o.durationMs,o.durationMs),l.minDurationMs=Math.min(l.minDurationMs??o.durationMs,o.durationMs),l.reads+=o.reads,l.rowsReturned+=o.rowsReturned,l.totalDurationMs+=o.durationMs,l.duration=l.totalDurationMs/l.count,!l.prismaQueryInfo&&o.prismaQueryInfo&&(l.prismaQueryInfo=o.prismaQueryInfo),l.tables.length===0&&o.tables.length>0&&(l.tables=o.tables)}let d=Array.from(s.values()).filter(o=>a===null||o.lastSeen>=a).sort((o,u)=>u.lastSeen-o.lastSeen).slice(0,n).map(({totalDurationMs:o,...u})=>u);return{generatedAt:Date.now(),queries:d}}function be(e){return typeof e=="number"&&Number.isInteger(e)&&Number.isSafeInteger(e)&&e>0?Math.min(e,C):C}function U(e){let t=5381;for(let r=0;r<e.length;r++)t=(t<<5)+t+e.charCodeAt(r)&4294967295;return(t>>>0).toString(36)}var P=new WeakMap;function H(e){let t=P.get(e);if(t&&!t.closed)return t.bridge;let r=Pe(e),n={bridge:{close:()=>(n.closed||(n.closed=!0,n.sessions.clear(),n.subscribers.clear(),n.queryQueue.enqueue=n.originalEnqueue,n.queryQueue.clearQueueForHandler=n.originalClearQueueForHandler,P.delete(e)),Promise.resolve()),subscribe:a=>(n.subscribers.add(a),()=>{n.subscribers.delete(a)})},closed:!1,originalClearQueueForHandler:r.clearQueueForHandler.bind(r),originalEnqueue:r.enqueue.bind(r),queryQueue:r,sessions:new Map,subscribers:new Set};return r.clearQueueForHandler=a=>{n.sessions.delete(a),n.originalClearQueueForHandler(a)},r.enqueue=async(a,s,d)=>{let o=he(n,a),u=Ne(s);Te(o,u);let c=Ie(o,u);if(!c)return await n.originalEnqueue(a,s,d);let l={commandTags:[],dataRowCount:0},f=new we.Parser,i=W.now(),m=await n.originalEnqueue(a,s,b=>{f.parse(b,ce=>{Me(l,ce)}),d(b)}),g=Math.max(0,W.now()-i);return Be(n,{applicationName:c.applicationName,durationMs:g,query:c.query,responseStats:l}),m},P.set(e,n),n.bridge}async function K(e){await P.get(e)?.bridge.close()}function Pe(e){let t=e.queryQueue;if(!t||typeof t.enqueue!="function"||typeof t.clearQueueForHandler!="function")throw new Error("PGLiteSocketServer query queue is unavailable for query insights capture");return t}function he(e,t){let r=e.sessions.get(t);if(r)return r;let n={applicationName:null,portals:new Map,statements:new Map};return e.sessions.set(t,n),n}function Te(e,t){if(t)switch(t.kind){case"startup":e.applicationName=t.applicationName;return;case"parse":e.statements.set(t.statementName,t.query);return;case"bind":{let r=e.statements.get(t.statementName);r&&e.portals.set(t.portalName,r);return}case"close":t.target==="portal"?e.portals.delete(t.name):e.statements.delete(t.name);return;default:return}}function Ie(e,t){if(!t)return null;if(t.kind==="query")return{applicationName:e.applicationName,query:t.query};if(t.kind==="execute"){let r=e.portals.get(t.portalName);return r?{applicationName:e.applicationName,query:r}:null}return null}function Ne(e){let t=Buffer.from(e);if(t.length<4)return null;let r=t.readInt32BE(0);if(t.length>=8&&r===t.length){let a=t.readInt32BE(4);if(a===196608||a===196608)return Re(t)}if(t.length<5)return null;switch(String.fromCharCode(t[0]??0)){case"Q":return _e(t);case"P":return xe(t);case"B":return Ae(t);case"E":return De(t);case"C":return ve(t);default:return null}}function Re(e){let t=8,r=null;for(;t<e.length-1;){let n=E(e,t);if(!n||n.value==="")break;t=n.nextOffset;let a=E(e,t);if(!a)break;t=a.nextOffset,n.value==="application_name"&&(r=a.value)}return{applicationName:r,kind:"startup"}}function _e(e){let t=E(e,5);return t?{kind:"query",query:t.value}:null}function xe(e){let t=E(e,5);if(!t)return null;let r=E(e,t.nextOffset);return r?{kind:"parse",query:r.value,statementName:t.value}:null}function Ae(e){let t=E(e,5);if(!t)return null;let r=E(e,t.nextOffset);return r?{kind:"bind",portalName:t.value,statementName:r.value}:null}function De(e){let t=E(e,5);return t?{kind:"execute",portalName:t.value}:null}function ve(e){if(e.length<7)return null;let t=e[5];if(t===void 0)return null;let r=E(e,6);return r?{kind:"close",name:r.value,target:t===80?"portal":"statement"}:null}function E(e,t){let r=e.indexOf(0,t);return r===-1?null:{nextOffset:r+1,value:e.toString("utf8",t,r)}}function Me(e,t){if(t.name==="dataRow"){e.dataRowCount+=1;return}t.name==="commandComplete"&&typeof t.text=="string"&&t.text.length>0&&e.commandTags.push(t.text)}function Be(e,t){let r=F(t.query),n=q(r.cleanedSql);if(!n)return;let a=G(r.cleanedSql),s={applicationName:t.applicationName,durationMs:t.durationMs,groupKey:k(r.prismaQueryInfo),prismaQueryInfo:r.prismaQueryInfo,query:r.cleanedSql,queryId:a,reads:0,rowsReturned:Le(t.responseStats),tables:n,timestamp:new Date().toISOString()};for(let d of e.subscribers)d([s])}function Le(e){let t=e.commandTags.reduce((r,n)=>r+Oe(n),0);return Math.max(t,e.dataRowCount)}function Oe(e){let t=e.trim().split(/\s+/).at(-1);return!t||!/^\d+$/.test(t)?0:Number(t)}import{protocol as J}from"@electric-sql/pglite";var y="_prisma_dev_wal",A="events",X="install_all_triggers",Y="capture_event",z="prisma_dev_wal_capture",w=new WeakMap,Ce=new Set(["ALTER","COMMIT","COPY","CREATE","DELETE","DROP","INSERT","MERGE","TRUNCATE","UPDATE"]);async function D(e,t){let r=w.get(e);if(r&&!r.closed)return r.bridge;let n=e.execProtocolRaw.bind(e),a=e.execProtocolRawStream.bind(e),s={bridge:{close:async()=>{s.closed||(s.closed=!0,s.subscribers.clear(),e.execProtocolRaw===d&&(e.execProtocolRaw=n),e.execProtocolRawStream===o&&(e.execProtocolRawStream=a),await s.pollPromise,w.delete(e))},poll:async()=>{await x(s,e)},subscribe:u=>(s.subscribers.add(u),()=>{s.subscribers.delete(u)})},closed:!1,ensureInfrastructurePromise:null,pendingPoll:!1,pollPromise:null,subscribers:new Set,suppressDepth:0},d=async(u,c)=>{let l=await n(u,c);return!s.closed&&s.suppressDepth===0&&V(l)&&x(s,e),l},o=async(u,c)=>{let l=[],f=new J.Parser,i=c?.onRawData;await a(u,{...c,onRawData:m=>{f.parse(m,g=>{l.push(g)}),i?.(m)}}),!s.closed&&s.suppressDepth===0&&v(l)&&x(s,e)};return e.execProtocolRaw=d,e.execProtocolRawStream=o,w.set(e,s),await Z(s,e),s.bridge}async function j(e){let t=w.get(e);t&&await t.bridge.close()}function v(e){for(let t of e){if(t.name!=="commandComplete"||typeof t.text!="string")continue;let r=t.text.split(/\s+/,1)[0]?.toUpperCase();if(r&&Ce.has(r))return!0}return!1}function V(e){if(e.length===0)return!1;let t=[];return new J.Parser().parse(e,n=>{t.push(n)}),v(t)}async function Z(e,t){e.ensureInfrastructurePromise??=M(e,t,async()=>{await t.exec(`CREATE SCHEMA IF NOT EXISTS "${y}"`),await t.exec(`
|
|
9
|
+
CREATE TABLE IF NOT EXISTS "${y}"."${A}" (
|
|
10
|
+
id BIGSERIAL PRIMARY KEY,
|
|
11
|
+
txid BIGINT NOT NULL DEFAULT txid_current(),
|
|
12
|
+
schema_name TEXT NOT NULL,
|
|
13
|
+
table_name TEXT NOT NULL,
|
|
14
|
+
op TEXT NOT NULL,
|
|
15
|
+
row_data JSONB,
|
|
16
|
+
old_row_data JSONB,
|
|
17
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
|
|
18
|
+
)
|
|
19
|
+
`),await t.exec(`
|
|
20
|
+
CREATE OR REPLACE FUNCTION "${y}"."${Y}"()
|
|
21
|
+
RETURNS trigger
|
|
22
|
+
LANGUAGE plpgsql
|
|
23
|
+
AS $$
|
|
24
|
+
BEGIN
|
|
25
|
+
IF TG_TABLE_SCHEMA = '${y}' THEN
|
|
26
|
+
RETURN COALESCE(NEW, OLD);
|
|
27
|
+
END IF;
|
|
28
|
+
|
|
29
|
+
INSERT INTO "${y}"."${A}" (
|
|
30
|
+
txid,
|
|
31
|
+
schema_name,
|
|
32
|
+
table_name,
|
|
33
|
+
op,
|
|
34
|
+
row_data,
|
|
35
|
+
old_row_data
|
|
36
|
+
)
|
|
37
|
+
VALUES (
|
|
38
|
+
txid_current(),
|
|
39
|
+
TG_TABLE_SCHEMA,
|
|
40
|
+
TG_TABLE_NAME,
|
|
41
|
+
lower(TG_OP),
|
|
42
|
+
CASE WHEN TG_OP IN ('INSERT', 'UPDATE') THEN to_jsonb(NEW) ELSE NULL END,
|
|
43
|
+
CASE WHEN TG_OP IN ('UPDATE', 'DELETE') THEN to_jsonb(OLD) ELSE NULL END
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
RETURN COALESCE(NEW, OLD);
|
|
47
|
+
END;
|
|
48
|
+
$$;
|
|
49
|
+
`),await t.exec(`
|
|
50
|
+
CREATE OR REPLACE FUNCTION "${y}"."${X}"()
|
|
51
|
+
RETURNS void
|
|
52
|
+
LANGUAGE plpgsql
|
|
53
|
+
AS $$
|
|
54
|
+
DECLARE
|
|
55
|
+
target REGCLASS;
|
|
56
|
+
BEGIN
|
|
57
|
+
FOR target IN
|
|
58
|
+
SELECT c.oid::regclass
|
|
59
|
+
FROM pg_class AS c
|
|
60
|
+
JOIN pg_namespace AS n ON n.oid = c.relnamespace
|
|
61
|
+
WHERE c.relkind IN ('r', 'p')
|
|
62
|
+
AND n.nspname NOT IN ('${y}', 'information_schema', 'pg_catalog')
|
|
63
|
+
AND n.nspname NOT LIKE 'pg_temp_%'
|
|
64
|
+
AND n.nspname NOT LIKE 'pg_toast%'
|
|
65
|
+
LOOP
|
|
66
|
+
IF EXISTS (
|
|
67
|
+
SELECT 1
|
|
68
|
+
FROM pg_trigger
|
|
69
|
+
WHERE tgrelid = target
|
|
70
|
+
AND tgname = '${z}'
|
|
71
|
+
) THEN
|
|
72
|
+
CONTINUE;
|
|
73
|
+
END IF;
|
|
74
|
+
|
|
75
|
+
EXECUTE format(
|
|
76
|
+
'CREATE TRIGGER %I AFTER INSERT OR UPDATE OR DELETE ON %s FOR EACH ROW EXECUTE FUNCTION "${y}"."${Y}"()',
|
|
77
|
+
'${z}',
|
|
78
|
+
target::text
|
|
79
|
+
);
|
|
80
|
+
END LOOP;
|
|
81
|
+
END;
|
|
82
|
+
$$;
|
|
83
|
+
`),await ee(e,t)}),await e.ensureInfrastructurePromise}async function ee(e,t){await M(e,t,async()=>{await t.query(`SELECT "${y}"."${X}"()`)})}async function Qe(e,t){await Z(e,t),await ee(e,t);let r=await M(e,t,async()=>await t.query(`
|
|
84
|
+
WITH drained AS (
|
|
85
|
+
DELETE FROM "${y}"."${A}"
|
|
86
|
+
RETURNING txid, schema_name, table_name, op, row_data, old_row_data, id
|
|
87
|
+
)
|
|
88
|
+
SELECT txid, schema_name, table_name, op, row_data, old_row_data
|
|
89
|
+
FROM drained
|
|
90
|
+
ORDER BY id
|
|
91
|
+
`));if(r.rows.length===0||e.subscribers.size===0)return;let n=r.rows.map($e);for(let a of e.subscribers)queueMicrotask(()=>{if(!e.closed&&e.subscribers.has(a))try{a(n)}catch(s){console.error("[WAL bridge] subscriber failed",s)}})}async function x(e,t){if(!e.closed){if(e.pollPromise){e.pendingPoll=!0,await e.pollPromise;return}e.pollPromise=(async()=>{do e.pendingPoll=!1,await Qe(e,t);while(e.pendingPoll&&!e.closed)})().finally(()=>{e.pollPromise=null}),await e.pollPromise}}async function M(e,t,r){e.suppressDepth+=1;try{return await r()}finally{e.suppressDepth-=1,e.suppressDepth===0&&!e.closed&&w.get(t)!==e&&(e.closed=!0)}}function $e(e){return{oldRecord:e.old_row_data,record:e.row_data,schema:e.schema_name,table:e.table_name,txid:String(e.txid),type:Ge(e.op)}}function Ge(e){switch(e.toLowerCase()){case"delete":return"delete";case"insert":return"insert";case"update":return"update";default:throw new Error(`Unsupported WAL bridge operation: ${e}`)}}var We=10,re="127.0.0.1",ne=128*1024*1024,ae=["-c","shared_buffers=16MB","-c","temp_buffers=1MB","-c","work_mem=1MB","-c","maintenance_work_mem=16MB","-c","wal_buffers=1MB"],p={connectionLimit:We,connectTimeout:0,database:"template1",maxIdleConnectionLifetime:0,password:"postgres",poolTimeout:0,socketTimeout:0,sslMode:"disable",username:"postgres"},He=`postgres://${p.username}:${p.password}@localhost`,B=new URLSearchParams({sslmode:p.sslMode}),oe=new URLSearchParams({...Object.fromEntries(B.entries()),connection_limit:String(p.connectionLimit),connect_timeout:String(p.connectTimeout),max_idle_connection_lifetime:String(p.maxIdleConnectionLifetime),pool_timeout:String(p.poolTimeout),socket_timeout:String(p.socketTimeout)});async function se(e){let{rows:t}=await e.query("SELECT EXISTS(SELECT 1 FROM pg_roles WHERE rolname = 'postgres') AS exists");t[0]?.exists?await e.exec(`ALTER ROLE ${p.username} WITH LOGIN SUPERUSER PASSWORD '${p.password}'`):await e.exec(`CREATE ROLE ${p.username} WITH LOGIN SUPERUSER PASSWORD '${p.password}'`),await e.exec(`SET ROLE ${p.username}`)}async function bt(e,t){if(e==="shadow_database"&&!t.dryRun)return await Ke(t);let r=e==="database"?t.databasePort:t.shadowDatabasePort;return t.dryRun?le(e,t,{db:null,port:r,server:null}):await ie(e,t,{port:r,updateServerStatePort:!0})}async function Ke(e,t=async()=>await ie("shadow_database",e,{port:0,updateServerStatePort:!1})){let{debug:r}=e,n=null,a=null,s=!1,d=new Set,o=qe(c),u=await Ye(o,e.shadowDatabasePort);return e.shadowDatabasePort=u,r&&console.debug(`[shadow_database] lazy proxy listening on port ${u}`),{...p,attachWalEventBridge:()=>Promise.reject(new Error("WAL bridge is only available for the primary database server")),attachQueryInsightsBridge:()=>Promise.reject(new Error("Query insights are only available for the primary database server")),close:async()=>{s=!0;for(let m of d)m.destroy();d.clear();let i=[];try{await ze(o),r&&console.debug(`[shadow_database] lazy proxy stopped on port ${u}`)}catch(m){console.error("[shadow_database] lazy proxy stop error",m),i.push(m)}try{await a?.catch(()=>null),await n?.close()}catch(m){console.error("[shadow_database] backend close error",m),i.push(m)}if(i.length>0)throw new AggregateError(i,"Failed to close shadow_database properly")},connectionString:h(u,B),dump:async()=>{},getPrimaryKeyColumns:()=>Promise.reject(new Error("Primary key resolution is only available for the primary database server")),port:u,prismaORMConnectionString:h(u,oe),terminalCommand:`PGPASSWORD=${p.password} PGSSLMODE=${p.sslMode} psql -h localhost -p ${u} -U ${p.username} -d ${p.database}`};function c(i){d.add(i);let m=()=>{d.delete(i)};i.once("close",m),i.once("error",m),i.pause(),l(i)}async function l(i){try{let m=await f();if(s){i.destroy();return}let g=ke({host:re,port:m.port});d.add(g);let b=()=>{d.delete(g)};g.once("close",b),g.once("error",b),i.once("close",()=>g.destroy()),i.once("error",()=>g.destroy()),g.once("error",()=>i.destroy()),g.once("connect",()=>{r&&console.debug(`[shadow_database] proxying connection to lazy backend on port ${m.port}`),i.resume(),i.pipe(g),g.pipe(i)})}catch(m){r&&console.error("[shadow_database] failed to start lazy backend",m),i.destroy(m instanceof Error?m:void 0)}}async function f(){if(n)return n;if(s)throw new Error("shadow_database is closed");return a||(r&&console.debug("[shadow_database] starting lazy backend..."),a=t().then(i=>(n=i,r&&console.debug(`[shadow_database] lazy backend started on port ${i.port}`),i)).catch(i=>{throw a=null,i})),await a}}async function ie(e,t,r){let{debug:n}=t,{port:a,updateServerStatePort:s}=r,o=await(e==="shadow_database"?Je:ue)(t.pgliteDataDirPath,n);n&&o.onNotification((i,m)=>{console.debug(`[${e}][${i}] ${m}`)});let{PGLiteSocketServer:u}=await import("@electric-sql/pglite-socket"),c=e==="shadow_database"?t.shadowDatabaseIdleTimeoutMillis:t.databaseIdleTimeoutMillis,l=new u({db:o,debug:n,idleTimeout:Number.isFinite(c)?c:0,inspect:n,maxConnections:p.connectionLimit,port:a});n&&(l.addEventListener("listening",i=>{let{detail:m}=i;console.debug(`[${e}] server listening on ${JSON.stringify(m)}`)}),l.addEventListener("connection",i=>{let{clientAddress:m,clientPort:g}=i.detail;console.debug(`[${e}] client connected from ${m}:${g}`)}),l.addEventListener("error",i=>{let{detail:m}=i;console.error(`[${e}] server error:`,m)}));try{await l.start()}catch(i){throw i instanceof Error&&"code"in i&&i.code==="EADDRINUSE"?new T(a):i}let f=Number(l.getServerConn().split(":").at(1));return s&&(t[e==="database"?"databasePort":"shadowDatabasePort"]=f),le(e,t,{db:o,port:f,server:l})}function le(e,t,r){let{debug:n}=t,{db:a,port:s,server:d}=r||{},o=new Map;return n&&console.debug(`[${e}] server started on port ${s}`),{...p,attachWalEventBridge:async()=>{if(e!=="database"||!a)throw new Error("WAL bridge is only available for the primary database server");return await D(a)},attachQueryInsightsBridge:()=>{if(e!=="database"||!d)throw new Error("Query insights are only available for the primary database server");return Promise.resolve(H(d))},close:async()=>{let u=[];try{await d?.stop(),n&&console.debug(`[${e}] server stopped on port ${s}`)}catch(c){console.error(`[${e}] server stop error`,c),u.push(c)}if(e==="database"){try{d&&await K(d),n&&console.debug(`[${e}] closed query insights bridge`)}catch(c){console.error(`[${e}] query insights bridge close error`,c),u.push(c)}try{a&&await j(a),n&&console.debug(`[${e}] closed WAL bridge`)}catch(c){console.error(`[${e}] WAL bridge close error`,c),u.push(c)}try{await a?.syncToFs(),n&&console.debug(`[${e}] synced to filesystem`)}catch(c){console.error(`[${e}] sync error`,c),u.push(c)}}try{await a?.close(),n&&console.debug(`[${e}] closed`)}catch(c){console.error(`[${e}] close error`,c),u.push(c)}if(u.length>0)throw new AggregateError(u,`Failed to close ${e} properly`)},connectionString:h(s,B),dump:async u=>{e==="shadow_database"||!a||await je({db:a,debug:n,destinationPath:u})},getPrimaryKeyColumns:async(u,c)=>{if(e==="shadow_database"||!a)throw new Error("Primary key resolution is only available for the primary database server");let l=`${u}.${c}`,f=o.get(l);return f||(f=Xe(a,u,c).catch(i=>{throw o.delete(l),i}),o.set(l,f)),await f},port:s,prismaORMConnectionString:h(s,oe),terminalCommand:`PGPASSWORD=${p.password} PGSSLMODE=${p.sslMode} psql -h localhost -p ${s} -U ${p.username} -d ${p.database}`}}function h(e,t){return`${He}:${e}/${p.database}?${t.toString()}`}async function ue(e,t){let{PGlite:r}=await import("@electric-sql/pglite"),n=await I(),a=e==="memory://"||!await L(Fe(e,"PG_VERSION")),s=await r.create({database:p.database,dataDir:e,debug:t?5:void 0,extensions:n.extensions,fsBundle:n.fsBundle,initialMemory:ne,loadDataDir:a?n.loadDataDir:void 0,relaxedDurability:!1,startParams:[...r.defaultStartParams,...ae],wasmModule:n.wasmModule});return await se(s),s}async function Ye(e,t){return await new Promise((r,n)=>{let a=o=>{if(d(),o.code==="EADDRINUSE"){n(new T(t));return}n(o)},s=()=>{let o=e.address();if(d(),!o||typeof o=="string"){n(new Error("Failed to determine TCP server port"));return}r(o.port)},d=()=>{e.off("error",a),e.off("listening",s)};e.once("error",a),e.once("listening",s),e.listen(t,re)})}async function ze(e){await new Promise((t,r)=>{e.close(n=>{let a=n;if(a&&a.code!=="ERR_SERVER_NOT_RUNNING"){r(a);return}t()})})}async function Je(e,t){let{PGlite:r}=await import("@electric-sql/pglite"),n=await I(),a=await r.create({database:p.database,dataDir:"memory://",debug:t?5:void 0,extensions:n.extensions,fsBundle:n.fsBundle,initialMemory:ne,loadDataDir:n.loadDataDir,relaxedDurability:!1,startParams:[...r.defaultStartParams,...ae],wasmModule:n.wasmModule});return await se(a),a}async function Xe(e,t,r){let{rows:n}=await e.query(`
|
|
92
|
+
SELECT attribute.attname AS column_name
|
|
93
|
+
FROM pg_constraint pk_constraint
|
|
94
|
+
INNER JOIN pg_class relation
|
|
95
|
+
ON relation.oid = pk_constraint.conrelid
|
|
96
|
+
INNER JOIN pg_namespace namespace
|
|
97
|
+
ON namespace.oid = relation.relnamespace
|
|
98
|
+
INNER JOIN unnest(pk_constraint.conkey) WITH ORDINALITY AS keys(attnum, ordinality)
|
|
99
|
+
ON TRUE
|
|
100
|
+
INNER JOIN pg_attribute attribute
|
|
101
|
+
ON attribute.attrelid = relation.oid
|
|
102
|
+
AND attribute.attnum = keys.attnum
|
|
103
|
+
WHERE pk_constraint.contype = 'p'
|
|
104
|
+
AND namespace.nspname = ${te(t)}
|
|
105
|
+
AND relation.relname = ${te(r)}
|
|
106
|
+
ORDER BY keys.ordinality
|
|
107
|
+
`);return n.map(a=>a.column_name)}function te(e){return`'${e.replaceAll("'","''")}'`}async function je(e){let{dataDir:t,db:r,debug:n,destinationPath:a}=e,s=r||await ue(t,n),{pgDump:d}=await import("@electric-sql/pglite-tools/pg_dump"),o=await d({args:["--schema-only","--no-owner"],fileName:a?Ue(a):void 0,pg:await s.clone()});return a?(n&&console.debug(`[DB] Dumping database to ${a}`),await O(o,a)):(n&&console.debug("[DB] Dumping database to memory"),await o.text())}export{Ze as a,et as b,D as c,v as d,V as e,bt as f,Ke as g,je as h};
|