Comparing version 0.8.4 to 0.8.5
@@ -1,2 +0,2 @@ | ||
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))}catch(e){a(e)}})),getData=async(e,t,r,s)=>new Promise(((n,o)=>{const a=e.transaction([t]).objectStore(t);if(s){const e={},t=a.openCursor();t.onsuccess=t=>{const s=t.target.result;s?(r.includes(s.key)&&(e[s.key]=s.value),s.continue()):n(e)},t.onerror=e=>o(e)}else{const e=a.get(r);e.onsuccess=()=>n(e.result),e.onerror=e=>o(e)}})),deleteData=async(e,t,r)=>new Promise(((s,n)=>{try{const o=e.transaction([t],"readwrite").objectStore(t).openCursor();o.onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())},o.onerror=e=>n(e),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=e=>s(e),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; | ||
const{isArray:isArray}=Array,connections=[],isString=e=>"string"==typeof e,has=async(e,t)=>{isString(e)||console.error(`${e} should be a string`);isString(t)||isArray(t)||console.error(`${t} should be a string or an array`);const r=await openDatabase(e,isArray(t)?t:[t]);return isArray(t)?t.every((e=>r.objectStoreNames.contains(e))):r.objectStoreNames.contains(t)},deleteDatabase=async e=>deleteDB(e),openDatabase=(e="default",t)=>new Promise(((r,s)=>{const a=window.indexedDB.open(e,1);a.onupgradeneeded=({target:e})=>{const{result:r}=e;t.forEach((e=>{if(!r.objectStoreNames.contains(e)){r.createObjectStore(e).onerror=e=>s(e.target.error)}}))},a.onsuccess=({target:e})=>{connections.push(a),r(e.result)},a.onerror=({target:e})=>s(e.result)})),setData=async(e,t,r,s,a)=>new Promise(((n,o)=>{try{const o=e.transaction([t],"readwrite").objectStore(t);if(a){const e=isArray(s)?()=>s.map(((e,t)=>o.put(e,t))):()=>Object.entries(s).map((([e,t])=>o.put(t,e)));n(Promise.all(e()))}else n(o.put(s,r))}catch(e){o(e)}})),getData=async(e,t,r,s)=>new Promise(((a,n)=>{e.objectStoreNames.contains(t)||console.error(`Store ${t} was not found in database ${e}`);const o=e.transaction([t]).objectStore(t);if(s){const e={},t=o.openCursor();t.onsuccess=t=>{const s=t.target.result;s?(r.includes(s.key)&&(e[s.key]=s.value),s.continue()):a(e)},t.onerror=e=>n(e)}else{const e=o.get(r);e.onsuccess=()=>a(e.result),e.onerror=e=>n(e)}})),deleteData=async(e,t,r)=>new Promise(((s,a)=>{try{const n=e.transaction([t],"readwrite").objectStore(t).openCursor();n.onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())},n.onerror=e=>a(e),s(db64)}catch(e){a(e)}})),clearStore=(e,t)=>new Promise(((r,s)=>{const a=e.transaction([t],"readwrite").objectStore(t).clear();a.onsuccess=()=>r(db64),a.onerror=e=>s(e.target.error)})),deleteDB=e=>{const t=[];return new Promise(((r,s)=>{const a=indexedDB.deleteDatabase(e);a.onsuccess=()=>r(db64),a.onerror=e=>s(e),a.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,r="enable-delete")=>{if(isString(e)||console.error(`${e} should be a string`),!isArray(t))return console.error(`${t} should be an array`);return"disable-delete"===r||await has(e,t)||(console.log("delete"),await deleteDB(e)),openDatabase(e,t)},has:has,use:(e,t)=>(isString(e)||console.error(`${e} should be a string`),isString(t)||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:deleteDatabase};module.exports=db64; | ||
//# sourceMappingURL=db64-cjs.min.map |
export default db64; | ||
declare namespace db64 { | ||
export function create(name: any, storeNames: any): Promise<any>; | ||
export function create(name: any, storeNames: any, storeDelete?: string): Promise<any>; | ||
export { has }; | ||
export function use(name: any, storeName: any): { | ||
@@ -12,5 +13,6 @@ set: (key: any, value: any) => Promise<any>; | ||
export function clear(name: any, storeName: any): Promise<any>; | ||
export function _delete(name: any): Promise<any>; | ||
export { _delete as delete }; | ||
export { deleteDatabase as delete }; | ||
} | ||
declare function has(name: any, storeName: any): Promise<any>; | ||
declare function deleteDatabase(name: any): Promise<any>; | ||
//# sourceMappingURL=db64.d.ts.map |
const { isArray } = Array | ||
const connections = [] | ||
const isString = value => typeof value === 'string' | ||
const has = async (name, storeName) => { | ||
if (!isString(name)) console.error(`${name} should be a string`) | ||
const isStringOrArray = isString(storeName) || isArray(storeName) | ||
if (!isStringOrArray) console.error(`${storeName} should be a string or an array`) | ||
const db = await openDatabase(name, isArray(storeName) ? storeName : [storeName]) | ||
if (isArray(storeName)) return storeName.every(store => db.objectStoreNames.contains(store)) | ||
return db.objectStoreNames.contains(storeName) | ||
} | ||
const deleteDatabase = async name => deleteDB(name) | ||
/* | ||
@@ -77,2 +91,6 @@ Creates a new database with given stores if the database and stores don't exist. | ||
const getData = async (database, storeName, key, entries) => new Promise((resolve, reject) => { | ||
if (!database.objectStoreNames.contains(storeName)) { | ||
console.error(`Store ${storeName} was not found in database ${database}`) | ||
} | ||
const objectStore = (database.transaction([storeName])).objectStore(storeName) | ||
@@ -182,11 +200,18 @@ | ||
const db64 = { | ||
create: async (name, storeNames) => { | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
create: async (name, storeNames, storeDelete = 'enable-delete') => { | ||
if (!isString(name)) console.error(`${name} should be a string`) | ||
if (!isArray(storeNames)) return console.error(`${storeNames} should be an array`) | ||
const shouldDisableDelete = storeDelete === 'disable-delete' | ||
if (!shouldDisableDelete && !await has(name, storeNames)) { | ||
console.log('delete') | ||
await deleteDB(name) | ||
} | ||
return openDatabase(name, storeNames) | ||
}, | ||
has, | ||
use: (name, storeName) => { | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
if (typeof name !== 'string') console.error(`${storeName} should be a string`) | ||
if (!isString(name)) console.error(`${name} should be a string`) | ||
if (!isString(storeName)) console.error(`${storeName} should be a string`) | ||
@@ -208,5 +233,5 @@ return { | ||
.then(database => clearStore(database, storeName)), | ||
delete: async name => deleteDB(name) | ||
delete: deleteDatabase, | ||
} | ||
export default db64 |
@@ -1,2 +0,2 @@ | ||
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))}catch(e){a(e)}})),getData=async(e,t,r,s)=>new Promise(((n,o)=>{const a=e.transaction([t]).objectStore(t);if(s){const e={},t=a.openCursor();t.onsuccess=t=>{const s=t.target.result;s?(r.includes(s.key)&&(e[s.key]=s.value),s.continue()):n(e)},t.onerror=e=>o(e)}else{const e=a.get(r);e.onsuccess=()=>n(e.result),e.onerror=e=>o(e)}})),deleteData=async(e,t,r)=>new Promise(((s,n)=>{try{const o=e.transaction([t],"readwrite").objectStore(t).openCursor();o.onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())},o.onerror=e=>n(e),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=e=>s(e),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; | ||
const{isArray:isArray}=Array,connections=[],isString=e=>"string"==typeof e,has=async(e,t)=>{isString(e)||console.error(`${e} should be a string`);isString(t)||isArray(t)||console.error(`${t} should be a string or an array`);const r=await openDatabase(e,isArray(t)?t:[t]);return isArray(t)?t.every((e=>r.objectStoreNames.contains(e))):r.objectStoreNames.contains(t)},deleteDatabase=async e=>deleteDB(e),openDatabase=(e="default",t)=>new Promise(((r,s)=>{const a=window.indexedDB.open(e,1);a.onupgradeneeded=({target:e})=>{const{result:r}=e;t.forEach((e=>{if(!r.objectStoreNames.contains(e)){r.createObjectStore(e).onerror=e=>s(e.target.error)}}))},a.onsuccess=({target:e})=>{connections.push(a),r(e.result)},a.onerror=({target:e})=>s(e.result)})),setData=async(e,t,r,s,a)=>new Promise(((n,o)=>{try{const o=e.transaction([t],"readwrite").objectStore(t);if(a){const e=isArray(s)?()=>s.map(((e,t)=>o.put(e,t))):()=>Object.entries(s).map((([e,t])=>o.put(t,e)));n(Promise.all(e()))}else n(o.put(s,r))}catch(e){o(e)}})),getData=async(e,t,r,s)=>new Promise(((a,n)=>{e.objectStoreNames.contains(t)||console.error(`Store ${t} was not found in database ${e}`);const o=e.transaction([t]).objectStore(t);if(s){const e={},t=o.openCursor();t.onsuccess=t=>{const s=t.target.result;s?(r.includes(s.key)&&(e[s.key]=s.value),s.continue()):a(e)},t.onerror=e=>n(e)}else{const e=o.get(r);e.onsuccess=()=>a(e.result),e.onerror=e=>n(e)}})),deleteData=async(e,t,r)=>new Promise(((s,a)=>{try{const n=e.transaction([t],"readwrite").objectStore(t).openCursor();n.onsuccess=e=>{const t=e.target.result;t&&((isArray(r)?r:[r]).includes(t.key)&&t.delete(),t.continue())},n.onerror=e=>a(e),s(db64)}catch(e){a(e)}})),clearStore=(e,t)=>new Promise(((r,s)=>{const a=e.transaction([t],"readwrite").objectStore(t).clear();a.onsuccess=()=>r(db64),a.onerror=e=>s(e.target.error)})),deleteDB=e=>{const t=[];return new Promise(((r,s)=>{const a=indexedDB.deleteDatabase(e);a.onsuccess=()=>r(db64),a.onerror=e=>s(e),a.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,r="enable-delete")=>{if(isString(e)||console.error(`${e} should be a string`),!isArray(t))return console.error(`${t} should be an array`);return"disable-delete"===r||await has(e,t)||(console.log("delete"),await deleteDB(e)),openDatabase(e,t)},has:has,use:(e,t)=>(isString(e)||console.error(`${e} should be a string`),isString(t)||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:deleteDatabase};export default db64; | ||
//# sourceMappingURL=db64.min.map |
{ | ||
"name": "db64", | ||
"version": "0.8.4", | ||
"version": "0.8.5", | ||
"description": "A Practical IndexedDB API", | ||
@@ -5,0 +5,0 @@ "main": "db64.js", |
@@ -5,2 +5,7 @@ # db64 | ||
## A Practical IndexedDB API | ||
> ## $${\color{darkred}[[Disclaimer]]}$$ | ||
> Starting from version 0.8.5, before creating a new databse, the function `db64.create(<database>,[<store>,<store>,...])` will now automatically delete the existing specified database if it does not contain the exact specified stores. | ||
> If you do not want this behavior, set the third argument to `disable-delete` e.g. `db64.create('db', ['x', 'y'], 'disable-delete')` | ||
### [Example](https://julienetie.github.io/db64/examples/input-data/) | ||
@@ -16,3 +21,3 @@ | ||
- No versioning | ||
- 2.38KB minified | 783 bytes _(brotli)_ | ||
- 2.91KB minified | 938 bytes _(brotli)_ | ||
@@ -73,7 +78,18 @@ E.g. | ||
**Create a database with stores** _(string, array)_ | ||
**Create a database with stores** _(string, array, string)_ | ||
```javascript | ||
await db64.create('game-consoles', ['n64', 'ps5', 'dreamcast', 'xbox-360']) | ||
// or | ||
await db64.create(..., ..., 'disable-delete') | ||
``` | ||
By default, if the database to create exists but dosn't have the expected stores, it will be deleted before being re-created. | ||
This can be disabled by using the `'disable-delete'` string. | ||
**Check if a database has a store** _(string, string | array)_ | ||
```javascript | ||
const hasN64 = async db64.has('game-consoles', 'n64') | ||
// or | ||
const hasStores = async db64.has('game-consoles', ['n64', 'dreamcast', 'ps5']) | ||
``` | ||
**Use a store** _(string, string)_ | ||
@@ -80,0 +96,0 @@ ```javascript |
@@ -5,2 +5,7 @@ # db64 | ||
## A Practical IndexedDB API | ||
> ## $${\color{darkred}[[Disclaimer]]}$$ | ||
> Starting from version 0.8.5, before creating a new databse, the function `db64.create(<database>,[<store>,<store>,...])` will now automatically delete the existing specified database if it does not contain the exact specified stores. | ||
> If you do not want this behavior, set the third argument to `disable-delete` e.g. `db64.create('db', ['x', 'y'], 'disable-delete')` | ||
### [Example](https://julienetie.github.io/db64/examples/input-data/) | ||
@@ -72,7 +77,18 @@ | ||
**Create a database with stores** _(string, array)_ | ||
**Create a database with stores** _(string, array, string)_ | ||
```javascript | ||
await db64.create('game-consoles', ['n64', 'ps5', 'dreamcast', 'xbox-360']) | ||
// or | ||
await db64.create(..., ..., 'disable-delete') | ||
``` | ||
By default, if the database to create exists but dosn't have the expected stores, it will be deleted before being re-created. | ||
This can be disabled by using the `'disable-delete'` string. | ||
**Check if a database has a store** _(string, string | array)_ | ||
```javascript | ||
const hasN64 = async db64.has('game-consoles', 'n64') | ||
// or | ||
const hasStores = async db64.has('game-consoles', ['n64', 'dreamcast', 'ps5']) | ||
``` | ||
**Use a store** _(string, string)_ | ||
@@ -79,0 +95,0 @@ ```javascript |
const { isArray } = Array | ||
const connections = [] | ||
const isString = value => typeof value === 'string' | ||
const has = async (name, storeName) => { | ||
if (!isString(name)) console.error(`${name} should be a string`) | ||
const isStringOrArray = isString(storeName) || isArray(storeName) | ||
if (!isStringOrArray) console.error(`${storeName} should be a string or an array`) | ||
const db = await openDatabase(name, isArray(storeName) ? storeName : [storeName]) | ||
if (isArray(storeName)) return storeName.every(store => db.objectStoreNames.contains(store)) | ||
return db.objectStoreNames.contains(storeName) | ||
} | ||
const deleteDatabase = async name => deleteDB(name) | ||
/* | ||
@@ -77,2 +91,6 @@ Creates a new database with given stores if the database and stores don't exist. | ||
const getData = async (database, storeName, key, entries) => new Promise((resolve, reject) => { | ||
if (!database.objectStoreNames.contains(storeName)) { | ||
console.error(`Store ${storeName} was not found in database ${database}`) | ||
} | ||
const objectStore = (database.transaction([storeName])).objectStore(storeName) | ||
@@ -182,11 +200,18 @@ | ||
const db64 = { | ||
create: async (name, storeNames) => { | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
create: async (name, storeNames, storeDelete = 'enable-delete') => { | ||
if (!isString(name)) console.error(`${name} should be a string`) | ||
if (!isArray(storeNames)) return console.error(`${storeNames} should be an array`) | ||
const shouldDisableDelete = storeDelete === 'disable-delete' | ||
if (!shouldDisableDelete && !await has(name, storeNames)) { | ||
console.log('delete') | ||
await deleteDB(name) | ||
} | ||
return openDatabase(name, storeNames) | ||
}, | ||
has, | ||
use: (name, storeName) => { | ||
if (typeof name !== 'string') console.error(`${name} should be a string`) | ||
if (typeof name !== 'string') console.error(`${storeName} should be a string`) | ||
if (!isString(name)) console.error(`${name} should be a string`) | ||
if (!isString(storeName)) console.error(`${storeName} should be a string`) | ||
@@ -208,5 +233,5 @@ return { | ||
.then(database => clearStore(database, storeName)), | ||
delete: async name => deleteDB(name) | ||
delete: deleteDatabase, | ||
} | ||
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
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
71095
1024
215