Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the IndexedDB getAll function #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
localForage-getItems
====================
[![npm](https://img.shields.io/npm/dm/localforage-getitems.svg)](https://www.npmjs.com/package/localforage-getitems)
Adds getItems method to [localForage](https://github.com/mozilla/localForage).
# @converse/localforage-getitems

This is a fork of `localforage-getitems` which adds support for the `getAll`
function for IndexedDB.

The upstream pull request is here: https://github.com/localForage/localForage-getItems/pull/12

The upstream project appears to be unmaintained and hasn't had any changes in 5
years.

## Requirements

* [localForage](https://github.com/mozilla/localForage) v1.4.0+
* for earlier versions of localforage, please use the v1.1.x releases

## Installation
`npm i localforage-getitems`
`npm i @converse/localforage-getitems`

## jsperf links
* [default driver order (indexedDB prefered)](https://jsperf.com/localforage-getitems-2017/1)
* [websql (not for firefox)](https://jsperf.com/localforage-getitems-websql-2017b/1)

## API

Just like `getItem()` but you can pass an array with the keys that need to be retrieved.
```js
var keys = ['asdf','asap','async'];
localforage.getItems(keys).then(function(results) {

```javascript
const keys = ['asdf','asap','async'];

localforage.getItems(keys).then((results) => {
console.log(results);
// prints:
// {
Expand All @@ -36,8 +44,10 @@ localforage.getItems(keys).then(function(results) {
```

Invoking `getItems()` without arguments (or with `null` as the first argument) will retrieve all the items of the current driver instance.
```js
localforage.getItems().then(function(results) { });

```javascript
localforage.getItems().then((results) => { });

// or by using callbacks
localforage.getItems(null, function(results) { });
localforage.getItems(null, results) => { });
```
24 changes: 0 additions & 24 deletions bower.json

This file was deleted.

78 changes: 45 additions & 33 deletions dist/localforage-getitems.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,49 +103,61 @@ function getItemsIndexedDB(keys /*, callback*/) {
var set = keys.sort(comparer);

var keyRangeValue = idbKeyRange.bound(keys[0], keys[keys.length - 1], false, false);
var req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

req.onsuccess = function () /*event*/{
var cursor = req.result; // event.target.result;

if (!cursor) {
resolve(result);
return;
}

var key = cursor.key;
var req;

while (key > set[i]) {
if ('getAll' in store) {
req = store.getAll(keyRangeValue);
req.onsuccess = function () {
var value = req.result;
if (value === undefined) {
value = null;
}
resolve(value);
};
} else {
req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

// The cursor has passed beyond this key. Check next.
i++;
req.onsuccess = function () /*event*/{
var cursor = req.result; // event.target.result;

if (i === set.length) {
// There is no next. Stop searching.
if (!cursor) {
resolve(result);
return;
}
}

if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;
var key = cursor.key;

while (key > set[i]) {
i++; // The cursor has passed beyond this key. Check next.

if (i === set.length) {
// There is no next. Stop searching.
resolve(result);
return;
}
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
}
};
if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
}
};
}

req.onerror = function () /*event*/{
reject(req.error);
Expand Down
78 changes: 45 additions & 33 deletions dist/localforage-getitems.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,49 +109,61 @@ function getItemsIndexedDB(keys /*, callback*/) {
var set = keys.sort(comparer);

var keyRangeValue = idbKeyRange.bound(keys[0], keys[keys.length - 1], false, false);
var req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

req.onsuccess = function () /*event*/{
var cursor = req.result; // event.target.result;

if (!cursor) {
resolve(result);
return;
}

var key = cursor.key;
var req;

while (key > set[i]) {
if ('getAll' in store) {
req = store.getAll(keyRangeValue);
req.onsuccess = function () {
var value = req.result;
if (value === undefined) {
value = null;
}
resolve(value);
};
} else {
req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

// The cursor has passed beyond this key. Check next.
i++;
req.onsuccess = function () /*event*/{
var cursor = req.result; // event.target.result;

if (i === set.length) {
// There is no next. Stop searching.
if (!cursor) {
resolve(result);
return;
}
}

if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;
var key = cursor.key;

while (key > set[i]) {
i++; // The cursor has passed beyond this key. Check next.

if (i === set.length) {
// There is no next. Stop searching.
resolve(result);
return;
}
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
}
};
if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
}
};
}

req.onerror = function () /*event*/{
reject(req.error);
Expand Down
86 changes: 49 additions & 37 deletions lib/getitems-indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,61 @@ export function getItemsIndexedDB(keys/*, callback*/) {
var set = keys.sort(comparer);

var keyRangeValue = IDBKeyRange.bound(keys[0], keys[keys.length - 1], false, false);
var req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

req.onsuccess = function (/*event*/) {
var cursor = req.result; // event.target.result;

if (!cursor) {
resolve(result);
return;
}

var key = cursor.key;

while (key > set[i]) {

// The cursor has passed beyond this key. Check next.
i++;

if (i === set.length) {
// There is no next. Stop searching.

var req;

if ('getAll' in store) {
req = store.getAll(keyRangeValue);
req.onsuccess = function() {
var value = req.result;
if (value === undefined) {
value = null;
}
resolve(value);
};
} else {
req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

req.onsuccess = function (/*event*/) {
var cursor = req.result; // event.target.result;

if (!cursor) {
resolve(result);
return;
}
}

if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;

var key = cursor.key;

while (key > set[i]) {
i++; // The cursor has passed beyond this key. Check next.

if (i === set.length) {
// There is no next. Stop searching.
resolve(result);
return;
}
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
}
}
};
}

req.onerror = function(/*event*/) {
reject(req.error);
Expand Down