idb-keyval
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -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; | ||
}); | ||
} | ||
}()); | ||
}; |
{ | ||
"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" | ||
} | ||
} |
@@ -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. |
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 @@ |
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
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
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
11284
9
250
77
3
1