Comparing version 0.7.9 to 0.8.0
@@ -1,2 +0,2 @@ | ||
const{isArray:isArray}=Array,connections=[],openDatabase=(e="default",t)=>new Promise(((r,o)=>{let a;try{a=window.indexedDB.open(e,1)}catch(e){o(e)}return a.onupgradeneeded=({target:e})=>{const{result:r}=e;t.forEach((e=>{if(!r.objectStoreNames.contains(e)){r.createObjectStore(e).onerror=e=>o(e.target.error)}}))},a.onsuccess=({target:e})=>{connections.push(a),r(e.result)},a.onerror=({target:e})=>o(e.result),a})),setData=async(e,t,r,o,a)=>{try{const s=e.transaction([t],"readwrite").objectStore(t);if(a){const e=isArray(o)?()=>o.map(((e,t)=>s.put(e,t))):()=>Object.entries(o).map((([e,t])=>s.put(t,e)));await Promise.all(e())}else await s.put(o,r)}catch(e){console.error(e)}return db64},getData=async(e,t,r,o)=>new Promise((a=>{const s=e.transaction([t]).objectStore(t);let n;if(o){const e={};s.openCursor().onsuccess=t=>{const o=t.target.result;o?(r.includes(o.key)&&(e[o.key]=o.value),o.continue()):a(e)}}else n=s.get(r),n.onsuccess=()=>a(n.result)})),deleteData=async(e,t,r)=>{try{const o=e.transaction([t],"readwrite").objectStore(t);o.openCursor().onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())}}catch(e){console.error(e)}return db64},clearStore=(e,t)=>new Promise(((r,o)=>{const a=e.transaction([t],"readwrite").objectStore(t).clear();a.onsuccess=r(db64),a.onerror=e=>o(e.target.error)})),deleteDB=e=>new Promise(((t,r)=>{const o=indexedDB.deleteDatabase(e);o.onsuccess=()=>t(db64),o.onerror=({target:e})=>r(new Error(`Error deleting database: ${e.error}`)),o.onblocked=()=>{for(const t of connections)t.result.name===e&&t.result.close();deleteDB(e)}})),db64={create:async(e,t)=>isArray(t)?(await openDatabase(e,t),db64):console.error("storeNames should be an array"),use:(e,t)=>(indexedDB.open(e,1).onsuccess=r=>{r.target.result.objectStoreNames.contains(t)||console.error(`Store ${t} does not exist. You may need to manage the lifecycle of databse "${e}" if outdated.`)},{set:async(r,o)=>openDatabase(e,t).then((e=>setData(e,t,r,o))).catch(console.error),setEntries:async r=>openDatabase(e,t).then((e=>setData(e,t,null,r,"entries"))).catch(console.error),get:async r=>openDatabase(e,t).then((e=>getData(e,t,r))).catch(console.error),getEntries:async r=>openDatabase(e,t).then((e=>getData(e,t,r,"entries"))).catch(console.error),delete:async r=>openDatabase(e,t).then((e=>deleteData(e,t,r))).catch(console.error)}),clear:async(e,t)=>openDatabase(e,t).then((e=>clearStore(e,t))).catch(console.error),delete:async e=>deleteDB(e).catch(console.error)};module.exports=db64; | ||
const{isArray:isArray}=Array,connections=[],openDatabase=(e="default",t)=>new Promise(((r,s)=>{const n=window.indexedDB.open(e,1);n.onupgradeneeded=({target:e})=>{const{result:r}=e;t.forEach((e=>{if(!r.objectStoreNames.contains(e)){r.createObjectStore(e).onerror=e=>s(e.target.error)}}))},n.onsuccess=({target:e})=>{connections.push(n),r(e.result)},n.onerror=({target:e})=>{s(e.result)}})),setData=async(e,t,r,s,n)=>new Promise(((o,a)=>{try{const a=e.transaction([t],"readwrite").objectStore(t);if(n){const e=isArray(s)?()=>s.map(((e,t)=>a.put(e,t))):()=>Object.entries(s).map((([e,t])=>a.put(t,e)));o(Promise.all(e()))}else o(a.put(s,r));o(db64)}catch(e){a(e)}})),getData=async(e,t,r,s)=>new Promise((n=>{const o=e.transaction([t]).objectStore(t);if(s){const e={};o.openCursor().onsuccess=t=>{const s=t.target.result;s?(r.includes(s.key)&&(e[s.key]=s.value),s.continue()):n(e)}}else{const e=o.get(r);e.onsuccess=()=>n(e.result)}})),deleteData=async(e,t,r)=>new Promise(((s,n)=>{try{const n=e.transaction([t],"readwrite").objectStore(t);n.openCursor().onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())},s(db64)}catch(e){n(e)}})),clearStore=(e,t)=>new Promise(((r,s)=>{const n=e.transaction([t],"readwrite").objectStore(t).clear();n.onsuccess=()=>r(db64),n.onerror=e=>s(e.target.error)})),deleteDB=e=>{const t=[];return new Promise(((r,s)=>{const n=indexedDB.deleteDatabase(e);n.onsuccess=()=>r(db64),n.onerror=({target:e})=>s(new Error(`Error deleting database: ${e.error}`)),n.onblocked=()=>{for(const r of connections)r.result.name===e&&(r.result.close(),t.push(e));t.includes(e)?r(db64):deleteDB(e)}}))},db64={create:async(e,t)=>("string"!=typeof e&&console.error(`${e} should be a string`),isArray(t)?openDatabase(e,t):console.error(`${t} should be an array`)),use:(e,t)=>("string"!=typeof e&&console.error(`${e} should be a string`),"string"!=typeof e&&console.error(`${t} should be a string`),{set:async(r,s)=>openDatabase(e,[t]).then((e=>setData(e,t,r,s))),setEntries:async r=>openDatabase(e,t).then((e=>setData(e,t,null,r,"entries"))),get:async r=>openDatabase(e,t).then((e=>getData(e,t,r))),getEntries:async r=>openDatabase(e,t).then((e=>getData(e,t,r,"entries"))),delete:async r=>openDatabase(e,t).then((e=>deleteData(e,t,r)))}),clear:async(e,t)=>openDatabase(e,t).then((e=>clearStore(e,t))),delete:async e=>deleteDB(e)};module.exports=db64; | ||
//# sourceMappingURL=db64-cjs.min.map |
export default db64; | ||
declare namespace db64 { | ||
export function create(name: any, storeNames: any): Promise<void | { | ||
create: (name: any, storeNames: any) => Promise<void | any>; | ||
use: (name: any, storeName: any) => { | ||
set: (key: any, value: any) => Promise<any>; | ||
setEntries: (value: any) => Promise<any>; | ||
get: (key: any) => Promise<any>; | ||
getEntries: (keys: any) => Promise<any>; | ||
delete: (keys: any) => Promise<any>; | ||
}; | ||
clear: (name: any, storeName: any) => Promise<any>; | ||
delete: (name: any) => Promise<any>; | ||
}>; | ||
export function create(name: any, storeNames: any): Promise<any>; | ||
export function use(name: any, storeName: any): { | ||
@@ -16,0 +5,0 @@ set: (key: any, value: any) => Promise<any>; |
147
dist/db64.js
@@ -12,10 +12,4 @@ const { isArray } = Array | ||
const openDatabase = (name = 'default', storeNames) => new Promise((resolve, reject) => { | ||
let database | ||
try { | ||
database = window.indexedDB.open(name, 1) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
const DBOpenRequest = window.indexedDB.open(name, 1) | ||
/* | ||
@@ -26,3 +20,3 @@ db64 does not revolve around versioning. Therefore, this function will only run once | ||
*/ | ||
database.onupgradeneeded = ({ target }) => { | ||
DBOpenRequest.onupgradeneeded = ({ target }) => { | ||
const { result } = target | ||
@@ -41,9 +35,10 @@ storeNames.forEach(storeName => { | ||
*/ | ||
database.onsuccess = ({ target }) => { | ||
connections.push(database) | ||
DBOpenRequest.onsuccess = ({ target }) => { | ||
connections.push(DBOpenRequest) | ||
resolve(target.result) | ||
} | ||
database.onerror = ({ target }) => reject(target.result) | ||
return database | ||
DBOpenRequest.onerror = ({ target }) => { | ||
reject(target.result) | ||
} | ||
}) | ||
@@ -61,6 +56,5 @@ | ||
*/ | ||
const setData = async (database, storeName, key, dataValue, entries) => { | ||
const setData = async (database, storeName, key, dataValue, entries) => new Promise((resolve, reject) => { | ||
try { | ||
const obStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
if (entries) { | ||
@@ -70,11 +64,11 @@ const dataEntries = isArray(dataValue) | ||
: () => Object.entries(dataValue).map(([key, value]) => obStore.put(value, key)) | ||
await Promise.all(dataEntries()) | ||
resolve(Promise.all(dataEntries())) | ||
} else { | ||
await obStore.put(dataValue, key) | ||
resolve(obStore.put(dataValue, key)) | ||
} | ||
resolve(db64) | ||
} catch (e) { | ||
console.error(e) | ||
reject(e) | ||
} | ||
return db64 | ||
} | ||
}) | ||
@@ -90,28 +84,27 @@ | ||
*/ | ||
const getData = async (database, storeName, key, entries) => { | ||
return new Promise((resolve) => { | ||
const objectStore = (database.transaction([storeName])).objectStore(storeName) | ||
let dataRequest | ||
if (entries) { | ||
const results = {} | ||
const cursorRequest = objectStore.openCursor() | ||
const getData = async (database, storeName, key, entries) => new Promise((resolve) => { | ||
const objectStore = (database.transaction([storeName])).objectStore(storeName) | ||
cursorRequest.onsuccess = e => { | ||
const cursor = e.target.result | ||
if (entries) { | ||
const results = {} | ||
const cursorRequest = objectStore.openCursor() | ||
if (cursor) { | ||
if (key.includes(cursor.key)) results[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
resolve(results) | ||
} | ||
cursorRequest.onsuccess = e => { | ||
const cursor = e.target.result | ||
if (cursor) { | ||
if (key.includes(cursor.key)) results[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
resolve(results) | ||
} | ||
} else { | ||
dataRequest = objectStore.get(key) | ||
dataRequest.onsuccess = () => resolve(dataRequest.result) | ||
} | ||
}) | ||
} | ||
} else { | ||
const dataRequest = objectStore.get(key) | ||
dataRequest.onsuccess = () => resolve(dataRequest.result) | ||
} | ||
}) | ||
/* | ||
@@ -124,3 +117,3 @@ Deletes an entry for a given store by key. | ||
*/ | ||
const deleteData = async (database, storeName, key) => { | ||
const deleteData = async (database, storeName, key) => new Promise((resolve, reject) => { | ||
try { | ||
@@ -138,7 +131,7 @@ const objectStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
} | ||
resolve(db64) | ||
} catch (e) { | ||
console.error(e) | ||
reject(e) | ||
} | ||
return db64 | ||
} | ||
}) | ||
@@ -152,11 +145,9 @@ | ||
*/ | ||
const clearStore = (database, storeName) => { | ||
return new Promise((resolve, reject) => { | ||
const objectStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
const objectStoreRequest = objectStore.clear() | ||
const clearStore = (database, storeName) => new Promise((resolve, reject) => { | ||
const objectStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
const objectStoreRequest = objectStore.clear() | ||
objectStoreRequest.onsuccess = resolve(db64) | ||
objectStoreRequest.onerror = e => reject(e.target.error) | ||
}) | ||
} | ||
objectStoreRequest.onsuccess = () => resolve(db64) | ||
objectStoreRequest.onerror = e => reject(e.target.error) | ||
}) | ||
@@ -170,16 +161,23 @@ | ||
const deleteDB = name => { | ||
const deletedDBs = [] | ||
return new Promise((resolve, reject) => { | ||
const deleteRequest = indexedDB.deleteDatabase(name) | ||
const DBDeleteRequest = indexedDB.deleteDatabase(name) | ||
deleteRequest.onsuccess = () => resolve(db64) | ||
DBDeleteRequest.onsuccess = () => resolve(db64) | ||
deleteRequest.onerror = ({ target }) => reject(new Error(`Error deleting database: ${target.error}`)) | ||
DBDeleteRequest.onerror = ({ target }) => reject(new Error(`Error deleting database: ${target.error}`)) | ||
deleteRequest.onblocked = () => { | ||
DBDeleteRequest.onblocked = () => { | ||
for (const database of connections) { | ||
if (database.result.name === name) { | ||
database.result.close() | ||
deletedDBs.push(name) | ||
} | ||
} | ||
deleteDB(name) | ||
if (!deletedDBs.includes(name)) { | ||
deleteDB(name) | ||
} else { | ||
resolve(db64) | ||
} | ||
} | ||
@@ -194,42 +192,29 @@ }) | ||
create: async (name, storeNames) => { | ||
if (!isArray(storeNames)) return console.error('storeNames should be an array') | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
if (!isArray(storeNames)) return console.error(`${storeNames} should be an array`) | ||
await openDatabase(name, storeNames) | ||
return db64 | ||
return openDatabase(name, storeNames) | ||
}, | ||
use: (name, storeName) => { | ||
const request = indexedDB.open(name, 1) | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
if (typeof name !== 'string') console.error(`${storeName} should be a string`) | ||
request.onsuccess = (event) => { | ||
const db = event.target.result | ||
if (!db.objectStoreNames.contains(storeName)) { | ||
console.error(`Store ${storeName} does not exist. You may need to manage the lifecycle of databse "${name}" if outdated.`) | ||
} | ||
} | ||
return { | ||
set: async (key, value) => openDatabase(name, storeName) | ||
.then(database => setData(database, storeName, key, value)) | ||
.catch(console.error), | ||
set: async (key, value) => openDatabase(name, [storeName]) | ||
.then(database => setData(database, storeName, key, value)), | ||
setEntries: async (value) => openDatabase(name, storeName) | ||
.then(database => setData(database, storeName, null, value, 'entries')) | ||
.catch(console.error), | ||
.then(database => setData(database, storeName, null, value, 'entries')), | ||
get: async key => openDatabase(name, storeName) | ||
.then(database => getData(database, storeName, key)) | ||
.catch(console.error), | ||
.then(database => getData(database, storeName, key)), | ||
getEntries: async (keys) => openDatabase(name, storeName) | ||
.then(database => getData(database, storeName, keys, 'entries')) | ||
.catch(console.error), | ||
.then(database => getData(database, storeName, keys, 'entries')), | ||
delete: async (keys) => openDatabase(name, storeName) | ||
.then(database => deleteData(database, storeName, keys)) | ||
.catch(console.error) | ||
.then(database => deleteData(database, storeName, keys)), | ||
} | ||
}, | ||
clear: async (name, storeName) => openDatabase(name, storeName) | ||
.then(database => clearStore(database, storeName)) | ||
.catch(console.error), | ||
.then(database => clearStore(database, storeName)), | ||
delete: async name => deleteDB(name) | ||
.catch(console.error) | ||
} | ||
export default db64 |
@@ -1,2 +0,2 @@ | ||
const{isArray:isArray}=Array,connections=[],openDatabase=(e="default",t)=>new Promise(((r,o)=>{let a;try{a=window.indexedDB.open(e,1)}catch(e){o(e)}return a.onupgradeneeded=({target:e})=>{const{result:r}=e;t.forEach((e=>{if(!r.objectStoreNames.contains(e)){r.createObjectStore(e).onerror=e=>o(e.target.error)}}))},a.onsuccess=({target:e})=>{connections.push(a),r(e.result)},a.onerror=({target:e})=>o(e.result),a})),setData=async(e,t,r,o,a)=>{try{const s=e.transaction([t],"readwrite").objectStore(t);if(a){const e=isArray(o)?()=>o.map(((e,t)=>s.put(e,t))):()=>Object.entries(o).map((([e,t])=>s.put(t,e)));await Promise.all(e())}else await s.put(o,r)}catch(e){console.error(e)}return db64},getData=async(e,t,r,o)=>new Promise((a=>{const s=e.transaction([t]).objectStore(t);let n;if(o){const e={};s.openCursor().onsuccess=t=>{const o=t.target.result;o?(r.includes(o.key)&&(e[o.key]=o.value),o.continue()):a(e)}}else n=s.get(r),n.onsuccess=()=>a(n.result)})),deleteData=async(e,t,r)=>{try{const o=e.transaction([t],"readwrite").objectStore(t);o.openCursor().onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())}}catch(e){console.error(e)}return db64},clearStore=(e,t)=>new Promise(((r,o)=>{const a=e.transaction([t],"readwrite").objectStore(t).clear();a.onsuccess=r(db64),a.onerror=e=>o(e.target.error)})),deleteDB=e=>new Promise(((t,r)=>{const o=indexedDB.deleteDatabase(e);o.onsuccess=()=>t(db64),o.onerror=({target:e})=>r(new Error(`Error deleting database: ${e.error}`)),o.onblocked=()=>{for(const t of connections)t.result.name===e&&t.result.close();deleteDB(e)}})),db64={create:async(e,t)=>isArray(t)?(await openDatabase(e,t),db64):console.error("storeNames should be an array"),use:(e,t)=>(indexedDB.open(e,1).onsuccess=r=>{r.target.result.objectStoreNames.contains(t)||console.error(`Store ${t} does not exist. You may need to manage the lifecycle of databse "${e}" if outdated.`)},{set:async(r,o)=>openDatabase(e,t).then((e=>setData(e,t,r,o))).catch(console.error),setEntries:async r=>openDatabase(e,t).then((e=>setData(e,t,null,r,"entries"))).catch(console.error),get:async r=>openDatabase(e,t).then((e=>getData(e,t,r))).catch(console.error),getEntries:async r=>openDatabase(e,t).then((e=>getData(e,t,r,"entries"))).catch(console.error),delete:async r=>openDatabase(e,t).then((e=>deleteData(e,t,r))).catch(console.error)}),clear:async(e,t)=>openDatabase(e,t).then((e=>clearStore(e,t))).catch(console.error),delete:async e=>deleteDB(e).catch(console.error)};export default db64; | ||
const{isArray:isArray}=Array,connections=[],openDatabase=(e="default",t)=>new Promise(((r,s)=>{const n=window.indexedDB.open(e,1);n.onupgradeneeded=({target:e})=>{const{result:r}=e;t.forEach((e=>{if(!r.objectStoreNames.contains(e)){r.createObjectStore(e).onerror=e=>s(e.target.error)}}))},n.onsuccess=({target:e})=>{connections.push(n),r(e.result)},n.onerror=({target:e})=>{s(e.result)}})),setData=async(e,t,r,s,n)=>new Promise(((o,a)=>{try{const a=e.transaction([t],"readwrite").objectStore(t);if(n){const e=isArray(s)?()=>s.map(((e,t)=>a.put(e,t))):()=>Object.entries(s).map((([e,t])=>a.put(t,e)));o(Promise.all(e()))}else o(a.put(s,r));o(db64)}catch(e){a(e)}})),getData=async(e,t,r,s)=>new Promise((n=>{const o=e.transaction([t]).objectStore(t);if(s){const e={};o.openCursor().onsuccess=t=>{const s=t.target.result;s?(r.includes(s.key)&&(e[s.key]=s.value),s.continue()):n(e)}}else{const e=o.get(r);e.onsuccess=()=>n(e.result)}})),deleteData=async(e,t,r)=>new Promise(((s,n)=>{try{const n=e.transaction([t],"readwrite").objectStore(t);n.openCursor().onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())},s(db64)}catch(e){n(e)}})),clearStore=(e,t)=>new Promise(((r,s)=>{const n=e.transaction([t],"readwrite").objectStore(t).clear();n.onsuccess=()=>r(db64),n.onerror=e=>s(e.target.error)})),deleteDB=e=>{const t=[];return new Promise(((r,s)=>{const n=indexedDB.deleteDatabase(e);n.onsuccess=()=>r(db64),n.onerror=({target:e})=>s(new Error(`Error deleting database: ${e.error}`)),n.onblocked=()=>{for(const r of connections)r.result.name===e&&(r.result.close(),t.push(e));t.includes(e)?r(db64):deleteDB(e)}}))},db64={create:async(e,t)=>("string"!=typeof e&&console.error(`${e} should be a string`),isArray(t)?openDatabase(e,t):console.error(`${t} should be an array`)),use:(e,t)=>("string"!=typeof e&&console.error(`${e} should be a string`),"string"!=typeof e&&console.error(`${t} should be a string`),{set:async(r,s)=>openDatabase(e,[t]).then((e=>setData(e,t,r,s))),setEntries:async r=>openDatabase(e,t).then((e=>setData(e,t,null,r,"entries"))),get:async r=>openDatabase(e,t).then((e=>getData(e,t,r))),getEntries:async r=>openDatabase(e,t).then((e=>getData(e,t,r,"entries"))),delete:async r=>openDatabase(e,t).then((e=>deleteData(e,t,r)))}),clear:async(e,t)=>openDatabase(e,t).then((e=>clearStore(e,t))),delete:async e=>deleteDB(e)};export default db64; | ||
//# sourceMappingURL=db64.min.map |
{ | ||
"name": "db64", | ||
"version": "0.7.9", | ||
"version": "0.8.0", | ||
"description": "A Practical IndexedDB API", | ||
@@ -5,0 +5,0 @@ "main": "db64.js", |
@@ -14,3 +14,3 @@ # db64 | ||
- No versioning | ||
- 2.51KB minified | ||
- 2.38KB minified | ||
@@ -137,5 +137,4 @@ E.g. | ||
It's important to consider step 4, if not you may leave users stuck because everything looks fine on your computer. | ||
Step 4 isn't specific to IndexedDB, it also applies to _localStorage_ but it's more prevalent with IndexedDB as there's | ||
a higher risk of the application breaking if you decide to make chages to the codebase in the future. | ||
It's important to consider step 4, if not you may leave users stuck because everything will look fine on your computer. | ||
Step 4 isn't specific to IndexedDB, it mostly applies to _localStorage_. It's the same for all persistent storage on all platforms. Your application is at risk of breaking if you decide to change the persistent data structure or add to the structure in the future without preemptively managing common user cases. | ||
@@ -142,0 +141,0 @@ If you do require versioning consider using [idb](https://github.com/jakearchibald/idb). **If you're not building a progressive web app (PWA) you probably don't need versioning**. |
@@ -136,5 +136,4 @@ # db64 | ||
It's important to consider step 4, if not you may leave users stuck because everything looks fine on your computer. | ||
Step 4 isn't specific to IndexedDB, it also applies to _localStorage_ but it's more prevalent with IndexedDB as there's | ||
a higher risk of the application breaking if you decide to make chages to the codebase in the future. | ||
It's important to consider step 4, if not you may leave users stuck because everything will look fine on your computer. | ||
Step 4 isn't specific to IndexedDB, it mostly applies to _localStorage_. It's the same for all persistent storage on all platforms. Your application is at risk of breaking if you decide to change the persistent data structure or add to the structure in the future without preemptively managing common user cases. | ||
@@ -141,0 +140,0 @@ If you do require versioning consider using [idb](https://github.com/jakearchibald/idb). **If you're not building a progressive web app (PWA) you probably don't need versioning**. |
147
src/db64.js
@@ -12,10 +12,4 @@ const { isArray } = Array | ||
const openDatabase = (name = 'default', storeNames) => new Promise((resolve, reject) => { | ||
let database | ||
try { | ||
database = window.indexedDB.open(name, 1) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
const DBOpenRequest = window.indexedDB.open(name, 1) | ||
/* | ||
@@ -26,3 +20,3 @@ db64 does not revolve around versioning. Therefore, this function will only run once | ||
*/ | ||
database.onupgradeneeded = ({ target }) => { | ||
DBOpenRequest.onupgradeneeded = ({ target }) => { | ||
const { result } = target | ||
@@ -41,9 +35,10 @@ storeNames.forEach(storeName => { | ||
*/ | ||
database.onsuccess = ({ target }) => { | ||
connections.push(database) | ||
DBOpenRequest.onsuccess = ({ target }) => { | ||
connections.push(DBOpenRequest) | ||
resolve(target.result) | ||
} | ||
database.onerror = ({ target }) => reject(target.result) | ||
return database | ||
DBOpenRequest.onerror = ({ target }) => { | ||
reject(target.result) | ||
} | ||
}) | ||
@@ -61,6 +56,5 @@ | ||
*/ | ||
const setData = async (database, storeName, key, dataValue, entries) => { | ||
const setData = async (database, storeName, key, dataValue, entries) => new Promise((resolve, reject) => { | ||
try { | ||
const obStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
if (entries) { | ||
@@ -70,11 +64,11 @@ const dataEntries = isArray(dataValue) | ||
: () => Object.entries(dataValue).map(([key, value]) => obStore.put(value, key)) | ||
await Promise.all(dataEntries()) | ||
resolve(Promise.all(dataEntries())) | ||
} else { | ||
await obStore.put(dataValue, key) | ||
resolve(obStore.put(dataValue, key)) | ||
} | ||
resolve(db64) | ||
} catch (e) { | ||
console.error(e) | ||
reject(e) | ||
} | ||
return db64 | ||
} | ||
}) | ||
@@ -90,28 +84,27 @@ | ||
*/ | ||
const getData = async (database, storeName, key, entries) => { | ||
return new Promise((resolve) => { | ||
const objectStore = (database.transaction([storeName])).objectStore(storeName) | ||
let dataRequest | ||
if (entries) { | ||
const results = {} | ||
const cursorRequest = objectStore.openCursor() | ||
const getData = async (database, storeName, key, entries) => new Promise((resolve) => { | ||
const objectStore = (database.transaction([storeName])).objectStore(storeName) | ||
cursorRequest.onsuccess = e => { | ||
const cursor = e.target.result | ||
if (entries) { | ||
const results = {} | ||
const cursorRequest = objectStore.openCursor() | ||
if (cursor) { | ||
if (key.includes(cursor.key)) results[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
resolve(results) | ||
} | ||
cursorRequest.onsuccess = e => { | ||
const cursor = e.target.result | ||
if (cursor) { | ||
if (key.includes(cursor.key)) results[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
resolve(results) | ||
} | ||
} else { | ||
dataRequest = objectStore.get(key) | ||
dataRequest.onsuccess = () => resolve(dataRequest.result) | ||
} | ||
}) | ||
} | ||
} else { | ||
const dataRequest = objectStore.get(key) | ||
dataRequest.onsuccess = () => resolve(dataRequest.result) | ||
} | ||
}) | ||
/* | ||
@@ -124,3 +117,3 @@ Deletes an entry for a given store by key. | ||
*/ | ||
const deleteData = async (database, storeName, key) => { | ||
const deleteData = async (database, storeName, key) => new Promise((resolve, reject) => { | ||
try { | ||
@@ -138,7 +131,7 @@ const objectStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
} | ||
resolve(db64) | ||
} catch (e) { | ||
console.error(e) | ||
reject(e) | ||
} | ||
return db64 | ||
} | ||
}) | ||
@@ -152,11 +145,9 @@ | ||
*/ | ||
const clearStore = (database, storeName) => { | ||
return new Promise((resolve, reject) => { | ||
const objectStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
const objectStoreRequest = objectStore.clear() | ||
const clearStore = (database, storeName) => new Promise((resolve, reject) => { | ||
const objectStore = (database.transaction([storeName], 'readwrite')).objectStore(storeName) | ||
const objectStoreRequest = objectStore.clear() | ||
objectStoreRequest.onsuccess = resolve(db64) | ||
objectStoreRequest.onerror = e => reject(e.target.error) | ||
}) | ||
} | ||
objectStoreRequest.onsuccess = () => resolve(db64) | ||
objectStoreRequest.onerror = e => reject(e.target.error) | ||
}) | ||
@@ -170,16 +161,23 @@ | ||
const deleteDB = name => { | ||
const deletedDBs = [] | ||
return new Promise((resolve, reject) => { | ||
const deleteRequest = indexedDB.deleteDatabase(name) | ||
const DBDeleteRequest = indexedDB.deleteDatabase(name) | ||
deleteRequest.onsuccess = () => resolve(db64) | ||
DBDeleteRequest.onsuccess = () => resolve(db64) | ||
deleteRequest.onerror = ({ target }) => reject(new Error(`Error deleting database: ${target.error}`)) | ||
DBDeleteRequest.onerror = ({ target }) => reject(new Error(`Error deleting database: ${target.error}`)) | ||
deleteRequest.onblocked = () => { | ||
DBDeleteRequest.onblocked = () => { | ||
for (const database of connections) { | ||
if (database.result.name === name) { | ||
database.result.close() | ||
deletedDBs.push(name) | ||
} | ||
} | ||
deleteDB(name) | ||
if (!deletedDBs.includes(name)) { | ||
deleteDB(name) | ||
} else { | ||
resolve(db64) | ||
} | ||
} | ||
@@ -194,42 +192,29 @@ }) | ||
create: async (name, storeNames) => { | ||
if (!isArray(storeNames)) return console.error('storeNames should be an array') | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
if (!isArray(storeNames)) return console.error(`${storeNames} should be an array`) | ||
await openDatabase(name, storeNames) | ||
return db64 | ||
return openDatabase(name, storeNames) | ||
}, | ||
use: (name, storeName) => { | ||
const request = indexedDB.open(name, 1) | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
if (typeof name !== 'string') console.error(`${storeName} should be a string`) | ||
request.onsuccess = (event) => { | ||
const db = event.target.result | ||
if (!db.objectStoreNames.contains(storeName)) { | ||
console.error(`Store ${storeName} does not exist. You may need to manage the lifecycle of databse "${name}" if outdated.`) | ||
} | ||
} | ||
return { | ||
set: async (key, value) => openDatabase(name, storeName) | ||
.then(database => setData(database, storeName, key, value)) | ||
.catch(console.error), | ||
set: async (key, value) => openDatabase(name, [storeName]) | ||
.then(database => setData(database, storeName, key, value)), | ||
setEntries: async (value) => openDatabase(name, storeName) | ||
.then(database => setData(database, storeName, null, value, 'entries')) | ||
.catch(console.error), | ||
.then(database => setData(database, storeName, null, value, 'entries')), | ||
get: async key => openDatabase(name, storeName) | ||
.then(database => getData(database, storeName, key)) | ||
.catch(console.error), | ||
.then(database => getData(database, storeName, key)), | ||
getEntries: async (keys) => openDatabase(name, storeName) | ||
.then(database => getData(database, storeName, keys, 'entries')) | ||
.catch(console.error), | ||
.then(database => getData(database, storeName, keys, 'entries')), | ||
delete: async (keys) => openDatabase(name, storeName) | ||
.then(database => deleteData(database, storeName, keys)) | ||
.catch(console.error) | ||
.then(database => deleteData(database, storeName, keys)), | ||
} | ||
}, | ||
clear: async (name, storeName) => openDatabase(name, storeName) | ||
.then(database => clearStore(database, storeName)) | ||
.catch(console.error), | ||
.then(database => clearStore(database, storeName)), | ||
delete: async name => deleteDB(name) | ||
.catch(console.error) | ||
} | ||
export default db64 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
17
51169
687
149