+13
@@ -0,0 +1,13 @@
|
||||
Copyright 2016, Jake Archibald
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
# IDB-Keyval
|
||||
|
||||
[](https://www.npmjs.com/package/idb-keyval)
|
||||
|
||||
This is a super-simple promise-based keyval store implemented with IndexedDB, originally based on [async-storage by Mozilla](https://github.com/mozilla-b2g/gaia/blob/master/shared/js/async_storage.js).
|
||||
|
||||
It's small and tree-shakeable. If you only use get/set, the library is 295 bytes (brotli'd), if you use all methods it's 573 bytes.
|
||||
|
||||
[localForage](https://github.com/localForage/localForage) offers similar functionality, but supports older browsers with broken/absent IDB implementations. Because of that, it's orders of magnitude bigger (~7k).
|
||||
|
||||
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 1k). The first example in its README is how to create a keyval store.
|
||||
|
||||
## Installing
|
||||
|
||||
### Recommended: Via npm + webpack/rollup/parcel/etc
|
||||
|
||||
```sh
|
||||
npm install idb-keyval
|
||||
```
|
||||
|
||||
Now you can require/import `idb-keyval`:
|
||||
|
||||
```js
|
||||
import { get, set } from 'idb-keyval';
|
||||
```
|
||||
|
||||
If you're targeting IE10/11, use the compat version, and import a `Promise` polyfill.
|
||||
|
||||
```js
|
||||
// Import a Promise polyfill
|
||||
import 'es6-promise/auto';
|
||||
import { get, set } from 'idb-keyval/compat';
|
||||
```
|
||||
|
||||
### All bundles
|
||||
|
||||
A well-behaved bundler should automatically pick the ES module or the CJS module depending on what it supports, but if you need to force it either way:
|
||||
|
||||
- `idb-keyval/dist/index.js` EcmaScript module.
|
||||
- `idb-keyval/dist/index.cjs` CommonJS module.
|
||||
|
||||
Legacy builds:
|
||||
|
||||
- `idb-keyval/compat` transpiled for older browsers; a well-behaved bundler will pick the ES module or CJS module as appropriate.
|
||||
- `idb-keyval/umd` UMD module, transpiled for older browsers; a well-behaved bundler will pick the appropriate module.
|
||||
- `idb-keyval/dist/compat.js` EcmaScript module, transpiled for older browsers.
|
||||
- `idb-keyval/dist/compat.cjs` CommonJS module, transpiled for older browsers.
|
||||
- `idb-keyval/dist/umd.js` UMD module, also transpiled for older browsers.
|
||||
|
||||
These built versions are also available on jsDelivr, e.g.:
|
||||
|
||||
```html
|
||||
<script src="https://cdn.jsdelivr.net/npm/idb-keyval@6/dist/umd.js"></script>
|
||||
<!-- Or in modern browsers: -->
|
||||
<script type="module">
|
||||
import { get, set } from 'https://cdn.jsdelivr.net/npm/idb-keyval@6/+esm';
|
||||
</script>
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### set:
|
||||
|
||||
```js
|
||||
import { set } from 'idb-keyval';
|
||||
|
||||
set('hello', 'world');
|
||||
```
|
||||
|
||||
Since this is IDB-backed, you can store anything structured-clonable (numbers, arrays, objects, dates, blobs etc), although old Edge doesn't support `null`. Keys can be numbers, strings, `Date`s, (IDB also allows arrays of those values, but IE doesn't support it).
|
||||
|
||||
All methods return promises:
|
||||
|
||||
```js
|
||||
import { set } from 'idb-keyval';
|
||||
|
||||
set('hello', 'world')
|
||||
.then(() => console.log('It worked!'))
|
||||
.catch((err) => console.log('It failed!', err));
|
||||
```
|
||||
|
||||
### get:
|
||||
|
||||
```js
|
||||
import { get } from 'idb-keyval';
|
||||
|
||||
// logs: "world"
|
||||
get('hello').then((val) => console.log(val));
|
||||
```
|
||||
|
||||
If there is no 'hello' key, then `val` will be `undefined`.
|
||||
|
||||
### setMany:
|
||||
|
||||
Set many keyval pairs at once. This is faster than calling `set` multiple times.
|
||||
|
||||
```js
|
||||
import { set, setMany } from 'idb-keyval';
|
||||
|
||||
// Instead of:
|
||||
Promise.all([set(123, 456), set('hello', 'world')])
|
||||
.then(() => console.log('It worked!'))
|
||||
.catch((err) => console.log('It failed!', err));
|
||||
|
||||
// It's faster to do:
|
||||
setMany([
|
||||
[123, 456],
|
||||
['hello', 'world'],
|
||||
])
|
||||
.then(() => console.log('It worked!'))
|
||||
.catch((err) => console.log('It failed!', err));
|
||||
```
|
||||
|
||||
This operation is also atomic – if one of the pairs can't be added, none will be added.
|
||||
|
||||
### getMany:
|
||||
|
||||
Get many keys at once. This is faster than calling `get` multiple times. Resolves with an array of values.
|
||||
|
||||
```js
|
||||
import { get, getMany } from 'idb-keyval';
|
||||
|
||||
// Instead of:
|
||||
Promise.all([get(123), get('hello')]).then(([firstVal, secondVal]) =>
|
||||
console.log(firstVal, secondVal),
|
||||
);
|
||||
|
||||
// It's faster to do:
|
||||
getMany([123, 'hello']).then(([firstVal, secondVal]) =>
|
||||
console.log(firstVal, secondVal),
|
||||
);
|
||||
```
|
||||
|
||||
### update:
|
||||
|
||||
Transforming a value (eg incrementing a number) using `get` and `set` is risky, as both `get` and `set` are async and non-atomic:
|
||||
|
||||
```js
|
||||
// Don't do this:
|
||||
import { get, set } from 'idb-keyval';
|
||||
|
||||
get('counter').then((val) =>
|
||||
set('counter', (val || 0) + 1);
|
||||
);
|
||||
|
||||
get('counter').then((val) =>
|
||||
set('counter', (val || 0) + 1);
|
||||
);
|
||||
```
|
||||
|
||||
With the above, both `get` operations will complete first, each returning `undefined`, then each set operation will be setting `1`. You could fix the above by queuing the second `get` on the first `set`, but that isn't always feasible across multiple pieces of code. Instead:
|
||||
|
||||
```js
|
||||
// Instead:
|
||||
import { update } from 'idb-keyval';
|
||||
|
||||
update('counter', (val) => (val || 0) + 1);
|
||||
update('counter', (val) => (val || 0) + 1);
|
||||
```
|
||||
|
||||
This will queue the updates automatically, so the first `update` set the `counter` to `1`, and the second `update` sets it to `2`.
|
||||
|
||||
### del:
|
||||
|
||||
Delete a particular key from the store.
|
||||
|
||||
```js
|
||||
import { del } from 'idb-keyval';
|
||||
|
||||
del('hello');
|
||||
```
|
||||
|
||||
### delMany:
|
||||
|
||||
Delete many keys at once. This is faster than calling `del` multiple times.
|
||||
|
||||
```js
|
||||
import { del, delMany } from 'idb-keyval';
|
||||
|
||||
// Instead of:
|
||||
Promise.all([del(123), del('hello')])
|
||||
.then(() => console.log('It worked!'))
|
||||
.catch((err) => console.log('It failed!', err));
|
||||
|
||||
// It's faster to do:
|
||||
delMany([123, 'hello'])
|
||||
.then(() => console.log('It worked!'))
|
||||
.catch((err) => console.log('It failed!', err));
|
||||
```
|
||||
|
||||
### clear:
|
||||
|
||||
Clear all values in the store.
|
||||
|
||||
```js
|
||||
import { clear } from 'idb-keyval';
|
||||
|
||||
clear();
|
||||
```
|
||||
|
||||
### entries:
|
||||
|
||||
Get all entries in the store. Each entry is an array of `[key, value]`.
|
||||
|
||||
```js
|
||||
import { entries } from 'idb-keyval';
|
||||
|
||||
// logs: [[123, 456], ['hello', 'world']]
|
||||
entries().then((entries) => console.log(entries));
|
||||
```
|
||||
|
||||
### keys:
|
||||
|
||||
Get all keys in the store.
|
||||
|
||||
```js
|
||||
import { keys } from 'idb-keyval';
|
||||
|
||||
// logs: [123, 'hello']
|
||||
keys().then((keys) => console.log(keys));
|
||||
```
|
||||
|
||||
### values:
|
||||
|
||||
Get all values in the store.
|
||||
|
||||
```js
|
||||
import { values } from 'idb-keyval';
|
||||
|
||||
// logs: [456, 'world']
|
||||
values().then((values) => console.log(values));
|
||||
```
|
||||
|
||||
### Custom stores:
|
||||
|
||||
By default, the methods above use an IndexedDB database named `keyval-store` and an object store named `keyval`. If you want to use something different, see [custom stores](./custom-stores.md).
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
'use strict';
|
||||
|
||||
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
||||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
||||
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
||||
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
||||
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
||||
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
||||
function promisifyRequest(request) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
// @ts-ignore - file size hacks
|
||||
request.oncomplete = request.onsuccess = function () {
|
||||
return resolve(request.result);
|
||||
};
|
||||
// @ts-ignore - file size hacks
|
||||
request.onabort = request.onerror = function () {
|
||||
return reject(request.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
function createStore(dbName, storeName) {
|
||||
var dbp;
|
||||
var getDB = function getDB() {
|
||||
if (dbp) return dbp;
|
||||
var request = indexedDB.open(dbName);
|
||||
request.onupgradeneeded = function () {
|
||||
return request.result.createObjectStore(storeName);
|
||||
};
|
||||
dbp = promisifyRequest(request);
|
||||
dbp.then(function (db) {
|
||||
// It seems like Safari sometimes likes to just close the connection.
|
||||
// It's supposed to fire this event when that happens. Let's hope it does!
|
||||
db.onclose = function () {
|
||||
return dbp = undefined;
|
||||
};
|
||||
}, function () {
|
||||
dbp = undefined;
|
||||
});
|
||||
return dbp;
|
||||
};
|
||||
return function (txMode, callback) {
|
||||
return getDB().then(function (db) {
|
||||
return callback(db.transaction(storeName, txMode).objectStore(storeName));
|
||||
});
|
||||
};
|
||||
}
|
||||
var defaultGetStoreFunc;
|
||||
function defaultGetStore() {
|
||||
if (!defaultGetStoreFunc) {
|
||||
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
|
||||
}
|
||||
return defaultGetStoreFunc;
|
||||
}
|
||||
/**
|
||||
* Get a value by its key.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function get(key) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
return promisifyRequest(store.get(key));
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set a value with a key.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function set(key, value) {
|
||||
var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
store.put(value, key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set multiple values at once. This is faster than calling set() multiple times.
|
||||
* It's also atomic – if one of the pairs can't be added, none will be added.
|
||||
*
|
||||
* @param entries Array of entries, where each entry is an array of `[key, value]`.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function setMany(entries) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
entries.forEach(function (entry) {
|
||||
return store.put(entry[1], entry[0]);
|
||||
});
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get multiple values by their keys
|
||||
*
|
||||
* @param keys
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function getMany(keys) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
return Promise.all(keys.map(function (key) {
|
||||
return promisifyRequest(store.get(key));
|
||||
}));
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Update a value. This lets you see the old value and update it as an atomic operation.
|
||||
*
|
||||
* @param key
|
||||
* @param updater A callback that takes the old value and returns a new value.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function update(key, updater) {
|
||||
var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
return (
|
||||
// Need to create the promise manually.
|
||||
// If I try to chain promises, the transaction closes in browsers
|
||||
// that use a promise polyfill (IE10/11).
|
||||
new Promise(function (resolve, reject) {
|
||||
var req = store.get(key);
|
||||
req.onsuccess = function () {
|
||||
try {
|
||||
store.put(updater(req.result), key);
|
||||
resolve(promisifyRequest(store.transaction));
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
req.onerror = function () {
|
||||
return reject(req.error);
|
||||
};
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Delete a particular key from the store.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function del(key) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
store.delete(key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Delete multiple keys at once.
|
||||
*
|
||||
* @param keys List of keys to delete.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function delMany(keys) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
keys.forEach(function (key) {
|
||||
return store.delete(key);
|
||||
});
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clear all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function clear() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
store.clear();
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
function eachCursor(store, callback) {
|
||||
store.openCursor().onsuccess = function () {
|
||||
if (!this.result) return;
|
||||
callback(this.result);
|
||||
this.result.continue();
|
||||
};
|
||||
return promisifyRequest(store.transaction);
|
||||
}
|
||||
/**
|
||||
* Get all keys in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function keys() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAllKeys) {
|
||||
return promisifyRequest(store.getAllKeys());
|
||||
}
|
||||
var items = [];
|
||||
return eachCursor(store, function (cursor) {
|
||||
return items.push(cursor.key);
|
||||
}).then(function () {
|
||||
return items;
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function values() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAll) {
|
||||
return promisifyRequest(store.getAll());
|
||||
}
|
||||
var items = [];
|
||||
return eachCursor(store, function (cursor) {
|
||||
return items.push(cursor.value);
|
||||
}).then(function () {
|
||||
return items;
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all entries in the store. Each entry is an array of `[key, value]`.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function entries() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
// Fast path for modern browsers
|
||||
// (although, hopefully we'll get a simpler path some day)
|
||||
if (store.getAll && store.getAllKeys) {
|
||||
return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) {
|
||||
var _ref2 = _slicedToArray(_ref, 2),
|
||||
keys = _ref2[0],
|
||||
values = _ref2[1];
|
||||
return keys.map(function (key, i) {
|
||||
return [key, values[i]];
|
||||
});
|
||||
});
|
||||
}
|
||||
var items = [];
|
||||
return eachCursor(store, function (cursor) {
|
||||
return items.push([cursor.key, cursor.value]);
|
||||
}).then(function () {
|
||||
return items;
|
||||
});
|
||||
});
|
||||
}
|
||||
exports.clear = clear;
|
||||
exports.createStore = createStore;
|
||||
exports.del = del;
|
||||
exports.delMany = delMany;
|
||||
exports.entries = entries;
|
||||
exports.get = get;
|
||||
exports.getMany = getMany;
|
||||
exports.keys = keys;
|
||||
exports.promisifyRequest = promisifyRequest;
|
||||
exports.set = set;
|
||||
exports.setMany = setMany;
|
||||
exports.update = update;
|
||||
exports.values = values;
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
export * from './';
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
||||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
||||
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
||||
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
||||
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
||||
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
||||
function promisifyRequest(request) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
// @ts-ignore - file size hacks
|
||||
request.oncomplete = request.onsuccess = function () {
|
||||
return resolve(request.result);
|
||||
};
|
||||
// @ts-ignore - file size hacks
|
||||
request.onabort = request.onerror = function () {
|
||||
return reject(request.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
function createStore(dbName, storeName) {
|
||||
var dbp;
|
||||
var getDB = function getDB() {
|
||||
if (dbp) return dbp;
|
||||
var request = indexedDB.open(dbName);
|
||||
request.onupgradeneeded = function () {
|
||||
return request.result.createObjectStore(storeName);
|
||||
};
|
||||
dbp = promisifyRequest(request);
|
||||
dbp.then(function (db) {
|
||||
// It seems like Safari sometimes likes to just close the connection.
|
||||
// It's supposed to fire this event when that happens. Let's hope it does!
|
||||
db.onclose = function () {
|
||||
return dbp = undefined;
|
||||
};
|
||||
}, function () {
|
||||
dbp = undefined;
|
||||
});
|
||||
return dbp;
|
||||
};
|
||||
return function (txMode, callback) {
|
||||
return getDB().then(function (db) {
|
||||
return callback(db.transaction(storeName, txMode).objectStore(storeName));
|
||||
});
|
||||
};
|
||||
}
|
||||
var defaultGetStoreFunc;
|
||||
function defaultGetStore() {
|
||||
if (!defaultGetStoreFunc) {
|
||||
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
|
||||
}
|
||||
return defaultGetStoreFunc;
|
||||
}
|
||||
/**
|
||||
* Get a value by its key.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function get(key) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
return promisifyRequest(store.get(key));
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set a value with a key.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function set(key, value) {
|
||||
var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
store.put(value, key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set multiple values at once. This is faster than calling set() multiple times.
|
||||
* It's also atomic – if one of the pairs can't be added, none will be added.
|
||||
*
|
||||
* @param entries Array of entries, where each entry is an array of `[key, value]`.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function setMany(entries) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
entries.forEach(function (entry) {
|
||||
return store.put(entry[1], entry[0]);
|
||||
});
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get multiple values by their keys
|
||||
*
|
||||
* @param keys
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function getMany(keys) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
return Promise.all(keys.map(function (key) {
|
||||
return promisifyRequest(store.get(key));
|
||||
}));
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Update a value. This lets you see the old value and update it as an atomic operation.
|
||||
*
|
||||
* @param key
|
||||
* @param updater A callback that takes the old value and returns a new value.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function update(key, updater) {
|
||||
var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
return (
|
||||
// Need to create the promise manually.
|
||||
// If I try to chain promises, the transaction closes in browsers
|
||||
// that use a promise polyfill (IE10/11).
|
||||
new Promise(function (resolve, reject) {
|
||||
var req = store.get(key);
|
||||
req.onsuccess = function () {
|
||||
try {
|
||||
store.put(updater(req.result), key);
|
||||
resolve(promisifyRequest(store.transaction));
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
req.onerror = function () {
|
||||
return reject(req.error);
|
||||
};
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Delete a particular key from the store.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function del(key) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
store.delete(key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Delete multiple keys at once.
|
||||
*
|
||||
* @param keys List of keys to delete.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function delMany(keys) {
|
||||
var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
keys.forEach(function (key) {
|
||||
return store.delete(key);
|
||||
});
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clear all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function clear() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readwrite', function (store) {
|
||||
store.clear();
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
function eachCursor(store, callback) {
|
||||
store.openCursor().onsuccess = function () {
|
||||
if (!this.result) return;
|
||||
callback(this.result);
|
||||
this.result.continue();
|
||||
};
|
||||
return promisifyRequest(store.transaction);
|
||||
}
|
||||
/**
|
||||
* Get all keys in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function keys() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAllKeys) {
|
||||
return promisifyRequest(store.getAllKeys());
|
||||
}
|
||||
var items = [];
|
||||
return eachCursor(store, function (cursor) {
|
||||
return items.push(cursor.key);
|
||||
}).then(function () {
|
||||
return items;
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function values() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAll) {
|
||||
return promisifyRequest(store.getAll());
|
||||
}
|
||||
var items = [];
|
||||
return eachCursor(store, function (cursor) {
|
||||
return items.push(cursor.value);
|
||||
}).then(function () {
|
||||
return items;
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all entries in the store. Each entry is an array of `[key, value]`.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function entries() {
|
||||
var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
|
||||
return customStore('readonly', function (store) {
|
||||
// Fast path for modern browsers
|
||||
// (although, hopefully we'll get a simpler path some day)
|
||||
if (store.getAll && store.getAllKeys) {
|
||||
return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) {
|
||||
var _ref2 = _slicedToArray(_ref, 2),
|
||||
keys = _ref2[0],
|
||||
values = _ref2[1];
|
||||
return keys.map(function (key, i) {
|
||||
return [key, values[i]];
|
||||
});
|
||||
});
|
||||
}
|
||||
var items = [];
|
||||
return eachCursor(store, function (cursor) {
|
||||
return items.push([cursor.key, cursor.value]);
|
||||
}).then(function () {
|
||||
return items;
|
||||
});
|
||||
});
|
||||
}
|
||||
export { clear, createStore, del, delMany, entries, get, getMany, keys, promisifyRequest, set, setMany, update, values };
|
||||
+213
@@ -0,0 +1,213 @@
|
||||
'use strict';
|
||||
|
||||
function promisifyRequest(request) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore - file size hacks
|
||||
request.oncomplete = request.onsuccess = () => resolve(request.result);
|
||||
// @ts-ignore - file size hacks
|
||||
request.onabort = request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
function createStore(dbName, storeName) {
|
||||
let dbp;
|
||||
const getDB = () => {
|
||||
if (dbp)
|
||||
return dbp;
|
||||
const request = indexedDB.open(dbName);
|
||||
request.onupgradeneeded = () => request.result.createObjectStore(storeName);
|
||||
dbp = promisifyRequest(request);
|
||||
dbp.then((db) => {
|
||||
// It seems like Safari sometimes likes to just close the connection.
|
||||
// It's supposed to fire this event when that happens. Let's hope it does!
|
||||
db.onclose = () => (dbp = undefined);
|
||||
}, () => {
|
||||
dbp = undefined;
|
||||
});
|
||||
return dbp;
|
||||
};
|
||||
return (txMode, callback) => getDB().then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName)));
|
||||
}
|
||||
let defaultGetStoreFunc;
|
||||
function defaultGetStore() {
|
||||
if (!defaultGetStoreFunc) {
|
||||
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
|
||||
}
|
||||
return defaultGetStoreFunc;
|
||||
}
|
||||
/**
|
||||
* Get a value by its key.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function get(key, customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => promisifyRequest(store.get(key)));
|
||||
}
|
||||
/**
|
||||
* Set a value with a key.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function set(key, value, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
store.put(value, key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set multiple values at once. This is faster than calling set() multiple times.
|
||||
* It's also atomic – if one of the pairs can't be added, none will be added.
|
||||
*
|
||||
* @param entries Array of entries, where each entry is an array of `[key, value]`.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function setMany(entries, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
entries.forEach((entry) => store.put(entry[1], entry[0]));
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get multiple values by their keys
|
||||
*
|
||||
* @param keys
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function getMany(keys, customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key)))));
|
||||
}
|
||||
/**
|
||||
* Update a value. This lets you see the old value and update it as an atomic operation.
|
||||
*
|
||||
* @param key
|
||||
* @param updater A callback that takes the old value and returns a new value.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function update(key, updater, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) =>
|
||||
// Need to create the promise manually.
|
||||
// If I try to chain promises, the transaction closes in browsers
|
||||
// that use a promise polyfill (IE10/11).
|
||||
new Promise((resolve, reject) => {
|
||||
const req = store.get(key);
|
||||
req.onsuccess = function () {
|
||||
try {
|
||||
store.put(updater(req.result), key);
|
||||
resolve(promisifyRequest(store.transaction));
|
||||
}
|
||||
catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
req.onerror = () => reject(req.error);
|
||||
}));
|
||||
}
|
||||
/**
|
||||
* Delete a particular key from the store.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function del(key, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
store.delete(key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Delete multiple keys at once.
|
||||
*
|
||||
* @param keys List of keys to delete.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function delMany(keys, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
keys.forEach((key) => store.delete(key));
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clear all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function clear(customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
store.clear();
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
function eachCursor(store, callback) {
|
||||
store.openCursor().onsuccess = function () {
|
||||
if (!this.result)
|
||||
return;
|
||||
callback(this.result);
|
||||
this.result.continue();
|
||||
};
|
||||
return promisifyRequest(store.transaction);
|
||||
}
|
||||
/**
|
||||
* Get all keys in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function keys(customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAllKeys) {
|
||||
return promisifyRequest(store.getAllKeys());
|
||||
}
|
||||
const items = [];
|
||||
return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function values(customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAll) {
|
||||
return promisifyRequest(store.getAll());
|
||||
}
|
||||
const items = [];
|
||||
return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all entries in the store. Each entry is an array of `[key, value]`.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function entries(customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => {
|
||||
// Fast path for modern browsers
|
||||
// (although, hopefully we'll get a simpler path some day)
|
||||
if (store.getAll && store.getAllKeys) {
|
||||
return Promise.all([
|
||||
promisifyRequest(store.getAllKeys()),
|
||||
promisifyRequest(store.getAll()),
|
||||
]).then(([keys, values]) => keys.map((key, i) => [key, values[i]]));
|
||||
}
|
||||
const items = [];
|
||||
return eachCursor(store, (cursor) => items.push([cursor.key, cursor.value])).then(() => items);
|
||||
});
|
||||
}
|
||||
|
||||
exports.clear = clear;
|
||||
exports.createStore = createStore;
|
||||
exports.del = del;
|
||||
exports.delMany = delMany;
|
||||
exports.entries = entries;
|
||||
exports.get = get;
|
||||
exports.getMany = getMany;
|
||||
exports.keys = keys;
|
||||
exports.promisifyRequest = promisifyRequest;
|
||||
exports.set = set;
|
||||
exports.setMany = setMany;
|
||||
exports.update = update;
|
||||
exports.values = values;
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
export declare function promisifyRequest<T = undefined>(request: IDBRequest<T> | IDBTransaction): Promise<T>;
|
||||
export declare function createStore(dbName: string, storeName: string): UseStore;
|
||||
export type UseStore = <T>(txMode: IDBTransactionMode, callback: (store: IDBObjectStore) => T | PromiseLike<T>) => Promise<T>;
|
||||
/**
|
||||
* Get a value by its key.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function get<T = any>(key: IDBValidKey, customStore?: UseStore): Promise<T | undefined>;
|
||||
/**
|
||||
* Set a value with a key.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function set(key: IDBValidKey, value: any, customStore?: UseStore): Promise<void>;
|
||||
/**
|
||||
* Set multiple values at once. This is faster than calling set() multiple times.
|
||||
* It's also atomic – if one of the pairs can't be added, none will be added.
|
||||
*
|
||||
* @param entries Array of entries, where each entry is an array of `[key, value]`.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function setMany(entries: [IDBValidKey, any][], customStore?: UseStore): Promise<void>;
|
||||
/**
|
||||
* Get multiple values by their keys
|
||||
*
|
||||
* @param keys
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function getMany<T = any>(keys: IDBValidKey[], customStore?: UseStore): Promise<(T | undefined)[]>;
|
||||
/**
|
||||
* Update a value. This lets you see the old value and update it as an atomic operation.
|
||||
*
|
||||
* @param key
|
||||
* @param updater A callback that takes the old value and returns a new value.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function update<T = any>(key: IDBValidKey, updater: (oldValue: T | undefined) => T, customStore?: UseStore): Promise<void>;
|
||||
/**
|
||||
* Delete a particular key from the store.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function del(key: IDBValidKey, customStore?: UseStore): Promise<void>;
|
||||
/**
|
||||
* Delete multiple keys at once.
|
||||
*
|
||||
* @param keys List of keys to delete.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function delMany(keys: IDBValidKey[], customStore?: UseStore): Promise<void>;
|
||||
/**
|
||||
* Clear all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function clear(customStore?: UseStore): Promise<void>;
|
||||
/**
|
||||
* Get all keys in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function keys<KeyType extends IDBValidKey>(customStore?: UseStore): Promise<KeyType[]>;
|
||||
/**
|
||||
* Get all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function values<T = any>(customStore?: UseStore): Promise<T[]>;
|
||||
/**
|
||||
* Get all entries in the store. Each entry is an array of `[key, value]`.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
export declare function entries<KeyType extends IDBValidKey, ValueType = any>(customStore?: UseStore): Promise<[KeyType, ValueType][]>;
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
function promisifyRequest(request) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore - file size hacks
|
||||
request.oncomplete = request.onsuccess = () => resolve(request.result);
|
||||
// @ts-ignore - file size hacks
|
||||
request.onabort = request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
function createStore(dbName, storeName) {
|
||||
let dbp;
|
||||
const getDB = () => {
|
||||
if (dbp)
|
||||
return dbp;
|
||||
const request = indexedDB.open(dbName);
|
||||
request.onupgradeneeded = () => request.result.createObjectStore(storeName);
|
||||
dbp = promisifyRequest(request);
|
||||
dbp.then((db) => {
|
||||
// It seems like Safari sometimes likes to just close the connection.
|
||||
// It's supposed to fire this event when that happens. Let's hope it does!
|
||||
db.onclose = () => (dbp = undefined);
|
||||
}, () => {
|
||||
dbp = undefined;
|
||||
});
|
||||
return dbp;
|
||||
};
|
||||
return (txMode, callback) => getDB().then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName)));
|
||||
}
|
||||
let defaultGetStoreFunc;
|
||||
function defaultGetStore() {
|
||||
if (!defaultGetStoreFunc) {
|
||||
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
|
||||
}
|
||||
return defaultGetStoreFunc;
|
||||
}
|
||||
/**
|
||||
* Get a value by its key.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function get(key, customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => promisifyRequest(store.get(key)));
|
||||
}
|
||||
/**
|
||||
* Set a value with a key.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function set(key, value, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
store.put(value, key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set multiple values at once. This is faster than calling set() multiple times.
|
||||
* It's also atomic – if one of the pairs can't be added, none will be added.
|
||||
*
|
||||
* @param entries Array of entries, where each entry is an array of `[key, value]`.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function setMany(entries, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
entries.forEach((entry) => store.put(entry[1], entry[0]));
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get multiple values by their keys
|
||||
*
|
||||
* @param keys
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function getMany(keys, customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key)))));
|
||||
}
|
||||
/**
|
||||
* Update a value. This lets you see the old value and update it as an atomic operation.
|
||||
*
|
||||
* @param key
|
||||
* @param updater A callback that takes the old value and returns a new value.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function update(key, updater, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) =>
|
||||
// Need to create the promise manually.
|
||||
// If I try to chain promises, the transaction closes in browsers
|
||||
// that use a promise polyfill (IE10/11).
|
||||
new Promise((resolve, reject) => {
|
||||
const req = store.get(key);
|
||||
req.onsuccess = function () {
|
||||
try {
|
||||
store.put(updater(req.result), key);
|
||||
resolve(promisifyRequest(store.transaction));
|
||||
}
|
||||
catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
req.onerror = () => reject(req.error);
|
||||
}));
|
||||
}
|
||||
/**
|
||||
* Delete a particular key from the store.
|
||||
*
|
||||
* @param key
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function del(key, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
store.delete(key);
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Delete multiple keys at once.
|
||||
*
|
||||
* @param keys List of keys to delete.
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function delMany(keys, customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
keys.forEach((key) => store.delete(key));
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clear all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function clear(customStore = defaultGetStore()) {
|
||||
return customStore('readwrite', (store) => {
|
||||
store.clear();
|
||||
return promisifyRequest(store.transaction);
|
||||
});
|
||||
}
|
||||
function eachCursor(store, callback) {
|
||||
store.openCursor().onsuccess = function () {
|
||||
if (!this.result)
|
||||
return;
|
||||
callback(this.result);
|
||||
this.result.continue();
|
||||
};
|
||||
return promisifyRequest(store.transaction);
|
||||
}
|
||||
/**
|
||||
* Get all keys in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function keys(customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAllKeys) {
|
||||
return promisifyRequest(store.getAllKeys());
|
||||
}
|
||||
const items = [];
|
||||
return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all values in the store.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function values(customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => {
|
||||
// Fast path for modern browsers
|
||||
if (store.getAll) {
|
||||
return promisifyRequest(store.getAll());
|
||||
}
|
||||
const items = [];
|
||||
return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get all entries in the store. Each entry is an array of `[key, value]`.
|
||||
*
|
||||
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
||||
*/
|
||||
function entries(customStore = defaultGetStore()) {
|
||||
return customStore('readonly', (store) => {
|
||||
// Fast path for modern browsers
|
||||
// (although, hopefully we'll get a simpler path some day)
|
||||
if (store.getAll && store.getAllKeys) {
|
||||
return Promise.all([
|
||||
promisifyRequest(store.getAllKeys()),
|
||||
promisifyRequest(store.getAll()),
|
||||
]).then(([keys, values]) => keys.map((key, i) => [key, values[i]]));
|
||||
}
|
||||
const items = [];
|
||||
return eachCursor(store, (cursor) => items.push([cursor.key, cursor.value])).then(() => items);
|
||||
});
|
||||
}
|
||||
|
||||
export { clear, createStore, del, delMany, entries, get, getMany, keys, promisifyRequest, set, setMany, update, values };
|
||||
+1
@@ -0,0 +1 @@
|
||||
function _slicedToArray(n,t){return _arrayWithHoles(n)||_iterableToArrayLimit(n,t)||_unsupportedIterableToArray(n,t)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(n,t){if(n){if("string"==typeof n)return _arrayLikeToArray(n,t);var r={}.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(n,t):void 0}}function _arrayLikeToArray(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=Array(t);r<t;r++)e[r]=n[r];return e}function _iterableToArrayLimit(n,t){var r=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null!=r){var e,o,u,i,a=[],c=!0,l=!1;try{if(u=(r=r.call(n)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(e=u.call(r)).done)&&(a.push(e.value),a.length!==t);c=!0);}catch(n){l=!0,o=n}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(l)throw o}}return a}}function _arrayWithHoles(n){if(Array.isArray(n))return n}function _typeof(n){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n},_typeof(n)}!function(n,t){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).idbKeyval={})}(this,(function(n){"use strict";function t(n){return new Promise((function(t,r){n.oncomplete=n.onsuccess=function(){return t(n.result)},n.onabort=n.onerror=function(){return r(n.error)}}))}function r(n,r){var e;return function(o,u){return function(){if(e)return e;var o=indexedDB.open(n);return o.onupgradeneeded=function(){return o.result.createObjectStore(r)},(e=t(o)).then((function(n){n.onclose=function(){return e=void 0}}),(function(){e=void 0})),e}().then((function(n){return u(n.transaction(r,o).objectStore(r))}))}}var e;function o(){return e||(e=r("keyval-store","keyval")),e}function u(n,r){return n.openCursor().onsuccess=function(){this.result&&(r(this.result),this.result.continue())},t(n.transaction)}n.clear=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readwrite",(function(n){return n.clear(),t(n.transaction)}))},n.createStore=r,n.del=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readwrite",(function(r){return r.delete(n),t(r.transaction)}))},n.delMany=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readwrite",(function(r){return n.forEach((function(n){return r.delete(n)})),t(r.transaction)}))},n.entries=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readonly",(function(n){if(n.getAll&&n.getAllKeys)return Promise.all([t(n.getAllKeys()),t(n.getAll())]).then((function(n){var t=_slicedToArray(n,2),r=t[0],e=t[1];return r.map((function(n,t){return[n,e[t]]}))}));var r=[];return u(n,(function(n){return r.push([n.key,n.value])})).then((function(){return r}))}))},n.get=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readonly",(function(r){return t(r.get(n))}))},n.getMany=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readonly",(function(r){return Promise.all(n.map((function(n){return t(r.get(n))})))}))},n.keys=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readonly",(function(n){if(n.getAllKeys)return t(n.getAllKeys());var r=[];return u(n,(function(n){return r.push(n.key)})).then((function(){return r}))}))},n.promisifyRequest=t,n.set=function(n,r){return(arguments.length>2&&void 0!==arguments[2]?arguments[2]:o())("readwrite",(function(e){return e.put(r,n),t(e.transaction)}))},n.setMany=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readwrite",(function(r){return n.forEach((function(n){return r.put(n[1],n[0])})),t(r.transaction)}))},n.update=function(n,r){return(arguments.length>2&&void 0!==arguments[2]?arguments[2]:o())("readwrite",(function(e){return new Promise((function(o,u){var i=e.get(n);i.onsuccess=function(){try{e.put(r(i.result),n),o(t(e.transaction))}catch(n){u(n)}},i.onerror=function(){return u(i.error)}}))}))},n.values=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readonly",(function(n){if(n.getAll)return t(n.getAll());var r=[];return u(n,(function(n){return r.push(n.value)})).then((function(){return r}))}))}}));
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from './';
|
||||
+1
@@ -0,0 +1 @@
|
||||
function _slicedToArray(n,t){return _arrayWithHoles(n)||_iterableToArrayLimit(n,t)||_unsupportedIterableToArray(n,t)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(n,t){if(n){if("string"==typeof n)return _arrayLikeToArray(n,t);var r={}.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(n,t):void 0}}function _arrayLikeToArray(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=Array(t);r<t;r++)e[r]=n[r];return e}function _iterableToArrayLimit(n,t){var r=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null!=r){var e,o,u,i,a=[],c=!0,l=!1;try{if(u=(r=r.call(n)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(e=u.call(r)).done)&&(a.push(e.value),a.length!==t);c=!0);}catch(n){l=!0,o=n}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(l)throw o}}return a}}function _arrayWithHoles(n){if(Array.isArray(n))return n}function _typeof(n){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n},_typeof(n)}!function(n,t){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).idbKeyval={})}(this,(function(n){"use strict";function t(n){return new Promise((function(t,r){n.oncomplete=n.onsuccess=function(){return t(n.result)},n.onabort=n.onerror=function(){return r(n.error)}}))}function r(n,r){var e;return function(o,u){return function(){if(e)return e;var o=indexedDB.open(n);return o.onupgradeneeded=function(){return o.result.createObjectStore(r)},(e=t(o)).then((function(n){n.onclose=function(){return e=void 0}}),(function(){e=void 0})),e}().then((function(n){return u(n.transaction(r,o).objectStore(r))}))}}var e;function o(){return e||(e=r("keyval-store","keyval")),e}function u(n,r){return n.openCursor().onsuccess=function(){this.result&&(r(this.result),this.result.continue())},t(n.transaction)}n.clear=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readwrite",(function(n){return n.clear(),t(n.transaction)}))},n.createStore=r,n.del=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readwrite",(function(r){return r.delete(n),t(r.transaction)}))},n.delMany=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readwrite",(function(r){return n.forEach((function(n){return r.delete(n)})),t(r.transaction)}))},n.entries=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readonly",(function(n){if(n.getAll&&n.getAllKeys)return Promise.all([t(n.getAllKeys()),t(n.getAll())]).then((function(n){var t=_slicedToArray(n,2),r=t[0],e=t[1];return r.map((function(n,t){return[n,e[t]]}))}));var r=[];return u(n,(function(n){return r.push([n.key,n.value])})).then((function(){return r}))}))},n.get=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readonly",(function(r){return t(r.get(n))}))},n.getMany=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readonly",(function(r){return Promise.all(n.map((function(n){return t(r.get(n))})))}))},n.keys=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readonly",(function(n){if(n.getAllKeys)return t(n.getAllKeys());var r=[];return u(n,(function(n){return r.push(n.key)})).then((function(){return r}))}))},n.promisifyRequest=t,n.set=function(n,r){return(arguments.length>2&&void 0!==arguments[2]?arguments[2]:o())("readwrite",(function(e){return e.put(r,n),t(e.transaction)}))},n.setMany=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:o())("readwrite",(function(r){return n.forEach((function(n){return r.put(n[1],n[0])})),t(r.transaction)}))},n.update=function(n,r){return(arguments.length>2&&void 0!==arguments[2]?arguments[2]:o())("readwrite",(function(e){return new Promise((function(o,u){var i=e.get(n);i.onsuccess=function(){try{e.put(r(i.result),n),o(t(e.transaction))}catch(n){u(n)}},i.onerror=function(){return u(i.error)}}))}))},n.values=function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:o())("readonly",(function(n){if(n.getAll)return t(n.getAll());var r=[];return u(n,(function(n){return r.push(n.value)})).then((function(){return r}))}))}}));
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"name": "idb-keyval",
|
||||
"version": "6.3.0",
|
||||
"description": "A super-simple-small keyval store built on top of IndexedDB",
|
||||
"main": "./dist/compat.cjs",
|
||||
"module": "./dist/compat.js",
|
||||
"unpkg": "./dist/umd.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"module": "./dist/index.js",
|
||||
"import": "./dist/index.js",
|
||||
"default": "./dist/index.cjs"
|
||||
},
|
||||
"./compat": {
|
||||
"types": "./dist/compat.d.ts",
|
||||
"module": "./dist/compat.js",
|
||||
"import": "./dist/compat.js",
|
||||
"default": "./dist/compat.cjs"
|
||||
},
|
||||
"./umd": {
|
||||
"types": "./dist/umd.d.ts",
|
||||
"module": "./dist/umd.js",
|
||||
"import": "./dist/umd.js",
|
||||
"default": "./dist/umd.cjs"
|
||||
},
|
||||
"./dist/*": "./dist/*",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"files": [
|
||||
"dist/**"
|
||||
],
|
||||
"type": "module",
|
||||
"types": "./dist/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"scripts": {
|
||||
"build": "rollup -c && node lib/size-report.js",
|
||||
"dev": "rollup -cw & serve",
|
||||
"prepack": "npm run build"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/jakearchibald/idb-keyval.git"
|
||||
},
|
||||
"keywords": [
|
||||
"idb",
|
||||
"indexeddb",
|
||||
"store",
|
||||
"keyval",
|
||||
"localstorage",
|
||||
"storage",
|
||||
"promise"
|
||||
],
|
||||
"author": "Jake Archibald",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/jakearchibald/idb-keyval/issues"
|
||||
},
|
||||
"homepage": "https://github.com/jakearchibald/idb-keyval#readme",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.27.1",
|
||||
"@babel/plugin-external-helpers": "^7.27.1",
|
||||
"@babel/plugin-transform-runtime": "^7.27.1",
|
||||
"@babel/preset-env": "^7.27.2",
|
||||
"@babel/runtime": "^7.27.1",
|
||||
"@rollup/plugin-babel": "^6.0.4",
|
||||
"@rollup/plugin-commonjs": "^28.0.3",
|
||||
"@rollup/plugin-node-resolve": "^16.0.1",
|
||||
"@rollup/plugin-terser": "^0.4.4",
|
||||
"@rollup/plugin-typescript": "^12.1.2",
|
||||
"@types/chai": "^5.2.2",
|
||||
"@types/mocha": "^10.0.10",
|
||||
"chai": "^5.2.0",
|
||||
"conditional-type-checks": "^1.0.6",
|
||||
"del": "^8.0.0",
|
||||
"filesize": "^10.1.6",
|
||||
"glob": "^11.0.2",
|
||||
"mocha": "^11.2.2",
|
||||
"prettier": "^3.5.3",
|
||||
"rollup": "^4.40.2",
|
||||
"serve": "^14.2.4",
|
||||
"tslib": "^2.8.1",
|
||||
"typescript": "^5.8.3"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user