Comparing version 4.0.4 to 4.0.5
@@ -43,5 +43,5 @@ 'use strict'; | ||
function isIteratorProp(target, prop) { | ||
return (prop === Symbol.asyncIterator && | ||
__chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || (prop === 'iterate' && | ||
__chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore])); | ||
return ((prop === Symbol.asyncIterator && | ||
__chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || | ||
(prop === 'iterate' && __chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore]))); | ||
} | ||
@@ -48,0 +48,0 @@ __chunk_1.addTraps(oldTraps => ({ |
@@ -9,12 +9,19 @@ 'use strict'; | ||
function getIdbProxyableTypes() { | ||
return idbProxyableTypes || | ||
(idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]); | ||
return (idbProxyableTypes || | ||
(idbProxyableTypes = [ | ||
IDBDatabase, | ||
IDBObjectStore, | ||
IDBIndex, | ||
IDBCursor, | ||
IDBTransaction, | ||
])); | ||
} | ||
// This is a function to prevent it throwing up in node environments. | ||
function getCursorAdvanceMethods() { | ||
return cursorAdvanceMethods || (cursorAdvanceMethods = [ | ||
IDBCursor.prototype.advance, | ||
IDBCursor.prototype.continue, | ||
IDBCursor.prototype.continuePrimaryKey, | ||
]); | ||
return (cursorAdvanceMethods || | ||
(cursorAdvanceMethods = [ | ||
IDBCursor.prototype.advance, | ||
IDBCursor.prototype.continue, | ||
IDBCursor.prototype.continuePrimaryKey, | ||
])); | ||
} | ||
@@ -43,3 +50,4 @@ const cursorRequestMap = new WeakMap(); | ||
}); | ||
promise.then((value) => { | ||
promise | ||
.then(value => { | ||
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval | ||
@@ -51,3 +59,4 @@ // (see wrapFunction). | ||
// Catching to avoid "Uncaught Promise exceptions" | ||
}).catch(() => { }); | ||
}) | ||
.catch(() => { }); | ||
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This | ||
@@ -95,4 +104,5 @@ // is because we create many promises from a single IDBRequest. | ||
if (prop === 'store') { | ||
return receiver.objectStoreNames[1] ? | ||
undefined : receiver.objectStore(receiver.objectStoreNames[0]); | ||
return receiver.objectStoreNames[1] | ||
? undefined | ||
: receiver.objectStore(receiver.objectStoreNames[0]); | ||
} | ||
@@ -104,4 +114,6 @@ } | ||
has(target, prop) { | ||
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store')) | ||
if (target instanceof IDBTransaction && | ||
(prop === 'done' || prop === 'store')) { | ||
return true; | ||
} | ||
return prop in target; | ||
@@ -108,0 +120,0 @@ }, |
@@ -18,3 +18,3 @@ 'use strict'; | ||
if (upgrade) { | ||
request.addEventListener('upgradeneeded', (event) => { | ||
request.addEventListener('upgradeneeded', event => { | ||
upgrade(__chunk_1.wrap(request.result), event.oldVersion, event.newVersion, __chunk_1.wrap(request.transaction)); | ||
@@ -25,4 +25,5 @@ }); | ||
request.addEventListener('blocked', () => blocked()); | ||
if (blocking) | ||
openPromise.then(db => db.addEventListener('versionchange', blocking)); | ||
if (blocking) { | ||
openPromise.then(db => db.addEventListener('versionchange', blocking)).catch(() => { }); | ||
} | ||
return openPromise; | ||
@@ -48,4 +49,5 @@ } | ||
!(prop in target) && | ||
typeof prop === 'string')) | ||
typeof prop === 'string')) { | ||
return; | ||
} | ||
if (cachedMethods.get(prop)) | ||
@@ -59,4 +61,5 @@ return cachedMethods.get(prop); | ||
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) || | ||
!(isWrite || readMethods.includes(targetFuncName))) | ||
!(isWrite || readMethods.includes(targetFuncName))) { | ||
return; | ||
} | ||
const method = async function (storeName, ...args) { | ||
@@ -63,0 +66,0 @@ // isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :( |
@@ -41,5 +41,5 @@ import { b as addTraps, c as instanceOfAny, d as reverseTransformCache, e as unwrap } from './chunk.js'; | ||
function isIteratorProp(target, prop) { | ||
return (prop === Symbol.asyncIterator && | ||
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || (prop === 'iterate' && | ||
instanceOfAny(target, [IDBIndex, IDBObjectStore])); | ||
return ((prop === Symbol.asyncIterator && | ||
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || | ||
(prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore]))); | ||
} | ||
@@ -46,0 +46,0 @@ addTraps(oldTraps => ({ |
@@ -7,12 +7,19 @@ const instanceOfAny = (object, constructors) => constructors.some(c => object instanceof c); | ||
function getIdbProxyableTypes() { | ||
return idbProxyableTypes || | ||
(idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]); | ||
return (idbProxyableTypes || | ||
(idbProxyableTypes = [ | ||
IDBDatabase, | ||
IDBObjectStore, | ||
IDBIndex, | ||
IDBCursor, | ||
IDBTransaction, | ||
])); | ||
} | ||
// This is a function to prevent it throwing up in node environments. | ||
function getCursorAdvanceMethods() { | ||
return cursorAdvanceMethods || (cursorAdvanceMethods = [ | ||
IDBCursor.prototype.advance, | ||
IDBCursor.prototype.continue, | ||
IDBCursor.prototype.continuePrimaryKey, | ||
]); | ||
return (cursorAdvanceMethods || | ||
(cursorAdvanceMethods = [ | ||
IDBCursor.prototype.advance, | ||
IDBCursor.prototype.continue, | ||
IDBCursor.prototype.continuePrimaryKey, | ||
])); | ||
} | ||
@@ -41,3 +48,4 @@ const cursorRequestMap = new WeakMap(); | ||
}); | ||
promise.then((value) => { | ||
promise | ||
.then(value => { | ||
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval | ||
@@ -49,3 +57,4 @@ // (see wrapFunction). | ||
// Catching to avoid "Uncaught Promise exceptions" | ||
}).catch(() => { }); | ||
}) | ||
.catch(() => { }); | ||
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This | ||
@@ -93,4 +102,5 @@ // is because we create many promises from a single IDBRequest. | ||
if (prop === 'store') { | ||
return receiver.objectStoreNames[1] ? | ||
undefined : receiver.objectStore(receiver.objectStoreNames[0]); | ||
return receiver.objectStoreNames[1] | ||
? undefined | ||
: receiver.objectStore(receiver.objectStoreNames[0]); | ||
} | ||
@@ -102,4 +112,6 @@ } | ||
has(target, prop) { | ||
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store')) | ||
if (target instanceof IDBTransaction && | ||
(prop === 'done' || prop === 'store')) { | ||
return true; | ||
} | ||
return prop in target; | ||
@@ -106,0 +118,0 @@ }, |
@@ -15,3 +15,3 @@ import { a as wrap, b as addTraps } from './chunk.js'; | ||
if (upgrade) { | ||
request.addEventListener('upgradeneeded', (event) => { | ||
request.addEventListener('upgradeneeded', event => { | ||
upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction)); | ||
@@ -22,4 +22,5 @@ }); | ||
request.addEventListener('blocked', () => blocked()); | ||
if (blocking) | ||
openPromise.then(db => db.addEventListener('versionchange', blocking)); | ||
if (blocking) { | ||
openPromise.then(db => db.addEventListener('versionchange', blocking)).catch(() => { }); | ||
} | ||
return openPromise; | ||
@@ -45,4 +46,5 @@ } | ||
!(prop in target) && | ||
typeof prop === 'string')) | ||
typeof prop === 'string')) { | ||
return; | ||
} | ||
if (cachedMethods.get(prop)) | ||
@@ -56,4 +58,5 @@ return cachedMethods.get(prop); | ||
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) || | ||
!(isWrite || readMethods.includes(targetFuncName))) | ||
!(isWrite || readMethods.includes(targetFuncName))) { | ||
return; | ||
} | ||
const method = async function (storeName, ...args) { | ||
@@ -60,0 +63,0 @@ // isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :( |
@@ -1,1 +0,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 p(e[t])},has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function d(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(l(this),t),p(o.get(this))}:function(...t){return p(e.apply(l(this),t))}:function(t,...n){const r=e.call(l(this),t,...n);return a.set(r,t.sort?t.sort():[t]),p(r)}}function f(e){return"function"==typeof e?d(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),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 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&&o.set(t,e)}).catch(()=>{}),c.set(t,e),t}(e);if(i.has(e))return i.get(e);const t=f(e);return t!==e&&(i.set(e,t),c.set(t,e)),t}const l=e=>c.get(e);const D=["get","getKey","getAll","getAllKeys","count"],v=["put","add","delete","clear"],B=new Map;function I(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=v.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!D.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}return u=(e=>({get:(t,n,r)=>I(t,n)||e.get(t,n,r),has:(t,n)=>!!I(t,n)||e.has(t,n)}))(u),e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o}={}){const s=indexedDB.open(e,t),a=p(s);return r&&s.addEventListener("upgradeneeded",e=>{r(p(s.result),e.oldVersion,e.newVersion,p(s.transaction))}),n&&s.addEventListener("blocked",()=>n()),o&&a.then(e=>e.addEventListener("versionchange",o)),a},e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),p(n).then(()=>void 0)},e.unwrap=l,e.wrap=p,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 p(e[t])},has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function d(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(l(this),t),p(o.get(this))}:function(...t){return p(e.apply(l(this),t))}:function(t,...n){const r=e.call(l(this),t,...n);return a.set(r,t.sort?t.sort():[t]),p(r)}}function f(e){return"function"==typeof e?d(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),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 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&&o.set(t,e)}).catch(()=>{}),c.set(t,e),t}(e);if(i.has(e))return i.get(e);const t=f(e);return t!==e&&(i.set(e,t),c.set(t,e)),t}const l=e=>c.get(e);const D=["get","getKey","getAll","getAllKeys","count"],v=["put","add","delete","clear"],B=new Map;function I(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=v.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!D.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}return u=(e=>({get:(t,n,r)=>I(t,n)||e.get(t,n,r),has:(t,n)=>!!I(t,n)||e.has(t,n)}))(u),e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o}={}){const s=indexedDB.open(e,t),a=p(s);return r&&s.addEventListener("upgradeneeded",e=>{r(p(s.result),e.oldVersion,e.newVersion,p(s.transaction))}),n&&s.addEventListener("blocked",()=>n()),o&&a.then(e=>e.addEventListener("versionchange",o)).catch(()=>{}),a},e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),p(n).then(()=>void 0)},e.unwrap=l,e.wrap=p,e}({}); |
@@ -1,1 +0,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])},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),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"],v=new Map;function g(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=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 v.set(t,s),s}d(e=>({get:(t,n,r)=>g(t,n)||e.get(t,n,r),has:(t,n)=>!!g(t,n)||e.has(t,n)}));const y=["continue","continuePrimaryKey","advance"],b={},h=new WeakMap,m=new WeakMap,w={get(e,t){if(!y.includes(t))return e[t];let n=b[t];return n||(n=b[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=>({get:(t,n,r)=>L(t,n)?E:e.get(t,n,r),has:(t,n)=>L(t,n)||e.has(t,n)})),e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o}={}){const s=indexedDB.open(e,t),a=l(s);return r&&s.addEventListener("upgradeneeded",e=>{r(l(s.result),e.oldVersion,e.newVersion,l(s.transaction))}),n&&s.addEventListener("blocked",()=>n()),o&&a.then(e=>e.addEventListener("versionchange",o)),a},e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),l(n).then(()=>void 0)},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])},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),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"],v=new Map;function g(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=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 v.set(t,s),s}d(e=>({get:(t,n,r)=>g(t,n)||e.get(t,n,r),has:(t,n)=>!!g(t,n)||e.has(t,n)}));const y=["continue","continuePrimaryKey","advance"],b={},h=new WeakMap,m=new WeakMap,w={get(e,t){if(!y.includes(t))return e[t];let n=b[t];return n||(n=b[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=>({get:(t,n,r)=>L(t,n)?E:e.get(t,n,r),has:(t,n)=>L(t,n)||e.has(t,n)})),e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o}={}){const s=indexedDB.open(e,t),a=l(s);return r&&s.addEventListener("upgradeneeded",e=>{r(l(s.result),e.oldVersion,e.newVersion,l(s.transaction))}),n&&s.addEventListener("blocked",()=>n()),o&&a.then(e=>e.addEventListener("versionchange",o)).catch(()=>{}),a},e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",()=>t()),l(n).then(()=>void 0)},e.unwrap=D,e.wrap=l,e}({}); |
@@ -11,3 +11,3 @@ # Changes in 4.x | ||
openDb('db-name', 1, (upgradeDb) => { | ||
openDb('db-name', 1, upgradeDb => { | ||
console.log(upgradeDb.oldVersion); | ||
@@ -30,5 +30,5 @@ console.log(upgradeDb.transaction); | ||
* `openDb` and `deleteDb` were renamed `openDB` and `deleteDB` to be more consistent with DOM naming. | ||
* The signature of `openDB` changed. The third parameter used to be the upgrade callback, it's now an option object which can include an `upgrade` method. | ||
* There's no `UpgradeDB` anymore. You get the same database `openDB` resolves with. Versions numbers and the upgrade transaction are included as additional parameters. | ||
- `openDb` and `deleteDb` were renamed `openDB` and `deleteDB` to be more consistent with DOM naming. | ||
- The signature of `openDB` changed. The third parameter used to be the upgrade callback, it's now an option object which can include an `upgrade` method. | ||
- There's no `UpgradeDB` anymore. You get the same database `openDB` resolves with. Versions numbers and the upgrade transaction are included as additional parameters. | ||
@@ -45,16 +45,16 @@ ### Promises & throwing | ||
* `iterateCursor` and `iterateKeyCursor` have been removed. These existed to work around browsers microtask issues which have since been fixed. Async iterators provide similar functionality. | ||
* All pseudo-private properties (those beginning with an underscore) are gone. Use `unwrap()` to get access to bare IDB objects. | ||
* `transaction.complete` was renamed to `transaction.done` to be shorter and more consistent with the DOM. | ||
* `getAll` is no longer polyfilled on indexes and stores. | ||
* The library no longer officially supports IE11. | ||
- `iterateCursor` and `iterateKeyCursor` have been removed. These existed to work around browsers microtask issues which have since been fixed. Async iterators provide similar functionality. | ||
- All pseudo-private properties (those beginning with an underscore) are gone. Use `unwrap()` to get access to bare IDB objects. | ||
- `transaction.complete` was renamed to `transaction.done` to be shorter and more consistent with the DOM. | ||
- `getAll` is no longer polyfilled on indexes and stores. | ||
- The library no longer officially supports IE11. | ||
## New stuff | ||
* The library now uses proxies, so objects will include everything from their plain-IDB equivalents. | ||
* TypeScript support has massively improved, including the ability to provide types for your database. | ||
* Optional support for async iterators, which makes handling cursors much easier. | ||
* Database objects now have shortcuts for single actions (like `get`, `put`, `add`, `getAll` etc etc). | ||
* For transactions that cover a single store `transaction.store` is a reference to that store. | ||
* `openDB` lets you add callbacks for when your database is blocking another connection, or when you're blocked by another connection. | ||
- The library now uses proxies, so objects will include everything from their plain-IDB equivalents. | ||
- TypeScript support has massively improved, including the ability to provide types for your database. | ||
- Optional support for async iterators, which makes handling cursors much easier. | ||
- Database objects now have shortcuts for single actions (like `get`, `put`, `add`, `getAll` etc etc). | ||
- For transactions that cover a single store `transaction.store` is a reference to that store. | ||
- `openDB` lets you add callbacks for when your database is blocking another connection, or when you're blocked by another connection. | ||
@@ -61,0 +61,0 @@ # Changes in 3.x |
@@ -17,3 +17,6 @@ import { instanceOfAny, Func } from './util'; | ||
if (!cachedFunc) { | ||
cachedFunc = methodMap[prop as string] = function (this: IDBPCursor, ...args: any) { | ||
cachedFunc = methodMap[prop as string] = function( | ||
this: IDBPCursor, | ||
...args: any | ||
) { | ||
advanceResults.set( | ||
@@ -30,4 +33,6 @@ this, | ||
async function* iterate(this: IDBPObjectStore | IDBPIndex | IDBPCursor, ...args: any[]): | ||
AsyncIterableIterator<any> { | ||
async function* iterate( | ||
this: IDBPObjectStore | IDBPIndex | IDBPCursor, | ||
...args: any[] | ||
): AsyncIterableIterator<any> { | ||
// tslint:disable-next-line:no-this-assignment | ||
@@ -58,7 +63,5 @@ let cursor: typeof this | null = this; | ||
return ( | ||
prop === Symbol.asyncIterator && | ||
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor]) | ||
) || ( | ||
prop === 'iterate' && | ||
instanceOfAny(target, [IDBIndex, IDBObjectStore]) | ||
(prop === Symbol.asyncIterator && | ||
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || | ||
(prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])) | ||
); | ||
@@ -65,0 +68,0 @@ } |
@@ -9,8 +9,15 @@ import { Func } from './util'; | ||
function getMethod(target: any, prop: string | number | symbol): Func | undefined { | ||
if (!( | ||
target instanceof IDBDatabase && | ||
!(prop in target) && | ||
typeof prop === 'string' | ||
)) return; | ||
function getMethod( | ||
target: any, | ||
prop: string | number | symbol, | ||
): Func | undefined { | ||
if ( | ||
!( | ||
target instanceof IDBDatabase && | ||
!(prop in target) && | ||
typeof prop === 'string' | ||
) | ||
) { | ||
return; | ||
} | ||
@@ -27,5 +34,11 @@ if (cachedMethods.get(prop)) return cachedMethods.get(prop); | ||
!(isWrite || readMethods.includes(targetFuncName)) | ||
) return; | ||
) { | ||
return; | ||
} | ||
const method = async function (this: IDBPDatabase, storeName: string, ...args: any[]) { | ||
const method = async function( | ||
this: IDBPDatabase, | ||
storeName: string, | ||
...args: any[] | ||
) { | ||
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :( | ||
@@ -45,4 +58,6 @@ const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly'); | ||
addTraps(oldTraps => ({ | ||
get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get!(target, prop, receiver), | ||
has: (target, prop) => !!getMethod(target, prop) || oldTraps.has!(target, prop), | ||
get: (target, prop, receiver) => | ||
getMethod(target, prop) || oldTraps.get!(target, prop, receiver), | ||
has: (target, prop) => | ||
!!getMethod(target, prop) || oldTraps.has!(target, prop), | ||
})); |
390
lib/entry.ts
@@ -39,3 +39,5 @@ import { wrap } from './wrap-idb-value'; | ||
export function openDB<DBTypes extends DBSchema | unknown = unknown>( | ||
name: string, version: number, { blocked, upgrade, blocking }: OpenDBCallbacks<DBTypes> = {}, | ||
name: string, | ||
version: number, | ||
{ blocked, upgrade, blocking }: OpenDBCallbacks<DBTypes> = {}, | ||
): Promise<IDBPDatabase<DBTypes>> { | ||
@@ -46,3 +48,3 @@ const request = indexedDB.open(name, version); | ||
if (upgrade) { | ||
request.addEventListener('upgradeneeded', (event) => { | ||
request.addEventListener('upgradeneeded', event => { | ||
upgrade( | ||
@@ -52,3 +54,3 @@ wrap(request.result) as IDBPDatabase<DBTypes>, | ||
event.newVersion, | ||
wrap(request.transaction!) as unknown as IDBPTransaction<DBTypes>, | ||
(wrap(request.transaction!) as unknown) as IDBPTransaction<DBTypes>, | ||
); | ||
@@ -59,3 +61,5 @@ }); | ||
if (blocked) request.addEventListener('blocked', () => blocked()); | ||
if (blocking) openPromise.then(db => db.addEventListener('versionchange', blocking)); | ||
if (blocking) { | ||
openPromise.then(db => db.addEventListener('versionchange', blocking)).catch(() => {}); | ||
} | ||
@@ -77,3 +81,6 @@ return openPromise; | ||
*/ | ||
export function deleteDB(name: string, { blocked }: DeleteDBCallbacks = {}): Promise<void> { | ||
export function deleteDB( | ||
name: string, | ||
{ blocked }: DeleteDBCallbacks = {}, | ||
): Promise<void> { | ||
const request = indexedDB.deleteDatabase(name); | ||
@@ -88,4 +95,6 @@ if (blocked) request.addEventListener('blocked', () => blocked()); | ||
type KnownKeys<T> = { | ||
[K in keyof T]: string extends K ? never : number extends K ? never : K | ||
} extends { [_ in keyof T]: infer U } ? U : never; | ||
[K in keyof T]: string extends K ? never : number extends K ? never : K; | ||
} extends { [_ in keyof T]: infer U } | ||
? U | ||
: never; | ||
@@ -113,4 +122,5 @@ type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
*/ | ||
export type StoreNames<DBTypes extends DBSchema | unknown> = | ||
DBTypes extends DBSchema ? KnownKeys<DBTypes> : string; | ||
export type StoreNames< | ||
DBTypes extends DBSchema | unknown | ||
> = DBTypes extends DBSchema ? KnownKeys<DBTypes> : string; | ||
@@ -123,4 +133,6 @@ /** | ||
*/ | ||
export type StoreValue<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = | ||
DBTypes extends DBSchema ? DBTypes[StoreName]['value'] : any; | ||
export type StoreValue< | ||
DBTypes extends DBSchema | unknown, | ||
StoreName extends StoreNames<DBTypes> | ||
> = DBTypes extends DBSchema ? DBTypes[StoreName]['value'] : any; | ||
@@ -133,4 +145,6 @@ /** | ||
*/ | ||
export type StoreKey<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = | ||
DBTypes extends DBSchema ? DBTypes[StoreName]['key'] : IDBValidKey; | ||
export type StoreKey< | ||
DBTypes extends DBSchema | unknown, | ||
StoreName extends StoreNames<DBTypes> | ||
> = DBTypes extends DBSchema ? DBTypes[StoreName]['key'] : IDBValidKey; | ||
@@ -143,4 +157,6 @@ /** | ||
*/ | ||
export type IndexNames<DBTypes extends DBSchema | unknown, StoreName extends StoreNames<DBTypes>> = | ||
DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] : string; | ||
export type IndexNames< | ||
DBTypes extends DBSchema | unknown, | ||
StoreName extends StoreNames<DBTypes> | ||
> = DBTypes extends DBSchema ? keyof DBTypes[StoreName]['indexes'] : string; | ||
@@ -157,5 +173,8 @@ /** | ||
StoreName extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName>, | ||
> = DBTypes extends DBSchema ? IndexName extends keyof DBTypes[StoreName]['indexes'] ? | ||
DBTypes[StoreName]['indexes'][IndexName] : IDBValidKey : IDBValidKey; | ||
IndexName extends IndexNames<DBTypes, StoreName> | ||
> = DBTypes extends DBSchema | ||
? IndexName extends keyof DBTypes[StoreName]['indexes'] | ||
? DBTypes[StoreName]['indexes'][IndexName] | ||
: IDBValidKey | ||
: IDBValidKey; | ||
@@ -166,6 +185,6 @@ type CursorSource< | ||
StoreName extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown, | ||
> = IndexName extends IndexNames<DBTypes, StoreName> ? | ||
IDBPIndex<DBTypes, TxStores, StoreName, IndexName> : | ||
IDBPObjectStore<DBTypes, TxStores, StoreName>; | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown | ||
> = IndexName extends IndexNames<DBTypes, StoreName> | ||
? IDBPIndex<DBTypes, TxStores, StoreName, IndexName> | ||
: IDBPObjectStore<DBTypes, TxStores, StoreName>; | ||
@@ -175,6 +194,6 @@ type CursorKey< | ||
StoreName extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown, | ||
> = IndexName extends IndexNames<DBTypes, StoreName> ? | ||
IndexKey<DBTypes, StoreName, IndexName> : | ||
StoreKey<DBTypes, StoreName>; | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown | ||
> = IndexName extends IndexNames<DBTypes, StoreName> | ||
? IndexKey<DBTypes, StoreName, IndexName> | ||
: StoreKey<DBTypes, StoreName>; | ||
@@ -189,14 +208,11 @@ type IDBPDatabaseExtends = Omit< | ||
*/ | ||
export interface TypedDOMStringList< | ||
T extends string | ||
> extends DOMStringList { | ||
contains(string: T): boolean; | ||
item(index: number): T | null; | ||
[index: number]: T; | ||
[Symbol.iterator](): IterableIterator<T>; | ||
export interface TypedDOMStringList<T extends string> extends DOMStringList { | ||
contains(string: T): boolean; | ||
item(index: number): T | null; | ||
[index: number]: T; | ||
[Symbol.iterator](): IterableIterator<T>; | ||
} | ||
export interface IDBPDatabase< | ||
DBTypes extends DBSchema | unknown = unknown, | ||
> extends IDBPDatabaseExtends { | ||
export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown> | ||
extends IDBPDatabaseExtends { | ||
/** | ||
@@ -211,5 +227,6 @@ * The names of stores in the database. | ||
*/ | ||
createObjectStore<Name extends StoreNames<DBTypes>> | ||
(name: Name, optionalParameters?: IDBObjectStoreParameters): | ||
IDBPObjectStore<DBTypes, StoreNames<DBTypes>[], Name>; | ||
createObjectStore<Name extends StoreNames<DBTypes>>( | ||
name: Name, | ||
optionalParameters?: IDBObjectStoreParameters, | ||
): IDBPObjectStore<DBTypes, StoreNames<DBTypes>[], Name>; | ||
/** | ||
@@ -227,8 +244,10 @@ * Deletes the object store with the given name. | ||
*/ | ||
transaction<Name extends StoreNames<DBTypes>> | ||
(storeNames: Name, mode?: IDBTransactionMode): | ||
IDBPTransaction<DBTypes, [Name]>; | ||
transaction<Names extends StoreNames<DBTypes>[]> | ||
(storeNames: Names, mode?: IDBTransactionMode): | ||
IDBPTransaction<DBTypes, Names>; | ||
transaction<Name extends StoreNames<DBTypes>>( | ||
storeNames: Name, | ||
mode?: IDBTransactionMode, | ||
): IDBPTransaction<DBTypes, [Name]>; | ||
transaction<Names extends StoreNames<DBTypes>[]>( | ||
storeNames: Names, | ||
mode?: IDBTransactionMode, | ||
): IDBPTransaction<DBTypes, Names>; | ||
@@ -286,3 +305,6 @@ // Shortcut methods | ||
*/ | ||
countFromIndex<Name extends StoreNames<DBTypes>, IndexName extends IndexNames<DBTypes, Name>>( | ||
countFromIndex< | ||
Name extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, Name> | ||
>( | ||
storeName: Name, | ||
@@ -332,3 +354,6 @@ indexName: IndexName, | ||
*/ | ||
getFromIndex<Name extends StoreNames<DBTypes>, IndexName extends IndexNames<DBTypes, Name>>( | ||
getFromIndex< | ||
Name extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, Name> | ||
>( | ||
storeName: Name, | ||
@@ -364,3 +389,6 @@ indexName: IndexName, | ||
*/ | ||
getAllFromIndex<Name extends StoreNames<DBTypes>, IndexName extends IndexNames<DBTypes, Name>>( | ||
getAllFromIndex< | ||
Name extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, Name> | ||
>( | ||
storeName: Name, | ||
@@ -397,9 +425,11 @@ indexName: IndexName, | ||
*/ | ||
getAllKeysFromIndex | ||
<Name extends StoreNames<DBTypes>, IndexName extends IndexNames<DBTypes, Name>>( | ||
storeName: Name, | ||
indexName: IndexName, | ||
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange, | ||
count?: number, | ||
): Promise<StoreKey<DBTypes, Name>[]>; | ||
getAllKeysFromIndex< | ||
Name extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, Name> | ||
>( | ||
storeName: Name, | ||
indexName: IndexName, | ||
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange, | ||
count?: number, | ||
): Promise<StoreKey<DBTypes, Name>[]>; | ||
/** | ||
@@ -432,3 +462,6 @@ * Retrieves the key of the first record in a store that matches the query. | ||
*/ | ||
getKeyFromIndex<Name extends StoreNames<DBTypes>, IndexName extends IndexNames<DBTypes, Name>>( | ||
getKeyFromIndex< | ||
Name extends StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, Name> | ||
>( | ||
storeName: Name, | ||
@@ -464,3 +497,3 @@ indexName: IndexName, | ||
DBTypes extends DBSchema | unknown = unknown, | ||
TxStores extends StoreNames<DBTypes>[] = StoreNames<DBTypes>[], | ||
TxStores extends StoreNames<DBTypes>[] = StoreNames<DBTypes>[] | ||
> extends IDBPTransactionExtends { | ||
@@ -482,11 +515,11 @@ /** | ||
*/ | ||
readonly store: TxStores[1] extends undefined ? | ||
IDBPObjectStore<DBTypes, TxStores, TxStores[0]> : undefined; | ||
readonly store: TxStores[1] extends undefined | ||
? IDBPObjectStore<DBTypes, TxStores, TxStores[0]> | ||
: undefined; | ||
/** | ||
* Returns an IDBObjectStore in the transaction's scope. | ||
*/ | ||
objectStore | ||
<StoreName extends TxStores[number]> | ||
(name: StoreName): | ||
IDBPObjectStore<DBTypes, TxStores, StoreName>; | ||
objectStore<StoreName extends TxStores[number]>( | ||
name: StoreName, | ||
): IDBPObjectStore<DBTypes, TxStores, StoreName>; | ||
} | ||
@@ -496,5 +529,18 @@ | ||
IDBObjectStore, | ||
'transaction' | 'add' | 'clear' | 'count' | 'createIndex' | 'delete' | 'get' | 'getAll' | | ||
'getAllKeys' | 'getKey' | 'index' | 'openCursor' | 'openKeyCursor' | 'put' | 'indexNames' | | ||
'name' | ||
| 'transaction' | ||
| 'add' | ||
| 'clear' | ||
| 'count' | ||
| 'createIndex' | ||
| 'delete' | ||
| 'get' | ||
| 'getAll' | ||
| 'getAllKeys' | ||
| 'getKey' | ||
| 'index' | ||
| 'openCursor' | ||
| 'openKeyCursor' | ||
| 'put' | ||
| 'indexNames' | ||
| 'name' | ||
>; | ||
@@ -505,3 +551,3 @@ | ||
TxStores extends StoreNames<DBTypes>[] = StoreNames<DBTypes>[], | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes> | ||
> extends IDBPObjectStoreExtends { | ||
@@ -525,4 +571,6 @@ /** | ||
*/ | ||
add(value: StoreValue<DBTypes, StoreName>, key?: StoreKey<DBTypes, StoreName> | IDBKeyRange): | ||
Promise<StoreKey<DBTypes, StoreName>>; | ||
add( | ||
value: StoreValue<DBTypes, StoreName>, | ||
key?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
): Promise<StoreKey<DBTypes, StoreName>>; | ||
/** | ||
@@ -541,6 +589,7 @@ * Deletes all records in store. | ||
*/ | ||
createIndex | ||
<IndexName extends IndexNames<DBTypes, StoreName>> | ||
(name: IndexName, keyPath: string | string[], options?: IDBIndexParameters): | ||
IDBPIndex<DBTypes, TxStores, StoreName, IndexName>; | ||
createIndex<IndexName extends IndexNames<DBTypes, StoreName>>( | ||
name: IndexName, | ||
keyPath: string | string[], | ||
options?: IDBIndexParameters, | ||
): IDBPIndex<DBTypes, TxStores, StoreName, IndexName>; | ||
/** | ||
@@ -555,4 +604,5 @@ * Deletes records in store matching the given query. | ||
*/ | ||
get(query: StoreKey<DBTypes, StoreName> | IDBKeyRange): | ||
Promise<StoreValue<DBTypes, StoreName> | undefined>; | ||
get( | ||
query: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
): Promise<StoreValue<DBTypes, StoreName> | undefined>; | ||
/** | ||
@@ -564,4 +614,6 @@ * Retrieves all values that match the query. | ||
*/ | ||
getAll(query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, count?: number): | ||
Promise<StoreValue<DBTypes, StoreName>[]>; | ||
getAll( | ||
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
count?: number, | ||
): Promise<StoreValue<DBTypes, StoreName>[]>; | ||
/** | ||
@@ -573,4 +625,6 @@ * Retrieves the keys of records matching the query. | ||
*/ | ||
getAllKeys(query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, count?: number): | ||
Promise<StoreKey<DBTypes, StoreName>[]>; | ||
getAllKeys( | ||
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
count?: number, | ||
): Promise<StoreKey<DBTypes, StoreName>[]>; | ||
/** | ||
@@ -581,9 +635,11 @@ * Retrieves the key of the first record that matches the query. | ||
*/ | ||
getKey(query: StoreKey<DBTypes, StoreName> | IDBKeyRange): | ||
Promise<StoreKey<DBTypes, StoreName> | undefined>; | ||
getKey( | ||
query: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
): Promise<StoreKey<DBTypes, StoreName> | undefined>; | ||
/** | ||
* Get a query of a given name. | ||
*/ | ||
index<IndexName extends IndexNames<DBTypes, StoreName>>(name: IndexName): | ||
IDBPIndex<DBTypes, TxStores, StoreName, IndexName>; | ||
index<IndexName extends IndexNames<DBTypes, StoreName>>( | ||
name: IndexName, | ||
): IDBPIndex<DBTypes, TxStores, StoreName, IndexName>; | ||
/** | ||
@@ -597,4 +653,6 @@ * Opens a cursor over the records matching the query. | ||
*/ | ||
openCursor(query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, direction?: IDBCursorDirection): | ||
Promise<IDBPCursorWithValue<DBTypes, TxStores, StoreName> | null>; | ||
openCursor( | ||
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
direction?: IDBCursorDirection, | ||
): Promise<IDBPCursorWithValue<DBTypes, TxStores, StoreName> | null>; | ||
/** | ||
@@ -608,4 +666,6 @@ * Opens a cursor over the keys matching the query. | ||
*/ | ||
openKeyCursor(query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, direction?: IDBCursorDirection): | ||
Promise<IDBPCursor<DBTypes, TxStores, StoreName> | null>; | ||
openKeyCursor( | ||
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
direction?: IDBCursorDirection, | ||
): Promise<IDBPCursor<DBTypes, TxStores, StoreName> | null>; | ||
/** | ||
@@ -616,9 +676,12 @@ * Put an item in the store. | ||
*/ | ||
put(value: StoreValue<DBTypes, StoreName>, key?: StoreKey<DBTypes, StoreName> | IDBKeyRange): | ||
Promise<StoreKey<DBTypes, StoreName>>; | ||
put( | ||
value: StoreValue<DBTypes, StoreName>, | ||
key?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
): Promise<StoreKey<DBTypes, StoreName>>; | ||
/** | ||
* Iterate over the store. | ||
*/ | ||
[Symbol.asyncIterator](): | ||
AsyncIterableIterator<IDBPCursorWithValueIteratorValue<DBTypes, TxStores, StoreName>>; | ||
[Symbol.asyncIterator](): AsyncIterableIterator< | ||
IDBPCursorWithValueIteratorValue<DBTypes, TxStores, StoreName> | ||
>; | ||
/** | ||
@@ -630,4 +693,8 @@ * Iterate over the records matching the query. | ||
*/ | ||
iterate(query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, direction?: IDBCursorDirection): | ||
AsyncIterableIterator<IDBPCursorWithValueIteratorValue<DBTypes, TxStores, StoreName>>; | ||
iterate( | ||
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange, | ||
direction?: IDBCursorDirection, | ||
): AsyncIterableIterator< | ||
IDBPCursorWithValueIteratorValue<DBTypes, TxStores, StoreName> | ||
>; | ||
} | ||
@@ -637,4 +704,10 @@ | ||
IDBIndex, | ||
'objectStore' | 'count' | 'get' | 'getAll' | 'getAllKeys' | 'getKey' | 'openCursor' | | ||
'openKeyCursor' | ||
| 'objectStore' | ||
| 'count' | ||
| 'get' | ||
| 'getAll' | ||
| 'getAllKeys' | ||
| 'getKey' | ||
| 'openCursor' | ||
| 'openKeyCursor' | ||
>; | ||
@@ -646,3 +719,6 @@ | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> = IndexNames<DBTypes, StoreName>, | ||
IndexName extends IndexNames<DBTypes, StoreName> = IndexNames< | ||
DBTypes, | ||
StoreName | ||
> | ||
> extends IDBPIndexExtends { | ||
@@ -657,3 +733,5 @@ /** | ||
*/ | ||
count(key?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange): Promise<number>; | ||
count( | ||
key?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, | ||
): Promise<number>; | ||
/** | ||
@@ -664,4 +742,5 @@ * Retrieves the value of the first record matching the query. | ||
*/ | ||
get(query: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange): | ||
Promise<StoreValue<DBTypes, StoreName> | undefined>; | ||
get( | ||
query: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, | ||
): Promise<StoreValue<DBTypes, StoreName> | undefined>; | ||
/** | ||
@@ -673,4 +752,6 @@ * Retrieves all values that match the query. | ||
*/ | ||
getAll(query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, count?: number): | ||
Promise<StoreValue<DBTypes, StoreName>[]>; | ||
getAll( | ||
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, | ||
count?: number, | ||
): Promise<StoreValue<DBTypes, StoreName>[]>; | ||
/** | ||
@@ -682,4 +763,6 @@ * Retrieves the keys of records matching the query. | ||
*/ | ||
getAllKeys(query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, count?: number): | ||
Promise<StoreKey<DBTypes, StoreName>[]>; | ||
getAllKeys( | ||
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, | ||
count?: number, | ||
): Promise<StoreKey<DBTypes, StoreName>[]>; | ||
/** | ||
@@ -690,4 +773,5 @@ * Retrieves the key of the first record that matches the query. | ||
*/ | ||
getKey(query: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange): | ||
Promise<StoreKey<DBTypes, StoreName> | undefined>; | ||
getKey( | ||
query: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange, | ||
): Promise<StoreKey<DBTypes, StoreName> | undefined>; | ||
/** | ||
@@ -704,3 +788,8 @@ * Opens a cursor over the records matching the query. | ||
direction?: IDBCursorDirection, | ||
): Promise<IDBPCursorWithValue<DBTypes, TxStores, StoreName, IndexName> | null>; | ||
): Promise<IDBPCursorWithValue< | ||
DBTypes, | ||
TxStores, | ||
StoreName, | ||
IndexName | ||
> | null>; | ||
/** | ||
@@ -742,4 +831,10 @@ * Opens a cursor over the keys matching the query. | ||
IDBCursor, | ||
'key' | 'primaryKey' | 'source' | 'advance' | 'continue' | 'continuePrimaryKey' | 'delete' | | ||
'update' | ||
| 'key' | ||
| 'primaryKey' | ||
| 'source' | ||
| 'advance' | ||
| 'continue' | ||
| 'continuePrimaryKey' | ||
| 'delete' | ||
| 'update' | ||
>; | ||
@@ -751,3 +846,3 @@ | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown | ||
> extends IDBPCursorExtends { | ||
@@ -779,4 +874,6 @@ /** | ||
*/ | ||
continue<T>(this: T, key?: CursorKey<DBTypes, StoreName, IndexName>): | ||
Promise<T | null>; | ||
continue<T>( | ||
this: T, | ||
key?: CursorKey<DBTypes, StoreName, IndexName>, | ||
): Promise<T | null>; | ||
/** | ||
@@ -792,9 +889,7 @@ * Advance the cursor by given keys. | ||
*/ | ||
continuePrimaryKey<T> | ||
( | ||
this: T, | ||
key: CursorKey<DBTypes, StoreName, IndexName>, | ||
primaryKey: StoreKey<DBTypes, StoreName>, | ||
): | ||
Promise<T | null>; | ||
continuePrimaryKey<T>( | ||
this: T, | ||
key: CursorKey<DBTypes, StoreName, IndexName>, | ||
primaryKey: StoreKey<DBTypes, StoreName>, | ||
): Promise<T | null>; | ||
/** | ||
@@ -807,8 +902,11 @@ * Delete the current record. | ||
*/ | ||
update(value: StoreValue<DBTypes, StoreName>): Promise<StoreKey<DBTypes, StoreName>>; | ||
update( | ||
value: StoreValue<DBTypes, StoreName>, | ||
): Promise<StoreKey<DBTypes, StoreName>>; | ||
/** | ||
* Iterate over the cursor. | ||
*/ | ||
[Symbol.asyncIterator](): | ||
AsyncIterableIterator<IDBPCursorIteratorValue<DBTypes, TxStores, StoreName, IndexName>>; | ||
[Symbol.asyncIterator](): AsyncIterableIterator< | ||
IDBPCursorIteratorValue<DBTypes, TxStores, StoreName, IndexName> | ||
>; | ||
} | ||
@@ -820,3 +918,3 @@ | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown | ||
> = Omit< | ||
@@ -831,4 +929,10 @@ IDBPCursor<DBTypes, TxStores, StoreName, IndexName>, | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown, | ||
> extends IDBPCursorIteratorValueExtends<DBTypes, TxStores, StoreName, IndexName> { | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown | ||
> | ||
extends IDBPCursorIteratorValueExtends< | ||
DBTypes, | ||
TxStores, | ||
StoreName, | ||
IndexName | ||
> { | ||
/** | ||
@@ -852,9 +956,7 @@ * Advances the cursor a given number of records. | ||
*/ | ||
continuePrimaryKey<T> | ||
( | ||
this: T, | ||
key: CursorKey<DBTypes, StoreName, IndexName>, | ||
primaryKey: StoreKey<DBTypes, StoreName>, | ||
): | ||
void; | ||
continuePrimaryKey<T>( | ||
this: T, | ||
key: CursorKey<DBTypes, StoreName, IndexName>, | ||
primaryKey: StoreKey<DBTypes, StoreName>, | ||
): void; | ||
} | ||
@@ -866,3 +968,3 @@ | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown | ||
> extends IDBPCursor<DBTypes, TxStores, StoreName, IndexName> { | ||
@@ -886,3 +988,3 @@ /** | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown | ||
> = Omit< | ||
@@ -897,4 +999,10 @@ IDBPCursorWithValue<DBTypes, TxStores, StoreName, IndexName>, | ||
StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>, | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown, | ||
> extends IDBPCursorWithValueIteratorValueExtends<DBTypes, TxStores, StoreName, IndexName> { | ||
IndexName extends IndexNames<DBTypes, StoreName> | unknown = unknown | ||
> | ||
extends IDBPCursorWithValueIteratorValueExtends< | ||
DBTypes, | ||
TxStores, | ||
StoreName, | ||
IndexName | ||
> { | ||
/** | ||
@@ -918,9 +1026,7 @@ * Advances the cursor a given number of records. | ||
*/ | ||
continuePrimaryKey<T> | ||
( | ||
this: T, | ||
key: CursorKey<DBTypes, StoreName, IndexName>, | ||
primaryKey: StoreKey<DBTypes, StoreName>, | ||
): | ||
void; | ||
continuePrimaryKey<T>( | ||
this: T, | ||
key: CursorKey<DBTypes, StoreName, IndexName>, | ||
primaryKey: StoreKey<DBTypes, StoreName>, | ||
): void; | ||
} |
export type Constructor = new (...args: any[]) => any; | ||
export type Func = (...args: any[]) => any; | ||
export const instanceOfAny = (object: any, constructors: Constructor[]): boolean => | ||
constructors.some(c => object instanceof c); | ||
export const instanceOfAny = ( | ||
object: any, | ||
constructors: Constructor[], | ||
): boolean => constructors.some(c => object instanceof c); |
import { | ||
IDBPCursor, IDBPCursorWithValue, IDBPDatabase, IDBPIndex, IDBPObjectStore, IDBPTransaction, | ||
IDBPCursor, | ||
IDBPCursorWithValue, | ||
IDBPDatabase, | ||
IDBPIndex, | ||
IDBPObjectStore, | ||
IDBPTransaction, | ||
} from './entry'; | ||
@@ -11,4 +16,12 @@ import { Constructor, Func, instanceOfAny } from './util'; | ||
function getIdbProxyableTypes(): Constructor[] { | ||
return idbProxyableTypes || | ||
(idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]); | ||
return ( | ||
idbProxyableTypes || | ||
(idbProxyableTypes = [ | ||
IDBDatabase, | ||
IDBObjectStore, | ||
IDBIndex, | ||
IDBCursor, | ||
IDBTransaction, | ||
]) | ||
); | ||
} | ||
@@ -18,12 +31,24 @@ | ||
function getCursorAdvanceMethods(): Func[] { | ||
return cursorAdvanceMethods || (cursorAdvanceMethods = [ | ||
IDBCursor.prototype.advance, | ||
IDBCursor.prototype.continue, | ||
IDBCursor.prototype.continuePrimaryKey, | ||
]); | ||
return ( | ||
cursorAdvanceMethods || | ||
(cursorAdvanceMethods = [ | ||
IDBCursor.prototype.advance, | ||
IDBCursor.prototype.continue, | ||
IDBCursor.prototype.continuePrimaryKey, | ||
]) | ||
); | ||
} | ||
const cursorRequestMap: WeakMap<IDBPCursor, IDBRequest<IDBCursor>> = new WeakMap(); | ||
const transactionDoneMap: WeakMap<IDBTransaction, Promise<void>> = new WeakMap(); | ||
const transactionStoreNamesMap: WeakMap<IDBTransaction, string[]> = new WeakMap(); | ||
const cursorRequestMap: WeakMap< | ||
IDBPCursor, | ||
IDBRequest<IDBCursor> | ||
> = new WeakMap(); | ||
const transactionDoneMap: WeakMap< | ||
IDBTransaction, | ||
Promise<void> | ||
> = new WeakMap(); | ||
const transactionStoreNamesMap: WeakMap< | ||
IDBTransaction, | ||
string[] | ||
> = new WeakMap(); | ||
const transformCache = new WeakMap(); | ||
@@ -50,13 +75,15 @@ export const reverseTransformCache = new WeakMap(); | ||
promise.then((value) => { | ||
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval | ||
// (see wrapFunction). | ||
if (value instanceof IDBCursor) { | ||
cursorRequestMap.set( | ||
value as unknown as IDBPCursor, | ||
request as unknown as IDBRequest<IDBCursor>, | ||
); | ||
} | ||
// Catching to avoid "Uncaught Promise exceptions" | ||
}).catch(() => {}); | ||
promise | ||
.then(value => { | ||
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval | ||
// (see wrapFunction). | ||
if (value instanceof IDBCursor) { | ||
cursorRequestMap.set( | ||
(value as unknown) as IDBPCursor, | ||
(request as unknown) as IDBRequest<IDBCursor>, | ||
); | ||
} | ||
// Catching to avoid "Uncaught Promise exceptions" | ||
}) | ||
.catch(() => {}); | ||
@@ -107,4 +134,5 @@ // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This | ||
if (prop === 'store') { | ||
return receiver.objectStoreNames[1] ? | ||
undefined : receiver.objectStore(receiver.objectStoreNames[0]); | ||
return receiver.objectStoreNames[1] | ||
? undefined | ||
: receiver.objectStore(receiver.objectStoreNames[0]); | ||
} | ||
@@ -116,3 +144,8 @@ } | ||
has(target, prop) { | ||
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store')) return true; | ||
if ( | ||
target instanceof IDBTransaction && | ||
(prop === 'done' || prop === 'store') | ||
) { | ||
return true; | ||
} | ||
return prop in target; | ||
@@ -122,3 +155,5 @@ }, | ||
export function addTraps(callback: (currentTraps: ProxyHandler<any>) => ProxyHandler<any>): void { | ||
export function addTraps( | ||
callback: (currentTraps: ProxyHandler<any>) => ProxyHandler<any>, | ||
): void { | ||
idbProxyTraps = callback(idbProxyTraps); | ||
@@ -136,3 +171,7 @@ } | ||
) { | ||
return function (this: IDBPDatabase, storeNames: string | string[], ...args: any[]) { | ||
return function( | ||
this: IDBPDatabase, | ||
storeNames: string | string[], | ||
...args: any[] | ||
) { | ||
const tx = func.call(unwrap(this), storeNames, ...args); | ||
@@ -153,3 +192,3 @@ transactionStoreNamesMap.set( | ||
if (getCursorAdvanceMethods().includes(func)) { | ||
return function (this: IDBPCursor, ...args: Parameters<T>) { | ||
return function(this: IDBPCursor, ...args: Parameters<T>) { | ||
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use | ||
@@ -162,3 +201,3 @@ // the original object. | ||
return function (this: any, ...args: Parameters<T>) { | ||
return function(this: any, ...args: Parameters<T>) { | ||
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use | ||
@@ -177,3 +216,4 @@ // the original object. | ||
if (instanceOfAny(value, getIdbProxyableTypes())) return new Proxy(value, idbProxyTraps); | ||
if (instanceOfAny(value, getIdbProxyableTypes())) | ||
return new Proxy(value, idbProxyTraps); | ||
@@ -193,3 +233,5 @@ // Return the same value back if we're not going to transform it. | ||
export function wrap(value: IDBTransaction): IDBPTransaction; | ||
export function wrap(value: IDBOpenDBRequest): Promise<IDBPDatabase | undefined>; | ||
export function wrap( | ||
value: IDBOpenDBRequest, | ||
): Promise<IDBPDatabase | undefined>; | ||
export function wrap<T>(value: IDBRequest<T>): Promise<T>; | ||
@@ -234,2 +276,3 @@ export function wrap(value: any): any { | ||
} | ||
export const unwrap: Unwrap = (value: any): any => reverseTransformCache.get(value); | ||
export const unwrap: Unwrap = (value: any): any => | ||
reverseTransformCache.get(value); |
{ | ||
"name": "idb", | ||
"version": "4.0.4", | ||
"version": "4.0.5", | ||
"description": "A small wrapper that makes IndexedDB usable", | ||
@@ -26,2 +26,3 @@ "main": "build/cjs/index.js", | ||
"mocha": "^6.0.2", | ||
"prettier": "^1.18.2", | ||
"rollup": "^1.6.1", | ||
@@ -33,7 +34,4 @@ "rollup-plugin-commonjs": "^9.2.1", | ||
"rollup-plugin-typescript2": "^0.20.1", | ||
"tslint": "^5.14.0", | ||
"tslint-config-airbnb": "^5.11.1", | ||
"tslint-react": "^3.6.0", | ||
"typescript": "^3.3.4000" | ||
} | ||
} |
101
README.md
@@ -8,11 +8,11 @@ # IndexedDB with usability. | ||
1. [API](#api) | ||
1. [`openDB`](#opendb) | ||
1. [`deleteDB`](#deletedb) | ||
1. [`unwrap`](#unwrap) | ||
1. [`wrap`](#wrap) | ||
1. [General enhancements](#general-enhancements) | ||
1. [`IDBDatabase` enhancements](#idbdatabase-enhancements) | ||
1. [`IDBTransaction` enhancements](#idbtransaction-enhancements) | ||
1. [`IDBCursor` enhancements](#idbcursor-enhancements) | ||
1. [Async iterators](#async-iterators) | ||
1. [`openDB`](#opendb) | ||
1. [`deleteDB`](#deletedb) | ||
1. [`unwrap`](#unwrap) | ||
1. [`wrap`](#wrap) | ||
1. [General enhancements](#general-enhancements) | ||
1. [`IDBDatabase` enhancements](#idbdatabase-enhancements) | ||
1. [`IDBTransaction` enhancements](#idbtransaction-enhancements) | ||
1. [`IDBCursor` enhancements](#idbcursor-enhancements) | ||
1. [Async iterators](#async-iterators) | ||
1. [Examples](#examples) | ||
@@ -41,7 +41,7 @@ 1. [TypeScript](#typescript) | ||
<script type="module"> | ||
import { openDB, deleteDB, wrap, unwrap } from 'https://unpkg.com/idb?module'; | ||
import { openDB, deleteDB, wrap, unwrap } from 'https://unpkg.com/idb?module'; | ||
async function doDatabaseStuff() { | ||
const db = await openDB(…); | ||
} | ||
async function doDatabaseStuff() { | ||
const db = await openDB(…); | ||
} | ||
</script> | ||
@@ -70,15 +70,15 @@ ``` | ||
// … | ||
} | ||
}, | ||
}); | ||
``` | ||
* `name`: Name of the database. | ||
* `version`: Schema version. | ||
* `upgrade` (optional): Called if this version of the database has never been opened before. Use it to specify the schema for the database. This is similar to the [`upgradeneeded` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/upgradeneeded_event) in plain IndexedDB. | ||
* `db`: An enhanced `IDBDatabase`. | ||
* `oldVersion`: Last version of the database opened by the user. | ||
* `newVersion`: Whatever new version you provided. | ||
* `transaction`: An enhanced transaction for this upgrade. This is useful if you need to get data from other stores as part of a migration. | ||
* `blocked` (optional): Called if there are older versions of the database open on the origin, so this version cannot open. This is similar to the [`blocked` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/blocked_event) in plain IndexedDB. | ||
* `blocking` (optional): Called if this connection is blocking a future version of the database from opening. This is similar to the [`versionchange` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event) in plain IndexedDB. | ||
- `name`: Name of the database. | ||
- `version`: Schema version. | ||
- `upgrade` (optional): Called if this version of the database has never been opened before. Use it to specify the schema for the database. This is similar to the [`upgradeneeded` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/upgradeneeded_event) in plain IndexedDB. | ||
- `db`: An enhanced `IDBDatabase`. | ||
- `oldVersion`: Last version of the database opened by the user. | ||
- `newVersion`: Whatever new version you provided. | ||
- `transaction`: An enhanced transaction for this upgrade. This is useful if you need to get data from other stores as part of a migration. | ||
- `blocked` (optional): Called if there are older versions of the database open on the origin, so this version cannot open. This is similar to the [`blocked` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/blocked_event) in plain IndexedDB. | ||
- `blocking` (optional): Called if this connection is blocking a future version of the database from opening. This is similar to the [`versionchange` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event) in plain IndexedDB. | ||
@@ -90,6 +90,11 @@ ## `deleteDB` | ||
```js | ||
await deleteDB(name); | ||
await deleteDB(name, { | ||
blocked() { | ||
// … | ||
}, | ||
}); | ||
``` | ||
* `name`: Name of the database. | ||
- `name`: Name of the database. | ||
- `blocked` (optional): Called if the database already exists and there are open connections that don’t close in response to a versionchange event, the request will be blocked until all they close. | ||
@@ -146,3 +151,3 @@ ## `unwrap` | ||
const store = tx.objectStore('keyval'); | ||
const val = await store.get('counter') || 0; | ||
const val = (await store.get('counter')) || 0; | ||
store.put(val + 1, 'counter'); | ||
@@ -157,3 +162,3 @@ await tx.done; | ||
const store = tx.objectStore('keyval'); | ||
const val = await store.get('counter') || 0; | ||
const val = (await store.get('counter')) || 0; | ||
// This is where things go wrong: | ||
@@ -278,3 +283,3 @@ const newVal = await fetch('/increment?val=' + val); | ||
This is very similar to `localStorage`, but async. If this is *all* you need, you may be interested in [idb-keyval](https://www.npmjs.com/package/idb-keyval). You can always upgrade to this library later. | ||
This is very similar to `localStorage`, but async. If this is _all_ you need, you may be interested in [idb-keyval](https://www.npmjs.com/package/idb-keyval). You can always upgrade to this library later. | ||
@@ -285,3 +290,3 @@ ```js | ||
db.createObjectStore('keyval'); | ||
} | ||
}, | ||
}); | ||
@@ -377,14 +382,14 @@ | ||
'favourite-number': { | ||
key: string, | ||
value: number, | ||
}, | ||
'products': { | ||
key: string; | ||
value: number; | ||
}; | ||
products: { | ||
value: { | ||
name: string, | ||
price: number, | ||
productCode: string, | ||
}, | ||
key: string, | ||
indexes: { 'by-price': number }, | ||
} | ||
name: string; | ||
price: number; | ||
productCode: string; | ||
}; | ||
key: string; | ||
indexes: { 'by-price': number }; | ||
}; | ||
} | ||
@@ -397,5 +402,7 @@ | ||
const productStore = db.createObjectStore('products', { keyPath: 'productCode' }); | ||
const productStore = db.createObjectStore('products', { | ||
keyPath: 'productCode', | ||
}); | ||
productStore.createIndex('by-price', 'price'); | ||
} | ||
}, | ||
}); | ||
@@ -428,7 +435,7 @@ | ||
interface MyDBV1 extends DBSchema { | ||
'favourite-number': { key: string, value: number }, | ||
'favourite-number': { key: string; value: number }; | ||
} | ||
interface MyDBV2 extends DBSchema { | ||
'fave-num': { key: string, value: number }, | ||
'fave-num': { key: string; value: number }; | ||
} | ||
@@ -439,3 +446,3 @@ | ||
// Cast a reference of the database to the old schema. | ||
const v1Db = db as unknown as IDBPDatabase<MyDBV1>; | ||
const v1Db = (db as unknown) as IDBPDatabase<MyDBV1>; | ||
@@ -449,8 +456,8 @@ if (oldVersion < 1) { | ||
} | ||
} | ||
}, | ||
}); | ||
``` | ||
You can also cast to a typeless database by omiting the type, eg `db as IDBPDatabase`. | ||
You can also cast to a typeless database by omitting the type, eg `db as IDBPDatabase`. | ||
Note: Types like `IDBPDatabase` are used by TypeScript only. The implementation uses proxies under the hood. |
@@ -19,3 +19,3 @@ // Since this library proxies IDB, I haven't retested all of IDB. I've tried to cover parts of the | ||
(async function () { | ||
(async function() { | ||
const edgeCompat = navigator.userAgent.includes('Edge/'); | ||
@@ -22,0 +22,0 @@ |
@@ -6,9 +6,11 @@ // Since this library proxies IDB, I haven't retested all of IDB. I've tried to cover parts of the | ||
import { assert } from 'chai'; | ||
import { | ||
IDBPDatabase, | ||
IDBPCursorWithValueIteratorValue, | ||
} from '../lib/'; | ||
import { IDBPDatabase, IDBPCursorWithValueIteratorValue } from '../lib/'; | ||
import '../lib/async-iterators'; | ||
import { assert as typeAssert, IsExact } from 'conditional-type-checks'; | ||
import { deleteDatabase, openDBWithData, TestDBSchema, ObjectStoreValue } from './utils'; | ||
import { | ||
deleteDatabase, | ||
openDBWithData, | ||
TestDBSchema, | ||
ObjectStoreValue, | ||
} from './utils'; | ||
@@ -35,18 +37,17 @@ suite('Async iterators', () => { | ||
for await (const cursor of store) { | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
TestDBSchema, ['key-val-store'], 'key-val-store', unknown | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
TestDBSchema, | ||
['key-val-store'], | ||
'key-val-store', | ||
unknown | ||
> | ||
> | ||
>>(true); | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.key, | ||
string | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.key, string>>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.value, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.value, number>>(true); | ||
@@ -66,18 +67,17 @@ keys.push(cursor.key); | ||
for await (const cursor of store) { | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
unknown, ['key-val-store'], 'key-val-store', unknown | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
unknown, | ||
['key-val-store'], | ||
'key-val-store', | ||
unknown | ||
> | ||
> | ||
>>(true); | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.key, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.key, IDBValidKey>>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.value, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.value, any>>(true); | ||
@@ -101,6 +101,8 @@ keys.push(cursor.key); | ||
typeAssert<IsExact< | ||
Parameters<typeof store.iterate>[0], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store.iterate>[0], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
@@ -114,6 +116,8 @@ for await (const _ of store.iterate('blah')) { | ||
typeAssert<IsExact< | ||
Parameters<typeof store.iterate>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store.iterate>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
@@ -151,18 +155,17 @@ for await (const _ of store.iterate('blah')) { | ||
for await (const cursor of index) { | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
TestDBSchema, ['object-store'], 'object-store', 'date' | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
TestDBSchema, | ||
['object-store'], | ||
'object-store', | ||
'date' | ||
> | ||
> | ||
>>(true); | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.key, | ||
Date | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.key, Date>>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.value, | ||
ObjectStoreValue | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.value, ObjectStoreValue>>(true); | ||
@@ -218,18 +221,17 @@ keys.push(cursor.key); | ||
for await (const cursor of index) { | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
unknown, ['object-store'], 'object-store', 'title' | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValueIteratorValue< | ||
unknown, | ||
['object-store'], | ||
'object-store', | ||
'title' | ||
> | ||
> | ||
>>(true); | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.key, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.key, IDBValidKey>>(true); | ||
typeAssert<IsExact< | ||
typeof cursor.value, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.value, any>>(true); | ||
@@ -282,6 +284,8 @@ keys.push(cursor.key); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.iterate>[0], | ||
Date | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.iterate>[0], | ||
Date | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
@@ -296,6 +300,8 @@ for await (const _ of index.iterate(new Date('2020-01-01'))) { | ||
typeAssert<IsExact< | ||
Parameters<typeof index.iterate>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.iterate>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
@@ -323,18 +329,17 @@ for await (const _ of index.iterate('foo')) { | ||
for await (const cursorIter of cursor) { | ||
typeAssert<IsExact< | ||
typeof cursorIter, | ||
IDBPCursorWithValueIteratorValue< | ||
TestDBSchema, ['key-val-store'], 'key-val-store', unknown | ||
typeAssert< | ||
IsExact< | ||
typeof cursorIter, | ||
IDBPCursorWithValueIteratorValue< | ||
TestDBSchema, | ||
['key-val-store'], | ||
'key-val-store', | ||
unknown | ||
> | ||
> | ||
>>(true); | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof cursorIter.key, | ||
string | ||
>>(true); | ||
typeAssert<IsExact<typeof cursorIter.key, string>>(true); | ||
typeAssert<IsExact< | ||
typeof cursorIter.value, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof cursorIter.value, number>>(true); | ||
@@ -341,0 +346,0 @@ keys.push(cursorIter.key); |
1342
test/main.ts
@@ -16,3 +16,7 @@ import 'mocha/mocha'; | ||
import { | ||
deleteDatabase, openDBWithSchema, openDBWithData, ObjectStoreValue, TestDBSchema, | ||
deleteDatabase, | ||
openDBWithSchema, | ||
openDBWithData, | ||
ObjectStoreValue, | ||
TestDBSchema, | ||
} from './utils'; | ||
@@ -32,11 +36,10 @@ | ||
typeAssert<IsExact< | ||
typeof schemaDB.objectStoreNames, | ||
TypedDOMStringList<'key-val-store' | 'object-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof schemaDB.objectStoreNames, | ||
TypedDOMStringList<'key-val-store' | 'object-store'> | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof db.objectStoreNames, | ||
DOMStringList | ||
>>(true); | ||
typeAssert<IsExact<typeof db.objectStoreNames, DOMStringList>>(true); | ||
}); | ||
@@ -48,11 +51,12 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.createObjectStore>[0], | ||
('key-val-store' | 'object-store') | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.createObjectStore>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof db.createObjectStore>[0], | ||
string | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof db.createObjectStore>[0], string>>( | ||
true, | ||
); | ||
}); | ||
@@ -64,11 +68,12 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.deleteObjectStore>[0], | ||
('key-val-store' | 'object-store') | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.deleteObjectStore>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof db.deleteObjectStore>[0], | ||
string | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof db.deleteObjectStore>[0], string>>( | ||
true, | ||
); | ||
}); | ||
@@ -80,11 +85,10 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.transaction>[0], | ||
('key-val-store' | 'object-store')[] | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.transaction>[0], | ||
('key-val-store' | 'object-store')[] | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof db.transaction>[0], | ||
string[] | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof db.transaction>[0], string[]>>(true); | ||
@@ -101,13 +105,12 @@ // Function getters should return the same instance. | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.get>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.get>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const val = await schemaDB.get('key-val-store', 'foo'); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number | undefined>>(true); | ||
@@ -118,6 +121,3 @@ assert.strictEqual(val, 123, 'Correct value from store'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -132,8 +132,9 @@ assert.strictEqual(val2, 456, 'Correct value from store'); | ||
assert.property(schemaDB, 'getFromIndex', 'Method exists'); | ||
const val = await schemaDB.getFromIndex('object-store', 'title', 'Article 1'); | ||
const val = await schemaDB.getFromIndex( | ||
'object-store', | ||
'title', | ||
'Article 1', | ||
); | ||
typeAssert<IsExact< | ||
typeof val, | ||
ObjectStoreValue | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, ObjectStoreValue | undefined>>(true); | ||
@@ -152,6 +153,3 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -169,3 +167,3 @@ assert.deepStrictEqual( | ||
test('getKey', async function () { | ||
test('getKey', async function() { | ||
if (!('getKey' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -177,13 +175,15 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.getKey>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.getKey>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const val = await schemaDB.getKey('key-val-store', IDBKeyRange.lowerBound('a')); | ||
const val = await schemaDB.getKey( | ||
'key-val-store', | ||
IDBKeyRange.lowerBound('a'), | ||
); | ||
typeAssert<IsExact< | ||
typeof val, | ||
string | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, string | undefined>>(true); | ||
@@ -194,6 +194,3 @@ assert.strictEqual(val, 'bar', 'Correct value'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
IDBValidKey | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, IDBValidKey | undefined>>(true); | ||
@@ -203,3 +200,3 @@ assert.strictEqual(val2, 'foo', 'Correct value'); | ||
test('getKeyFromIndex', async function () { | ||
test('getKeyFromIndex', async function() { | ||
if (!('getKey' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -212,9 +209,8 @@ const schemaDB = await openDBWithData(); | ||
const val = await schemaDB.getKeyFromIndex( | ||
'object-store', 'title', IDBKeyRange.lowerBound('A'), | ||
'object-store', | ||
'title', | ||
IDBKeyRange.lowerBound('A'), | ||
); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number | undefined>>(true); | ||
@@ -224,9 +220,8 @@ assert.strictEqual(val, 1, 'Correct value'); | ||
const val2 = await db.getKeyFromIndex( | ||
'object-store', 'date', IDBKeyRange.lowerBound(new Date('1990-01-01')), | ||
'object-store', | ||
'date', | ||
IDBKeyRange.lowerBound(new Date('1990-01-01')), | ||
); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
IDBValidKey | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, IDBValidKey | undefined>>(true); | ||
@@ -236,3 +231,3 @@ assert.strictEqual(val2, 4, 'Correct value'); | ||
test('getAll', async function () { | ||
test('getAll', async function() { | ||
if (!('getAll' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -244,13 +239,12 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.getAll>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.getAll>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const val = await schemaDB.getAll('key-val-store'); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number[]>>(true); | ||
@@ -261,6 +255,3 @@ assert.deepStrictEqual(val, [456, 123, 789], 'Correct values from store'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any[]>>(true); | ||
@@ -270,3 +261,3 @@ assert.deepStrictEqual(val2, [456, 123, 789], 'Correct values from store'); | ||
test('getAllFromIndex', async function () { | ||
test('getAllFromIndex', async function() { | ||
if (!('getAll' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -279,6 +270,3 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
ObjectStoreValue[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, ObjectStoreValue[]>>(true); | ||
@@ -314,6 +302,3 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any[]>>(true); | ||
@@ -348,3 +333,3 @@ assert.deepStrictEqual( | ||
test('getAllKeys', async function () { | ||
test('getAllKeys', async function() { | ||
if (!('getAllKeys' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -356,27 +341,31 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.getAllKeys>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.getAllKeys>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const val = await schemaDB.getAllKeys('key-val-store'); | ||
typeAssert<IsExact< | ||
typeof val, | ||
string[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, string[]>>(true); | ||
assert.deepStrictEqual(val, ['bar', 'foo', 'hello'], 'Correct values from store'); | ||
assert.deepStrictEqual( | ||
val, | ||
['bar', 'foo', 'hello'], | ||
'Correct values from store', | ||
); | ||
const val2 = await db.getAllKeys('key-val-store'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any[]>>(true); | ||
assert.deepStrictEqual(val2, ['bar', 'foo', 'hello'], 'Correct values from store'); | ||
assert.deepStrictEqual( | ||
val2, | ||
['bar', 'foo', 'hello'], | ||
'Correct values from store', | ||
); | ||
}); | ||
test('getAllKeysFromIndex', async function () { | ||
test('getAllKeysFromIndex', async function() { | ||
if (!('getAllKeys' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -389,6 +378,3 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number[]>>(true); | ||
@@ -399,6 +385,3 @@ assert.deepStrictEqual(val, [4, 3, 2, 1], 'Correct values from store'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any[]>>(true); | ||
@@ -414,13 +397,12 @@ assert.deepStrictEqual(val2, [1, 2, 3, 4], 'Correct values from store'); | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.count>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.count>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const val = await schemaDB.count('key-val-store'); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number>>(true); | ||
@@ -431,6 +413,3 @@ assert.strictEqual(val, 3, 'Correct count'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, number>>(true); | ||
@@ -447,6 +426,3 @@ assert.strictEqual(val2, 4, 'Correct count'); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number>>(true); | ||
@@ -456,9 +432,8 @@ assert.strictEqual(val, 4, 'Correct count'); | ||
const val2 = await db.countFromIndex( | ||
'object-store', 'title', IDBKeyRange.lowerBound('Article 10'), | ||
'object-store', | ||
'title', | ||
IDBKeyRange.lowerBound('Article 10'), | ||
); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, number>>(true); | ||
@@ -474,13 +449,12 @@ assert.strictEqual(val2, 3, 'Correct count'); | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.put>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.put>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const key = await schemaDB.put('key-val-store', 234, 'new'); | ||
typeAssert<IsExact< | ||
typeof key, | ||
string | ||
>>(true); | ||
typeAssert<IsExact<typeof key, string>>(true); | ||
@@ -499,6 +473,3 @@ assert.strictEqual(key, 'new'); | ||
typeAssert<IsExact< | ||
typeof key2, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof key2, IDBValidKey>>(true); | ||
@@ -509,6 +480,3 @@ assert.strictEqual(key2, 5); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -532,13 +500,12 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.add>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.add>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
const key = await schemaDB.add('key-val-store', 234, 'new'); | ||
typeAssert<IsExact< | ||
typeof key, | ||
string | ||
>>(true); | ||
typeAssert<IsExact<typeof key, string>>(true); | ||
@@ -557,6 +524,3 @@ assert.strictEqual(key, 'new'); | ||
typeAssert<IsExact< | ||
typeof key2, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof key2, IDBValidKey>>(true); | ||
@@ -567,6 +531,3 @@ assert.strictEqual(key2, 5); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -590,6 +551,8 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.delete>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.delete>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
@@ -613,6 +576,8 @@ await schemaDB.delete('key-val-store', 'foo'); | ||
typeAssert<IsExact< | ||
Parameters<typeof schemaDB.clear>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof schemaDB.clear>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
@@ -646,16 +611,9 @@ await schemaDB.clear('key-val-store'); | ||
typeAssert<IsExact< | ||
typeof tx1.objectStoreNames, | ||
['key-val-store'] | ||
>>(true); | ||
typeAssert<IsExact<typeof tx1.objectStoreNames, ['key-val-store']>>(true); | ||
typeAssert<IsExact< | ||
typeof tx2.objectStoreNames, | ||
['object-store'] | ||
>>(true); | ||
typeAssert<IsExact<typeof tx2.objectStoreNames, ['object-store']>>(true); | ||
typeAssert<IsExact< | ||
typeof tx3.objectStoreNames, | ||
('object-store' | 'key-val-store')[] | ||
>>(true); | ||
typeAssert< | ||
IsExact<typeof tx3.objectStoreNames, ('object-store' | 'key-val-store')[]> | ||
>(true); | ||
@@ -665,6 +623,3 @@ // Without schema it should still work: | ||
typeAssert<IsExact< | ||
typeof tx4.objectStoreNames, | ||
['key-val-store'] | ||
>>(true); | ||
typeAssert<IsExact<typeof tx4.objectStoreNames, ['key-val-store']>>(true); | ||
}); | ||
@@ -678,13 +633,7 @@ | ||
typeAssert<IsExact< | ||
typeof tx.db, | ||
IDBPDatabase<TestDBSchema> | ||
>>(true); | ||
typeAssert<IsExact<typeof tx.db, IDBPDatabase<TestDBSchema>>>(true); | ||
const tx2 = db.transaction('key-val-store'); | ||
typeAssert<IsExact< | ||
typeof tx2.db, | ||
IDBPDatabase | ||
>>(true); | ||
typeAssert<IsExact<typeof tx2.db, IDBPDatabase>>(true); | ||
}); | ||
@@ -708,6 +657,8 @@ | ||
typeAssert<IsExact< | ||
typeof tx.store, | ||
IDBPObjectStore<TestDBSchema, ['key-val-store'], 'key-val-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof tx.store, | ||
IDBPObjectStore<TestDBSchema, ['key-val-store'], 'key-val-store'> | ||
> | ||
>(true); | ||
@@ -722,6 +673,3 @@ assert.instanceOf(tx.store, IDBObjectStore); | ||
typeAssert<IsExact< | ||
typeof tx2.store, | ||
undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof tx2.store, undefined>>(true); | ||
@@ -743,16 +691,16 @@ assert.isUndefined(tx2.store); | ||
typeAssert<IsExact< | ||
Parameters<typeof tx1.objectStore>[0], | ||
'key-val-store' | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof tx1.objectStore>[0], 'key-val-store'>>( | ||
true, | ||
); | ||
typeAssert<IsExact< | ||
Parameters<typeof tx3.objectStore>[0], | ||
'key-val-store' | 'object-store' | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof tx3.objectStore>[0], | ||
'key-val-store' | 'object-store' | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof tx4.objectStore>[0], | ||
'object-store' | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof tx4.objectStore>[0], 'object-store'>>( | ||
true, | ||
); | ||
@@ -776,11 +724,15 @@ // The spec says object stores from the same transaction should be equal. | ||
typeAssert<IsExact< | ||
typeof store, | ||
IDBPObjectStore<TestDBSchema, ['key-val-store'], 'key-val-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof store, | ||
IDBPObjectStore<TestDBSchema, ['key-val-store'], 'key-val-store'> | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof schemalessStore, | ||
IDBPObjectStore<any, ['object-store'], 'object-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof schemalessStore, | ||
IDBPObjectStore<any, ['object-store'], 'object-store'> | ||
> | ||
>(true); | ||
@@ -802,6 +754,5 @@ assert.strictEqual(store.name, 'key-val-store'); | ||
typeAssert<IsExact< | ||
typeof wrappedTx, | ||
IDBPTransaction<unknown, string[]> | ||
>>(true); | ||
typeAssert<IsExact<typeof wrappedTx, IDBPTransaction<unknown, string[]>>>( | ||
true, | ||
); | ||
@@ -820,11 +771,5 @@ assert.property(wrappedTx, 'store'); | ||
typeAssert<IsExact< | ||
typeof unwrappedTx, | ||
IDBTransaction | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedTx, IDBTransaction>>(true); | ||
typeAssert<IsExact< | ||
typeof unwrappedTx2, | ||
IDBTransaction | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedTx2, IDBTransaction>>(true); | ||
@@ -851,11 +796,5 @@ assert.notProperty(unwrappedTx, 'store'); | ||
typeAssert<IsExact< | ||
typeof tx.store.indexNames, | ||
('date' | 'title')[] | ||
>>(true); | ||
typeAssert<IsExact<typeof tx.store.indexNames, ('date' | 'title')[]>>(true); | ||
typeAssert<IsExact< | ||
typeof tx2.store.indexNames, | ||
string[] | ||
>>(true); | ||
typeAssert<IsExact<typeof tx2.store.indexNames, string[]>>(true); | ||
}); | ||
@@ -871,16 +810,7 @@ | ||
typeAssert<IsExact< | ||
typeof tx.store.name, | ||
'object-store' | ||
>>(true); | ||
typeAssert<IsExact<typeof tx.store.name, 'object-store'>>(true); | ||
typeAssert<IsExact< | ||
typeof tx2.store.name, | ||
'object-store' | ||
>>(true); | ||
typeAssert<IsExact<typeof tx2.store.name, 'object-store'>>(true); | ||
typeAssert<IsExact< | ||
typeof store.name, | ||
'object-store' | ||
>>(true); | ||
typeAssert<IsExact<typeof store.name, 'object-store'>>(true); | ||
}); | ||
@@ -894,19 +824,26 @@ | ||
const tx2 = db.transaction('object-store'); | ||
const store = | ||
schemaDB.transaction(['object-store', 'key-val-store']).objectStore('object-store'); | ||
const store = schemaDB | ||
.transaction(['object-store', 'key-val-store']) | ||
.objectStore('object-store'); | ||
typeAssert<IsExact< | ||
typeof tx.store.transaction, | ||
IDBPTransaction<TestDBSchema, ['object-store']> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof tx.store.transaction, | ||
IDBPTransaction<TestDBSchema, ['object-store']> | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof tx2.store.transaction, | ||
IDBPTransaction<unknown, ['object-store']> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof tx2.store.transaction, | ||
IDBPTransaction<unknown, ['object-store']> | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof store.transaction, | ||
IDBPTransaction<TestDBSchema, ('object-store' | 'key-val-store')[]> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof store.transaction, | ||
IDBPTransaction<TestDBSchema, ('object-store' | 'key-val-store')[]> | ||
> | ||
>(true); | ||
}); | ||
@@ -920,18 +857,14 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.add>[0], | ||
number | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store1.add>[0], number>>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.add>[1], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.add>[1], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const key = await store1.add(234, 'new'); | ||
typeAssert<IsExact< | ||
typeof key, | ||
string | ||
>>(true); | ||
typeAssert<IsExact<typeof key, string>>(true); | ||
@@ -944,11 +877,10 @@ const val = await store1.get('new'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.add>[0], | ||
any | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store2.add>[0], any>>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.add>[1], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.add>[1], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
@@ -961,13 +893,7 @@ const key2 = await store2.add({ | ||
typeAssert<IsExact< | ||
typeof key2, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof key2, IDBValidKey>>(true); | ||
const val2 = await store2.get(5); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -1010,13 +936,12 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.count>[0], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.count>[0], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await store1.count(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number>>(true); | ||
@@ -1027,13 +952,12 @@ assert.strictEqual(val, 3, 'Correct count'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.count>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.count>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val2 = await store2.count(); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, number>>(true); | ||
@@ -1049,13 +973,9 @@ assert.strictEqual(val2, 4, 'Correct count'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.createIndex>[0], | ||
'date' | 'title' | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof store1.createIndex>[0], 'date' | 'title'> | ||
>(true); | ||
const store2 = db.transaction('object-store').store; | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.createIndex>[0], | ||
string | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store2.createIndex>[0], string>>(true); | ||
}); | ||
@@ -1069,6 +989,5 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.delete>[0], | ||
string | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof store1.delete>[0], string | IDBKeyRange> | ||
>(true); | ||
@@ -1082,6 +1001,5 @@ await store1.delete('foo'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.delete>[0], | ||
IDBValidKey | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof store2.delete>[0], IDBValidKey | IDBKeyRange> | ||
>(true); | ||
@@ -1100,13 +1018,9 @@ await store2.delete(1); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.get>[0], | ||
string | IDBKeyRange | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store1.get>[0], string | IDBKeyRange>>( | ||
true, | ||
); | ||
const val = await store1.get('foo'); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number | undefined>>(true); | ||
@@ -1117,13 +1031,9 @@ assert.strictEqual(val, 123, 'Correct value from store'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.get>[0], | ||
IDBValidKey | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof store2.get>[0], IDBValidKey | IDBKeyRange> | ||
>(true); | ||
const val2 = await store2.get('bar'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -1133,3 +1043,3 @@ assert.strictEqual(val2, 456, 'Correct value from store'); | ||
test('getAll', async function () { | ||
test('getAll', async function() { | ||
if (!('getAll' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -1141,13 +1051,12 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.getAll>[0], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.getAll>[0], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await store1.getAll(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number[]>>(true); | ||
@@ -1158,13 +1067,12 @@ assert.deepStrictEqual(val, [456, 123, 789], 'Correct values from store'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.getAll>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.getAll>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val2 = await store2.getAll(); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any[]>>(true); | ||
@@ -1174,3 +1082,3 @@ assert.deepStrictEqual(val2, [456, 123, 789], 'Correct values from store'); | ||
test('getAllKeys', async function () { | ||
test('getAllKeys', async function() { | ||
if (!('getAllKeys' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -1182,34 +1090,40 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.getAllKeys>[0], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.getAllKeys>[0], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await store1.getAllKeys(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
string[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, string[]>>(true); | ||
assert.deepStrictEqual(val, ['bar', 'foo', 'hello'], 'Correct values from store'); | ||
assert.deepStrictEqual( | ||
val, | ||
['bar', 'foo', 'hello'], | ||
'Correct values from store', | ||
); | ||
const store2 = db.transaction('key-val-store').store; | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.getAllKeys>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.getAllKeys>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val2 = await store2.getAllKeys(); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any[]>>(true); | ||
assert.deepStrictEqual(val2, ['bar', 'foo', 'hello'], 'Correct values from store'); | ||
assert.deepStrictEqual( | ||
val2, | ||
['bar', 'foo', 'hello'], | ||
'Correct values from store', | ||
); | ||
}); | ||
test('getKey', async function () { | ||
test('getKey', async function() { | ||
if (!('getKey' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -1221,13 +1135,9 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.getKey>[0], | ||
string | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof store1.getKey>[0], string | IDBKeyRange> | ||
>(true); | ||
const val = await store1.getKey(IDBKeyRange.lowerBound('a')); | ||
typeAssert<IsExact< | ||
typeof val, | ||
string | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, string | undefined>>(true); | ||
@@ -1238,13 +1148,9 @@ assert.strictEqual(val, 'bar', 'Correct value'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.getKey>[0], | ||
IDBValidKey | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof store2.getKey>[0], IDBValidKey | IDBKeyRange> | ||
>(true); | ||
const val2 = await store2.getKey(IDBKeyRange.lowerBound('c')); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
IDBValidKey | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, IDBValidKey | undefined>>(true); | ||
@@ -1260,13 +1166,9 @@ assert.strictEqual(val2, 'foo', 'Correct value'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.index>[0], | ||
'date' | 'title' | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store1.index>[0], 'date' | 'title'>>( | ||
true, | ||
); | ||
const store2 = db.transaction('object-store').store; | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.index>[0], | ||
string | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store2.index>[0], string>>(true); | ||
}); | ||
@@ -1280,13 +1182,22 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.openCursor>[0], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.openCursor>[0], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor1 = await store1.openCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor1, | ||
IDBPCursorWithValue<TestDBSchema, ['key-val-store'], 'key-val-store', unknown> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor1, | ||
IDBPCursorWithValue< | ||
TestDBSchema, | ||
['key-val-store'], | ||
'key-val-store', | ||
unknown | ||
> | null | ||
> | ||
>(true); | ||
@@ -1297,13 +1208,22 @@ assert.instanceOf(cursor1, IDBCursorWithValue); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.openCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.openCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor2 = await store2.openCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor2, | ||
IDBPCursorWithValue<unknown, ['object-store'], 'object-store', unknown> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor2, | ||
IDBPCursorWithValue< | ||
unknown, | ||
['object-store'], | ||
'object-store', | ||
unknown | ||
> | null | ||
> | ||
>(true); | ||
@@ -1313,3 +1233,3 @@ assert.instanceOf(cursor2, IDBCursorWithValue); | ||
test('openKeyCursor', async function () { | ||
test('openKeyCursor', async function() { | ||
if (!('openKeyCursor' in IDBObjectStore.prototype)) this.skip(); | ||
@@ -1321,27 +1241,40 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.openKeyCursor>[0], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.openKeyCursor>[0], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor1 = await store1.openKeyCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor1, | ||
IDBPCursor<TestDBSchema, ['key-val-store'], 'key-val-store', unknown> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor1, | ||
IDBPCursor< | ||
TestDBSchema, | ||
['key-val-store'], | ||
'key-val-store', | ||
unknown | ||
> | null | ||
> | ||
>(true); | ||
const store2 = db.transaction('object-store').store; | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.openKeyCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.openKeyCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor2 = await store2.openKeyCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor2, | ||
IDBPCursor<unknown, ['object-store'], 'object-store', unknown> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor2, | ||
IDBPCursor<unknown, ['object-store'], 'object-store', unknown> | null | ||
> | ||
>(true); | ||
}); | ||
@@ -1355,18 +1288,14 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.put>[0], | ||
number | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store1.put>[0], number>>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof store1.put>[1], | ||
string | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store1.put>[1], | ||
string | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const key = await store1.put(234, 'new'); | ||
typeAssert<IsExact< | ||
typeof key, | ||
string | ||
>>(true); | ||
typeAssert<IsExact<typeof key, string>>(true); | ||
@@ -1379,11 +1308,10 @@ const val = await store1.get('new'); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.put>[0], | ||
any | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof store2.put>[0], any>>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof store2.put>[1], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof store2.put>[1], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
@@ -1396,13 +1324,7 @@ const key2 = await store2.put({ | ||
typeAssert<IsExact< | ||
typeof key2, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof key2, IDBValidKey>>(true); | ||
const val2 = await store2.get(5); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -1432,6 +1354,3 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
typeof wrappedStore, | ||
IDBPObjectStore | ||
>>(true); | ||
typeAssert<IsExact<typeof wrappedStore, IDBPObjectStore>>(true); | ||
@@ -1450,11 +1369,5 @@ assert.instanceOf(wrappedStore.get('blah'), Promise); | ||
typeAssert<IsExact< | ||
typeof unwrappedStore1, | ||
IDBObjectStore | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedStore1, IDBObjectStore>>(true); | ||
typeAssert<IsExact< | ||
typeof unwrappedStore2, | ||
IDBObjectStore | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedStore2, IDBObjectStore>>(true); | ||
@@ -1478,20 +1391,32 @@ assert.instanceOf(unwrappedStore1.get('foo'), IDBRequest); | ||
const index1 = schemaDB.transaction('object-store').store.index('date'); | ||
const index2 = schemaDB.transaction(['object-store', 'key-val-store']) | ||
.objectStore('object-store').index('date'); | ||
const index2 = schemaDB | ||
.transaction(['object-store', 'key-val-store']) | ||
.objectStore('object-store') | ||
.index('date'); | ||
const index3 = db.transaction('object-store').store.index('date'); | ||
typeAssert<IsExact< | ||
typeof index1.objectStore, | ||
IDBPObjectStore<TestDBSchema, ['object-store'], 'object-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof index1.objectStore, | ||
IDBPObjectStore<TestDBSchema, ['object-store'], 'object-store'> | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof index2.objectStore, | ||
IDBPObjectStore<TestDBSchema, ('object-store' | 'key-val-store')[], 'object-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof index2.objectStore, | ||
IDBPObjectStore< | ||
TestDBSchema, | ||
('object-store' | 'key-val-store')[], | ||
'object-store' | ||
> | ||
> | ||
>(true); | ||
typeAssert<IsExact< | ||
typeof index3.objectStore, | ||
IDBPObjectStore<unknown, ['object-store'], 'object-store'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof index3.objectStore, | ||
IDBPObjectStore<unknown, ['object-store'], 'object-store'> | ||
> | ||
>(true); | ||
}); | ||
@@ -1505,13 +1430,12 @@ | ||
typeAssert<IsExact< | ||
Parameters<typeof index1.count>[0], | ||
Date | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index1.count>[0], | ||
Date | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await index1.count(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number>>(true); | ||
@@ -1522,13 +1446,12 @@ assert.strictEqual(val, 4, 'Correct count'); | ||
typeAssert<IsExact< | ||
Parameters<typeof index2.count>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index2.count>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val2 = await index2.count(); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, number>>(true); | ||
@@ -1544,13 +1467,9 @@ assert.strictEqual(val2, 4, 'Correct count'); | ||
typeAssert<IsExact< | ||
Parameters<typeof index1.get>[0], | ||
Date | IDBKeyRange | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof index1.get>[0], Date | IDBKeyRange>>( | ||
true, | ||
); | ||
const val = await index1.get(new Date('2019-01-03')); | ||
typeAssert<IsExact< | ||
typeof val, | ||
ObjectStoreValue | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, ObjectStoreValue | undefined>>(true); | ||
@@ -1569,13 +1488,9 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
Parameters<typeof index2.get>[0], | ||
IDBValidKey | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof index2.get>[0], IDBValidKey | IDBKeyRange> | ||
>(true); | ||
const val2 = await index2.get('Article 2'); | ||
typeAssert<IsExact< | ||
typeof val2, | ||
any | ||
>>(true); | ||
typeAssert<IsExact<typeof val2, any>>(true); | ||
@@ -1593,3 +1508,3 @@ assert.deepStrictEqual( | ||
test('getAll', async function () { | ||
test('getAll', async function() { | ||
if (!('getAll' in IDBIndex.prototype)) this.skip(); | ||
@@ -1602,13 +1517,12 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.getAll>[0], | ||
Date | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.getAll>[0], | ||
Date | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await index.getAll(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
ObjectStoreValue[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, ObjectStoreValue[]>>(true); | ||
@@ -1646,13 +1560,12 @@ assert.deepStrictEqual( | ||
typeAssert<IsExact< | ||
Parameters<typeof index.getAll>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.getAll>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await index.getAll(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, any[]>>(true); | ||
@@ -1688,3 +1601,3 @@ assert.deepStrictEqual( | ||
test('getAllKeys', async function () { | ||
test('getAllKeys', async function() { | ||
if (!('getAllKeys' in IDBIndex.prototype)) this.skip(); | ||
@@ -1697,13 +1610,12 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.getAllKeys>[0], | ||
Date | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.getAllKeys>[0], | ||
Date | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await index.getAllKeys(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number[]>>(true); | ||
@@ -1716,13 +1628,12 @@ assert.deepStrictEqual(val, [4, 3, 2, 1], 'Correct values from store'); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.getAllKeys>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.getAllKeys>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const val = await index.getAllKeys(); | ||
typeAssert<IsExact< | ||
typeof val, | ||
any[] | ||
>>(true); | ||
typeAssert<IsExact<typeof val, any[]>>(true); | ||
@@ -1740,13 +1651,11 @@ assert.deepStrictEqual(val, [1, 2, 3, 4], 'Correct values from store'); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.getKey>[0], | ||
Date | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof index.getKey>[0], Date | IDBKeyRange> | ||
>(true); | ||
const val = await index.getKey(IDBKeyRange.lowerBound(new Date('1990-01-01'))); | ||
const val = await index.getKey( | ||
IDBKeyRange.lowerBound(new Date('1990-01-01')), | ||
); | ||
typeAssert<IsExact< | ||
typeof val, | ||
number | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, number | undefined>>(true); | ||
@@ -1759,13 +1668,9 @@ assert.strictEqual(val, 4, 'Correct value'); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.getKey>[0], | ||
IDBValidKey | IDBKeyRange | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof index.getKey>[0], IDBValidKey | IDBKeyRange> | ||
>(true); | ||
const val = await index.getKey(IDBKeyRange.lowerBound('A')); | ||
typeAssert<IsExact< | ||
typeof val, | ||
IDBValidKey | undefined | ||
>>(true); | ||
typeAssert<IsExact<typeof val, IDBValidKey | undefined>>(true); | ||
@@ -1783,13 +1688,22 @@ assert.strictEqual(val, 1, 'Correct value'); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.openCursor>[0], | ||
Date | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.openCursor>[0], | ||
Date | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor = await index.openCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValue<TestDBSchema, ['object-store'], 'object-store', 'date'> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValue< | ||
TestDBSchema, | ||
['object-store'], | ||
'object-store', | ||
'date' | ||
> | null | ||
> | ||
>(true); | ||
@@ -1802,13 +1716,22 @@ assert.instanceOf(cursor, IDBCursorWithValue); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.openCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.openCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor = await index.openCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValue<unknown, ['object-store'], 'object-store', 'title'> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursorWithValue< | ||
unknown, | ||
['object-store'], | ||
'object-store', | ||
'title' | ||
> | null | ||
> | ||
>(true); | ||
@@ -1826,13 +1749,22 @@ assert.instanceOf(cursor, IDBCursorWithValue); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.openKeyCursor>[0], | ||
Date | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.openKeyCursor>[0], | ||
Date | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor = await index.openKeyCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursor<TestDBSchema, ['object-store'], 'object-store', 'date'> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursor< | ||
TestDBSchema, | ||
['object-store'], | ||
'object-store', | ||
'date' | ||
> | null | ||
> | ||
>(true); | ||
@@ -1845,13 +1777,17 @@ assert.instanceOf(cursor, IDBCursor); | ||
typeAssert<IsExact< | ||
Parameters<typeof index.openKeyCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
Parameters<typeof index.openKeyCursor>[0], | ||
IDBValidKey | IDBKeyRange | undefined | ||
> | ||
>(true); | ||
const cursor = await index.openKeyCursor(); | ||
typeAssert<IsExact< | ||
typeof cursor, | ||
IDBPCursor<unknown, ['object-store'], 'object-store', 'title'> | null | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor, | ||
IDBPCursor<unknown, ['object-store'], 'object-store', 'title'> | null | ||
> | ||
>(true); | ||
@@ -1874,6 +1810,3 @@ assert.instanceOf(cursor, IDBCursor); | ||
typeAssert<IsExact< | ||
typeof wrappedIndex, | ||
IDBPIndex | ||
>>(true); | ||
typeAssert<IsExact<typeof wrappedIndex, IDBPIndex>>(true); | ||
@@ -1892,11 +1825,5 @@ assert.instanceOf(wrappedIndex.get('blah'), Promise); | ||
typeAssert<IsExact< | ||
typeof unwrappedIndex1, | ||
IDBIndex | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedIndex1, IDBIndex>>(true); | ||
typeAssert<IsExact< | ||
typeof unwrappedIndex2, | ||
IDBIndex | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedIndex2, IDBIndex>>(true); | ||
@@ -1929,9 +1856,9 @@ assert.instanceOf(unwrappedIndex1.get('foo'), IDBRequest); | ||
typeAssert<IsExact< | ||
typeof cursor.key, | ||
Date | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.key, Date>>(true); | ||
assert.instanceOf(cursor.key, Date); | ||
assert.strictEqual(cursor.key.valueOf(), new Date('2019-01-01').valueOf()); | ||
assert.strictEqual( | ||
cursor.key.valueOf(), | ||
new Date('2019-01-01').valueOf(), | ||
); | ||
} | ||
@@ -1948,6 +1875,3 @@ | ||
typeAssert<IsExact< | ||
typeof cursor.key, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.key, IDBValidKey>>(true); | ||
@@ -1971,6 +1895,3 @@ assert.strictEqual(cursor.key, 'Article 1'); | ||
typeAssert<IsExact< | ||
typeof cursor.primaryKey, | ||
number | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.primaryKey, number>>(true); | ||
@@ -1989,6 +1910,3 @@ assert.strictEqual(cursor.primaryKey, 4); | ||
typeAssert<IsExact< | ||
typeof cursor.primaryKey, | ||
IDBValidKey | ||
>>(true); | ||
typeAssert<IsExact<typeof cursor.primaryKey, IDBValidKey>>(true); | ||
@@ -2012,6 +1930,8 @@ assert.strictEqual(cursor.primaryKey, 1); | ||
typeAssert<IsExact< | ||
typeof cursor.source, | ||
IDBPIndex<TestDBSchema, ['object-store'], 'object-store', 'date'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor.source, | ||
IDBPIndex<TestDBSchema, ['object-store'], 'object-store', 'date'> | ||
> | ||
>(true); | ||
} | ||
@@ -2028,6 +1948,8 @@ | ||
typeAssert<IsExact< | ||
typeof cursor.source, | ||
IDBPIndex<unknown, ['object-store'], 'object-store', 'title'> | ||
>>(true); | ||
typeAssert< | ||
IsExact< | ||
typeof cursor.source, | ||
IDBPIndex<unknown, ['object-store'], 'object-store', 'title'> | ||
> | ||
>(true); | ||
} | ||
@@ -2092,6 +2014,5 @@ }); | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.continue>[0], | ||
Date | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof cursor.continue>[0], Date | undefined> | ||
>(true); | ||
@@ -2117,6 +2038,5 @@ cursor = await cursor.continue(new Date('2019-01-02T05:00:00.000Z')); | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.continue>[0], | ||
IDBValidKey | undefined | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof cursor.continue>[0], IDBValidKey | undefined> | ||
>(true); | ||
@@ -2134,3 +2054,3 @@ cursor = await cursor.continue('Article 20'); | ||
test('continuePrimaryKey', async function () { | ||
test('continuePrimaryKey', async function() { | ||
if (!('continuePrimaryKey' in IDBCursor.prototype)) this.skip(); | ||
@@ -2149,11 +2069,9 @@ const schemaDB = await openDBWithData(); | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.continuePrimaryKey>[0], | ||
Date | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof cursor.continuePrimaryKey>[0], Date> | ||
>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.continuePrimaryKey>[1], | ||
number | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof cursor.continuePrimaryKey>[1], number> | ||
>(true); | ||
@@ -2182,11 +2100,9 @@ cursor = await cursor.continuePrimaryKey( | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.continuePrimaryKey>[0], | ||
IDBValidKey | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof cursor.continuePrimaryKey>[0], IDBValidKey> | ||
>(true); | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.continuePrimaryKey>[1], | ||
IDBValidKey | ||
>>(true); | ||
typeAssert< | ||
IsExact<Parameters<typeof cursor.continuePrimaryKey>[1], IDBValidKey> | ||
>(true); | ||
@@ -2204,3 +2120,3 @@ cursor = await cursor.continuePrimaryKey('Article 3', 3.5); | ||
test('delete', async function () { | ||
test('delete', async function() { | ||
if (!('delete' in IDBCursor.prototype)) this.skip(); | ||
@@ -2235,3 +2151,3 @@ const schemaDB = await openDBWithData(); | ||
test('update', async function () { | ||
test('update', async function() { | ||
if (!('update' in IDBCursor.prototype)) this.skip(); | ||
@@ -2246,6 +2162,3 @@ const schemaDB = await openDBWithData(); | ||
while (cursor) { | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.update>[0], | ||
number | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof cursor.update>[0], number>>(true); | ||
@@ -2264,6 +2177,3 @@ cursor.update(cursor.value + 1); | ||
while (cursor) { | ||
typeAssert<IsExact< | ||
Parameters<typeof cursor.update>[0], | ||
any | ||
>>(true); | ||
typeAssert<IsExact<Parameters<typeof cursor.update>[0], any>>(true); | ||
@@ -2283,10 +2193,9 @@ cursor.update(cursor.value + 1); | ||
{ | ||
const cursor = await schemaDB.transaction('object-store').store.openCursor(); | ||
const cursor = await schemaDB | ||
.transaction('object-store') | ||
.store.openCursor(); | ||
if (!cursor) throw Error('expected cursor'); | ||
const unwrappedCursor = unwrap(cursor); | ||
typeAssert<IsExact< | ||
typeof unwrappedCursor, | ||
IDBCursorWithValue | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedCursor, IDBCursorWithValue>>(true); | ||
@@ -2301,6 +2210,3 @@ assert.strictEqual(unwrappedCursor.continue(), undefined); | ||
typeAssert<IsExact< | ||
typeof unwrappedCursor, | ||
IDBCursorWithValue | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedCursor, IDBCursorWithValue>>(true); | ||
@@ -2325,27 +2231,31 @@ assert.strictEqual(unwrappedCursor.continue(), undefined); | ||
{ | ||
const cursor = await schemaDB.transaction('object-store', 'readwrite').store.openCursor(); | ||
const cursor = await schemaDB | ||
.transaction('object-store', 'readwrite') | ||
.store.openCursor(); | ||
if (!cursor) throw Error('expected cursor'); | ||
const unwrappedCursor = unwrap(cursor); | ||
typeAssert<IsExact< | ||
typeof unwrappedCursor, | ||
IDBCursorWithValue | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedCursor, IDBCursorWithValue>>(true); | ||
assert.instanceOf(unwrappedCursor.update(unwrappedCursor.value), IDBRequest); | ||
assert.instanceOf( | ||
unwrappedCursor.update(unwrappedCursor.value), | ||
IDBRequest, | ||
); | ||
} | ||
{ | ||
const cursor = await db.transaction('object-store', 'readwrite').store.openCursor(); | ||
const cursor = await db | ||
.transaction('object-store', 'readwrite') | ||
.store.openCursor(); | ||
if (!cursor) throw Error('expected cursor'); | ||
const unwrappedCursor = unwrap(cursor); | ||
typeAssert<IsExact< | ||
typeof unwrappedCursor, | ||
IDBCursorWithValue | ||
>>(true); | ||
typeAssert<IsExact<typeof unwrappedCursor, IDBCursorWithValue>>(true); | ||
assert.instanceOf(unwrappedCursor.update(unwrappedCursor.value), IDBRequest); | ||
assert.instanceOf( | ||
unwrappedCursor.update(unwrappedCursor.value), | ||
IDBRequest, | ||
); | ||
} | ||
}); | ||
}); |
import 'mocha/mocha'; | ||
import { assert } from 'chai'; | ||
import { openDB, IDBPDatabase, IDBPTransaction, wrap, unwrap } from '../lib/'; | ||
import { assert as typeAssert, IsExact } from 'conditional-type-checks'; | ||
import { | ||
openDB, | ||
IDBPDatabase, | ||
IDBPTransaction, | ||
wrap, | ||
unwrap, | ||
} from '../lib/'; | ||
import { assert as typeAssert, IsExact } from 'conditional-type-checks'; | ||
import { getNextVersion, TestDBSchema, dbName, openDBWithSchema, deleteDatabase } from './utils'; | ||
getNextVersion, | ||
TestDBSchema, | ||
dbName, | ||
openDBWithSchema, | ||
deleteDatabase, | ||
} from './utils'; | ||
@@ -23,9 +23,7 @@ suite('openDb', () => { | ||
const version = getNextVersion(); | ||
db = await openDB<TestDBSchema>(dbName, version, { | ||
db = (await openDB<TestDBSchema>(dbName, version, { | ||
upgrade(db, oldVersion, newVersion, tx) { | ||
upgradeRun = true; | ||
typeAssert<IsExact< | ||
typeof db, IDBPDatabase<TestDBSchema> | ||
>>(true); | ||
typeAssert<IsExact<typeof db, IDBPDatabase<TestDBSchema>>>(true); | ||
assert.instanceOf(db, IDBDatabase, 'db instance'); | ||
@@ -36,10 +34,7 @@ | ||
typeAssert<IsExact< | ||
typeof tx, | ||
IDBPTransaction<TestDBSchema> | ||
>>(true); | ||
typeAssert<IsExact<typeof tx, IDBPTransaction<TestDBSchema>>>(true); | ||
assert.instanceOf(tx, IDBTransaction, 'db instance'); | ||
assert.strictEqual(tx.mode, 'versionchange', 'tx mode'); | ||
}, | ||
}) as IDBPDatabase; | ||
})) as IDBPDatabase; | ||
@@ -55,10 +50,4 @@ assert.isTrue(upgradeRun, 'upgrade run'); | ||
upgradeRun = true; | ||
typeAssert<IsExact< | ||
typeof db, | ||
IDBPDatabase | ||
>>(true); | ||
typeAssert<IsExact< | ||
typeof tx, | ||
IDBPTransaction | ||
>>(true); | ||
typeAssert<IsExact<typeof db, IDBPDatabase>>(true); | ||
typeAssert<IsExact<typeof tx, IDBPTransaction>>(true); | ||
}, | ||
@@ -76,4 +65,6 @@ }); | ||
db = await openDB<TestDBSchema>(dbName, getNextVersion(), { | ||
blocked() { blockedCalled = true; }, | ||
db = (await openDB<TestDBSchema>(dbName, getNextVersion(), { | ||
blocked() { | ||
blockedCalled = true; | ||
}, | ||
blocking() { | ||
@@ -85,3 +76,3 @@ blockingCalled = true; | ||
}, | ||
}) as IDBPDatabase; | ||
})) as IDBPDatabase; | ||
@@ -91,6 +82,10 @@ assert.isFalse(blockedCalled); | ||
db = await openDB<TestDBSchema>(dbName, getNextVersion(), { | ||
blocked() { newDbBlockedCalled = true; }, | ||
blocking() { newDbBlockingCalled = true; }, | ||
}) as IDBPDatabase; | ||
db = (await openDB<TestDBSchema>(dbName, getNextVersion(), { | ||
blocked() { | ||
newDbBlockedCalled = true; | ||
}, | ||
blocking() { | ||
newDbBlockingCalled = true; | ||
}, | ||
})) as IDBPDatabase; | ||
@@ -104,6 +99,8 @@ assert.isFalse(blockedCalled); | ||
test('wrap', async () => { | ||
let wrappedRequest: Promise<IDBPDatabase | undefined> = Promise.resolve(undefined); | ||
let wrappedRequest: Promise<IDBPDatabase | undefined> = Promise.resolve( | ||
undefined, | ||
); | ||
// Let's do it the old fashioned way | ||
const idb = await new Promise<IDBDatabase>(async (resolve) => { | ||
const idb = await new Promise<IDBDatabase>(async resolve => { | ||
const request = indexedDB.open(dbName, getNextVersion()); | ||
@@ -117,10 +114,11 @@ wrappedRequest = wrap(request); | ||
typeAssert<IsExact< | ||
typeof db, | ||
IDBPDatabase | ||
>>(true); | ||
typeAssert<IsExact<typeof db, IDBPDatabase>>(true); | ||
assert.instanceOf(db, IDBDatabase, 'DB type'); | ||
assert.property(db, 'getAllFromIndex', 'DB looks wrapped'); | ||
assert.strictEqual(db, await wrappedRequest, 'Wrapped request and wrapped db are same'); | ||
assert.strictEqual( | ||
db, | ||
await wrappedRequest, | ||
'Wrapped request and wrapped db are same', | ||
); | ||
}); | ||
@@ -132,15 +130,9 @@ | ||
typeAssert<IsExact< | ||
typeof request, | ||
IDBOpenDBRequest | ||
>>(true); | ||
typeAssert<IsExact<typeof request, IDBOpenDBRequest>>(true); | ||
assert.instanceOf(request, IDBOpenDBRequest, 'Request type'); | ||
db = await openPromise as IDBPDatabase; | ||
db = (await openPromise) as IDBPDatabase; | ||
const idb = unwrap(db); | ||
typeAssert<IsExact< | ||
typeof idb, | ||
IDBDatabase | ||
>>(true); | ||
typeAssert<IsExact<typeof idb, IDBDatabase>>(true); | ||
@@ -160,3 +152,3 @@ assert.instanceOf(idb, IDBDatabase, 'DB type'); | ||
test('deleteDb', async () => { | ||
db = await openDBWithSchema() as IDBPDatabase; | ||
db = (await openDBWithSchema()) as IDBPDatabase; | ||
assert.lengthOf(db.objectStoreNames, 2, 'DB has two stores'); | ||
@@ -175,3 +167,5 @@ db.close(); | ||
db = await openDB(dbName, getNextVersion(), { | ||
blocked() { blockedCalled = true; }, | ||
blocked() { | ||
blockedCalled = true; | ||
}, | ||
blocking() { | ||
@@ -189,3 +183,5 @@ blockingCalled = true; | ||
await deleteDatabase({ | ||
blocked() { closeDbBlockedCalled = true; }, | ||
blocked() { | ||
closeDbBlockedCalled = true; | ||
}, | ||
}); | ||
@@ -192,0 +188,0 @@ |
import { | ||
DBSchema, IDBPDatabase, openDB, DeleteDBCallbacks, deleteDB, | ||
DBSchema, | ||
IDBPDatabase, | ||
openDB, | ||
DeleteDBCallbacks, | ||
deleteDB, | ||
} from '../lib/'; | ||
@@ -13,9 +17,9 @@ | ||
'key-val-store': { | ||
key: string, | ||
value: number, | ||
key: string; | ||
value: number; | ||
}; | ||
'object-store': { | ||
value: ObjectStoreValue, | ||
key: number, | ||
indexes: { date: Date, title: string }, | ||
value: ObjectStoreValue; | ||
key: number; | ||
indexes: { date: Date; title: string }; | ||
}; | ||
@@ -22,0 +26,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14
39
4856
451
198455