From 9d5153a903fdb86c15445e1259bc6e8b0cc5b131 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Tue, 12 May 2020 16:35:38 +0100 Subject: [PATCH] Better errors from shortcut methods (#181) * Errors from DB operation should take priority * Update deps and simplify tests * Testing the fix * Documenting the benefits of awaiting operations * Updating build --- README.md | 37 ++++++++------ build/cjs/database-extras.js.map | 2 +- build/cjs/index.js | 2 +- build/esm/database-extras.js.map | 2 +- build/esm/index.js | 2 +- build/iife/index-min.js | 2 +- build/iife/with-async-ittr-min.js | 2 +- generic-tsconfig.json | 2 +- package-lock.json | 82 ++++++++++++++++++------------- package.json | 8 +-- src/database-extras.ts | 2 +- test/iterate.ts | 4 +- test/main.ts | 26 +++++++++- test/missing-types.d.ts | 4 ++ test/open.ts | 4 +- tsconfig.json | 2 +- 16 files changed, 119 insertions(+), 64 deletions(-) create mode 100644 test/missing-types.d.ts diff --git a/README.md b/README.md index 573ec30..4a189e7 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ An IDB transaction auto-closes if it doesn't have anything left do once microtas const tx = db.transaction('keyval', 'readwrite'); const store = tx.objectStore('keyval'); const val = (await store.get('counter')) || 0; -store.put(val + 1, 'counter'); +await store.put(val + 1, 'counter'); await tx.done; ``` @@ -162,7 +162,7 @@ const val = (await store.get('counter')) || 0; // This is where things go wrong: const newVal = await fetch('/increment?val=' + val); // And this throws an error: -store.put(newVal, 'counter'); +await store.put(newVal, 'counter'); await tx.done; ``` @@ -215,10 +215,15 @@ Transactions have a `.done` promise which resolves when the transaction complete ```js const tx = db.transaction(storeName, 'readwrite'); -tx.store.put('foo', 'bar'); -await tx.done; +await Promise.all([ + tx.store.put('bar', 'foo'), + tx.store.put('world', 'hello'), + tx.done, +]); ``` +If you're writing to the database, `tx.done` is the signal that everything was successfully committed to the database. However, it's still beneficial to await the individual operations, as you'll see the error that caused the transition to fail. + ## `IDBCursor` enhancements Cursor advance methods (`advance`, `continue`, `continuePrimaryKey`) return a promise for the cursor, or null if there are no further values to provide. @@ -338,17 +343,19 @@ async function demo() { // Add multiple articles in one transaction: { const tx = db.transaction('articles', 'readwrite'); - tx.store.add({ - title: 'Article 2', - date: new Date('2019-01-01'), - body: '…', - }); - tx.store.add({ - title: 'Article 3', - date: new Date('2019-01-02'), - body: '…', - }); - await tx.done; + await Promise.all([ + tx.store.add({ + title: 'Article 2', + date: new Date('2019-01-01'), + body: '…', + }), + tx.store.add({ + title: 'Article 3', + date: new Date('2019-01-02'), + body: '…', + }), + tx.done, + ]); } // Get all the articles in date order: diff --git a/build/cjs/database-extras.js.map b/build/cjs/database-extras.js.map index 98a3660..06a123a 100644 --- a/build/cjs/database-extras.js.map +++ b/build/cjs/database-extras.js.map @@ -1 +1 @@ -{"version":3,"file":"database-extras.js","sourceRoot":"","sources":["../../src/database-extras.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAGhD,MAAM,WAAW,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC;AACvE,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;AACvD,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgB,CAAC;AAE9C,SAAS,SAAS,CAChB,MAAW,EACX,IAA8B;IAE9B,IACE,CAAC,CACC,MAAM,YAAY,WAAW;QAC7B,CAAC,CAAC,IAAI,IAAI,MAAM,CAAC;QACjB,OAAO,IAAI,KAAK,QAAQ,CACzB,EACD;QACA,OAAO;KACR;IAED,IAAI,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAE5D,MAAM,cAAc,GAAW,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;IAC9D,MAAM,QAAQ,GAAG,IAAI,KAAK,cAAc,CAAC;IACzC,MAA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AElB,SAAiB,EACjB,GAAG,IAAW;QAEd,wEAAwE;QACxE,MAAM,EAAE,GAAG,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;QAC3E,IAAI,MAAM,GAAgC,EAAE,CAAC,KAAK,CAAC;QACnD,IAAI,QAAQ;YAAE,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAClD,MAAM,SAAS,GAAG,MAAO,MAAc,CAAC,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;QACjE,IAAI,OAAO;YAAE,MAAM,EAAE,CAAC,IAAI,CAAC;QAC3B,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;IAEF,aAAa,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,YAAY,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAC1B,GAAG,QAAQ;IACX,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,CAC9B,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,QAAQ,CAAC,GAAI,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC;IAClE,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CACpB,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,QAAQ,CAAC,GAAI,CAAC,MAAM,EAAE,IAAI,CAAC;CAC3D,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/build/cjs/index.js b/build/cjs/index.js index 26d328c..b323403 100644 --- a/build/cjs/index.js +++ b/build/cjs/index.js @@ -69,7 +69,7 @@ function getMethod(target, prop) { let target = tx.store; if (useIndex) target = target.index(args.shift()); - const returnVal = target[targetFuncName](...args); + const returnVal = await target[targetFuncName](...args); if (isWrite) await tx.done; return returnVal; diff --git a/build/esm/database-extras.js.map b/build/esm/database-extras.js.map index 98a3660..06a123a 100644 --- a/build/esm/database-extras.js.map +++ b/build/esm/database-extras.js.map @@ -1 +1 @@ -{"version":3,"file":"database-extras.js","sourceRoot":"","sources":["../../src/database-extras.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAGhD,MAAM,WAAW,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC;AACvE,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;AACvD,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgB,CAAC;AAE9C,SAAS,SAAS,CAChB,MAAW,EACX,IAA8B;IAE9B,IACE,CAAC,CACC,MAAM,YAAY,WAAW;QAC7B,CAAC,CAAC,IAAI,IAAI,MAAM,CAAC;QACjB,OAAO,IAAI,KAAK,QAAQ,CACzB,EACD;QACA,OAAO;KACR;IAED,IAAI,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAE5D,MAAM,cAAc,GAAW,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;IAC9D,MAAM,QAAQ,GAAG,IAAI,KAAK,cAAc,CAAC;IACzC,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;IAEtD;IACE,4EAA4E;IAC5E,CAAC,CAAC,cAAc,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC;QACrE,CAAC,CAAC,OAAO,IAAI,WAAW,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC,EAClD;QACA,OAAO;KACR;IAED,MAAM,MAA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,CAAC,CAAC;QAClD,MAAM,SAAS,GAAG,MAAO,MAAc,CAAC,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;QACjE,IAAI,OAAO;YAAE,MAAM,EAAE,CAAC,IAAI,CAAC;QAC3B,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;IAEF,aAAa,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,YAAY,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAC1B,GAAG,QAAQ;IACX,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,CAC9B,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,QAAQ,CAAC,GAAI,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC;IAClE,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CACpB,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,QAAQ,CAAC,GAAI,CAAC,MAAM,EAAE,IAAI,CAAC;CAC3D,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/build/esm/index.js b/build/esm/index.js index a38132d..811cc84 100644 --- a/build/esm/index.js +++ b/build/esm/index.js @@ -66,7 +66,7 @@ function getMethod(target, prop) { let target = tx.store; if (useIndex) target = target.index(args.shift()); - const returnVal = target[targetFuncName](...args); + const returnVal = await target[targetFuncName](...args); if (isWrite) await tx.done; return returnVal; diff --git a/build/iife/index-min.js b/build/iife/index-min.js index 6df8e88..bee90e4 100644 --- a/build/iife/index-min.js +++ b/build/iife/index-min.js @@ -1 +1 @@ -var idb=function(e){"use strict";let t,n;const r=new WeakMap,o=new WeakMap,s=new WeakMap,a=new WeakMap,i=new WeakMap;let c={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return o.get(e);if("objectStoreNames"===t)return e.objectStoreNames||s.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return p(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function u(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(n||(n=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(f(this),t),p(r.get(this))}:function(...t){return p(e.apply(f(this),t))}:function(t,...n){const r=e.call(f(this),t,...n);return s.set(r,t.sort?t.sort():[t]),p(r)}}function d(e){return"function"==typeof e?u(e):(e instanceof IDBTransaction&&function(e){if(o.has(e))return;const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)});o.set(e,t)}(e),n=e,(t||(t=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])).some(e=>n instanceof e)?new Proxy(e,c):e);var n}function p(e){if(e instanceof IDBRequest)return function(e){const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(p(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)});return t.then(t=>{t instanceof IDBCursor&&r.set(t,e)}).catch(()=>{}),i.set(t,e),t}(e);if(a.has(e))return a.get(e);const t=d(e);return t!==e&&(a.set(e,t),i.set(t,e)),t}const f=e=>i.get(e);const l=["get","getKey","getAll","getAllKeys","count"],D=["put","add","delete","clear"],v=new Map;function b(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(v.get(t))return v.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=D.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!l.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let a=s.store;r&&(a=a.index(t.shift()));const i=a[n](...t);return o&&await s.done,i};return v.set(t,s),s}return c=(e=>({...e,get:(t,n,r)=>b(t,n)||e.get(t,n,r),has:(t,n)=>!!b(t,n)||e.has(t,n)}))(c),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),p(n).then(()=>{})},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const a=indexedDB.open(e,t),i=p(a);return r&&a.addEventListener("upgradeneeded",e=>{r(p(a.result),e.oldVersion,e.newVersion,p(a.transaction))}),n&&a.addEventListener("blocked",()=>n()),i.then(e=>{s&&e.addEventListener("close",()=>s()),o&&e.addEventListener("versionchange",()=>o())}).catch(()=>{}),i},e.unwrap=f,e.wrap=p,e}({}); +var idb=function(e){"use strict";let t,n;const r=new WeakMap,o=new WeakMap,s=new WeakMap,a=new WeakMap,i=new WeakMap;let c={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return o.get(e);if("objectStoreNames"===t)return e.objectStoreNames||s.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return p(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function u(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(n||(n=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(f(this),t),p(r.get(this))}:function(...t){return p(e.apply(f(this),t))}:function(t,...n){const r=e.call(f(this),t,...n);return s.set(r,t.sort?t.sort():[t]),p(r)}}function d(e){return"function"==typeof e?u(e):(e instanceof IDBTransaction&&function(e){if(o.has(e))return;const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)});o.set(e,t)}(e),n=e,(t||(t=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])).some(e=>n instanceof e)?new Proxy(e,c):e);var n}function p(e){if(e instanceof IDBRequest)return function(e){const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(p(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)});return t.then(t=>{t instanceof IDBCursor&&r.set(t,e)}).catch(()=>{}),i.set(t,e),t}(e);if(a.has(e))return a.get(e);const t=d(e);return t!==e&&(a.set(e,t),i.set(t,e)),t}const f=e=>i.get(e);const l=["get","getKey","getAll","getAllKeys","count"],D=["put","add","delete","clear"],v=new Map;function b(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(v.get(t))return v.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=D.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!l.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let a=s.store;r&&(a=a.index(t.shift()));const i=await a[n](...t);return o&&await s.done,i};return v.set(t,s),s}return c=(e=>({...e,get:(t,n,r)=>b(t,n)||e.get(t,n,r),has:(t,n)=>!!b(t,n)||e.has(t,n)}))(c),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),p(n).then(()=>{})},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const a=indexedDB.open(e,t),i=p(a);return r&&a.addEventListener("upgradeneeded",e=>{r(p(a.result),e.oldVersion,e.newVersion,p(a.transaction))}),n&&a.addEventListener("blocked",()=>n()),i.then(e=>{s&&e.addEventListener("close",()=>s()),o&&e.addEventListener("versionchange",()=>o())}).catch(()=>{}),i},e.unwrap=f,e.wrap=p,e}({}); diff --git a/build/iife/with-async-ittr-min.js b/build/iife/with-async-ittr-min.js index 2365b73..e5c912d 100644 --- a/build/iife/with-async-ittr-min.js +++ b/build/iife/with-async-ittr-min.js @@ -1 +1 @@ -var idb=function(e){"use strict";const t=(e,t)=>t.some(t=>e instanceof t);let n,r;const o=new WeakMap,s=new WeakMap,a=new WeakMap,i=new WeakMap,c=new WeakMap;let u={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return s.get(e);if("objectStoreNames"===t)return e.objectStoreNames||a.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return l(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function d(e){u=e(u)}function f(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(r||(r=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(D(this),t),l(o.get(this))}:function(...t){return l(e.apply(D(this),t))}:function(t,...n){const r=e.call(D(this),t,...n);return a.set(r,t.sort?t.sort():[t]),l(r)}}function p(e){return"function"==typeof e?f(e):(e instanceof IDBTransaction&&function(e){if(s.has(e))return;const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)});s.set(e,t)}(e),t(e,n||(n=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,u):e)}function l(e){if(e instanceof IDBRequest)return function(e){const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(l(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)});return t.then(t=>{t instanceof IDBCursor&&o.set(t,e)}).catch(()=>{}),c.set(t,e),t}(e);if(i.has(e))return i.get(e);const t=p(e);return t!==e&&(i.set(e,t),c.set(t,e)),t}const D=e=>c.get(e);const I=["get","getKey","getAll","getAllKeys","count"],B=["put","add","delete","clear"],b=new Map;function v(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(b.get(t))return b.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=B.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!I.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let a=s.store;r&&(a=a.index(t.shift()));const i=a[n](...t);return o&&await s.done,i};return b.set(t,s),s}d(e=>({...e,get:(t,n,r)=>v(t,n)||e.get(t,n,r),has:(t,n)=>!!v(t,n)||e.has(t,n)}));const g=["continue","continuePrimaryKey","advance"],y={},h=new WeakMap,m=new WeakMap,w={get(e,t){if(!g.includes(t))return e[t];let n=y[t];return n||(n=y[t]=function(...e){h.set(this,m.get(this)[t](...e))}),n}};async function*E(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;t=t;const n=new Proxy(t,w);for(m.set(n,t),c.set(n,D(t));t;)yield n,t=await(h.get(n)||t.continue()),h.delete(n)}function L(e,n){return n===Symbol.asyncIterator&&t(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===n&&t(e,[IDBIndex,IDBObjectStore])}return d(e=>({...e,get:(t,n,r)=>L(t,n)?E:e.get(t,n,r),has:(t,n)=>L(t,n)||e.has(t,n)})),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),l(n).then(()=>{})},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const a=indexedDB.open(e,t),i=l(a);return r&&a.addEventListener("upgradeneeded",e=>{r(l(a.result),e.oldVersion,e.newVersion,l(a.transaction))}),n&&a.addEventListener("blocked",()=>n()),i.then(e=>{s&&e.addEventListener("close",()=>s()),o&&e.addEventListener("versionchange",()=>o())}).catch(()=>{}),i},e.unwrap=D,e.wrap=l,e}({}); +var idb=function(e){"use strict";const t=(e,t)=>t.some(t=>e instanceof t);let n,r;const o=new WeakMap,s=new WeakMap,a=new WeakMap,i=new WeakMap,c=new WeakMap;let u={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return s.get(e);if("objectStoreNames"===t)return e.objectStoreNames||a.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return l(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function d(e){u=e(u)}function f(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(r||(r=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(D(this),t),l(o.get(this))}:function(...t){return l(e.apply(D(this),t))}:function(t,...n){const r=e.call(D(this),t,...n);return a.set(r,t.sort?t.sort():[t]),l(r)}}function p(e){return"function"==typeof e?f(e):(e instanceof IDBTransaction&&function(e){if(s.has(e))return;const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)});s.set(e,t)}(e),t(e,n||(n=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,u):e)}function l(e){if(e instanceof IDBRequest)return function(e){const t=new Promise((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(l(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)});return t.then(t=>{t instanceof IDBCursor&&o.set(t,e)}).catch(()=>{}),c.set(t,e),t}(e);if(i.has(e))return i.get(e);const t=p(e);return t!==e&&(i.set(e,t),c.set(t,e)),t}const D=e=>c.get(e);const I=["get","getKey","getAll","getAllKeys","count"],B=["put","add","delete","clear"],b=new Map;function v(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(b.get(t))return b.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=B.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!I.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let a=s.store;r&&(a=a.index(t.shift()));const i=await a[n](...t);return o&&await s.done,i};return b.set(t,s),s}d(e=>({...e,get:(t,n,r)=>v(t,n)||e.get(t,n,r),has:(t,n)=>!!v(t,n)||e.has(t,n)}));const g=["continue","continuePrimaryKey","advance"],y={},h=new WeakMap,w=new WeakMap,m={get(e,t){if(!g.includes(t))return e[t];let n=y[t];return n||(n=y[t]=function(...e){h.set(this,w.get(this)[t](...e))}),n}};async function*E(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;t=t;const n=new Proxy(t,m);for(w.set(n,t),c.set(n,D(t));t;)yield n,t=await(h.get(n)||t.continue()),h.delete(n)}function L(e,n){return n===Symbol.asyncIterator&&t(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===n&&t(e,[IDBIndex,IDBObjectStore])}return d(e=>({...e,get:(t,n,r)=>L(t,n)?E:e.get(t,n,r),has:(t,n)=>L(t,n)||e.has(t,n)})),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),l(n).then(()=>{})},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const a=indexedDB.open(e,t),i=l(a);return r&&a.addEventListener("upgradeneeded",e=>{r(l(a.result),e.oldVersion,e.newVersion,l(a.transaction))}),n&&a.addEventListener("blocked",()=>n()),i.then(e=>{s&&e.addEventListener("close",()=>s()),o&&e.addEventListener("versionchange",()=>o())}).catch(()=>{}),i},e.unwrap=D,e.wrap=l,e}({}); diff --git a/generic-tsconfig.json b/generic-tsconfig.json index 93721f1..7d3e261 100644 --- a/generic-tsconfig.json +++ b/generic-tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "ES2020", + "target": "ES2019", "downlevelIteration": true, "module": "esnext", "strict": true, diff --git a/package-lock.json b/package-lock.json index 555c8ba..6a2de63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,13 +57,15 @@ } }, "@rollup/plugin-commonjs": { - "version": "11.0.2", - "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-11.0.2.tgz", - "integrity": "sha512-MPYGZr0qdbV5zZj8/2AuomVpnRVXRU5XKXb3HVniwRoRCreGlf5kOE081isNWeiLIi6IYkwTX9zE0/c7V8g81g==", + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz", + "integrity": "sha512-Ycr12N3ZPN96Fw2STurD21jMqzKwL9QuFhms3SD7KKRK7oaXUsBU9Zt0jL/rOPHiPYisI21/rXGO3jr9BnLHUA==", "dev": true, "requires": { - "@rollup/pluginutils": "^3.0.0", + "@rollup/pluginutils": "^3.0.8", + "commondir": "^1.0.1", "estree-walker": "^1.0.1", + "glob": "^7.1.2", "is-reference": "^1.1.2", "magic-string": "^0.25.2", "resolve": "^1.11.0" @@ -78,12 +80,14 @@ } }, "@rollup/pluginutils": { - "version": "3.0.8", - "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.0.8.tgz", - "integrity": "sha512-rYGeAc4sxcZ+kPG/Tw4/fwJODC3IXHYDH4qusdN/b6aLw5LPUbzpecYbEJh4sVQGPFJxd2dBU4kc1H3oy9/bnw==", + "version": "3.0.10", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.0.10.tgz", + "integrity": "sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw==", "dev": true, "requires": { - "estree-walker": "^1.0.1" + "@types/estree": "0.0.39", + "estree-walker": "^1.0.1", + "picomatch": "^2.2.2" }, "dependencies": { "estree-walker": { @@ -91,6 +95,12 @@ "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-1.0.1.tgz", "integrity": "sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==", "dev": true + }, + "picomatch": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", + "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==", + "dev": true } } }, @@ -385,6 +395,12 @@ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", "dev": true }, + "commondir": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", + "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", + "dev": true + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -593,9 +609,9 @@ "dev": true }, "fsevents": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.2.tgz", - "integrity": "sha512-R4wDiBwZ0KzpgOWetKDug1FZcYhqYnUYKtfZYt4mD5SBz76q0KR4Q9o7GIPamsVPGmW3EYPPJ0dOOjvx32ldZA==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz", + "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==", "dev": true, "optional": true }, @@ -931,18 +947,18 @@ "dev": true }, "mkdirp": { - "version": "0.5.3", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.3.tgz", - "integrity": "sha512-P+2gwrFqx8lhew375MQHHeTlY8AuOJSrGf0R5ddkEndUkmwpgUob/vQuBD1V22/Cw1/lJr4x+EjllSezBThzBg==", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", "dev": true, "requires": { "minimist": "^1.2.5" } }, "mocha": { - "version": "7.1.1", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-7.1.1.tgz", - "integrity": "sha512-3qQsu3ijNS3GkWcccT5Zw0hf/rWvu1fTN9sPvEd81hlwsr30GX2GcDSSoBxo24IR8FelmrAydGC6/1J5QQP4WA==", + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-7.1.2.tgz", + "integrity": "sha512-o96kdRKMKI3E8U0bjnfqW4QMk12MwZ4mhdBTf+B5a1q9+aq2HRnj+3ZdJu0B/ZhJeK78MgYuv6L8d/rA5AeBJA==", "dev": true, "requires": { "ansi-colors": "3.2.3", @@ -958,7 +974,7 @@ "js-yaml": "3.13.1", "log-symbols": "3.0.0", "minimatch": "3.0.4", - "mkdirp": "0.5.3", + "mkdirp": "0.5.5", "ms": "2.1.1", "node-environment-flags": "1.0.6", "object.assign": "4.1.0", @@ -1053,9 +1069,9 @@ } }, "p-limit": { - "version": "2.2.2", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.2.tgz", - "integrity": "sha512-WGR+xHecKTr7EbUEhyLSh5Dube9JtdiG78ufaeLxTgpudf/20KqyMioIUZJAezlTIi6evxuoUs9YXc11cU+yzQ==", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", + "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", "dev": true, "requires": { "p-try": "^2.0.0" @@ -1116,9 +1132,9 @@ "dev": true }, "prettier": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.2.tgz", - "integrity": "sha512-5xJQIPT8BraI7ZnaDwSbu5zLrB6vvi8hVV58yHQ+QK64qrY40dULy0HSRlQ2/2IdzeBpjhDkqdcFBnFeDEMVdg==", + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", + "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", "dev": true }, "readdirp": { @@ -1167,9 +1183,9 @@ } }, "rollup": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.2.0.tgz", - "integrity": "sha512-iAu/j9/WJ0i+zT0sAMuQnsEbmOKzdQ4Yxu5rbPs9aUCyqveI1Kw3H4Fi9NWfCOpb8luEySD2lDyFWL9CrLE8iw==", + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.9.1.tgz", + "integrity": "sha512-kEZn76R0j+WD4AActu5Np4RShNlewTtJhdUaWNtHwHnAA8AOapyXgH6O7NL2RkcQLHnl49oz1xW1VrDcu5yP+Q==", "dev": true, "requires": { "fsevents": "~2.1.2" @@ -1287,9 +1303,9 @@ } }, "string.prototype.trimend": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.0.tgz", - "integrity": "sha512-EEJnGqa/xNfIg05SxiPSqRS7S9qwDhYts1TSLR1BQfYUfPe1stofgGKvwERK9+9yf+PpfBMlpBaCHucXGPQfUA==", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz", + "integrity": "sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g==", "dev": true, "requires": { "define-properties": "^1.1.3", @@ -1319,9 +1335,9 @@ } }, "string.prototype.trimstart": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.0.tgz", - "integrity": "sha512-iCP8g01NFYiiBOnwG1Xc3WZLyoo+RuBymwIlWncShXDDJYWN6DbnM3odslBJdgCdRlq94B5s63NWAZlcn2CS4w==", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz", + "integrity": "sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw==", "dev": true, "requires": { "define-properties": "^1.1.3", diff --git a/package.json b/package.json index bb504fc..211a76e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "author": "Jake Archibald", "license": "ISC", "devDependencies": { - "@rollup/plugin-commonjs": "^11.0.2", + "@rollup/plugin-commonjs": "^11.1.0", "@types/chai": "^4.2.11", "@types/mocha": "^7.0.2", "chai": "^4.2.0", @@ -28,9 +28,9 @@ "del": "^5.1.0", "filesize": "^6.1.0", "glob": "^7.1.6", - "mocha": "^7.1.1", - "prettier": "^2.0.2", - "rollup": "^2.2.0", + "mocha": "^7.1.2", + "prettier": "^2.0.5", + "rollup": "^2.9.1", "rollup-plugin-node-resolve": "^5.2.0", "rollup-plugin-terser": "^5.3.0", "typescript": "^3.8.3" diff --git a/src/database-extras.ts b/src/database-extras.ts index 6d53c23..b66be58 100644 --- a/src/database-extras.ts +++ b/src/database-extras.ts @@ -43,7 +43,7 @@ function getMethod( const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly'); let target: IDBPObjectStore | IDBPIndex = tx.store; if (useIndex) target = target.index(args.shift()); - const returnVal = (target as any)[targetFuncName](...args); + const returnVal = await (target as any)[targetFuncName](...args); if (isWrite) await tx.done; return returnVal; }; diff --git a/test/iterate.ts b/test/iterate.ts index 6aad17b..99bfa34 100644 --- a/test/iterate.ts +++ b/test/iterate.ts @@ -2,7 +2,7 @@ // library that behave differently to IDB, or may cause accidental differences. import 'mocha/mocha'; -import { assert } from 'chai'; +import chai from 'chai/chai'; import { IDBPDatabase, IDBPCursorWithValueIteratorValue } from '../src/'; import '../src/async-iterators'; import { assert as typeAssert, IsExact } from 'conditional-type-checks'; @@ -13,6 +13,8 @@ import { ObjectStoreValue, } from './utils'; +const { assert } = chai; + suite('Async iterators', () => { let db: IDBPDatabase; diff --git a/test/main.ts b/test/main.ts index 978f60c..b5fcda2 100644 --- a/test/main.ts +++ b/test/main.ts @@ -1,5 +1,5 @@ import 'mocha/mocha'; -import { assert } from 'chai'; +import chai from 'chai/chai'; import { IDBPDatabase, IDBPTransaction, @@ -24,6 +24,8 @@ import { dbName, } from './utils'; +const { assert } = chai; + suite('IDBPDatabase', () => { let db: IDBPDatabase; @@ -513,6 +515,28 @@ suite('IDBPDatabase', () => { ); }); + test('add - error type', async () => { + const schemaDB = await openDBWithData(); + db = schemaDB as IDBPDatabase; + + // This test ensures the shortcut methods correctly pass-through the + // error generated by the operation, rather then deferring to the + // transaction abort. + try { + await schemaDB.add('object-store', { + title: 'Foo', + date: new Date(), + id: 1, + }); + const error = new Error(`Didn't throw`); + error.name = 'DidntThrowError'; + throw error; + } catch (error) { + assert.instanceOf(error, DOMException); + assert.strictEqual(error.name, 'ConstraintError'); + } + }); + test('delete', async () => { const schemaDB = await openDBWithData(); db = schemaDB as IDBPDatabase; diff --git a/test/missing-types.d.ts b/test/missing-types.d.ts new file mode 100644 index 0000000..f56b570 --- /dev/null +++ b/test/missing-types.d.ts @@ -0,0 +1,4 @@ +declare module 'chai/chai' { + var chai: typeof import('chai'); + export default chai; +} diff --git a/test/open.ts b/test/open.ts index a416357..ab844e2 100644 --- a/test/open.ts +++ b/test/open.ts @@ -1,5 +1,5 @@ import 'mocha/mocha'; -import { assert } from 'chai'; +import chai from 'chai/chai'; import { openDB, IDBPDatabase, IDBPTransaction, wrap, unwrap } from '../src/'; import { assert as typeAssert, IsExact } from 'conditional-type-checks'; import { @@ -10,6 +10,8 @@ import { deleteDatabase, } from './utils'; +const { assert } = chai; + suite('openDb', () => { let db: IDBPDatabase; diff --git a/tsconfig.json b/tsconfig.json index 29adf7a..abc8b2b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,5 +10,5 @@ "declarationDir": "./build/esm/", "outDir": "./build/esm/" }, - "files": ["lib/index.ts", "lib/async-iterators.ts"] + "files": ["src/index.ts", "src/async-iterators.ts"] }