class a{db;opts;txDepth=0;constructor(t={}){this.opts={filename:t.filename??"wkms.sqlite3",mode:t.mode??"auto"}}async connect(){if(this.db)return;const s=(await import("@sqlite.org/sqlite-wasm")).default,e=await s();if(this.resolveMode()==="opfs"){if(!e.oo1.OpfsDb)throw new Error("SqliteWasmStore: requested OPFS mode but sqlite3.oo1.OpfsDb is unavailable");this.db=new e.oo1.OpfsDb(this.opts.filename,"ct")}else this.db=new e.oo1.DB(":memory:","ct")}async close(){this.db?.close(),this.db=void 0}async exec(t){this.requireDb().exec({sql:t})}async run(t,s=[]){const e=this.requireDb();e.exec({sql:t,bind:s});const o=e.changes(),i=e.lastInsertRowid?.();return{changes:o,lastInsertRowid:i}}async all(t,s=[]){return this.requireDb().exec({sql:t,bind:s,rowMode:"object",returnValue:"resultRows"})??[]}async get(t,s=[]){return(await this.all(t,s))[0]}async transaction(t){const s=this.txDepth===0,e=`wkms_tx_${this.txDepth}`;this.txDepth++;try{s?await this.exec("BEGIN"):await this.exec(`SAVEPOINT ${e}`);const o=await t(this);return s?await this.exec("COMMIT"):await this.exec(`RELEASE SAVEPOINT ${e}`),this.txDepth--,o}catch(o){try{s?await this.exec("ROLLBACK"):await this.exec(`ROLLBACK TO SAVEPOINT ${e}`)}catch{}throw this.txDepth--,o}}requireDb(){if(!this.db)throw new Error("SqliteWasmStore: connect() was not called");return this.db}resolveMode(){return this.opts.mode!=="auto"?this.opts.mode:typeof navigator<"u"&&typeof navigator.storage?.getDirectory=="function"?"opfs":"memory"}}export{a as SqliteWasmStore};
//# sourceMappingURL=sqlite_wasm-BEyr2sGl.js.map
