tinybase 4.4.2 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import{addDatabaseChangeListener as e}from"expo-sqlite/next";const n=e=>typeof e,l="tinybase",t=",",o=n(""),r=(e,n)=>e.repeat(n),i=Promise,u=setInterval,d=clearInterval,a=e=>null==e,c=(e,n,l)=>a(e)?null==l?void 0:l():n(e),s=e=>n(e)==o,v=(e,n,l)=>e.slice(n,l),y=e=>e.length,f=e=>{return n=function*(){return i.all(e)},new Promise(((e,l)=>{var t=e=>{try{r(n.next(e))}catch(e){l(e)}},o=e=>{try{r(n.throw(e))}catch(e){l(e)}},r=n=>n.done?e(n.value):Promise.resolve(n.value).then(t,o);r((n=n.apply(void 0,null)).next())}));var n},p=(e,n="")=>e.join(n),E=(e,n)=>e.map(n),h=e=>0==y(e),m=(e,n)=>e.filter(n),T=(e,...n)=>e.push(...n),A=(e,n)=>{var l;return null!=(l=null==e?void 0:e.has(n))&&l},O=e=>{var n;return[...null!=(n=null==e?void 0:e.values())?n:[]]},L=(e,n)=>null==e?void 0:e.delete(n),P=Object,b=e=>P.getPrototypeOf(e),w=P.keys,N=P.freeze,R=(e=[])=>P.fromEntries(e),x=(...e)=>P.assign({},...e),S=(e,n)=>E(P.entries(e),(([e,l])=>n(l,e))),g=e=>P.values(e),C=e=>y(w(e)),D=e=>(e=>!a(e)&&c(b(e),(e=>e==P.prototype||a(b(e))),(()=>!0)))(e)&&0==C(e),I=e=>new Map(e),F=e=>{var n;return[...null!=(n=null==e?void 0:e.keys())?n:[]]},M=(e,n)=>null==e?void 0:e.get(n),$=(e,n)=>{var l;return E([...null!=(l=null==e?void 0:e.entries())?l:[]],(([e,l])=>n(l,e)))},j=(e,n,l)=>a(l)?(L(e,n),e):null==e?void 0:e.set(n,l),_=(e,n,l)=>(A(e,n)||j(e,n,l()),M(e,n)),B=(e,n,l,t=j)=>(S(n,((n,t)=>l(e,t,n))),((e,n)=>{((e,n)=>{null==e||e.forEach(n)})(e,((e,l)=>n(l)))})(e,(l=>((e,n)=>!a(((e,n)=>c(e,(e=>e[n])))(e,n)))(n,l)?0:t(e,l))),e),H=e=>new Set(Array.isArray(e)||a(e)?e:[e]),W=(e,n)=>null==e?void 0:e.add(n),k="_",J="_id",U=e=>`"${e.replace(/"/g,'""')}"`;var Y=Object.defineProperty,q=Object.getOwnPropertySymbols,z=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable,K=(e,n,l)=>n in e?Y(e,n,{enumerable:!0,configurable:!0,writable:!0,value:l}):e[n]=l,V=(e,n)=>{for(var l in n||(n={}))z.call(n,l)&&K(e,l,n[l]);if(q)for(var l of q(n))G.call(n,l)&&K(e,l,n[l]);return e},Q=(e,n,l)=>new Promise(((t,o)=>{var r=e=>{try{u(l.next(e))}catch(e){o(e)}},i=e=>{try{u(l.throw(e))}catch(e){o(e)}},u=e=>e.done?t(e.value):Promise.resolve(e.value).then(r,i);u((l=l.apply(e,n)).next())}));const X="FROM pragma_table_",Z="WHERE",ee=(e,n,l)=>{const o=I();return[()=>Q(void 0,null,(function*(){return B(o,R(yield f(E(yield e("SELECT name "+X+"list WHERE schema='main'AND type='table'AND name IN("+le(n)+")",n),(n=>Q(void 0,[n],(function*({name:n}){return[n,R(E(yield e("SELECT name,type "+X+"info(?)",[n]),(({name:e,type:n})=>[e,n])))]})))))),((e,n,l)=>j(o,n,B(_(o,n,I),l,((e,n,l)=>{l!=M(e,n)&&j(e,n,l)}),((e,n)=>j(e,n))))),((e,n)=>j(o,n)))})),(n,l)=>Q(void 0,null,(function*(){return((e,n)=>!a(M(M(o,e),n)))(n,l)?R(m(E(yield e("SELECT*FROM"+U(n)),(e=>{return[e[l],(n=V({},e),t=l,delete n[t],n)];var n,t})),(([e,n])=>!a(e)&&!D(n)))):{}})),(n,l,r,i,u,d=!1)=>Q(void 0,null,(function*(){const c=H();S(null!=r?r:{},(e=>E(w(null!=e?e:{}),(e=>W(c,e)))));const s=O(c);if(!d&&u&&h(s)&&A(o,n))return yield e("DROP TABLE"+U(n)),void j(o,n);if(h(s)||A(o,n)){const t=M(o,n),r=H(F(t));yield f([...E(s,(l=>Q(void 0,null,(function*(){L(r,l)||(yield e(`ALTER TABLE${U(n)}ADD${U(l)}`),j(t,l,""))})))),...!d&&i?E(O(r),(o=>Q(void 0,null,(function*(){o!=l&&(yield e(`ALTER TABLE${U(n)}DROP${U(o)}`),j(t,o))})))):[]])}else yield e(`CREATE TABLE${U(n)}(${U(l)} PRIMARY KEY ON CONFLICT REPLACE${p(E(s,(e=>t+U(e))))});`),j(o,n,I([[l,""],...E(s,(e=>[e,""]))]));if(d)a(r)?yield e("DELETE FROM"+U(n)+"WHERE 1"):yield f(S(r,((t,o)=>Q(void 0,null,(function*(){a(t)?yield e("DELETE FROM"+U(n)+Z+U(l)+"=?",[o]):h(s)||(yield ne(e,n,l,w(t),[o,...g(t)]))})))));else if(h(s))A(o,n)&&(yield e("DELETE FROM"+U(n)+"WHERE 1"));else{const t=m(F(M(o,n)),(e=>e!=l)),i=[],u=[];S(null!=r?r:{},((e,n)=>{T(i,n,...E(t,(n=>null==e?void 0:e[n]))),T(u,n)})),yield ne(e,n,l,t,i),yield e("DELETE FROM"+U(n)+Z+U(l)+"NOT IN("+le(u)+")",u)}})),n=>Q(void 0,null,(function*(){let t;yield e("BEGIN");try{t=yield n()}catch(e){null==l||l(e)}return yield e("END"),t}))]},ne=(e,n,l,o,i)=>Q(void 0,null,(function*(){return yield e("INSERT INTO"+U(n)+"("+U(l)+p(E(o,(e=>t+U(e))))+")VALUES"+v(r(`,(?${r(",?",y(o))})`,y(i)/(y(o)+1)),1)+"ON CONFLICT("+U(l)+")DO UPDATE SET"+p(E(o,(e=>U(e)+"=excluded."+U(e))),t),i)})),le=e=>p(E(e,(()=>"?")),t),te=JSON.parse;var oe=(e,n,l)=>new Promise(((t,o)=>{var r=e=>{try{u(l.next(e))}catch(e){o(e)}},i=e=>{try{u(l.throw(e))}catch(e){o(e)}},u=e=>e.done?t(e.value):Promise.resolve(e.value).then(r,i);u((l=l.apply(e,n)).next())}));const re=I(),ie=I(),ue=(e,n,l,t,o,r,[i,u]=[],d=[])=>{let s,v,y,f=0,p=0;_(re,d,(()=>0)),_(ie,d,(()=>[]));const E=e=>oe(void 0,null,(function*(){return 2!=f&&(f=1,yield h.schedule((()=>oe(void 0,null,(function*(){yield e(),f=0}))))),h})),h={load:(l,t)=>oe(void 0,null,(function*(){return yield E((()=>oe(void 0,null,(function*(){try{e.setContent(yield n())}catch(n){e.setContent([l,t])}}))))})),startAutoLoad:(...l)=>oe(void 0,[...l],(function*(l={},o={}){return h.stopAutoLoad(),yield h.load(l,o),p=1,y=t(((l,t)=>oe(void 0,null,(function*(){if(t){const n=t();yield E((()=>oe(void 0,null,(function*(){return e.setTransactionChanges(n)}))))}else yield E((()=>oe(void 0,null,(function*(){var t;try{e.setContent(null!=(t=null==l?void 0:l())?t:yield n())}catch(e){null==r||r(e)}}))))})))),h})),stopAutoLoad:()=>(p&&(o(y),y=void 0,p=0),h),save:n=>oe(void 0,null,(function*(){return 1!=f&&(f=2,yield h.schedule((()=>oe(void 0,null,(function*(){try{yield l(e.getContent,n)}catch(e){null==r||r(e)}f=0}))))),h})),startAutoSave:()=>oe(void 0,null,(function*(){return yield h.stopAutoSave().save(),s=e.addDidFinishTransactionListener(((e,n)=>{const[l,t]=n();D(l)&&D(t)||h.save((()=>[l,t]))})),h})),stopAutoSave:()=>(c(s,e.delListener),s=void 0,h),schedule:(...e)=>oe(void 0,null,(function*(){return T(M(ie,d),...e),yield oe(void 0,null,(function*(){if(!M(re,d)){for(j(re,d,1);!a((e=M(ie,d),v=e.shift()));)try{yield v()}catch(e){null==r||r(e)}j(re,d,0)}var e})),h})),getStore:()=>e,destroy:()=>h.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return i&&(h[i]=()=>u),N(h)};var de=(e,n,l)=>new Promise(((t,o)=>{var r=e=>{try{u(l.next(e))}catch(e){o(e)}},i=e=>{try{u(l.throw(e))}catch(e){o(e)}},u=e=>e.done?t(e.value):Promise.resolve(e.value).then(r,i);u((l=l.apply(e,n)).next())}));const ae="store",ce=(e,n,l,t,o,[r],i,u)=>{const[d,a,c,s]=ee(n,i,o);return ue(e,(()=>de(void 0,null,(function*(){return yield s((()=>de(void 0,null,(function*(){var e,n;return yield d(),te(null!=(n=null==(e=(yield a(r,J))[k])?void 0:e[ae])?n:"null")}))))}))),(e=>de(void 0,null,(function*(){return yield s((()=>de(void 0,null,(function*(){var n,l;yield d(),yield c(r,J,{[k]:{[ae]:(l=null!=(n=e())?n:null,JSON.stringify(l,((e,n)=>n instanceof Map?P.fromEntries([...n]):n)))}},!0,!0)}))))}))),l,t,o,["getDb",u],u)};var se=(e,n,l)=>new Promise(((t,o)=>{var r=e=>{try{u(l.next(e))}catch(e){o(e)}},i=e=>{try{u(l.throw(e))}catch(e){o(e)}},u=e=>e.done?t(e.value):Promise.resolve(e.value).then(r,i);u((l=l.apply(e,n)).next())}));const ve=(e,n,l,t,o,[r,i,[u,d,c]],s,v)=>{const[y,p,E,h]=ee(n,s,o),T=(e,n)=>se(void 0,null,(function*(){return yield f($(i,((l,t)=>se(void 0,[l,t],(function*([l,t,o,r],i){const u=e[i];n&&void 0===u||(yield E(l,t,u,o,r,n))})))))})),A=(e,n)=>se(void 0,null,(function*(){return d?yield E(c,J,{[k]:e},!0,!0,n):null}));return ue(e,(()=>se(void 0,null,(function*(){return yield h((()=>se(void 0,null,(function*(){yield y();const e=yield se(void 0,null,(function*(){return R(m(yield f($(r,((e,n)=>se(void 0,[e,n],(function*([e,n],l){return[e,yield p(l,n)]}))))),(e=>!D(e[1]))))})),n=yield se(void 0,null,(function*(){return u?(yield p(c,J))[k]:{}}));return D(e)&&a(n)?void 0:[e,n]}))))}))),((e,n)=>se(void 0,null,(function*(){return yield h((()=>se(void 0,null,(function*(){if(yield y(),a(n)){const[n,l]=e();yield T(n),yield A(l)}else{const[e,l]=n();yield T(e,!0),yield A(l,!0)}}))))}))),l,t,o,["getDb",v],v)},ye="json",fe="autoLoadIntervalSeconds",pe="rowIdColumnName",Ee="tableId",he="tableName",me={mode:ye,[fe]:1},Te={load:0,save:0,[he]:l+"_values"},Ae=(e,n,l,t)=>{const o=I();return S(e,((e,r)=>{const i=v(g(x(n,s(e)?{[l]:e}:e)),0,C(n));a(i[0])||t(r,i[0])||j(o,r,i)})),o};var Oe=(e,n,l)=>new Promise(((t,o)=>{var r=e=>{try{u(l.next(e))}catch(e){o(e)}},i=e=>{try{u(l.throw(e))}catch(e){o(e)}},u=e=>e.done?t(e.value):Promise.resolve(e.value).then(r,i);u((l=l.apply(e,n)).next())}));const Le="pragma ",Pe="data_version",be="schema_version",we=(e,n,t,o,r,i,a,c)=>{let y,f;const[p,E,h,m]=(e=>{const n=(e=>x(me,s(e)?{storeTableName:e}:null!=e?e:{}))(e),t=n[fe];if(n.mode==ye){const{storeTableName:e=l}=n;return[1,t,[e],H(e)]}const{tables:{load:o={},save:r={}}={},values:i={}}=n,u=v(g(x(Te,i)),0,C(Te)),d=u[2],a=H(d);return[0,t,[Ae(o,{[Ee]:null,[pe]:J},Ee,(e=>W(a,e)&&e==d)),Ae(r,{[he]:null,[pe]:J,deleteEmptyColumns:0,deleteEmptyTable:0},he,((e,n)=>W(a,n)&&n==d)),u],a]})(n);return(p?ce:ve)(e,i?(e,n)=>Oe(void 0,null,(function*(){return i(e,n),yield t(e,n)})):t,(e=>[u((()=>Oe(void 0,null,(function*(){try{const n=(yield t(Le+Pe))[0][Pe],l=(yield t(Le+be))[0][be];n==(null!=y?y:y=n)&&l==(null!=f?f:f=l)||(e(),y=n,f=l)}catch(e){}}))),1e3*E),o((n=>m.has(n)?e():0))]),(([e,n])=>{d(e),y=f=null,r(n)}),a,h,O(m),c)},Ne=(n,l,t,o,r)=>we(n,t,((e,...n)=>{return t=[e,...n],o=function*(e,n=[]){return yield l.allAsync(e,n)},new Promise(((e,n)=>{var l=e=>{try{i(o.next(e))}catch(e){n(e)}},r=e=>{try{i(o.throw(e))}catch(e){n(e)}},i=n=>n.done?e(n.value):Promise.resolve(n.value).then(l,r);i((o=o.apply(void 0,t)).next())}));var t,o}),(n=>e((({tableName:e})=>n(e)))),(e=>e.remove()),o,r,l);export{Ne as createExpoSqliteNextPersister};
@@ -0,0 +1 @@
1
+ import{addDatabaseChangeListener as a}from"expo-sqlite/next";const t=a=>typeof a,e="tinybase",s=",",n=t(""),i=(a,t)=>a.repeat(t),c=Promise,o=setInterval,r=clearInterval,l=a=>null==a,w=(a,t,e)=>l(a)?e?.():t(a),y=a=>t(a)==n,E=(a,t,e)=>a.slice(t,e),u=a=>a.length,d=async a=>c.all(a),m=(a,t="")=>a.join(t),p=(a,t)=>a.map(t),v=a=>0==u(a),T=(a,t)=>a.filter(t),A=(a,...t)=>a.push(...t),L=(a,t)=>a?.has(t)??!1,f=a=>[...a?.values()??[]],N=(a,t)=>a?.delete(t),h=Object,O=a=>h.getPrototypeOf(a),R=h.keys,C=h.freeze,D=(a=[])=>h.fromEntries(a),S=(...a)=>h.assign({},...a),g=(a,t)=>p(h.entries(a),(([a,e])=>t(e,a))),b=a=>h.values(a),I=a=>u(R(a)),F=a=>(a=>!l(a)&&w(O(a),(a=>a==h.prototype||l(O(a))),(()=>!0)))(a)&&0==I(a),M=a=>new Map(a),$=a=>[...a?.keys()??[]],P=(a,t)=>a?.get(t),_=(a,t)=>p([...a?.entries()??[]],(([a,e])=>t(e,a))),B=(a,t,e)=>l(e)?(N(a,t),a):a?.set(t,e),x=(a,t,e)=>(L(a,t)||B(a,t,e()),P(a,t)),H=(a,t,e,s=B)=>(g(t,((t,s)=>e(a,s,t))),((a,t)=>{((a,t)=>{a?.forEach(t)})(a,((a,e)=>t(e)))})(a,(e=>((a,t)=>!l(((a,t)=>w(a,(a=>a[t])))(a,t)))(t,e)?0:s(a,e))),a),W=a=>new Set(Array.isArray(a)||l(a)?a:[a]),j=(a,t)=>a?.add(t),k="_",J="_id",U=a=>`"${a.replace(/"/g,'""')}"`,Y="FROM pragma_table_",q="WHERE",z=(a,t,e)=>{const n=M();return[async()=>H(n,D(await d(p(await a("SELECT name "+Y+"list WHERE schema='main'AND type='table'AND name IN("+K(t)+")",t),(async({name:t})=>[t,D(p(await a("SELECT name,type "+Y+"info(?)",[t]),(({name:a,type:t})=>[a,t])))])))),((a,t,e)=>B(n,t,H(x(n,t,M),e,((a,t,e)=>{e!=P(a,t)&&B(a,t,e)}),((a,t)=>B(a,t))))),((a,t)=>B(n,t))),async(t,e)=>((a,t)=>!l(P(P(n,a),t)))(t,e)?D(T(p(await a("SELECT*FROM"+U(t)),(a=>{return[a[e],(t={...a},s=e,delete t[s],t)];var t,s})),(([a,t])=>!l(a)&&!F(t)))):{},async(t,e,i,c,o,r=!1)=>{const w=W();g(i??{},(a=>p(R(a??{}),(a=>j(w,a)))));const y=f(w);if(!r&&o&&v(y)&&L(n,t))return await a("DROP TABLE"+U(t)),void B(n,t);if(v(y)||L(n,t)){const s=P(n,t),i=W($(s));await d([...p(y,(async e=>{N(i,e)||(await a(`ALTER TABLE${U(t)}ADD${U(e)}`),B(s,e,""))})),...!r&&c?p(f(i),(async n=>{n!=e&&(await a(`ALTER TABLE${U(t)}DROP${U(n)}`),B(s,n))})):[]])}else await a(`CREATE TABLE${U(t)}(${U(e)} PRIMARY KEY ON CONFLICT REPLACE${m(p(y,(a=>s+U(a))))});`),B(n,t,M([[e,""],...p(y,(a=>[a,""]))]));if(r)l(i)?await a("DELETE FROM"+U(t)+"WHERE 1"):await d(g(i,(async(s,n)=>{l(s)?await a("DELETE FROM"+U(t)+q+U(e)+"=?",[n]):v(y)||await G(a,t,e,R(s),[n,...b(s)])})));else if(v(y))L(n,t)&&await a("DELETE FROM"+U(t)+"WHERE 1");else{const s=T($(P(n,t)),(a=>a!=e)),c=[],o=[];g(i??{},((a,t)=>{A(c,t,...p(s,(t=>a?.[t]))),A(o,t)})),await G(a,t,e,s,c),await a("DELETE FROM"+U(t)+q+U(e)+"NOT IN("+K(o)+")",o)}},async t=>{let s;await a("BEGIN");try{s=await t()}catch(a){e?.(a)}return await a("END"),s}]},G=async(a,t,e,n,c)=>await a("INSERT INTO"+U(t)+"("+U(e)+m(p(n,(a=>s+U(a))))+")VALUES"+E(i(`,(?${i(",?",u(n))})`,u(c)/(u(n)+1)),1)+"ON CONFLICT("+U(e)+")DO UPDATE SET"+m(p(n,(a=>U(a)+"=excluded."+U(a))),s),c),K=a=>m(p(a,(()=>"?")),s),V=JSON.parse,Q=M(),X=M(),Z=(a,t,e,s,n,i,[c,o]=[],r=[])=>{let y,E,u,d=0,m=0;x(Q,r,(()=>0)),x(X,r,(()=>[]));const p=async a=>(2!=d&&(d=1,await v.schedule((async()=>{await a(),d=0}))),v),v={load:async(e,s)=>await p((async()=>{try{a.setContent(await t())}catch{a.setContent([e,s])}})),startAutoLoad:async(e={},n={})=>(v.stopAutoLoad(),await v.load(e,n),m=1,u=s((async(e,s)=>{if(s){const t=s();await p((async()=>a.setTransactionChanges(t)))}else await p((async()=>{try{a.setContent(e?.()??await t())}catch(a){i?.(a)}}))})),v),stopAutoLoad:()=>(m&&(n(u),u=void 0,m=0),v),save:async t=>(1!=d&&(d=2,await v.schedule((async()=>{try{await e(a.getContent,t)}catch(a){i?.(a)}d=0}))),v),startAutoSave:async()=>(await v.stopAutoSave().save(),y=a.addDidFinishTransactionListener(((a,t)=>{const[e,s]=t();F(e)&&F(s)||v.save((()=>[e,s]))})),v),stopAutoSave:()=>(w(y,a.delListener),y=void 0,v),schedule:async(...a)=>(A(P(X,r),...a),await(async()=>{if(!P(Q,r)){for(B(Q,r,1);!l((a=P(X,r),E=a.shift()));)try{await E()}catch(a){i?.(a)}B(Q,r,0)}var a})(),v),getStore:()=>a,destroy:()=>v.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return c&&(v[c]=()=>o),C(v)},aa="store",ta=(a,t,e,s,n,[i],c,o)=>{const[r,l,w,y]=z(t,c,n);return Z(a,(async()=>await y((async()=>(await r(),V((await l(i,J))[k]?.[aa]??"null"))))),(async a=>await y((async()=>{var t;await r(),await w(i,J,{[k]:{[aa]:(t=a()??null,JSON.stringify(t,((a,t)=>t instanceof Map?h.fromEntries([...t]):t)))}},!0,!0)}))),e,s,n,["getDb",o],o)},ea=(a,t,e,s,n,[i,c,[o,r,w]],y,E)=>{const[u,m,p,v]=z(t,y,n),A=async(a,t)=>await d(_(c,(async([e,s,n,i],c)=>{const o=a[c];t&&void 0===o||await p(e,s,o,n,i,t)}))),L=async(a,t)=>r?await p(w,J,{[k]:a},!0,!0,t):null;return Z(a,(async()=>await v((async()=>{await u();const a=await(async()=>D(T(await d(_(i,(async([a,t],e)=>[a,await m(e,t)]))),(a=>!F(a[1])))))(),t=await(async()=>o?(await m(w,J))[k]:{})();return F(a)&&l(t)?void 0:[a,t]}))),(async(a,t)=>await v((async()=>{if(await u(),l(t)){const[t,e]=a();await A(t),await L(e)}else{const[a,e]=t();await A(a,!0),await L(e,!0)}}))),e,s,n,["getDb",E],E)},sa="json",na="autoLoadIntervalSeconds",ia="rowIdColumnName",ca="tableId",oa="tableName",ra={mode:sa,[na]:1},la={load:0,save:0,[oa]:e+"_values"},wa=(a,t,e,s)=>{const n=M();return g(a,((a,i)=>{const c=E(b(S(t,y(a)?{[e]:a}:a)),0,I(t));l(c[0])||s(i,c[0])||B(n,i,c)})),n},ya="pragma ",Ea="data_version",ua="schema_version",da=(a,t,s,n,i,c,l,w)=>{let u,d;const[m,p,v,T]=(a=>{const t=(a=>S(ra,y(a)?{storeTableName:a}:a??{}))(a),s=t[na];if(t.mode==sa){const{storeTableName:a=e}=t;return[1,s,[a],W(a)]}const{tables:{load:n={},save:i={}}={},values:c={}}=t,o=E(b(S(la,c)),0,I(la)),r=o[2],l=W(r);return[0,s,[wa(n,{[ca]:null,[ia]:J},ca,(a=>j(l,a)&&a==r)),wa(i,{[oa]:null,[ia]:J,deleteEmptyColumns:0,deleteEmptyTable:0},oa,((a,t)=>j(l,t)&&t==r)),o],l]})(t);return(m?ta:ea)(a,c?async(a,t)=>(c(a,t),await s(a,t)):s,(a=>[o((async()=>{try{const t=(await s(ya+Ea))[0][Ea],e=(await s(ya+ua))[0][ua];t==(u??=t)&&e==(d??=e)||(a(),u=t,d=e)}catch{}}),1e3*p),n((t=>T.has(t)?a():0))]),(([a,t])=>{r(a),u=d=null,i(t)}),l,v,f(T),w)},ma=(t,e,s,n,i)=>da(t,s,(async(a,t=[])=>await e.allAsync(a,t)),(t=>a((({tableName:a})=>t(a)))),(a=>a.remove()),n,i,e);export{ma as createExpoSqliteNextPersister};
@@ -0,0 +1,168 @@
1
+ /**
2
+ * The persister-expo-sqlite-next module of the TinyBase project lets you save
3
+ * and load Store data to and from a local Expo-SQLite 'next' database (in an
4
+ * appropriate React Native environment).
5
+ *
6
+ * This module provides a Persister for the modern version of Expo's
7
+ * [SQLite](https://docs.expo.dev/versions/latest/sdk/sqlite)
8
+ * library, designated 'next' as of November 2023. This API should be used if
9
+ * you are installing the `expo-sqlite/next` module. In the future we expect
10
+ * this module to become the default.
11
+ *
12
+ * Note that TinyBase support for the legacy version of Expo-SQLite
13
+ * (`expo-sqlite`) is still available in the persister-expo-sqlite module.
14
+ *
15
+ * Note that this Persister is currently experimental as Expo themselves iterate
16
+ * on the underlying database library API.
17
+ * @see Persisting Data guide
18
+ * @packageDocumentation
19
+ * @module persister-expo-sqlite-next
20
+ * @since v4.5.0
21
+ */
22
+
23
+ import {DatabasePersisterConfig, Persister} from '../persisters';
24
+ import {Database} from 'expo-sqlite/next';
25
+ import {Store} from '../store';
26
+
27
+ /**
28
+ * The ExpoSqliteNextPersister interface is a minor extension to the Persister
29
+ * interface.
30
+ *
31
+ * It simply provides an extra getDb method for accessing a reference to the
32
+ * database instance the Store is being persisted to.
33
+ * @since v4.5.0
34
+ */
35
+ export interface ExpoSqliteNextPersister extends Persister {
36
+ /**
37
+ * The getDb method returns a reference to the database instance the Store is
38
+ * being persisted to.
39
+ * @returns A reference to the database instance.
40
+ * @example
41
+ * This example creates a Persister object against a newly-created Store and
42
+ * then gets the database instance back out again.
43
+ *
44
+ * ```js yolo
45
+ * const db = SQLite.openDatabaseSync('my.db');
46
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
47
+ * const persister = createExpoSqliteNextPersister(store, db, 'my_tinybase');
48
+ *
49
+ * console.log(persister.getDb() == db);
50
+ * // -> true
51
+ *
52
+ * persister.destroy();
53
+ * ```
54
+ * @category Getter
55
+ * @since v4.5.0
56
+ */
57
+ getDb: () => Database;
58
+ }
59
+
60
+ /**
61
+ * The createExpoSqliteNextPersister function creates a Persister object that
62
+ * can persist the Store to a local Expo-SQLite database (in an appropriate
63
+ * React Native environment).
64
+ *
65
+ * Note that this Persister is currently experimental as Expo themselves iterate
66
+ * on the underlying database library API.
67
+ *
68
+ * As well as providing a reference to the Store to persist, you must provide a
69
+ * `db` parameter which identifies the database instance.
70
+ *
71
+ * A database Persister uses one of two modes: either a JSON serialization of
72
+ * the whole Store stored in a single row of a table (the default), or a tabular
73
+ * mapping of Table Ids to database table names and vice-versa).
74
+ *
75
+ * The third argument is a DatabasePersisterConfig object that configures which
76
+ * of those modes to use, and settings for each. If the third argument is simply
77
+ * a string, it is used as the `storeTableName` property of the JSON
78
+ * serialization.
79
+ *
80
+ * See the documentation for the DpcJson and DpcTabular types for more
81
+ * information on how both of those modes can be configured.
82
+ * @param store The Store to persist.
83
+ * @param db The database instance that was returned from
84
+ * `SQLite.openDatabase(...)`.
85
+ * @param configOrStoreTableName A DatabasePersisterConfig to configure the
86
+ * persistence mode (or a string to set the `storeTableName` property of the
87
+ * JSON serialization).
88
+ * @param onSqlCommand An optional handler called every time the Persister
89
+ * executes a SQL command or query. This is suitable for logging persistence
90
+ * behavior in a development environment, since v4.0.4.
91
+ * @param onIgnoredError An optional handler for the errors that the Persister
92
+ * would otherwise ignore when trying to save or load data. This is suitable for
93
+ * debugging persistence issues in a development environment, since v4.0.4.
94
+ * @returns A reference to the new ExpoSqliteNextPersister object.
95
+ * @example
96
+ * This example creates a ExpoSqliteNextPersister object and persists the Store
97
+ * to a local SQLite database as a JSON serialization into the `my_tinybase`
98
+ * table. It makes a change to the database directly and then reloads it back
99
+ * into the Store.
100
+ *
101
+ * ```js yolo
102
+ * const db = SQLite.openDatabaseSync('my.db');
103
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
104
+ * const persister = createExpoSqliteNextPersister(store, db, 'my_tinybase');
105
+ *
106
+ * await persister.save();
107
+ * // Store will be saved to the database.
108
+ *
109
+ * console.log(
110
+ * await new Promise((resolve) =>
111
+ * db.allAsync('SELECT * FROM my_tinybase;').then(resolve),
112
+ * ),
113
+ * );
114
+ * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
115
+ *
116
+ * await new Promise((resolve) =>
117
+ * db.allAsync(
118
+ * 'UPDATE my_tinybase SET store = ' +
119
+ * `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
120
+ * ),
121
+ * );
122
+ * await persister.load();
123
+ * console.log(store.getTables());
124
+ * // -> {pets: {felix: {species: 'cat'}}}
125
+ *
126
+ * persister.destroy();
127
+ * ```
128
+ * @example
129
+ * This example creates a ExpoSqliteNextPersister object and persists the Store
130
+ * to a local SQLite database with tabular mapping.
131
+ *
132
+ * ```js yolo
133
+ * const db = SQLite.openDatabaseSync('my.db');
134
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
135
+ * const persister = createExpoSqliteNextPersister(store, db, {
136
+ * mode: 'tabular',
137
+ * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
138
+ * });
139
+ *
140
+ * await persister.save();
141
+ * console.log(
142
+ * await new Promise((resolve) =>
143
+ * db.allAsync('SELECT * FROM pets;').then(resolve),
144
+ * ),
145
+ * );
146
+ * // -> [{_id: 'fido', species: 'dog'}]
147
+ *
148
+ * await new Promise((resolve) =>
149
+ * db
150
+ * .allAsync(`INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`)
151
+ * .then(resolve),
152
+ * );
153
+ * await persister.load();
154
+ * console.log(store.getTables());
155
+ * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
156
+ *
157
+ * persister.destroy();
158
+ * ```
159
+ * @category Creation
160
+ * @since v4.5.0
161
+ */
162
+ export function createExpoSqliteNextPersister(
163
+ store: Store,
164
+ db: Database,
165
+ configOrStoreTableName?: DatabasePersisterConfig | string,
166
+ onSqlCommand?: (sql: string, args?: any[]) => void,
167
+ onIgnoredError?: (error: any) => void,
168
+ ): ExpoSqliteNextPersister;
@@ -3,6 +3,14 @@
3
3
  * load Store data to and from a local Expo-SQLite database (in an appropriate
4
4
  * React Native environment).
5
5
  *
6
+ * This module provides a Persister for the legacy version of Expo's
7
+ * [SQLite](https://docs.expo.dev/versions/latest/sdk/sqlite) library. This API
8
+ * should be used if you are installing the `expo-sqlite` module.
9
+ *
10
+ * Note that TinyBase support for the modern version of Expo-SQLite
11
+ * (`expo-sqlite-next`) is currently available in the persister-expo-sqlite-next
12
+ * module.
13
+ *
6
14
  * Note that this Persister is currently experimental as Expo themselves iterate
7
15
  * on the underlying database library API.
8
16
  * @see Persisting Data guide
@@ -19,6 +19,7 @@
19
19
  * |persister-sqlite-wasm|createSqliteWasmPersister|SQLite in a browser, via [sqlite-wasm](https://github.com/tomayac/sqlite-wasm)|
20
20
  * |persister-cr-sqlite-wasm|createCrSqliteWasmPersister|SQLite CRDTs, via [cr-sqlite-wasm](https://github.com/vlcn-io/cr-sqlite)|
21
21
  * |persister-expo-sqlite|createExpoSqlitePersister|SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite)|
22
+ * |persister-expo-sqlite-next|createExpoSqliteNextPersister|SQLite in React Native, via [expo-sqlite/next](https://github.com/expo/expo/tree/main/packages/expo-sqlite/next)|
22
23
  * |persister-yjs|createYjsPersister|Yjs CRDTs, via [yjs](https://github.com/yjs/yjs)|
23
24
  * |persister-automerge|createSqliteWasmPersister|Automerge CRDTs, via [automerge-repo](https://github.com/automerge/automerge-repo)|
24
25
  *
@@ -0,0 +1,181 @@
1
+ /**
2
+ * The persister-expo-sqlite-next module of the TinyBase project lets you save
3
+ * and load Store data to and from a local Expo-SQLite 'next' database (in an
4
+ * appropriate React Native environment).
5
+ *
6
+ * This module provides a Persister for the modern version of Expo's
7
+ * [SQLite](https://docs.expo.dev/versions/latest/sdk/sqlite)
8
+ * library, designated 'next' as of November 2023. This API should be used if
9
+ * you are installing the `expo-sqlite/next` module. In the future we expect
10
+ * this module to become the default.
11
+ *
12
+ * Note that TinyBase support for the legacy version of Expo-SQLite
13
+ * (`expo-sqlite`) is still available in the persister-expo-sqlite module.
14
+ *
15
+ * Note that this Persister is currently experimental as Expo themselves iterate
16
+ * on the underlying database library API.
17
+ * @see Persisting Data guide
18
+ * @packageDocumentation
19
+ * @module persister-expo-sqlite-next
20
+ * @since v4.5.0
21
+ */
22
+
23
+ import {DatabasePersisterConfig, Persister} from '../persisters';
24
+ import {OptionalSchemas, Store} from '../store';
25
+ import {Database} from 'expo-sqlite/next';
26
+
27
+ /**
28
+ * The ExpoSqliteNextPersister interface is a minor extension to the Persister
29
+ * interface.
30
+ *
31
+ * It simply provides an extra getDb method for accessing a reference to the
32
+ * database instance the Store is being persisted to.
33
+ * @since v4.5.0
34
+ */
35
+ export interface ExpoSqliteNextPersister<Schemas extends OptionalSchemas>
36
+ extends Persister<Schemas> {
37
+ /**
38
+ * The getDb method returns a reference to the database instance the Store is
39
+ * being persisted to.
40
+ * @returns A reference to the database instance.
41
+ * @example
42
+ * This example creates a Persister object against a newly-created Store and
43
+ * then gets the database instance back out again.
44
+ *
45
+ * ```js yolo
46
+ * const db = SQLite.openDatabaseSync('my.db');
47
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
48
+ * const persister = createExpoSqliteNextPersister(store, db, 'my_tinybase');
49
+ *
50
+ * console.log(persister.getDb() == db);
51
+ * // -> true
52
+ *
53
+ * persister.destroy();
54
+ * ```
55
+ * @category Getter
56
+ * @since v4.5.0
57
+ */
58
+ getDb: () => Database;
59
+ }
60
+
61
+ /**
62
+ * The createExpoSqliteNextPersister function creates a Persister object that
63
+ * can persist the Store to a local Expo-SQLite database (in an appropriate
64
+ * React Native environment).
65
+ *
66
+ * This has schema-based typing. The following is a simplified representation:
67
+ *
68
+ * ```ts override
69
+ * createExpoSqliteNextPersister(
70
+ * store: Store,
71
+ * db: Database,
72
+ * configOrStoreTableName?: DatabasePersisterConfig | string,
73
+ * onSqlCommand?: (sql: string, args?: any[]) => void,
74
+ * onIgnoredError?: (error: any) => void,
75
+ * ): ExpoSqliteNextPersister;
76
+ * ```
77
+ *
78
+ * Note that this Persister is currently experimental as Expo themselves iterate
79
+ * on the underlying database library API.
80
+ *
81
+ * As well as providing a reference to the Store to persist, you must provide a
82
+ * `db` parameter which identifies the database instance.
83
+ *
84
+ * A database Persister uses one of two modes: either a JSON serialization of
85
+ * the whole Store stored in a single row of a table (the default), or a tabular
86
+ * mapping of Table Ids to database table names and vice-versa).
87
+ *
88
+ * The third argument is a DatabasePersisterConfig object that configures which
89
+ * of those modes to use, and settings for each. If the third argument is simply
90
+ * a string, it is used as the `storeTableName` property of the JSON
91
+ * serialization.
92
+ *
93
+ * See the documentation for the DpcJson and DpcTabular types for more
94
+ * information on how both of those modes can be configured.
95
+ * @param store The Store to persist.
96
+ * @param db The database instance that was returned from
97
+ * `SQLite.openDatabase(...)`.
98
+ * @param configOrStoreTableName A DatabasePersisterConfig to configure the
99
+ * persistence mode (or a string to set the `storeTableName` property of the
100
+ * JSON serialization).
101
+ * @param onSqlCommand An optional handler called every time the Persister
102
+ * executes a SQL command or query. This is suitable for logging persistence
103
+ * behavior in a development environment, since v4.0.4.
104
+ * @param onIgnoredError An optional handler for the errors that the Persister
105
+ * would otherwise ignore when trying to save or load data. This is suitable for
106
+ * debugging persistence issues in a development environment, since v4.0.4.
107
+ * @returns A reference to the new ExpoSqliteNextPersister object.
108
+ * @example
109
+ * This example creates a ExpoSqliteNextPersister object and persists the Store
110
+ * to a local SQLite database as a JSON serialization into the `my_tinybase`
111
+ * table. It makes a change to the database directly and then reloads it back
112
+ * into the Store.
113
+ *
114
+ * ```js yolo
115
+ * const db = SQLite.openDatabaseSync('my.db');
116
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
117
+ * const persister = createExpoSqliteNextPersister(store, db, 'my_tinybase');
118
+ *
119
+ * await persister.save();
120
+ * // Store will be saved to the database.
121
+ *
122
+ * console.log(
123
+ * await new Promise((resolve) =>
124
+ * db.allAsync('SELECT * FROM my_tinybase;').then(resolve),
125
+ * ),
126
+ * );
127
+ * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
128
+ *
129
+ * await new Promise((resolve) =>
130
+ * db.allAsync(
131
+ * 'UPDATE my_tinybase SET store = ' +
132
+ * `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
133
+ * ),
134
+ * );
135
+ * await persister.load();
136
+ * console.log(store.getTables());
137
+ * // -> {pets: {felix: {species: 'cat'}}}
138
+ *
139
+ * persister.destroy();
140
+ * ```
141
+ * @example
142
+ * This example creates a ExpoSqliteNextPersister object and persists the Store
143
+ * to a local SQLite database with tabular mapping.
144
+ *
145
+ * ```js yolo
146
+ * const db = SQLite.openDatabaseSync('my.db');
147
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
148
+ * const persister = createExpoSqliteNextPersister(store, db, {
149
+ * mode: 'tabular',
150
+ * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
151
+ * });
152
+ *
153
+ * await persister.save();
154
+ * console.log(
155
+ * await new Promise((resolve) =>
156
+ * db.allAsync('SELECT * FROM pets;').then(resolve),
157
+ * ),
158
+ * );
159
+ * // -> [{_id: 'fido', species: 'dog'}]
160
+ *
161
+ * await new Promise((resolve) =>
162
+ * db
163
+ * .allAsync(`INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`)
164
+ * .then(resolve),
165
+ * );
166
+ * await persister.load();
167
+ * console.log(store.getTables());
168
+ * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
169
+ *
170
+ * persister.destroy();
171
+ * ```
172
+ * @category Creation
173
+ * @since v4.5.0
174
+ */
175
+ export function createExpoSqliteNextPersister<Schemas extends OptionalSchemas>(
176
+ store: Store<Schemas>,
177
+ db: Database,
178
+ configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
179
+ onSqlCommand?: (sql: string, args?: any[]) => void,
180
+ onIgnoredError?: (error: any) => void,
181
+ ): ExpoSqliteNextPersister<Schemas>;
@@ -3,6 +3,14 @@
3
3
  * load Store data to and from a local Expo-SQLite database (in an appropriate
4
4
  * React Native environment).
5
5
  *
6
+ * This module provides a Persister for the legacy version of Expo's
7
+ * [SQLite](https://docs.expo.dev/versions/latest/sdk/sqlite) library. This API
8
+ * should be used if you are installing the `expo-sqlite` module.
9
+ *
10
+ * Note that TinyBase support for the modern version of Expo-SQLite
11
+ * (`expo-sqlite-next`) is currently available in the persister-expo-sqlite-next
12
+ * module.
13
+ *
6
14
  * Note that this Persister is currently experimental as Expo themselves iterate
7
15
  * on the underlying database library API.
8
16
  * @see Persisting Data guide
@@ -19,6 +19,7 @@
19
19
  * |persister-sqlite-wasm|createSqliteWasmPersister|SQLite in a browser, via [sqlite-wasm](https://github.com/tomayac/sqlite-wasm)|
20
20
  * |persister-cr-sqlite-wasm|createCrSqliteWasmPersister|SQLite CRDTs, via [cr-sqlite-wasm](https://github.com/vlcn-io/cr-sqlite)|
21
21
  * |persister-expo-sqlite|createExpoSqlitePersister|SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite)|
22
+ * |persister-expo-sqlite-next|createExpoSqliteNextPersister|SQLite in React Native, via [expo-sqlite/next](https://github.com/expo/expo/tree/main/packages/expo-sqlite/next)|
22
23
  * |persister-yjs|createYjsPersister|Yjs CRDTs, via [yjs](https://github.com/yjs/yjs)|
23
24
  * |persister-automerge|createSqliteWasmPersister|Automerge CRDTs, via [automerge-repo](https://github.com/automerge/automerge-repo)|
24
25
  *
@@ -0,0 +1 @@
1
+ var a,t;a=this,t=function(a,t){"use strict";const e=a=>typeof a,s="tinybase",n=",",i=e(""),o=(a,t)=>a.repeat(t),c=Promise,r=setInterval,l=clearInterval,y=a=>null==a,w=(a,t,e)=>y(a)?e?.():t(a),E=a=>e(a)==i,d=(a,t,e)=>a.slice(t,e),u=a=>a.length,p=async a=>c.all(a),f=(a,t="")=>a.join(t),T=(a,t)=>a.map(t),m=a=>0==u(a),v=(a,t)=>a.filter(t),A=(a,...t)=>a.push(...t),L=(a,t)=>a?.has(t)??!1,h=a=>[...a?.values()??[]],N=(a,t)=>a?.delete(t),O=Object,R=a=>O.getPrototypeOf(a),S=O.keys,b=O.freeze,g=(a=[])=>O.fromEntries(a),C=(...a)=>O.assign({},...a),D=(a,t)=>T(O.entries(a),(([a,e])=>t(e,a))),x=a=>O.values(a),I=a=>u(S(a)),F=a=>(a=>!y(a)&&w(R(a),(a=>a==O.prototype||y(R(a))),(()=>!0)))(a)&&0==I(a),M=a=>new Map(a),P=a=>[...a?.keys()??[]],$=(a,t)=>a?.get(t),_=(a,t)=>T([...a?.entries()??[]],(([a,e])=>t(e,a))),q=(a,t,e)=>y(e)?(N(a,t),a):a?.set(t,e),B=(a,t,e)=>(L(a,t)||q(a,t,e()),$(a,t)),j=(a,t,e,s=q)=>(D(t,((t,s)=>e(a,s,t))),((a,t)=>{((a,t)=>{a?.forEach(t)})(a,((a,e)=>t(e)))})(a,(e=>((a,t)=>!y(((a,t)=>w(a,(a=>a[t])))(a,t)))(t,e)?0:s(a,e))),a),H=a=>new Set(Array.isArray(a)||y(a)?a:[a]),W=(a,t)=>a?.add(t),k="_",J="_id",U=a=>`"${a.replace(/"/g,'""')}"`,Y="FROM pragma_table_",z="WHERE",G=(a,t,e)=>{const s=M();return[async()=>j(s,g(await p(T(await a("SELECT name "+Y+"list WHERE schema='main'AND type='table'AND name IN("+V(t)+")",t),(async({name:t})=>[t,g(T(await a("SELECT name,type "+Y+"info(?)",[t]),(({name:a,type:t})=>[a,t])))])))),((a,t,e)=>q(s,t,j(B(s,t,M),e,((a,t,e)=>{e!=$(a,t)&&q(a,t,e)}),((a,t)=>q(a,t))))),((a,t)=>q(s,t))),async(t,e)=>((a,t)=>!y($($(s,a),t)))(t,e)?g(v(T(await a("SELECT*FROM"+U(t)),(a=>{return[a[e],(t={...a},s=e,delete t[s],t)];var t,s})),(([a,t])=>!y(a)&&!F(t)))):{},async(t,e,i,o,c,r=!1)=>{const l=H();D(i??{},(a=>T(S(a??{}),(a=>W(l,a)))));const w=h(l);if(!r&&c&&m(w)&&L(s,t))return await a("DROP TABLE"+U(t)),void q(s,t);if(m(w)||L(s,t)){const n=$(s,t),i=H(P(n));await p([...T(w,(async e=>{N(i,e)||(await a(`ALTER TABLE${U(t)}ADD${U(e)}`),q(n,e,""))})),...!r&&o?T(h(i),(async s=>{s!=e&&(await a(`ALTER TABLE${U(t)}DROP${U(s)}`),q(n,s))})):[]])}else await a(`CREATE TABLE${U(t)}(${U(e)} PRIMARY KEY ON CONFLICT REPLACE${f(T(w,(a=>n+U(a))))});`),q(s,t,M([[e,""],...T(w,(a=>[a,""]))]));if(r)y(i)?await a("DELETE FROM"+U(t)+"WHERE 1"):await p(D(i,(async(s,n)=>{y(s)?await a("DELETE FROM"+U(t)+z+U(e)+"=?",[n]):m(w)||await K(a,t,e,S(s),[n,...x(s)])})));else if(m(w))L(s,t)&&await a("DELETE FROM"+U(t)+"WHERE 1");else{const n=v(P($(s,t)),(a=>a!=e)),o=[],c=[];D(i??{},((a,t)=>{A(o,t,...T(n,(t=>a?.[t]))),A(c,t)})),await K(a,t,e,n,o),await a("DELETE FROM"+U(t)+z+U(e)+"NOT IN("+V(c)+")",c)}},async t=>{let s;await a("BEGIN");try{s=await t()}catch(a){e?.(a)}return await a("END"),s}]},K=async(a,t,e,s,i)=>await a("INSERT INTO"+U(t)+"("+U(e)+f(T(s,(a=>n+U(a))))+")VALUES"+d(o(`,(?${o(",?",u(s))})`,u(i)/(u(s)+1)),1)+"ON CONFLICT("+U(e)+")DO UPDATE SET"+f(T(s,(a=>U(a)+"=excluded."+U(a))),n),i),V=a=>f(T(a,(()=>"?")),n),Q=JSON.parse,X=M(),Z=M(),aa=(a,t,e,s,n,i,[o,c]=[],r=[])=>{let l,E,d,u=0,p=0;B(X,r,(()=>0)),B(Z,r,(()=>[]));const f=async a=>(2!=u&&(u=1,await T.schedule((async()=>{await a(),u=0}))),T),T={load:async(e,s)=>await f((async()=>{try{a.setContent(await t())}catch{a.setContent([e,s])}})),startAutoLoad:async(e={},n={})=>(T.stopAutoLoad(),await T.load(e,n),p=1,d=s((async(e,s)=>{if(s){const t=s();await f((async()=>a.setTransactionChanges(t)))}else await f((async()=>{try{a.setContent(e?.()??await t())}catch(a){i?.(a)}}))})),T),stopAutoLoad:()=>(p&&(n(d),d=void 0,p=0),T),save:async t=>(1!=u&&(u=2,await T.schedule((async()=>{try{await e(a.getContent,t)}catch(a){i?.(a)}u=0}))),T),startAutoSave:async()=>(await T.stopAutoSave().save(),l=a.addDidFinishTransactionListener(((a,t)=>{const[e,s]=t();F(e)&&F(s)||T.save((()=>[e,s]))})),T),stopAutoSave:()=>(w(l,a.delListener),l=void 0,T),schedule:async(...a)=>(A($(Z,r),...a),await(async()=>{if(!$(X,r)){for(q(X,r,1);!y((a=$(Z,r),E=a.shift()));)try{await E()}catch(a){i?.(a)}q(X,r,0)}var a})(),T),getStore:()=>a,destroy:()=>T.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return o&&(T[o]=()=>c),b(T)},ta="store",ea=(a,t,e,s,n,[i],o,c)=>{const[r,l,y,w]=G(t,o,n);return aa(a,(async()=>await w((async()=>(await r(),Q((await l(i,J))[k]?.[ta]??"null"))))),(async a=>await w((async()=>{var t;await r(),await y(i,J,{[k]:{[ta]:(t=a()??null,JSON.stringify(t,((a,t)=>t instanceof Map?O.fromEntries([...t]):t)))}},!0,!0)}))),e,s,n,["getDb",c],c)},sa=(a,t,e,s,n,[i,o,[c,r,l]],w,E)=>{const[d,u,f,T]=G(t,w,n),m=async(a,t)=>await p(_(o,(async([e,s,n,i],o)=>{const c=a[o];t&&void 0===c||await f(e,s,c,n,i,t)}))),A=async(a,t)=>r?await f(l,J,{[k]:a},!0,!0,t):null;return aa(a,(async()=>await T((async()=>{await d();const a=await(async()=>g(v(await p(_(i,(async([a,t],e)=>[a,await u(e,t)]))),(a=>!F(a[1])))))(),t=await(async()=>c?(await u(l,J))[k]:{})();return F(a)&&y(t)?void 0:[a,t]}))),(async(a,t)=>await T((async()=>{if(await d(),y(t)){const[t,e]=a();await m(t),await A(e)}else{const[a,e]=t();await m(a,!0),await A(e,!0)}}))),e,s,n,["getDb",E],E)},na="json",ia="autoLoadIntervalSeconds",oa="rowIdColumnName",ca="tableId",ra="tableName",la={mode:na,[ia]:1},ya={load:0,save:0,[ra]:s+"_values"},wa=(a,t,e,s)=>{const n=M();return D(a,((a,i)=>{const o=d(x(C(t,E(a)?{[e]:a}:a)),0,I(t));y(o[0])||s(i,o[0])||q(n,i,o)})),n},Ea="pragma ",da="data_version",ua="schema_version",pa=(a,t,e,n,i,o,c,y)=>{let w,u;const[p,f,T,m]=(a=>{const t=(a=>C(la,E(a)?{storeTableName:a}:a??{}))(a),e=t[ia];if(t.mode==na){const{storeTableName:a=s}=t;return[1,e,[a],H(a)]}const{tables:{load:n={},save:i={}}={},values:o={}}=t,c=d(x(C(ya,o)),0,I(ya)),r=c[2],l=H(r);return[0,e,[wa(n,{[ca]:null,[oa]:J},ca,(a=>W(l,a)&&a==r)),wa(i,{[ra]:null,[oa]:J,deleteEmptyColumns:0,deleteEmptyTable:0},ra,((a,t)=>W(l,t)&&t==r)),c],l]})(t);return(p?ea:sa)(a,o?async(a,t)=>(o(a,t),await e(a,t)):e,(a=>[r((async()=>{try{const t=(await e(Ea+da))[0][da],s=(await e(Ea+ua))[0][ua];t==(w??=t)&&s==(u??=s)||(a(),w=t,u=s)}catch{}}),1e3*f),n((t=>m.has(t)?a():0))]),(([a,t])=>{l(a),w=u=null,i(t)}),c,T,h(m),y)};a.createExpoSqliteNextPersister=(a,e,s,n,i)=>pa(a,s,(async(a,t=[])=>await e.allAsync(a,t)),(a=>t.addDatabaseChangeListener((({tableName:t})=>a(t)))),(a=>a.remove()),n,i,e)},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("expo-sqlite/next")):"function"==typeof define&&define.amd?define(["exports","expo-sqlite/next"],t):t((a="undefined"!=typeof globalThis?globalThis:a||self).TinyBasePersisterExpoSqliteNext={},a["expo-sqlite/next"]);
@@ -0,0 +1 @@
1
+ var e,n;e=this,n=function(e,n){"use strict";const t=e=>typeof e,l="tinybase",o=",",i=t(""),r=(e,n)=>e.repeat(n),u=Promise,d=setInterval,a=clearInterval,s=e=>null==e,c=(e,n,t)=>s(e)?null==t?void 0:t():n(e),v=e=>t(e)==i,y=(e,n,t)=>e.slice(n,t),f=e=>e.length,p=e=>{return n=function*(){return u.all(e)},new Promise(((e,t)=>{var l=e=>{try{i(n.next(e))}catch(e){t(e)}},o=e=>{try{i(n.throw(e))}catch(e){t(e)}},i=n=>n.done?e(n.value):Promise.resolve(n.value).then(l,o);i((n=n.apply(void 0,null)).next())}));var n},h=(e,n="")=>e.join(n),E=(e,n)=>e.map(n),m=e=>0==f(e),T=(e,n)=>e.filter(n),x=(e,...n)=>e.push(...n),b=(e,n)=>{var t;return null!=(t=null==e?void 0:e.has(n))&&t},P=e=>{var n;return[...null!=(n=null==e?void 0:e.values())?n:[]]},A=(e,n)=>null==e?void 0:e.delete(n),L=Object,O=e=>L.getPrototypeOf(e),N=L.keys,w=L.freeze,g=(e=[])=>L.fromEntries(e),R=(...e)=>L.assign({},...e),S=(e,n)=>E(L.entries(e),(([e,t])=>n(t,e))),C=e=>L.values(e),D=e=>f(N(e)),I=e=>(e=>!s(e)&&c(O(e),(e=>e==L.prototype||s(O(e))),(()=>!0)))(e)&&0==D(e),F=e=>new Map(e),M=e=>{var n;return[...null!=(n=null==e?void 0:e.keys())?n:[]]},$=(e,n)=>null==e?void 0:e.get(n),j=(e,n)=>{var t;return E([...null!=(t=null==e?void 0:e.entries())?t:[]],(([e,t])=>n(t,e)))},_=(e,n,t)=>s(t)?(A(e,n),e):null==e?void 0:e.set(n,t),q=(e,n,t)=>(b(e,n)||_(e,n,t()),$(e,n)),B=(e,n,t,l=_)=>(S(n,((n,l)=>t(e,l,n))),((e,n)=>{((e,n)=>{null==e||e.forEach(n)})(e,((e,t)=>n(t)))})(e,(t=>((e,n)=>!s(((e,n)=>c(e,(e=>e[n])))(e,n)))(n,t)?0:l(e,t))),e),H=e=>new Set(Array.isArray(e)||s(e)?e:[e]),W=(e,n)=>null==e?void 0:e.add(n),k="_",J="_id",U=e=>`"${e.replace(/"/g,'""')}"`;var Y=Object.defineProperty,z=Object.getOwnPropertySymbols,G=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable,V=(e,n,t)=>n in e?Y(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,Q=(e,n)=>{for(var t in n||(n={}))G.call(n,t)&&V(e,t,n[t]);if(z)for(var t of z(n))K.call(n,t)&&V(e,t,n[t]);return e},X=(e,n,t)=>new Promise(((l,o)=>{var i=e=>{try{u(t.next(e))}catch(e){o(e)}},r=e=>{try{u(t.throw(e))}catch(e){o(e)}},u=e=>e.done?l(e.value):Promise.resolve(e.value).then(i,r);u((t=t.apply(e,n)).next())}));const Z="FROM pragma_table_",ee="WHERE",ne=(e,n,t)=>{const l=F();return[()=>X(void 0,null,(function*(){return B(l,g(yield p(E(yield e("SELECT name "+Z+"list WHERE schema='main'AND type='table'AND name IN("+le(n)+")",n),(n=>X(void 0,[n],(function*({name:n}){return[n,g(E(yield e("SELECT name,type "+Z+"info(?)",[n]),(({name:e,type:n})=>[e,n])))]})))))),((e,n,t)=>_(l,n,B(q(l,n,F),t,((e,n,t)=>{t!=$(e,n)&&_(e,n,t)}),((e,n)=>_(e,n))))),((e,n)=>_(l,n)))})),(n,t)=>X(void 0,null,(function*(){return((e,n)=>!s($($(l,e),n)))(n,t)?g(T(E(yield e("SELECT*FROM"+U(n)),(e=>{return[e[t],(n=Q({},e),l=t,delete n[l],n)];var n,l})),(([e,n])=>!s(e)&&!I(n)))):{}})),(n,t,i,r,u,d=!1)=>X(void 0,null,(function*(){const a=H();S(null!=i?i:{},(e=>E(N(null!=e?e:{}),(e=>W(a,e)))));const c=P(a);if(!d&&u&&m(c)&&b(l,n))return yield e("DROP TABLE"+U(n)),void _(l,n);if(m(c)||b(l,n)){const o=$(l,n),i=H(M(o));yield p([...E(c,(t=>X(void 0,null,(function*(){A(i,t)||(yield e(`ALTER TABLE${U(n)}ADD${U(t)}`),_(o,t,""))})))),...!d&&r?E(P(i),(l=>X(void 0,null,(function*(){l!=t&&(yield e(`ALTER TABLE${U(n)}DROP${U(l)}`),_(o,l))})))):[]])}else yield e(`CREATE TABLE${U(n)}(${U(t)} PRIMARY KEY ON CONFLICT REPLACE${h(E(c,(e=>o+U(e))))});`),_(l,n,F([[t,""],...E(c,(e=>[e,""]))]));if(d)s(i)?yield e("DELETE FROM"+U(n)+"WHERE 1"):yield p(S(i,((l,o)=>X(void 0,null,(function*(){s(l)?yield e("DELETE FROM"+U(n)+ee+U(t)+"=?",[o]):m(c)||(yield te(e,n,t,N(l),[o,...C(l)]))})))));else if(m(c))b(l,n)&&(yield e("DELETE FROM"+U(n)+"WHERE 1"));else{const o=T(M($(l,n)),(e=>e!=t)),r=[],u=[];S(null!=i?i:{},((e,n)=>{x(r,n,...E(o,(n=>null==e?void 0:e[n]))),x(u,n)})),yield te(e,n,t,o,r),yield e("DELETE FROM"+U(n)+ee+U(t)+"NOT IN("+le(u)+")",u)}})),n=>X(void 0,null,(function*(){let l;yield e("BEGIN");try{l=yield n()}catch(e){null==t||t(e)}return yield e("END"),l}))]},te=(e,n,t,l,i)=>X(void 0,null,(function*(){return yield e("INSERT INTO"+U(n)+"("+U(t)+h(E(l,(e=>o+U(e))))+")VALUES"+y(r(`,(?${r(",?",f(l))})`,f(i)/(f(l)+1)),1)+"ON CONFLICT("+U(t)+")DO UPDATE SET"+h(E(l,(e=>U(e)+"=excluded."+U(e))),o),i)})),le=e=>h(E(e,(()=>"?")),o),oe=JSON.parse;var ie=(e,n,t)=>new Promise(((l,o)=>{var i=e=>{try{u(t.next(e))}catch(e){o(e)}},r=e=>{try{u(t.throw(e))}catch(e){o(e)}},u=e=>e.done?l(e.value):Promise.resolve(e.value).then(i,r);u((t=t.apply(e,n)).next())}));const re=F(),ue=F(),de=(e,n,t,l,o,i,[r,u]=[],d=[])=>{let a,v,y,f=0,p=0;q(re,d,(()=>0)),q(ue,d,(()=>[]));const h=e=>ie(void 0,null,(function*(){return 2!=f&&(f=1,yield E.schedule((()=>ie(void 0,null,(function*(){yield e(),f=0}))))),E})),E={load:(t,l)=>ie(void 0,null,(function*(){return yield h((()=>ie(void 0,null,(function*(){try{e.setContent(yield n())}catch(n){e.setContent([t,l])}}))))})),startAutoLoad:(...t)=>ie(void 0,[...t],(function*(t={},o={}){return E.stopAutoLoad(),yield E.load(t,o),p=1,y=l(((t,l)=>ie(void 0,null,(function*(){if(l){const n=l();yield h((()=>ie(void 0,null,(function*(){return e.setTransactionChanges(n)}))))}else yield h((()=>ie(void 0,null,(function*(){var l;try{e.setContent(null!=(l=null==t?void 0:t())?l:yield n())}catch(e){null==i||i(e)}}))))})))),E})),stopAutoLoad:()=>(p&&(o(y),y=void 0,p=0),E),save:n=>ie(void 0,null,(function*(){return 1!=f&&(f=2,yield E.schedule((()=>ie(void 0,null,(function*(){try{yield t(e.getContent,n)}catch(e){null==i||i(e)}f=0}))))),E})),startAutoSave:()=>ie(void 0,null,(function*(){return yield E.stopAutoSave().save(),a=e.addDidFinishTransactionListener(((e,n)=>{const[t,l]=n();I(t)&&I(l)||E.save((()=>[t,l]))})),E})),stopAutoSave:()=>(c(a,e.delListener),a=void 0,E),schedule:(...e)=>ie(void 0,null,(function*(){return x($(ue,d),...e),yield ie(void 0,null,(function*(){if(!$(re,d)){for(_(re,d,1);!s((e=$(ue,d),v=e.shift()));)try{yield v()}catch(e){null==i||i(e)}_(re,d,0)}var e})),E})),getStore:()=>e,destroy:()=>E.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return r&&(E[r]=()=>u),w(E)};var ae=(e,n,t)=>new Promise(((l,o)=>{var i=e=>{try{u(t.next(e))}catch(e){o(e)}},r=e=>{try{u(t.throw(e))}catch(e){o(e)}},u=e=>e.done?l(e.value):Promise.resolve(e.value).then(i,r);u((t=t.apply(e,n)).next())}));const se="store",ce=(e,n,t,l,o,[i],r,u)=>{const[d,a,s,c]=ne(n,r,o);return de(e,(()=>ae(void 0,null,(function*(){return yield c((()=>ae(void 0,null,(function*(){var e,n;return yield d(),oe(null!=(n=null==(e=(yield a(i,J))[k])?void 0:e[se])?n:"null")}))))}))),(e=>ae(void 0,null,(function*(){return yield c((()=>ae(void 0,null,(function*(){var n,t;yield d(),yield s(i,J,{[k]:{[se]:(t=null!=(n=e())?n:null,JSON.stringify(t,((e,n)=>n instanceof Map?L.fromEntries([...n]):n)))}},!0,!0)}))))}))),t,l,o,["getDb",u],u)};var ve=(e,n,t)=>new Promise(((l,o)=>{var i=e=>{try{u(t.next(e))}catch(e){o(e)}},r=e=>{try{u(t.throw(e))}catch(e){o(e)}},u=e=>e.done?l(e.value):Promise.resolve(e.value).then(i,r);u((t=t.apply(e,n)).next())}));const ye=(e,n,t,l,o,[i,r,[u,d,a]],c,v)=>{const[y,f,h,E]=ne(n,c,o),m=(e,n)=>ve(void 0,null,(function*(){return yield p(j(r,((t,l)=>ve(void 0,[t,l],(function*([t,l,o,i],r){const u=e[r];n&&void 0===u||(yield h(t,l,u,o,i,n))})))))})),x=(e,n)=>ve(void 0,null,(function*(){return d?yield h(a,J,{[k]:e},!0,!0,n):null}));return de(e,(()=>ve(void 0,null,(function*(){return yield E((()=>ve(void 0,null,(function*(){yield y();const e=yield ve(void 0,null,(function*(){return g(T(yield p(j(i,((e,n)=>ve(void 0,[e,n],(function*([e,n],t){return[e,yield f(t,n)]}))))),(e=>!I(e[1]))))})),n=yield ve(void 0,null,(function*(){return u?(yield f(a,J))[k]:{}}));return I(e)&&s(n)?void 0:[e,n]}))))}))),((e,n)=>ve(void 0,null,(function*(){return yield E((()=>ve(void 0,null,(function*(){if(yield y(),s(n)){const[n,t]=e();yield m(n),yield x(t)}else{const[e,t]=n();yield m(e,!0),yield x(t,!0)}}))))}))),t,l,o,["getDb",v],v)},fe="json",pe="autoLoadIntervalSeconds",he="rowIdColumnName",Ee="tableId",me="tableName",Te={mode:fe,[pe]:1},xe={load:0,save:0,[me]:l+"_values"},be=(e,n,t,l)=>{const o=F();return S(e,((e,i)=>{const r=y(C(R(n,v(e)?{[t]:e}:e)),0,D(n));s(r[0])||l(i,r[0])||_(o,i,r)})),o};var Pe=(e,n,t)=>new Promise(((l,o)=>{var i=e=>{try{u(t.next(e))}catch(e){o(e)}},r=e=>{try{u(t.throw(e))}catch(e){o(e)}},u=e=>e.done?l(e.value):Promise.resolve(e.value).then(i,r);u((t=t.apply(e,n)).next())}));const Ae="pragma ",Le="data_version",Oe="schema_version",Ne=(e,n,t,o,i,r,u,s)=>{let c,f;const[p,h,E,m]=(e=>{const n=(e=>R(Te,v(e)?{storeTableName:e}:null!=e?e:{}))(e),t=n[pe];if(n.mode==fe){const{storeTableName:e=l}=n;return[1,t,[e],H(e)]}const{tables:{load:o={},save:i={}}={},values:r={}}=n,u=y(C(R(xe,r)),0,D(xe)),d=u[2],a=H(d);return[0,t,[be(o,{[Ee]:null,[he]:J},Ee,(e=>W(a,e)&&e==d)),be(i,{[me]:null,[he]:J,deleteEmptyColumns:0,deleteEmptyTable:0},me,((e,n)=>W(a,n)&&n==d)),u],a]})(n);return(p?ce:ye)(e,r?(e,n)=>Pe(void 0,null,(function*(){return r(e,n),yield t(e,n)})):t,(e=>[d((()=>Pe(void 0,null,(function*(){try{const n=(yield t(Ae+Le))[0][Le],l=(yield t(Ae+Oe))[0][Oe];n==(null!=c?c:c=n)&&l==(null!=f?f:f=l)||(e(),c=n,f=l)}catch(e){}}))),1e3*h),o((n=>m.has(n)?e():0))]),(([e,n])=>{a(e),c=f=null,i(n)}),u,E,P(m),s)};e.createExpoSqliteNextPersister=(e,t,l,o,i)=>Ne(e,l,((e,...n)=>{return l=[e,...n],o=function*(e,n=[]){return yield t.allAsync(e,n)},new Promise(((e,n)=>{var t=e=>{try{r(o.next(e))}catch(e){n(e)}},i=e=>{try{r(o.throw(e))}catch(e){n(e)}},r=n=>n.done?e(n.value):Promise.resolve(n.value).then(t,i);r((o=o.apply(void 0,l)).next())}));var l,o}),(e=>n.addDatabaseChangeListener((({tableName:n})=>e(n)))),(e=>e.remove()),o,i,t)},"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("expo-sqlite/next")):"function"==typeof define&&define.amd?define(["exports","expo-sqlite/next"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBasePersisterExpoSqliteNext={},e["expo-sqlite/next"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinybase",
3
- "version": "4.4.2",
3
+ "version": "4.5.0",
4
4
  "author": "jamesgpearce",
5
5
  "repository": "github:tinyplex/tinybase",
6
6
  "license": "MIT",
@@ -256,7 +256,7 @@
256
256
  "eslint-plugin-jsdoc": "^46.9.0",
257
257
  "eslint-plugin-react": "7.33.2",
258
258
  "eslint-plugin-react-hooks": "^4.6.0",
259
- "expo-sqlite": "^11.6.0",
259
+ "expo-sqlite": "^12.1.0",
260
260
  "fake-indexeddb": "^5.0.1",
261
261
  "gulp": "^4.0.2",
262
262
  "gulp-gzip": "^1.4.2",
@@ -267,8 +267,8 @@
267
267
  "jest-fetch-mock": "^3.0.3",
268
268
  "jest-puppeteer": "^9.0.1",
269
269
  "less": "^4.2.0",
270
- "partykit": "^0.0.35",
271
- "partysocket": "^0.0.14",
270
+ "partykit": "^0.0.37",
271
+ "partysocket": "^0.0.15",
272
272
  "prettier": "^3.1.0",
273
273
  "puppeteer": "21.1.1",
274
274
  "react": "^18.2.0",
@@ -291,8 +291,8 @@
291
291
  "@sqlite.org/sqlite-wasm": "^3.44.0-build1",
292
292
  "@vlcn.io/crsqlite-wasm": "^0.15.2",
293
293
  "automerge-repo": "^0.1.0",
294
- "partykit": "^0.0.35",
295
- "partysocket": "^0.0.14",
294
+ "partykit": "^0.0.37",
295
+ "partysocket": "^0.0.15",
296
296
  "prettier": "^3.0.3",
297
297
  "react": "^18.2.0",
298
298
  "react-dom": "^18.2.0",