sqlite-worker
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -53,4 +53,8 @@ 'use strict'; | ||
const uint8array = db.export(); | ||
assign(store('readwrite').put(uint8array, keyPath), { | ||
assign(store('readwrite').put(uint8array, keyPath).transaction, { | ||
onsuccess() { | ||
console.log('SUCCESS'); | ||
}, | ||
oncomplete() { | ||
console.log('COMPLETE'); | ||
resolve(); | ||
@@ -60,2 +64,3 @@ if (options.update) | ||
}, | ||
onabort: onerror, | ||
onerror | ||
@@ -62,0 +67,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
const e=new WeakMap,t=(t,...n)=>{const{t:o,v:s}=((e,t)=>{const n=[e[0]],o=[];for(let s=0,l=0,a=0,{length:c}=t;l<c;l++)t[l]instanceof r?n[s]+=t[l].v+e[l+1]:(o[a++]=l,n[++s]=e[l+1]);return{t:n,v:o}})(t,n),l=e.get(t)||e.set(t,{}).get(t);return(l[o]||(l[o]=[o])).concat(s.map((e=>n[e])))};function r(e){this.v=e}const n=(e,r)=>(n,...s)=>new Promise(((a,c)=>{n.some(l)&&c(o(new Error("SQLITE_ERROR: SQL injection hazard")));const[i,...u]=t(n,...s);e[r](i.join("?"),u,((e,t)=>{e?c(e):a(t)}))})),o=e=>(e.code="SQLITE_ERROR",e),s=(e,...t)=>new r(function(e){for(var t=e[0],r=1,n=arguments.length;r<n;r++)t+=arguments[r]+e[r];return t}(e,...t)),l=e=>e.includes("?");function a(e){return{all:n(e,"all"),get:n(e,"get"),query:n(e,"run"),raw:s}}const{assign:c}=Object,i=(e,t=1)=>new Promise(((r,n)=>{c(indexedDB.open(e,t),{onupgradeneeded({target:{result:e,transaction:t}}){e.objectStoreNames.contains("sqlite")||e.createObjectStore("sqlite").createIndex("buffer","buffer",{unique:!0}),c(t,{oncomplete(){r(e)}})},onsuccess({target:{result:e}}){r(e)},onerror:n})})),u=(e={})=>new Promise(((t,r)=>{const{url:n}=import.meta,o=e.dir||n.slice(0,n.lastIndexOf("/"))+"/../dist";self.exports={},self.module={exports:exports},import(o+"/sql-wasm.js").then((()=>{const n=self.module.exports;delete self.exports,Promise.all([i(e.name||"sqlite-worker"),n({locateFile:e=>o+"/"+e})]).then((([n,{Database:o}])=>{const s=e=>n.transaction(["sqlite"],e).objectStore("sqlite");c(s("readonly").get("buffer"),{onsuccess(){let r=Promise.resolve();const{result:n}=this,l=new o(n||e.database||new Uint8Array(0)),i=()=>{r=r.then((()=>new Promise(((t,r)=>{const n=l.export();c(s("readwrite").put(n,"buffer"),{onsuccess(){t(),e.update&&e.update(n)},onerror:r})}))))};n||i();const{all:u,get:m,query:d,raw:p}=a({all(e,t,r){try{const n=l.exec(e,t),o=[];n.forEach(f,o),r(null,o)}catch(e){r(e)}},get(e,t,r){try{const n=l.exec(e+" LIMIT 1",t),o=[];n.forEach(f,o),r(null,o.shift()||null)}catch(e){r(e)}},run(e,t,r){try{r(null,l.run(e,t))}catch(e){r(e)}}});let g=0;t({all:u,get:m,raw:p,query(t){return/\b(?:INSERT|DELETE|UPDATE)\b/i.test(t[0])&&(clearTimeout(g),g=setTimeout(i,e.timeout||250)),d.apply(this,arguments)}})},onerror:r})}),r)}))}));function f({columns:e,values:t}){for(let{length:r}=t,n=0;n<r;n++){const r=t[n],o={};for(let{length:t}=e,n=0;n<t;n++)o[e[n]]=r[n];this.push(o)}}const{assign:m}=Object,d=new Map;let p=0;function g(e){const t=import.meta.url,r=e=>(t,...r)=>n(e,{template:t,values:r}),n=(e,t)=>new Promise(((r,n)=>{const s=p++;d.set(s,{resolve:r,reject:n}),o.postMessage({id:s,action:e,options:t})})),o=m(new Worker(e.worker||t.slice(0,t.lastIndexOf("/"))+"/worker.js",{type:"module"}),{onmessage({data:{id:e,result:t,error:r}}){const{resolve:n,reject:o}=d.get(e);d.delete(e),r?o(r):n(t)}});return n("init",m({library:t},e)).then((()=>({all:r("all"),get:r("get"),query:r("query")})))}export{g as SQLiteWorker,u as init}; | ||
const e=new WeakMap,t=(t,...o)=>{const{t:n,v:s}=((e,t)=>{const o=[e[0]],n=[];for(let s=0,l=0,a=0,{length:c}=t;l<c;l++)t[l]instanceof r?o[s]+=t[l].v+e[l+1]:(n[a++]=l,o[++s]=e[l+1]);return{t:o,v:n}})(t,o),l=e.get(t)||e.set(t,{}).get(t);return(l[n]||(l[n]=[n])).concat(s.map((e=>o[e])))};function r(e){this.v=e}const o=(e,r)=>(o,...s)=>new Promise(((a,c)=>{o.some(l)&&c(n(new Error("SQLITE_ERROR: SQL injection hazard")));const[i,...u]=t(o,...s);e[r](i.join("?"),u,((e,t)=>{e?c(e):a(t)}))})),n=e=>(e.code="SQLITE_ERROR",e),s=(e,...t)=>new r(function(e){for(var t=e[0],r=1,o=arguments.length;r<o;r++)t+=arguments[r]+e[r];return t}(e,...t)),l=e=>e.includes("?");function a(e){return{all:o(e,"all"),get:o(e,"get"),query:o(e,"run"),raw:s}}const{assign:c}=Object,i=(e,t=1)=>new Promise(((r,o)=>{c(indexedDB.open(e,t),{onupgradeneeded({target:{result:e,transaction:t}}){e.objectStoreNames.contains("sqlite")||e.createObjectStore("sqlite").createIndex("buffer","buffer",{unique:!0}),c(t,{oncomplete(){r(e)}})},onsuccess({target:{result:e}}){r(e)},onerror:o})})),u=(e={})=>new Promise(((t,r)=>{const{url:o}=import.meta,n=e.dir||o.slice(0,o.lastIndexOf("/"))+"/../dist";self.exports={},self.module={exports:exports},import(n+"/sql-wasm.js").then((()=>{const o=self.module.exports;delete self.exports,Promise.all([i(e.name||"sqlite-worker"),o({locateFile:e=>n+"/"+e})]).then((([o,{Database:n}])=>{const s=e=>o.transaction(["sqlite"],e).objectStore("sqlite");c(s("readonly").get("buffer"),{onsuccess(){let r=Promise.resolve();const{result:o}=this,l=new n(o||e.database||new Uint8Array(0)),i=()=>{r=r.then((()=>new Promise(((t,r)=>{const o=l.export();c(s("readwrite").put(o,"buffer").transaction,{onsuccess(){console.log("SUCCESS")},oncomplete(){console.log("COMPLETE"),t(),e.update&&e.update(o)},onabort:r,onerror:r})}))))};o||i();const{all:u,get:f,query:p,raw:d}=a({all(e,t,r){try{const o=l.exec(e,t),n=[];o.forEach(m,n),r(null,n)}catch(e){r(e)}},get(e,t,r){try{const o=l.exec(e+" LIMIT 1",t),n=[];o.forEach(m,n),r(null,n.shift()||null)}catch(e){r(e)}},run(e,t,r){try{r(null,l.run(e,t))}catch(e){r(e)}}});let g=0;t({all:u,get:f,raw:d,query(t){return/\b(?:INSERT|DELETE|UPDATE)\b/i.test(t[0])&&(clearTimeout(g),g=setTimeout(i,e.timeout||250)),p.apply(this,arguments)}})},onerror:r})}),r)}))}));function m({columns:e,values:t}){for(let{length:r}=t,o=0;o<r;o++){const r=t[o],n={};for(let{length:t}=e,o=0;o<t;o++)n[e[o]]=r[o];this.push(n)}}const{assign:f}=Object,p=new Map;let d=0;function g(e){const t=import.meta.url,r=e=>(t,...r)=>o(e,{template:t,values:r}),o=(e,t)=>new Promise(((r,o)=>{const s=d++;p.set(s,{resolve:r,reject:o}),n.postMessage({id:s,action:e,options:t})})),n=f(new Worker(e.worker||t.slice(0,t.lastIndexOf("/"))+"/worker.js",{type:"module"}),{onmessage({data:{id:e,result:t,error:r}}){const{resolve:o,reject:n}=p.get(e);p.delete(e),r?n(r):o(t)}});return o("init",f({library:t},e)).then((()=>({all:r("all"),get:r("get"),query:r("query")})))}export{g as SQLiteWorker,u as init}; |
@@ -52,4 +52,4 @@ import SQLiteTag from 'sqlite-tag'; | ||
const uint8array = db.export(); | ||
assign(store('readwrite').put(uint8array, keyPath), { | ||
onsuccess() { | ||
assign(store('readwrite').put(uint8array, keyPath).transaction, { | ||
oncomplete() { | ||
resolve(); | ||
@@ -59,2 +59,3 @@ if (options.update) | ||
}, | ||
onabort: onerror, | ||
onerror | ||
@@ -61,0 +62,0 @@ }); |
{ | ||
"name": "sqlite-worker", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "A simple, and persistent, SQLite database for Web and Workers", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
1216107
683