@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 • 4.23 kB
JavaScript
import{a as v,b as A,c as q,d as z,f as _,g as B,i as l,j as P}from"./chunk-M6G2OE44.js";import{d as R}from"./chunk-STOZMFXW.js";import{e as T,f,g as b,h as a,j as w}from"./chunk-BTBUZ646.js";w();w();function E(h){let s=h.e;return s.query=h.query,s.params=h.params,s.queryOptions=h.options,s}var d,p,t,y,x,m,O,F=class{constructor(){f(this,t);this.serializers={...A};this.parsers={...v};f(this,d,!1);f(this,p,!1)}async _initArrayTypes({force:s=!1}={}){if(T(this,d)&&!s)return;b(this,d,!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 r of e.rows)this.serializers[r.typarray]=n=>q(n,this.serializers[r.oid],r.typarray),this.parsers[r.typarray]=n=>z(n,this.parsers[r.oid],r.typarray)}async refreshArrayTypes(){await this._initArrayTypes({force:!0})}async query(s,e,r){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await a(this,t,x).call(this,s,e,r))}async sql(s,...e){let{query:r,params:n}=R(s,...e);return await this.query(r,n)}async exec(s,e){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await a(this,t,m).call(this,s,e))}async describeQuery(s,e){try{await a(this,t,y).call(this,l.parse({text:s,types:e?.paramTypes}),e);let r=await a(this,t,y).call(this,l.describe({type:"S"}),e),n=r.messages.find(c=>c.name==="parameterDescription"),i=r.messages.find(c=>c.name==="rowDescription"),o=n?.dataTypeIDs.map(c=>({dataTypeID:c,serializer:this.serializers[c]}))??[],u=i?.fields.map(c=>({name:c.name,dataTypeID:c.dataTypeID,parser:this.parsers[c.dataTypeID]}))??[];return{queryParams:o,resultFields:u}}catch(r){throw r instanceof P?E({e:r,options:e,params:void 0,query:s}):r}finally{await a(this,t,y).call(this,l.sync(),e)}}async transaction(s){return await this._checkReady(),await this._runExclusiveTransaction(async()=>{await a(this,t,m).call(this,"BEGIN"),b(this,p,!0);let e=!1,r=()=>{if(e)throw new Error("Transaction is closed")},n={query:async(i,o,u)=>(r(),await a(this,t,x).call(this,i,o,u)),sql:async(i,...o)=>{let{query:u,params:c}=R(i,...o);return await a(this,t,x).call(this,u,c)},exec:async(i,o)=>(r(),await a(this,t,m).call(this,i,o)),rollback:async()=>{r(),await a(this,t,m).call(this,"ROLLBACK"),e=!0},listen:async(i,o)=>(r(),await this.listen(i,o,n)),get closed(){return e}};try{let i=await s(n);return e||(e=!0,await a(this,t,m).call(this,"COMMIT")),b(this,p,!1),i}catch(i){throw e||await a(this,t,m).call(this,"ROLLBACK"),b(this,p,!1),i}})}async runExclusive(s){return await this._runExclusiveQuery(s)}};d=new WeakMap,p=new WeakMap,t=new WeakSet,y=async function(s,e={}){return await this.execProtocol(s,{...e,syncToFs:!1})},x=async function(s,e=[],r){return await this._runExclusiveQuery(async()=>{a(this,t,O).call(this,"runQuery",s,e,r),await this._handleBlob(r?.blob);let n;try{let{messages:o}=await a(this,t,y).call(this,l.parse({text:s,types:r?.paramTypes}),r),u=B((await a(this,t,y).call(this,l.describe({type:"S"}),r)).messages),c=e.map((g,S)=>{let D=u[S];if(g==null)return null;let Q=r?.serializers?.[D]??this.serializers[D];return Q?Q(g):g.toString()});n=[...o,...(await a(this,t,y).call(this,l.bind({values:c}),r)).messages,...(await a(this,t,y).call(this,l.describe({type:"P"}),r)).messages,...(await a(this,t,y).call(this,l.execute({}),r)).messages]}catch(o){throw o instanceof P?E({e:o,options:r,params:e,query:s}):o}finally{await a(this,t,y).call(this,l.sync(),r)}await this._cleanupBlob(),T(this,p)||await this.syncToFs();let i=await this._getWrittenBlob();return _(n,this.parsers,r,i)[0]})},m=async function(s,e){return await this._runExclusiveQuery(async()=>{a(this,t,O).call(this,"runExec",s,e),await this._handleBlob(e?.blob);let r;try{r=(await a(this,t,y).call(this,l.query(s),e)).messages}catch(i){throw i instanceof P?E({e:i,options:e,params:void 0,query:s}):i}finally{await a(this,t,y).call(this,l.sync(),e)}this._cleanupBlob(),T(this,p)||await this.syncToFs();let n=await this._getWrittenBlob();return _(r,this.parsers,e,n)})},O=function(...s){this.debug>0&&console.log(...s)};export{F as a};
//# sourceMappingURL=chunk-HWTMPVRX.js.map