UNPKG

@electric-sql/pglite

Version:

PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It is only 3.7mb gzipped.

9 lines 3.87 kB
import{a as O,b as D,c as Q,d as v,f,g as A,i as o}from"./chunk-7PRRATDV.js";import{d as g}from"./chunk-STOZMFXW.js";import{e as w,f as P,g as p,h as a,j as E}from"./chunk-BTBUZ646.js";E();var b,u,r,l,d,h,R,z=class{constructor(){P(this,r);this.serializers={...D};this.parsers={...O};P(this,b,!1);P(this,u,!1)}async _initArrayTypes({force:t=!1}={}){if(w(this,b)&&!t)return;p(this,b,!0);let e=await this.query(` SELECT b.oid, b.typarray FROM pg_catalog.pg_type a LEFT JOIN pg_catalog.pg_type b ON b.oid = a.typelem WHERE a.typcategory = 'A' GROUP BY b.oid, b.typarray ORDER BY b.oid `);for(let s of e.rows)this.serializers[s.typarray]=i=>Q(i,this.serializers[s.oid],s.typarray),this.parsers[s.typarray]=i=>v(i,this.parsers[s.oid],s.typarray)}async refreshArrayTypes(){await this._initArrayTypes({force:!0})}async query(t,e,s){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await a(this,r,d).call(this,t,e,s))}async sql(t,...e){let{query:s,params:i}=g(t,...e);return await this.query(s,i)}async exec(t,e){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await a(this,r,h).call(this,t,e))}async describeQuery(t,e){try{await a(this,r,l).call(this,o.parse({text:t,types:e?.paramTypes}),e);let s=await a(this,r,l).call(this,o.describe({type:"S"}),e),i=s.messages.find(n=>n.name==="parameterDescription"),c=s.messages.find(n=>n.name==="rowDescription"),y=i?.dataTypeIDs.map(n=>({dataTypeID:n,serializer:this.serializers[n]}))??[],m=c?.fields.map(n=>({name:n.name,dataTypeID:n.dataTypeID,parser:this.parsers[n.dataTypeID]}))??[];return{queryParams:y,resultFields:m}}finally{await a(this,r,l).call(this,o.sync(),e)}}async transaction(t){return await this._checkReady(),await this._runExclusiveTransaction(async()=>{await a(this,r,h).call(this,"BEGIN"),p(this,u,!0);let e=!1,s=()=>{if(e)throw new Error("Transaction is closed")},i={query:async(c,y,m)=>(s(),await a(this,r,d).call(this,c,y,m)),sql:async(c,...y)=>{let{query:m,params:n}=g(c,...y);return await a(this,r,d).call(this,m,n)},exec:async(c,y)=>(s(),await a(this,r,h).call(this,c,y)),rollback:async()=>{s(),await a(this,r,h).call(this,"ROLLBACK"),e=!0},get closed(){return e}};try{let c=await t(i);return e||(e=!0,await a(this,r,h).call(this,"COMMIT")),p(this,u,!1),c}catch(c){throw e||await a(this,r,h).call(this,"ROLLBACK"),p(this,u,!1),c}})}async runExclusive(t){return await this._runExclusiveQuery(t)}};b=new WeakMap,u=new WeakMap,r=new WeakSet,l=async function(t,e={}){return await this.execProtocol(t,{...e,syncToFs:!1})},d=async function(t,e=[],s){return await this._runExclusiveQuery(async()=>{a(this,r,R).call(this,"runQuery",t,e,s),await this._handleBlob(s?.blob);let i;try{let{messages:y}=await a(this,r,l).call(this,o.parse({text:t,types:s?.paramTypes}),s),m=A((await a(this,r,l).call(this,o.describe({type:"S"}),s)).messages),n=e.map((T,B)=>{let x=m[B];if(T==null)return null;let _=s?.serializers?.[x]??this.serializers[x];return _?_(T):T.toString()});i=[...y,...(await a(this,r,l).call(this,o.bind({values:n}),s)).messages,...(await a(this,r,l).call(this,o.describe({type:"P"}),s)).messages,...(await a(this,r,l).call(this,o.execute({}),s)).messages]}finally{await a(this,r,l).call(this,o.sync(),s)}await this._cleanupBlob(),w(this,u)||await this.syncToFs();let c=await this._getWrittenBlob();return f(i,this.parsers,s,c)[0]})},h=async function(t,e){return await this._runExclusiveQuery(async()=>{a(this,r,R).call(this,"runExec",t,e),await this._handleBlob(e?.blob);let s;try{s=(await a(this,r,l).call(this,o.query(t),e)).messages}finally{await a(this,r,l).call(this,o.sync(),e)}this._cleanupBlob(),w(this,u)||await this.syncToFs();let i=await this._getWrittenBlob();return f(s,this.parsers,e,i)})},R=function(...t){this.debug>0&&console.log(...t)};export{z as a}; //# sourceMappingURL=chunk-TGYMLQND.js.map