-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtables.js
More file actions
1 lines (1 loc) · 5.35 KB
/
Copy pathtables.js
File metadata and controls
1 lines (1 loc) · 5.35 KB
1
const e=["name","type","notnull","dflt_value","pk"],{keys:t}=Object,n=(e,n,a)=>new Promise(((i,u)=>{const p=r(n),m=t(a);e.all(`PRAGMA table_info('${p}')`,((t,n)=>{const{length:h}=n,f=m.map(c,a),d=`tmp_${p}_backup`,E=`CREATE TABLE IF NOT EXISTS '${h?d:p}' (${o(f)})`;h?e.run(E,(t=>{t?u(t):e.all(`PRAGMA table_info('${d}')`,((t,c)=>{if(n.length!==c.length||n.some(l,c)){const t=n.filter(s,c).map((e=>`'${r(e.name)}'`)),l=t.map((e=>`'${p}'.${e}`));e.run(`INSERT INTO '${d}' (${o(t)})\n SELECT ${o(l)} FROM '${p}'`,(t=>{t?e.run(`DROP TABLE IF EXISTS '${d}'`,(()=>u(t))):e.run(`DROP TABLE IF EXISTS '${p}'`,(()=>{e.run(`ALTER TABLE '${d}' RENAME TO '${p}'`,i)}))}))}else e.run(`DROP TABLE IF EXISTS '${d}'`,i)}))})):e.run(E,(e=>{e?u(e):i()}))}))})),r=e=>e.replace(/'/g,"''"),o=e=>e.join(", ");function s(e){return-1<this.findIndex((({name:t})=>t===e.name))}function c(e){return`'${r(e)}' ${this[e]}`}function l(t,n){const r=this[n];return e.some((e=>t[e]!==r[e]))}const a=(e,r)=>e(r).then((e=>r.tables?((e,r)=>new Promise(((o,s)=>{const c=[];for(const o of t(r))c.push(n(e,o,r[o]));Promise.all(c).then(o,s)})))((({all:e,query:t})=>({all(t,n){e([t]).then((e=>n(null,e)),n)},run(e,n){t([e]).then((()=>n()),n)}}))(e),r.tables).then((()=>e)):e));function i(e){for(var t=e[0],n=1,r=arguments.length;n<r;n++)t+=arguments[n]+e[n];return t}const{isArray:u}=Array;class p extends String{}const{defineProperty:m}=Object,h=(e,t)=>(n,...r)=>new Promise(((o,s)=>{if(n.some((e=>e.includes("?")))){const e=new Error("SQLITE_ERROR: SQL injection hazard");e.code="SQLITE_ERROR",s(e)}else{const[c,...l]=((e,...t)=>{const n=[e[0]],r=[n];for(let o=0;o<t.length;o++)t[o]instanceof p?n[n.length-1]+=t[o]+e[o+1]:(u(t[o])?(n.push(...t[o].slice(1).map((e=>","))),r.push(...t[o])):r.push(t[o]),n.push(e[o+1]));return r})(n,...r);e[t](c.join("?"),l,((e,t)=>{e?s(e):o(t)}))}}));function f(e){const t=h(e,"run");return{transaction(){let e=t(["BEGIN TRANSACTION"]);return m(((...n)=>{e=e.then((()=>t(...n)))}),"commit",{value:()=>e=e.then((()=>t(["COMMIT"])))})},all:h(e,"all"),get:h(e,"get"),raw:(e,...t)=>{return n=i(e,...t),new p(n);var n},close:()=>e.close(),query:t}}const{assign:d}=Object,E="function"==typeof importScripts,T=E?".":import.meta.url.replace(/\/[^/]*$/,""),g=e=>new Promise(((t,n)=>{const r=()=>{const e=self.module.exports;delete self.exports,self.module=void 0,t(e)};if(self.exports={},self.module={exports:exports},E)importScripts(e),r();else{const{head:t}=document;d(t.appendChild(document.createElement("script")),{onload(){t.removeChild(this),r()},onerror:n,src:e})}})),w="sqlite",S="buffer",I=(e,t=1)=>new Promise(((n,r)=>{d(indexedDB.open(e,t),{onupgradeneeded({target:{result:e,transaction:t}}){e.objectStoreNames.contains(w)||e.createObjectStore(w).createIndex(S,S,{unique:!0}),d(t,{oncomplete(){n(e)}})},onsuccess({target:{result:e}}){n(e)},onerror:r})})),b=(e={})=>new Promise(((t,n)=>{const r=e.dist||T;g(r+"/sql-wasm.js").then((({default:o})=>{Promise.all([I(e.name||"sqlite-worker"),o({locateFile:e=>r+"/"+e})]).then((([r,{Database:o}])=>{const s=e=>r.transaction([w],e).objectStore(w);d(s("readonly").get(S),{onsuccess(){let n=Promise.resolve();const{result:r}=this,c=new o(r||e.database||new Uint8Array(0)),l=()=>n=n.then((()=>new Promise(((t,n)=>{const r=c.export();d(s("readwrite").put(r,S).transaction,{oncomplete(){t(),e.update&&e.update(r)},onabort:n,onerror:n})}))));r||l();const{all:a,get:i,query:u,raw:p,transaction:m}=f({all(e,t,n){try{const r=c.exec(e,t),o=[];r.forEach(y,o),n(null,o)}catch(e){n(e)}},get(e,t,n){try{const r=c.exec(e+" LIMIT 1",t),o=[];r.forEach(y,o),n(null,o.shift()||null)}catch(e){n(e)}},run(e,t,n){try{n(null,c.run(e,t))}catch(e){n(e)}}});let h=0;t({all:a,get:i,raw:p,transaction:m,create_function:(e,t)=>c.create_function(e,t),close:()=>(clearTimeout(h),l().then((()=>c.close()))),query(t){return/\b(?:INSERT|DELETE|UPDATE)\b/i.test(t[0])&&(clearTimeout(h),h=setTimeout(l,e.timeout||250)),u.apply(this,arguments)}})},onerror:n})}),n)}))}));function y({columns:e,values:t}){for(let{length:n}=t,r=0;r<n;r++){const n=t[r],o={};for(let{length:t}=e,r=0;r<t;r++)o[e[r]]=n[r];this.push(o)}}const R=(e={})=>a(b,e),$=new WeakMap,A=(e,...t)=>{const{t:n,v:r}=O(e,t),o=$.get(e)||$.set(e,{}).get(e);return(o[n]||(o[n]=[n])).concat(r.map((e=>t[e])))},O=(e,t)=>{const n=[e[0]],r=[];for(let o=0,s=0,c=0,{length:l}=t;s<l;s++)t[s]instanceof P?n[o]+=t[s].v+e[s+1]:(r[c++]=s,n[++o]=e[s+1]);return{t:n,v:r}};function P(e){this.v=e}const{defineProperty:j}=Object,v=new Map,x=(e,...t)=>new P(i(e,...t)),L=e=>URL.createObjectURL(new Blob([`importScripts('${e}')`],{type:"text/javascript"}));let M=0;function N(e={}){const{credentials:t}=e,n=e.dist||T,r=e.worker||n+"/worker.js",o=e=>(t,...n)=>{const[r,...o]=A(t,...n);return s(e,{template:r,values:o})},s=(e,t)=>new Promise(((n,r)=>{const o=M++;v.set(o,{resolve:n,reject:r}),c.postMessage({id:o,action:e,options:t})})),c=d(new Worker(/^(?:\.|\/)/.test(r)?r:L(r),{credentials:t}),{onmessage({data:{id:e,result:t,error:n}}){const{resolve:r,reject:o}=v.get(e);v.delete(e),n?o(n):r(t)}}),l=o("query");return s("init",d({dist:n,library:n+"/init.js"},e)).then((()=>({transaction(){let e=l(["BEGIN TRANSACTION"]);return j(((...t)=>{e=e.then((()=>l(...t)))}),"commit",{value:()=>e=e.then((()=>l(["COMMIT"])))})},close:()=>s("close"),create_function:(e,t)=>s("create_function",[e,t.toString()]),all:o("all"),get:o("get"),query:l,raw:x})))}function _(e={}){return a(N,e)}export{_ as SQLiteWorker,R as init};