UNPKG

12.7 kBSource Map (JSON)View Raw
1{"version":3,"file":"indexeddb-2dd34f7d.cjs","sources":["../indexeddb.js"],"sourcesContent":["/* eslint-env browser */\n\n/**\n * Helpers to work with IndexedDB.\n *\n * @module indexeddb\n */\n\nimport * as promise from './promise.js'\nimport * as error from './error.js'\n\n/* c8 ignore start */\n\n/**\n * IDB Request to Promise transformer\n *\n * @param {IDBRequest} request\n * @return {Promise<any>}\n */\nexport const rtop = request => promise.create((resolve, reject) => {\n // @ts-ignore\n request.onerror = event => reject(new Error(event.target.error))\n // @ts-ignore\n request.onsuccess = event => resolve(event.target.result)\n})\n\n/**\n * @param {string} name\n * @param {function(IDBDatabase):any} initDB Called when the database is first created\n * @return {Promise<IDBDatabase>}\n */\nexport const openDB = (name, initDB) => promise.create((resolve, reject) => {\n const request = indexedDB.open(name)\n /**\n * @param {any} event\n */\n request.onupgradeneeded = event => initDB(event.target.result)\n /**\n * @param {any} event\n */\n request.onerror = event => reject(error.create(event.target.error))\n /**\n * @param {any} event\n */\n request.onsuccess = event => {\n /**\n * @type {IDBDatabase}\n */\n const db = event.target.result\n db.onversionchange = () => { db.close() }\n if (typeof addEventListener !== 'undefined') {\n addEventListener('unload', () => db.close())\n }\n resolve(db)\n }\n})\n\n/**\n * @param {string} name\n */\nexport const deleteDB = name => rtop(indexedDB.deleteDatabase(name))\n\n/**\n * @param {IDBDatabase} db\n * @param {Array<Array<string>|Array<string|IDBObjectStoreParameters|undefined>>} definitions\n */\nexport const createStores = (db, definitions) => definitions.forEach(d =>\n // @ts-ignore\n db.createObjectStore.apply(db, d)\n)\n\n/**\n * @param {IDBDatabase} db\n * @param {Array<string>} stores\n * @param {\"readwrite\"|\"readonly\"} [access]\n * @return {Array<IDBObjectStore>}\n */\nexport const transact = (db, stores, access = 'readwrite') => {\n const transaction = db.transaction(stores, access)\n return stores.map(store => getStore(transaction, store))\n}\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange} [range]\n * @return {Promise<number>}\n */\nexport const count = (store, range) =>\n rtop(store.count(range))\n\n/**\n * @param {IDBObjectStore} store\n * @param {String | number | ArrayBuffer | Date | Array<any> } key\n * @return {Promise<String | number | ArrayBuffer | Date | Array<any>>}\n */\nexport const get = (store, key) =>\n rtop(store.get(key))\n\n/**\n * @param {IDBObjectStore} store\n * @param {String | number | ArrayBuffer | Date | IDBKeyRange | Array<any> } key\n */\nexport const del = (store, key) =>\n rtop(store.delete(key))\n\n/**\n * @param {IDBObjectStore} store\n * @param {String | number | ArrayBuffer | Date | boolean} item\n * @param {String | number | ArrayBuffer | Date | Array<any>} [key]\n */\nexport const put = (store, item, key) =>\n rtop(store.put(item, key))\n\n/**\n * @param {IDBObjectStore} store\n * @param {String | number | ArrayBuffer | Date | boolean} item\n * @param {String | number | ArrayBuffer | Date | Array<any>} key\n * @return {Promise<any>}\n */\nexport const add = (store, item, key) =>\n rtop(store.add(item, key))\n\n/**\n * @param {IDBObjectStore} store\n * @param {String | number | ArrayBuffer | Date} item\n * @return {Promise<number>} Returns the generated key\n */\nexport const addAutoKey = (store, item) =>\n rtop(store.add(item))\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange} [range]\n * @param {number} [limit]\n * @return {Promise<Array<any>>}\n */\nexport const getAll = (store, range, limit) =>\n rtop(store.getAll(range, limit))\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange} [range]\n * @param {number} [limit]\n * @return {Promise<Array<any>>}\n */\nexport const getAllKeys = (store, range, limit) =>\n rtop(store.getAllKeys(range, limit))\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange|null} query\n * @param {'next'|'prev'|'nextunique'|'prevunique'} direction\n * @return {Promise<any>}\n */\nexport const queryFirst = (store, query, direction) => {\n /**\n * @type {any}\n */\n let first = null\n return iterateKeys(store, query, key => {\n first = key\n return false\n }, direction).then(() => first)\n}\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange?} [range]\n * @return {Promise<any>}\n */\nexport const getLastKey = (store, range = null) => queryFirst(store, range, 'prev')\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange?} [range]\n * @return {Promise<any>}\n */\nexport const getFirstKey = (store, range = null) => queryFirst(store, range, 'next')\n\n/**\n * @typedef KeyValuePair\n * @type {Object}\n * @property {any} k key\n * @property {any} v Value\n */\n\n/**\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange} [range]\n * @param {number} [limit]\n * @return {Promise<Array<KeyValuePair>>}\n */\nexport const getAllKeysValues = (store, range, limit) =>\n // @ts-ignore\n promise.all([getAllKeys(store, range, limit), getAll(store, range, limit)]).then(([ks, vs]) => ks.map((k, i) => ({ k, v: vs[i] })))\n\n/**\n * @param {any} request\n * @param {function(IDBCursorWithValue):void|boolean|Promise<void|boolean>} f\n * @return {Promise<void>}\n */\nconst iterateOnRequest = (request, f) => promise.create((resolve, reject) => {\n request.onerror = reject\n /**\n * @param {any} event\n */\n request.onsuccess = async event => {\n const cursor = event.target.result\n if (cursor === null || (await f(cursor)) === false) {\n return resolve()\n }\n cursor.continue()\n }\n})\n\n/**\n * Iterate on keys and values\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange|null} keyrange\n * @param {function(any,any):void|boolean|Promise<void|boolean>} f Callback that receives (value, key)\n * @param {'next'|'prev'|'nextunique'|'prevunique'} direction\n */\nexport const iterate = (store, keyrange, f, direction = 'next') =>\n iterateOnRequest(store.openCursor(keyrange, direction), cursor => f(cursor.value, cursor.key))\n\n/**\n * Iterate on the keys (no values)\n *\n * @param {IDBObjectStore} store\n * @param {IDBKeyRange|null} keyrange\n * @param {function(any):void|boolean|Promise<void|boolean>} f callback that receives the key\n * @param {'next'|'prev'|'nextunique'|'prevunique'} direction\n */\nexport const iterateKeys = (store, keyrange, f, direction = 'next') =>\n iterateOnRequest(store.openKeyCursor(keyrange, direction), cursor => f(cursor.key))\n\n/**\n * Open store from transaction\n * @param {IDBTransaction} t\n * @param {String} store\n * @returns {IDBObjectStore}\n */\nexport const getStore = (t, store) => t.objectStore(store)\n\n/**\n * @param {any} lower\n * @param {any} upper\n * @param {boolean} lowerOpen\n * @param {boolean} upperOpen\n */\nexport const createIDBKeyRangeBound = (lower, upper, lowerOpen, upperOpen) => IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen)\n\n/**\n * @param {any} upper\n * @param {boolean} upperOpen\n */\nexport const createIDBKeyRangeUpperBound = (upper, upperOpen) => IDBKeyRange.upperBound(upper, upperOpen)\n\n/**\n * @param {any} lower\n * @param {boolean} lowerOpen\n */\nexport const createIDBKeyRangeLowerBound = (lower, lowerOpen) => IDBKeyRange.lowerBound(lower, lowerOpen)\n\n/* c8 ignore stop */\n"],"names":["promise.create","error.create","promise.all"],"mappings":";;;;;AAAA;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,IAAI,GAAG,OAAO,IAAIA,cAAc,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;AACnE;AACA,EAAE,OAAO,CAAC,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAC;AAClE;AACA,EAAE,OAAO,CAAC,SAAS,GAAG,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAC;AAC3D,CAAC,EAAC;AACF;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,CAAC,IAAI,EAAE,MAAM,KAAKA,cAAc,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;AAC5E,EAAE,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,IAAI,EAAC;AACtC;AACA;AACA;AACA,EAAE,OAAO,CAAC,eAAe,GAAG,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAC;AAChE;AACA;AACA;AACA,EAAE,OAAO,CAAC,OAAO,GAAG,KAAK,IAAI,MAAM,CAACC,YAAY,CAAC,KAAK,CAAC,MAAM,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