Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

idb-kv-store

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

idb-kv-store - npm Package Compare versions

Comparing version 2.2.2 to 3.0.0

2

idbkvstore.min.js

@@ -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.IdbKvStore=e()}})(function(){var e,r,n;return function e(r,n,t){function o(i,c){if(!n[i]){if(!r[i]){var s=typeof require=="function"&&require;if(!c&&s)return s(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var f=n[i]={exports:{}};r[i][0].call(f.exports,function(e){var n=r[i][1][e];return o(n?n:e)},f,f.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=u;var t=typeof window==="undefined"?self:window;var o=t.indexedDB||t.mozIndexedDB||t.webkitIndexedDB||t.msIndexedDB;function u(e,r){var n=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!(this instanceof u))return new u(e,r);if(!r)r={};n._db=null;n._queue=[];var t=o.open(e);t.onerror=function(e){c(e,r.onerror)};t.onsuccess=function(e){n._db=e.target.result;n._drainQueue();if(r.onready)r.onready()};t.onupgradeneeded=function(e){var r=e.target.result;r.createObjectStore("kv")}}u.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 s=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;s++;if(s===e.length)t.cb(null,o)})})}else{var a=n._db.transaction("kv","readonly");var f=a.objectStore("kv").get(e);f.onsuccess=function(e){t.cb(null,e.target.result)};a.onerror=function(e){c(e,t.cb)}}return t.promise};u.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");var s=u.objectStore("kv").put(r,e);s.onsuccess=function(){o.cb(null)};u.onerror=function(e){c(e,o.cb)}}return o.promise};u.prototype.json=function(e){var r=this;var n=i(e);if(!r._db){r._queue.push({type:"json",cb:n.cb})}else{var t=r._db.transaction("kv","readonly");var o=t.objectStore("kv").openCursor();var u={};o.onsuccess=function(e){var r=e.target.result;if(r){u[r.key]=r.value;r.continue()}else{n.cb(null,u)}};t.onerror=function(e){c(e,n.cb)}}return n.promise};u.prototype.keys=function(e){var r=this;var n=i(e);if(!r._db){r._queue.push({type:"keys",cb:n.cb})}else{var t=r._db.transaction("kv","readonly");var o=t.objectStore("kv").openCursor();var u=[];o.onsuccess=function(e){var r=e.target.result;if(r){u.push(r.key);r.continue()}else{n.cb(null,u)}};t.onerror=function(e){c(e,n.cb)}}return n.promise};u.prototype.remove=function(e,r){var n=this;var t=i(r);if(!n._db){n._queue.push({type:"remove",key:e,cb:t.cb})}else{var o=n._db.transaction("kv","readwrite");var u=o.objectStore("kv").delete(e);u.onsuccess=function(e){t.cb(null)};o.onerror=function(e){c(e,t.cb)}}return t.promise};u.prototype.clear=function(e){var r=this;var n=i(e);if(!r._db){r._queue.push({type:"clear",cb:n.cb})}else{var t=r._db.transaction("kv","readwrite");var o=t.objectStore("kv").clear();o.onsuccess=function(e){n.cb(null)};t.onerror=function(e){c(e,n.cb)}}return n.promise};u.prototype.count=function(e){var r=this;var n=i(e);if(!r._db){r._queue.push({type:"count",cb:n.cb})}else{var t=r._db.transaction("kv","readonly");var o=t.objectStore("kv").count();o.onsuccess=function(e){n.cb(null,e.target.result)};t.onerror=function(e){c(e,n.cb)}}return n.promise};u.prototype.add=function(e,r,n){var t=this;var o=i(n);if(!t._db){t._queue.push({type:"add",key:e,value:r,cb:o.cb})}else{var u=t._db.transaction("kv","readwrite");var s=u.objectStore("kv").add(r,e);s.onsuccess=function(e){o.cb(null)};u.onerror=function(e){c(e,o.cb)}}return o.promise};u.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)}else if(n.type==="remove"){e.remove(n.key,n.cb)}else if(n.type==="clear"){e.clear(n.cb)}else if(n.type==="count"){e.count(n.cb)}else if(n.type==="add"){e.add(n.key,n.value,n.cb)}}e._queue=null};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}function c(e,r){var n=new Error("IDB error");n.event=e;if(r){r(n)}else{throw n}}},{}]},{},[])("/")});
(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.IdbKvStore=e()}})(function(){var e,r,o;return function e(r,o,n){function t(i,u){if(!o[i]){if(!r[i]){var c=typeof require=="function"&&require;if(!u&&c)return c(i,!0);if(s)return s(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var f=o[i]={exports:{}};r[i][0].call(f.exports,function(e){var o=r[i][1][e];return t(o?o:e)},f,f.exports,e,r,o,n)}return o[i].exports}var s=typeof require=="function"&&require;for(var i=0;i<n.length;i++)t(n[i]);return t}({"/":[function(e,r,o){r.exports=s;var n=typeof window==="undefined"?self:window;var t=n.indexedDB||n.mozIndexedDB||n.webkitIndexedDB||n.msIndexedDB;function s(e,r){var o=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!t)throw new Error("IndexedDb not supported");if(!(this instanceof s))return new s(e,r);o._db=null;o._closed=false;o._queue=[];var n=t.open(e);n.onerror=i;n.onsuccess=c;n.onupgradeneeded=a;function i(e){o.close();u(e,r)}function c(e){if(o._closed){e.target.result.close()}else{o._db=e.target.result;o._db.onclose=f;o._drainQueue();if(r)r(null)}}function a(e){if(o._closed)return;var r=e.target.result;r.createObjectStore("kv")}function f(){o.close()}}s.prototype.get=function(e,r){var o=this;if(o._closed)throw new Error("Database is closed");var n=i(r);if(!o._db){o._queue.push([o.get,e,n.cb])}else if(Array.isArray(e)){var t=[];var s=false;var c=0;e.forEach(function(r,i){o.get(e[i],function(r,o){if(s)return;if(r){s=true;n.cb(r)}t[i]=o;c++;if(c===e.length)n.cb(null,t)})})}else{var a=o._db.transaction("kv","readonly");var f=a.objectStore("kv").get(e);f.onsuccess=function(e){n.cb(null,e.target.result)};a.onerror=function(e){u(e,n.cb)}}return n.promise};s.prototype.set=function(e,r,o){var n=this;if(n._closed)throw new Error("Database is closed");var t=i(o);if(!n._db){n._queue.push([n.set,e,r,t.cb])}else{var s=n._db.transaction("kv","readwrite");var c=s.objectStore("kv").put(r,e);c.onsuccess=function(){t.cb(null)};s.onerror=function(e){u(e,t.cb)}}return t.promise};s.prototype.json=function(e){var r=this;if(r._closed)throw new Error("Database is closed");var o=i(e);if(!r._db){r._queue.push([r.json,o.cb])}else{var n=r._db.transaction("kv","readonly");var t=n.objectStore("kv").openCursor();var s={};t.onsuccess=function(e){var r=e.target.result;if(r){s[r.key]=r.value;r.continue()}else{o.cb(null,s)}};n.onerror=function(e){u(e,o.cb)}}return o.promise};s.prototype.keys=function(e){var r=this;if(r._closed)throw new Error("Database is closed");var o=i(e);if(!r._db){r._queue.push([r.keys,o.cb])}else{var n=r._db.transaction("kv","readonly");var t=n.objectStore("kv").openCursor();var s=[];t.onsuccess=function(e){var r=e.target.result;if(r){s.push(r.key);r.continue()}else{o.cb(null,s)}};n.onerror=function(e){u(e,o.cb)}}return o.promise};s.prototype.remove=function(e,r){var o=this;if(o._closed)throw new Error("Database is closed");var n=i(r);if(!o._db){o._queue.push([o.remove,e,n.cb])}else{var t=o._db.transaction("kv","readwrite");var s=t.objectStore("kv").delete(e);s.onsuccess=function(e){n.cb(null)};t.onerror=function(e){u(e,n.cb)}}return n.promise};s.prototype.clear=function(e){var r=this;if(r._closed)throw new Error("Database is closed");var o=i(e);if(!r._db){r._queue.push([r.clear,o.cb])}else{var n=r._db.transaction("kv","readwrite");var t=n.objectStore("kv").clear();t.onsuccess=function(e){o.cb(null)};n.onerror=function(e){u(e,o.cb)}}return o.promise};s.prototype.count=function(e){var r=this;if(r._closed)throw new Error("Database is closed");var o=i(e);if(!r._db){r._queue.push([r.count,o.cb])}else{var n=r._db.transaction("kv","readonly");var t=n.objectStore("kv").count();t.onsuccess=function(e){o.cb(null,e.target.result)};n.onerror=function(e){u(e,o.cb)}}return o.promise};s.prototype.add=function(e,r,o){var n=this;if(n._closed)throw new Error("Database is closed");var t=i(o);if(!n._db){n._queue.push([n.add,e,r,t.cb])}else{var s=n._db.transaction("kv","readwrite");var c=s.objectStore("kv").add(r,e);c.onsuccess=function(e){t.cb(null)};s.onerror=function(e){u(e,t.cb)}}return t.promise};s.prototype.close=function(){if(this._closed)return;this._closed=true;if(this._db)this._db.close();this._queue=null};s.prototype._drainQueue=function(){var e=this;for(var r=0;r<e._queue.length;r++){var o=e._queue[r];var n=o.splice(1);o[0].apply(e,n)}e._queue=null};function i(e){var r={cb:e};if(typeof Promise==="function"&&e==null){r.promise=new Promise(function(e,o){r.cb=function(r,n){if(r)return o(r);else return e(n)}})}if(!r.cb)r.cb=function e(){};return r}function u(e,r){var o=new Error("IDB error");o.event=e;if(r){r(o)}else{throw o}}},{}]},{},[])("/")});

@@ -1,2 +0,2 @@

module.exports = IdbKeyStore
module.exports = IdbKvStore

@@ -6,39 +6,51 @@ var scope = typeof window === 'undefined' ? self : window // eslint-disable-line

function IdbKeyStore (name, opts) {
function IdbKvStore (name, cb) {
var self = this
if (typeof name !== 'string') throw new Error('A name must be supplied of type string')
if (!(this instanceof IdbKeyStore)) return new IdbKeyStore(name, opts)
if (!opts) opts = {}
if (!IDB) throw new Error('IndexedDb not supported')
if (!(this instanceof IdbKvStore)) return new IdbKvStore(name, cb)
self._db = null
self._closed = false
self._queue = []
var request = IDB.open(name)
request.onerror = onerror
request.onsuccess = onsuccess
request.onupgradeneeded = onupgradeneeded
request.onerror = function (event) {
onerror(event, opts.onerror)
function onerror (event) {
self.close()
handleError(event, cb)
}
request.onsuccess = function (event) {
self._db = event.target.result
self._drainQueue()
if (opts.onready) opts.onready()
function onsuccess (event) {
if (self._closed) {
event.target.result.close()
} else {
self._db = event.target.result
self._db.onclose = onclose
self._drainQueue()
if (cb) cb(null)
}
}
request.onupgradeneeded = function (event) {
function onupgradeneeded (event) {
if (self._closed) return
var db = event.target.result
db.createObjectStore('kv')
}
function onclose () {
self.close()
}
}
IdbKeyStore.prototype.get = function (key, cb) {
IdbKvStore.prototype.get = function (key, cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'get',
key: key,
cb: defer.cb
})
self._queue.push([self.get, key, defer.cb])
} else if (Array.isArray(key)) {

@@ -69,3 +81,3 @@ var result = []

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -77,13 +89,9 @@ }

IdbKeyStore.prototype.set = function (key, value, cb) {
IdbKvStore.prototype.set = function (key, value, cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'set',
key: key,
value: value,
cb: defer.cb
})
self._queue.push([self.set, key, value, defer.cb])
} else {

@@ -98,3 +106,3 @@ var transaction = self._db.transaction('kv', 'readwrite')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -106,11 +114,9 @@ }

IdbKeyStore.prototype.json = function (cb) {
IdbKvStore.prototype.json = function (cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'json',
cb: defer.cb
})
self._queue.push([self.json, defer.cb])
} else {

@@ -132,3 +138,3 @@ var transaction = self._db.transaction('kv', 'readonly')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -140,11 +146,9 @@ }

IdbKeyStore.prototype.keys = function (cb) {
IdbKvStore.prototype.keys = function (cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'keys',
cb: defer.cb
})
self._queue.push([self.keys, defer.cb])
} else {

@@ -166,3 +170,3 @@ var transaction = self._db.transaction('kv', 'readonly')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -174,12 +178,9 @@ }

IdbKeyStore.prototype.remove = function (key, cb) {
IdbKvStore.prototype.remove = function (key, cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'remove',
key: key,
cb: defer.cb
})
self._queue.push([self.remove, key, defer.cb])
} else {

@@ -194,3 +195,3 @@ var transaction = self._db.transaction('kv', 'readwrite')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -202,11 +203,9 @@ }

