idb-keyval
Advanced tools
| 'use strict'; | ||
| var db; | ||
| function getDB() { | ||
| if (!db) { | ||
| db = new Promise(function(resolve, reject) { | ||
| var openreq = indexedDB.open('keyval-store', 1); | ||
| openreq.onerror = function() { | ||
| reject(openreq.error); | ||
| }; | ||
| openreq.onupgradeneeded = function() { | ||
| // First time setup: create an empty object store | ||
| openreq.result.createObjectStore('keyval'); | ||
| }; | ||
| openreq.onsuccess = function() { | ||
| resolve(openreq.result); | ||
| }; | ||
| }); | ||
| } | ||
| return db; | ||
| } | ||
| function withStore(type, callback) { | ||
| return getDB().then(function(db) { | ||
| return new Promise(function(resolve, reject) { | ||
| var transaction = db.transaction('keyval', type); | ||
| transaction.oncomplete = function() { | ||
| resolve(); | ||
| }; | ||
| transaction.onerror = function() { | ||
| reject(transaction.error); | ||
| }; | ||
| callback(transaction.objectStore('keyval')); | ||
| }); | ||
| }); | ||
| } | ||
| var idbKeyval = { | ||
| get: function(key) { | ||
| var req; | ||
| return withStore('readonly', function(store) { | ||
| req = store.get(key); | ||
| }).then(function() { | ||
| return req.result; | ||
| }); | ||
| }, | ||
| set: function(key, value) { | ||
| return withStore('readwrite', function(store) { | ||
| store.put(value, key); | ||
| }); | ||
| }, | ||
| delete: function(key) { | ||
| return withStore('readwrite', function(store) { | ||
| store.delete(key); | ||
| }); | ||
| }, | ||
| clear: function() { | ||
| return withStore('readwrite', function(store) { | ||
| store.clear(); | ||
| }); | ||
| }, | ||
| keys: function() { | ||
| var keys = []; | ||
| return withStore('readonly', function(store) { | ||
| // This would be store.getAllKeys(), but it isn't supported by Edge or Safari. | ||
| // And openKeyCursor isn't supported by Safari. | ||
| (store.openKeyCursor || store.openCursor).call(store).onsuccess = function() { | ||
| if (!this.result) return; | ||
| keys.push(this.result.key); | ||
| this.result.continue(); | ||
| }; | ||
| }).then(function() { | ||
| return keys; | ||
| }); | ||
| } | ||
| }; | ||
| module.exports = idbKeyval; |
| var idbKeyval = (function () { | ||
| 'use strict'; | ||
| var db; | ||
| function getDB() { | ||
| if (!db) { | ||
| db = new Promise(function(resolve, reject) { | ||
| var openreq = indexedDB.open('keyval-store', 1); | ||
| openreq.onerror = function() { | ||
| reject(openreq.error); | ||
| }; | ||
| openreq.onupgradeneeded = function() { | ||
| // First time setup: create an empty object store | ||
| openreq.result.createObjectStore('keyval'); | ||
| }; | ||
| openreq.onsuccess = function() { | ||
| resolve(openreq.result); | ||
| }; | ||
| }); | ||
| } | ||
| return db; | ||
| } | ||
| function withStore(type, callback) { | ||
| return getDB().then(function(db) { | ||
| return new Promise(function(resolve, reject) { | ||
| var transaction = db.transaction('keyval', type); | ||
| transaction.oncomplete = function() { | ||
| resolve(); | ||
| }; | ||
| transaction.onerror = function() { | ||
| reject(transaction.error); | ||
| }; | ||
| callback(transaction.objectStore('keyval')); | ||
| }); | ||
| }); | ||
| } | ||
| var idbKeyval = { | ||
| get: function(key) { | ||
| var req; | ||
| return withStore('readonly', function(store) { | ||
| req = store.get(key); | ||
| }).then(function() { | ||
| return req.result; | ||
| }); | ||
| }, | ||
| set: function(key, value) { | ||
| return withStore('readwrite', function(store) { | ||
| store.put(value, key); | ||
| }); | ||
| }, | ||
| delete: function(key) { | ||
| return withStore('readwrite', function(store) { | ||
| store.delete(key); | ||
| }); | ||
| }, | ||
| clear: function() { | ||
| return withStore('readwrite', function(store) { | ||
| store.clear(); | ||
| }); | ||
| }, | ||
| keys: function() { | ||
| var keys = []; | ||
| return withStore('readonly', function(store) { | ||
| // This would be store.getAllKeys(), but it isn't supported by Edge or Safari. | ||
| // And openKeyCursor isn't supported by Safari. | ||
| (store.openKeyCursor || store.openCursor).call(store).onsuccess = function() { | ||
| if (!this.result) return; | ||
| keys.push(this.result.key); | ||
| this.result.continue(); | ||
| }; | ||
| }).then(function() { | ||
| return keys; | ||
| }); | ||
| } | ||
| }; | ||
| return idbKeyval; | ||
| }()); |
| var idbKeyval=function(){"use strict";var db;function getDB(){if(!db){db=new Promise(function(resolve,reject){var openreq=indexedDB.open("keyval-store",1);openreq.onerror=function(){reject(openreq.error)};openreq.onupgradeneeded=function(){openreq.result.createObjectStore("keyval")};openreq.onsuccess=function(){resolve(openreq.result)}})}return db}function withStore(type,callback){return getDB().then(function(db){return new Promise(function(resolve,reject){var transaction=db.transaction("keyval",type);transaction.oncomplete=function(){resolve()};transaction.onerror=function(){reject(transaction.error)};callback(transaction.objectStore("keyval"))})})}var idbKeyval={get:function(key){var req;return withStore("readonly",function(store){req=store.get(key)}).then(function(){return req.result})},set:function(key,value){return withStore("readwrite",function(store){store.put(value,key)})},delete:function(key){return withStore("readwrite",function(store){store.delete(key)})},clear:function(){return withStore("readwrite",function(store){store.clear()})},keys:function(){var keys=[];return withStore("readonly",function(store){(store.openKeyCursor||store.openCursor).call(store).onsuccess=function(){if(!this.result)return;keys.push(this.result.key);this.result.continue()}}).then(function(){return keys})}};return idbKeyval}(); |
| export default { | ||
| input: 'idb-keyval.js', | ||
| output: [{ | ||
| file: 'dist/idb-keyval-iife.js', | ||
| format: 'iife', | ||
| name: 'idbKeyval', | ||
| }, { | ||
| file: 'dist/idb-keyval-cjs.js', | ||
| format: 'cjs', | ||
| name: 'idbKeyval', | ||
| }] | ||
| } |
+68
-81
@@ -1,91 +0,78 @@ | ||
| (function() { | ||
| 'use strict'; | ||
| var db; | ||
| var db; | ||
| function getDB() { | ||
| if (!db) { | ||
| db = new Promise(function(resolve, reject) { | ||
| var openreq = indexedDB.open('keyval-store', 1); | ||
| function getDB() { | ||
| if (!db) { | ||
| db = new Promise(function(resolve, reject) { | ||
| var openreq = indexedDB.open('keyval-store', 1); | ||
| openreq.onerror = function() { | ||
| reject(openreq.error); | ||
| }; | ||
| openreq.onerror = function() { | ||
| reject(openreq.error); | ||
| }; | ||
| openreq.onupgradeneeded = function() { | ||
| // First time setup: create an empty object store | ||
| openreq.result.createObjectStore('keyval'); | ||
| }; | ||
| openreq.onupgradeneeded = function() { | ||
| // First time setup: create an empty object store | ||
| openreq.result.createObjectStore('keyval'); | ||
| }; | ||
| openreq.onsuccess = function() { | ||
| resolve(openreq.result); | ||
| }; | ||
| }); | ||
| } | ||
| return db; | ||
| openreq.onsuccess = function() { | ||
| resolve(openreq.result); | ||
| }; | ||
| }); | ||
| } | ||
| return db; | ||
| } | ||
| function withStore(type, callback) { | ||
| return getDB().then(function(db) { | ||
| return new Promise(function(resolve, reject) { | ||
| var transaction = db.transaction('keyval', type); | ||
| transaction.oncomplete = function() { | ||
| resolve(); | ||
| }; | ||
| transaction.onerror = function() { | ||
| reject(transaction.error); | ||
| }; | ||
| callback(transaction.objectStore('keyval')); | ||
| }); | ||
| function withStore(type, callback) { | ||
| return getDB().then(function(db) { | ||
| return new Promise(function(resolve, reject) { | ||
| var transaction = db.transaction('keyval', type); | ||
| transaction.oncomplete = function() { | ||
| resolve(); | ||
| }; | ||
| transaction.onerror = function() { | ||
| reject(transaction.error); | ||
| }; | ||
| callback(transaction.objectStore('keyval')); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| var idbKeyval = { | ||
| get: function(key) { | ||
| var req; | ||
| return withStore('readonly', function(store) { | ||
| req = store.get(key); | ||
| }).then(function() { | ||
| return req.result; | ||
| }); | ||
| }, | ||
| set: function(key, value) { | ||
| return withStore('readwrite', function(store) { | ||
| store.put(value, key); | ||
| }); | ||
| }, | ||
| delete: function(key) { | ||
| return withStore('readwrite', function(store) { | ||
| store.delete(key); | ||
| }); | ||
| }, | ||
| clear: function() { | ||
| return withStore('readwrite', function(store) { | ||
| store.clear(); | ||
| }); | ||
| }, | ||
| keys: function() { | ||
| var keys = []; | ||
| return withStore('readonly', function(store) { | ||
| // This would be store.getAllKeys(), but it isn't supported by Edge or Safari. | ||
| // And openKeyCursor isn't supported by Safari. | ||
| (store.openKeyCursor || store.openCursor).call(store).onsuccess = function() { | ||
| if (!this.result) return; | ||
| keys.push(this.result.key); | ||
| this.result.continue(); | ||
| }; | ||
| }).then(function() { | ||
| return keys; | ||
| }); | ||
| } | ||
| }; | ||
| if (typeof module != 'undefined' && module.exports) { | ||
| module.exports = idbKeyval; | ||
| } else if (typeof define === 'function' && define.amd) { | ||
| define('idbKeyval', [], function() { | ||
| return idbKeyval; | ||
| export default { | ||
| get: function(key) { | ||
| var req; | ||
| return withStore('readonly', function(store) { | ||
| req = store.get(key); | ||
| }).then(function() { | ||
| return req.result; | ||
| }); | ||
| } else { | ||
| self.idbKeyval = idbKeyval; | ||
| }, | ||
| set: function(key, value) { | ||
| return withStore('readwrite', function(store) { | ||
| store.put(value, key); | ||
| }); | ||
| }, | ||
| delete: function(key) { | ||
| return withStore('readwrite', function(store) { | ||
| store.delete(key); | ||
| }); | ||
| }, | ||
| clear: function() { | ||
| return withStore('readwrite', function(store) { | ||
| store.clear(); | ||
| }); | ||
| }, | ||
| keys: function() { | ||
| var keys = []; | ||
| return withStore('readonly', function(store) { | ||
| // This would be store.getAllKeys(), but it isn't supported by Edge or Safari. | ||
| // And openKeyCursor isn't supported by Safari. | ||
| (store.openKeyCursor || store.openCursor).call(store).onsuccess = function() { | ||
| if (!this.result) return; | ||
| keys.push(this.result.key); | ||
| this.result.continue(); | ||
| }; | ||
| }).then(function() { | ||
| return keys; | ||
| }); | ||
| } | ||
| }()); | ||
| }; |
+10
-4
| { | ||
| "name": "idb-keyval", | ||
| "version": "2.3.0", | ||
| "version": "2.4.0", | ||
| "description": "A super-simple-small keyval store built on top of IndexedDB", | ||
| "main": "idb-keyval.js", | ||
| "main": "dist/idb-keyval-cjs.js", | ||
| "module": "idb-keyval.js", | ||
| "typings": "typings.d.ts", | ||
| "scripts": { | ||
| "build": "uglifyjs idb-keyval.js --screw-ie8 -mc --output dist/idb-keyval-min.js" | ||
| "build": "del dist && rollup -c && uglifyjs -o dist/idb-keyval-iife.min.js dist/idb-keyval-iife.js" | ||
| }, | ||
@@ -24,2 +25,5 @@ "repository": { | ||
| "author": "Jake Archibald", | ||
| "contributors": [ | ||
| "Benny Powers" | ||
| ], | ||
| "license": "Apache-2.0", | ||
@@ -31,4 +35,6 @@ "bugs": { | ||
| "devDependencies": { | ||
| "uglify-js": "^2.7.0" | ||
| "del-cli": "^1.1.0", | ||
| "rollup": "^0.56.5", | ||
| "uglify-es": "^3.3.9" | ||
| } | ||
| } |
+21
-1
@@ -7,3 +7,3 @@ # IDB-Keyval | ||
| This is only a keyval store. If you need to do more complex things like iteration & indexing, check out [IDB on NPM](https://www.npmjs.com/package/idb) (a little heavier at 1.7k). The first example in its README is how to recreate this library. | ||
| This is only a keyval store. If you need to do more complex things like iteration & indexing, check out [IDB on NPM](https://www.npmjs.com/package/idb) (a little heavier at 1.7k). The first example in its README is how to recreate this library. | ||
@@ -58,1 +58,21 @@ ## Usage | ||
| That's it! | ||
| ## Installing | ||
| ### Via npm | ||
| ```sh | ||
| npm install idb-keyval | ||
| ``` | ||
| Now you can require/import `idb-keyval`: | ||
| ```js | ||
| const idbKeyval = require('idb-keyval'); | ||
| ``` | ||
| ### Via <script> | ||
| `idb-keyval.js` is a valid JS module. | ||
| `dist/idb-keyval.iffe.js` can be used in browsers that don't support modules. `idbKeyval` is created as a global. |
+5
-5
| declare module 'idb-keyval' { | ||
| interface IDBKeyVal<Key> { | ||
| /** Add a new value or update an existing one */ | ||
| set<Value>(key: Key, value: Value): PromiseLike<void>; | ||
| set<Value>(key: Key, value: Value): Promise<void>; | ||
| /** Get a value by key */ | ||
| get<Value>(key: Key): PromiseLike<Value>; | ||
| get<Value>(key: Key): Promise<Value>; | ||
| /** Get all keys in the database */ | ||
| keys(): PromiseLike<Key[]>; | ||
| keys(): Promise<Key[]>; | ||
| /** Delete an entry in the database by key */ | ||
| delete(key: Key): PromiseLike<void>; | ||
| delete(key: Key): Promise<void>; | ||
| /** Delete all entries in the database */ | ||
| clear(): PromiseLike<void>; | ||
| clear(): Promise<void>; | ||
| } | ||
@@ -18,0 +18,0 @@ |
Sorry, the diff of this file is not supported yet
| !function(){"use strict";function e(){return t||(t=new Promise(function(e,n){var t=indexedDB.open("keyval-store",1);t.onerror=function(){n(t.error)},t.onupgradeneeded=function(){t.result.createObjectStore("keyval")},t.onsuccess=function(){e(t.result)}})),t}function n(n,t){return e().then(function(e){return new Promise(function(r,o){var u=e.transaction("keyval",n);u.oncomplete=function(){r()},u.onerror=function(){o(u.error)},t(u.objectStore("keyval"))})})}var t,r={get:function(e){var t;return n("readonly",function(n){t=n.get(e)}).then(function(){return t.result})},set:function(e,t){return n("readwrite",function(n){n.put(t,e)})},"delete":function(e){return n("readwrite",function(n){n["delete"](e)})},clear:function(){return n("readwrite",function(e){e.clear()})},keys:function(){var e=[];return n("readonly",function(n){(n.openKeyCursor||n.openCursor).call(n).onsuccess=function(){this.result&&(e.push(this.result.key),this.result["continue"]())}}).then(function(){return e})}};"undefined"!=typeof module&&module.exports?module.exports=r:"function"==typeof define&&define.amd?define("idbKeyval",[],function(){return r}):self.idbKeyval=r}(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
11284
63.7%9
28.57%250
138.1%77
35.09%3
200%1
Infinity%