idb-kv-store
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var r;if(typeof window!=="undefined"){r=window}else if(typeof global!=="undefined"){r=global}else if(typeof self!=="undefined"){r=self}else{r=this}r.IdbKeyStore=e()}})(function(){var e,r,n;return function e(r,n,t){function o(u,f){if(!n[u]){if(!r[u]){var s=typeof require=="function"&&require;if(!f&&s)return s(u,!0);if(i)return i(u,!0);var c=new Error("Cannot find module '"+u+"'");throw c.code="MODULE_NOT_FOUND",c}var a=n[u]={exports:{}};r[u][0].call(a.exports,function(e){var n=r[u][1][e];return o(n?n:e)},a,a.exports,e,r,n,t)}return n[u].exports}var i=typeof require=="function"&&require;for(var u=0;u<t.length;u++)o(t[u]);return o}({"/":[function(e,r,n){r.exports=o;var t=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB;function o(e,r){var n=this;if(!e)throw new Error("A name must be supplied");if(!(this instanceof o))return new o(e,r);if(!r)r={};n._db=null;n._queue=[];var i=t.open(e);i.onerror=function(e){var n=new Error("IDB error");n.event=e;if(r.onerror){r.onerror(n)}else{throw n}};i.onsuccess=function(e){n._db=e.target.result;n._drainQueue();if(r.onready)r.onready()};i.onupgradeneeded=function(e){var r=e.target.result;r.createObjectStore("kv")}}function i(e){var r={cb:e};if(typeof Promise==="function"&&e==null){r.promise=new Promise(function(e,n){r.cb=function(r,t){if(r)return n(r);else return e(t)}})}if(!r.cb)r.cb=function e(){};return r}o.prototype.get=function(e,r){var n=this;var t=i(r);if(!n._db){n._queue.push({type:"get",key:e,cb:t.cb})}else if(Array.isArray(e)){var o=[];var u=false;var f=0;e.forEach(function(r,i){n.get(e[i],function(r,n){if(u)return;if(r){u=true;t.cb(r)}o[i]=n;f++;if(f===e.length)t.cb(null,o)})})}else{var s=n._db.transaction(["kv"],"readonly").objectStore("kv").get(e);s.onsuccess=function(e){t.cb(null,e.target.result)}}return t.promise};o.prototype.set=function(e,r,n){var t=this;var o=i(n);if(!t._db){t._queue.push({type:"set",key:e,value:r,cb:o.cb})}else{var u=t._db.transaction(["kv"],"readwrite").objectStore("kv").put(r,e);u.onsuccess=function(){o.cb(null)}}return o.promise};o.prototype._drainQueue=function(){var e=this;for(var r=0;r<e._queue.length;r++){var n=e._queue[r];if(n.type==="get"){e.get(n.key,n.cb)}else if(n.type==="set"){e.set(n.key,n.value,n.cb)}}e._queue=null}},{}]},{},[])("/")}); | ||
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var r;if(typeof window!=="undefined"){r=window}else if(typeof global!=="undefined"){r=global}else if(typeof self!=="undefined"){r=self}else{r=this}r.IdbKeyStore=e()}})(function(){var e,r,n;return function e(r,n,t){function o(i,s){if(!n[i]){if(!r[i]){var f=typeof require=="function"&&require;if(!s&&f)return f(i,!0);if(u)return u(i,!0);var c=new Error("Cannot find module '"+i+"'");throw c.code="MODULE_NOT_FOUND",c}var a=n[i]={exports:{}};r[i][0].call(a.exports,function(e){var n=r[i][1][e];return o(n?n:e)},a,a.exports,e,r,n,t)}return n[i].exports}var u=typeof require=="function"&&require;for(var i=0;i<t.length;i++)o(t[i]);return o}({"/":[function(e,r,n){r.exports=o;var t=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB;function o(e,r){var n=this;if(!e)throw new Error("A name must be supplied");if(!(this instanceof o))return new o(e,r);if(!r)r={};n._db=null;n._queue=[];var u=t.open(e);u.onerror=function(e){var n=new Error("IDB error");n.event=e;if(r.onerror){r.onerror(n)}else{throw n}};u.onsuccess=function(e){n._db=e.target.result;n._drainQueue();if(r.onready)r.onready()};u.onupgradeneeded=function(e){var r=e.target.result;r.createObjectStore("kv")}}function u(e){var r={cb:e};if(typeof Promise==="function"&&e==null){r.promise=new Promise(function(e,n){r.cb=function(r,t){if(r)return n(r);else return e(t)}})}if(!r.cb)r.cb=function e(){};return r}o.prototype.get=function(e,r){var n=this;var t=u(r);if(!n._db){n._queue.push({type:"get",key:e,cb:t.cb})}else if(Array.isArray(e)){var o=[];var i=false;var s=0;e.forEach(function(r,u){n.get(e[u],function(r,n){if(i)return;if(r){i=true;t.cb(r)}o[u]=n;s++;if(s===e.length)t.cb(null,o)})})}else{var f=n._db.transaction("kv","readonly").objectStore("kv").get(e);f.onsuccess=function(e){t.cb(null,e.target.result)}}return t.promise};o.prototype.set=function(e,r,n){var t=this;var o=u(n);if(!t._db){t._queue.push({type:"set",key:e,value:r,cb:o.cb})}else{var i=t._db.transaction("kv","readwrite").objectStore("kv").put(r,e);i.onsuccess=function(){o.cb(null)}}return o.promise};o.prototype.json=function(e){var r=this;var n=u(e);if(!r._db){r._queue.push({type:"json",cb:n.cb})}else{var t=r._db.transaction("kv","readonly").objectStore("kv").openCursor();var o={};t.onsuccess=function(e){var r=e.target.result;if(r){o[r.key]=r.value;r.continue()}else{n.cb(null,o)}}}return n.promise};o.prototype.keys=function(e){var r=this;var n=u(e);if(!r._db){r._queue.push({type:"keys",cb:n.cb})}else{var t=r._db.transaction("kv","readonly").objectStore("kv").openCursor();var o=[];t.onsuccess=function(e){var r=e.target.result;if(r){o.push(r.key);r.continue()}else{n.cb(null,o)}}}return n.promise};o.prototype._drainQueue=function(){var e=this;for(var r=0;r<e._queue.length;r++){var n=e._queue[r];if(n.type==="get"){e.get(n.key,n.cb)}else if(n.type==="set"){e.set(n.key,n.value,n.cb)}else if(n.type==="json"){e.json(n.cb)}else if(n.type==="keys"){e.keys(n.cb)}}e._queue=null}},{}]},{},[])("/")}); |
66
index.js
@@ -83,3 +83,3 @@ module.exports = IdbKeyStore | ||
} else { | ||
var request = self._db.transaction(['kv'], 'readonly') | ||
var request = self._db.transaction('kv', 'readonly') | ||
.objectStore('kv') | ||
@@ -108,3 +108,3 @@ .get(key) | ||
} else { | ||
var request = self._db.transaction(['kv'], 'readwrite') | ||
var request = self._db.transaction('kv', 'readwrite') | ||
.objectStore('kv') | ||
@@ -121,2 +121,60 @@ .put(value, key) | ||
IdbKeyStore.prototype.json = function (cb) { | ||
var self = this | ||
var defer = promisify(cb) | ||
if (!self._db) { | ||
self._queue.push({ | ||
type: 'json', | ||
cb: defer.cb | ||
}) | ||
} else { | ||
var request = self._db.transaction('kv', 'readonly') | ||
.objectStore('kv') | ||
.openCursor() | ||
var json = {} | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
if (cursor) { | ||
json[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
defer.cb(null, json) | ||
} | ||
} | ||
} | ||
return defer.promise | ||
} | ||
IdbKeyStore.prototype.keys = function (cb) { | ||
var self = this | ||
var defer = promisify(cb) | ||
if (!self._db) { | ||
self._queue.push({ | ||
type: 'keys', | ||
cb: defer.cb | ||
}) | ||
} else { | ||
var request = self._db.transaction('kv', 'readonly') | ||
.objectStore('kv') | ||
.openCursor() | ||
var keys = [] | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
if (cursor) { | ||
keys.push(cursor.key) | ||
cursor.continue() | ||
} else { | ||
defer.cb(null, keys) | ||
} | ||
} | ||
} | ||
return defer.promise | ||
} | ||
IdbKeyStore.prototype._drainQueue = function () { | ||
@@ -130,2 +188,6 @@ var self = this | ||
self.set(item.key, item.value, item.cb) | ||
} else if (item.type === 'json') { | ||
self.json(item.cb) | ||
} else if (item.type === 'keys') { | ||
self.keys(item.cb) | ||
} | ||
@@ -132,0 +194,0 @@ } |
{ | ||
"name": "idb-kv-store", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Persistent key-value store for web browsers backed by IndexDB", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,10 +49,18 @@ # idb-kv-store [![Build Status](https://travis-ci.org/xuset/idb-kv-store.svg?branch=master)](https://travis-ci.org/xuset/idb-kv-store) [![npm](https://img.shields.io/npm/v/idb-kv-store.svg)](https://npmjs.org/package/idb-kv-store) | ||
Stores the `value` at `key`; the value can be retreived through `store.get(key)`. When the store operation completes, `cb` is called with `cb(err)`. `err` is null if the store was successful. If `cb` is undefined then a promise is returned instead. | ||
Stores the `value` at `key`; the value can be retrieved through `store.get(key)`. When the store operation completes, `cb` is called with `cb(err)`. `err` is null if the store was successful. If `cb` is undefined then a promise is returned instead. If the key already exists then the old value is replaced with the new one. | ||
### `store.get(key, [cb])` | ||
Retreives the value at `key`. When the value is retreived, `cb` is called with `cb(err, value)`. If the retreival was successful then `err` will be null. If `cb` is undefined then a promise is returned instead. | ||
retrieves the value at `key`. When the value is retrieved, `cb` is called with `cb(err, value)`. If the retrieval was successful then `err` will be null. If `cb` is undefined then a promise is returned instead. If the key does not exist then undefined is returned as the `value`; no error is raised. | ||
### `store.keys([cb])` | ||
Retrieves the list of keys stored. When the list is retrieved, `cb` is called with `cb(err, keys)`. If `cb` is undefined then a promise is returned. | ||
### `store.json([cb])` | ||
retrieves the entire key-value store as a json object. When the json representation has been retrieved, `cb` is called with `cb(err, json)`. If `cb` is undefined, then a promise is returned. | ||
## License | ||
MIT. Copyright (c) Austin Middleton. |
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
12336
175
66