IdbKeyStore.prototype.clear = function (cb) {
IdbKvStore.prototype.clear = function (cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'clear',
cb: defer.cb
})
self._queue.push([self.clear, defer.cb])
} else {

@@ -221,3 +220,3 @@ var transaction = self._db.transaction('kv', 'readwrite')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -229,11 +228,9 @@ }

IdbKeyStore.prototype.count = function (cb) {
IdbKvStore.prototype.count = function (cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'count',
cb: defer.cb
})
self._queue.push([self.count, defer.cb])
} else {

@@ -248,3 +245,3 @@ var transaction = self._db.transaction('kv', 'readonly')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -256,13 +253,9 @@ }

IdbKeyStore.prototype.add = function (key, value, cb) {
IdbKvStore.prototype.add = function (key, value, cb) {
var self = this
if (self._closed) throw new Error('Database is closed')
var defer = promisify(cb)
if (!self._db) {
self._queue.push({
type: 'add',
key: key,
value: value,
cb: defer.cb
})
self._queue.push([self.add, key, value, defer.cb])
} else {

@@ -277,3 +270,3 @@ var transaction = self._db.transaction('kv', 'readwrite')

transaction.onerror = function (event) {
onerror(event, defer.cb)
handleError(event, defer.cb)
}

@@ -285,23 +278,15 @@ }

IdbKeyStore.prototype._drainQueue = function () {
IdbKvStore.prototype.close = function () {
if (this._closed) return
this._closed = true
if (this._db) this._db.close()
this._queue = null
}
IdbKvStore.prototype._drainQueue = function () {
var self = this
for (var i = 0; i < self._queue.length; i++) {
var item = self._queue[i]
if (item.type === 'get') {
self.get(item.key, item.cb)
} else if (item.type === 'set') {
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)
} else if (item.type === 'remove') {
self.remove(item.key, item.cb)
} else if (item.type === 'clear') {
self.clear(item.cb)
} else if (item.type === 'count') {
self.count(item.cb)
} else if (item.type === 'add') {
self.add(item.key, item.value, item.cb)
}
var args = item.splice(1)
item[0].apply(self, args)
}

@@ -328,3 +313,3 @@ self._queue = null

function onerror (event, cb) {
function handleError (event, cb) {
var err = new Error('IDB error')

@@ -331,0 +316,0 @@ err.event = event

{
"name": "idb-kv-store",
"version": "2.2.2",
"description": "Persistent key-value store for web browsers backed by IndexDB",
"version": "3.0.0",
"description": "Persistent key-value store for web browsers backed by IndexedDB",
"main": "index.js",

@@ -17,2 +17,3 @@ "repository": "github:xuset/idb-kv-store",

"keywords": [
"indexeddb",
"indexdb",

@@ -19,0 +20,0 @@ "idb",

# 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)
Persistent key-value store for web browsers backed by IndexDB
Persistent key-value store for web browsers backed by IndexedDB
[![Sauce Test Status](https://saucelabs.com/browser-matrix/xuset-idb-kv.svg)](https://saucelabs.com/u/xuset-idb-kv)
idb-kv-store uses asynchronous get/set operations to persist everything in IndexDB. Sometimes IndexDB is needed over something like localStorage due to storage size constraints or simply, localStorage is not available within web workers. Since IndexDB presents a complex api, storing simple key-value pairs can be complicated which this project greatly simplifies. Since everything is persisted to IndexDB, the data you store is available across multiple web sessions and within web workers.
idb-kv-store uses asynchronous get/set operations to persist everything in IndexedDB. Sometimes IndexedDB is needed over something like localStorage due to storage size constraints or simply, localStorage is not available within web workers. Since IndexedDB presents a complex api, storing simple key-value pairs can be complicated which this project greatly simplifies. Since everything is persisted to IndexedDB, the data you store is available across multiple web sessions and within web workers.
This module can be used with [browserify](http://browserify.org/) or the [idbkvstore.min.js](idbkvstore.min.js) script can be included which will attach `IdbKvStore` to `window`.
This module can be used with [browserify](http://browserify.org/) or the [idbkvstore.min.js](https://raw.githubusercontent.com/xuset/idb-kv-store/master/idbkvstore.min.js) script can be included which will attach `IdbKvStore` to `window`.

@@ -37,9 +37,7 @@ ## Usage

### `store = new Store(name, [opts])`
### `store = new Store(name, [cb])`
Instantiates a new key-value store. `name` is the name of the database used to persist the data. So multiple Store instances with the same name will be sharing the same data.
`opts` can take the following options:
* `opts.onready` - A zero argument function to call when the IndexDB database is open
* `opts.onerror` - This function is called when IndexDB experiences an error. It accepts one error argument. If this is undefined, the error is thrown instead.
`cb(err)` is called when the databases is opened. If the open was successful then `err` is null, otherwise `err` contains the error.

@@ -78,4 +76,9 @@ ### `store.set(key, value, [cb])`

### `store.close()`
Closes the IndexedDB database and frees the internal resources. All subsequent calls to methods in `store` will throw errors.
## License
MIT. Copyright (c) Austin Middleton.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc