Skip to content

Commit 2f9d3cf

Browse files
committed
- API addition: Allow batch operations and add docs and tests;
- Docs: Clarify which methods are not available on table objects (fixes issue #95 (and #123)) - Testing: Fix server-update test - Testing: Add or increase a few timeouts (for Firefox) and reduce FF errors in console - Refactoring: Add events on transaction through `addEventListener` instead of on* for better interoperability;
1 parent a04dba4 commit 2f9d3cf

File tree

15 files changed

+919
-80
lines changed

15 files changed

+919
-80
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGES
22

3+
## Batch version (unreleased)
4+
5+
- API addition: Add `tableBatch` and `batch` methods
6+
37
## Filter by object version (unreleased)
48

59
- API addition: Allow supplying objects to `filter()`

README.md

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ schemas: {
335335

336336
## General server/store methods
337337

338-
Note that by default the methods below (not including `close`,
339-
`addEventListener`, and `removeEventListener`) can be called either as
340-
`server.people.xxx( arg1, arg2, ... )` or
338+
Note that by default the methods below (not including `close`, `batch`,
339+
`addEventListener`, `removeEventListener`, and the server event methods)
340+
can be called either as `server.people.xxx( arg1, arg2, ... )` or
341341
`server.xxx( 'people', arg1, arg2, ... )`.
342342

343343
To reduce some memory requirements or avoid a however unlikely
@@ -415,6 +415,91 @@ server.people.clear()
415415
});
416416
```
417417

418+
#### Batch operations (in a single transaction)
419+
420+
##### `batch`
421+
422+
This method allows batch operations across multiple stores using the
423+
formats available to the `transactionalBatch` method of
424+
[`idb-batch`](https://github.com/brettz9/idb-batch).
425+
426+
The first argument to this method is an array of operations.
427+
Unlike for the default behavior of `transactionalBatch` in
428+
[`idb-batch`](https://github.com/treojs/idb-batch/), however,
429+
any function-based operations will be passed the db.js `Server` as second
430+
argument, allowing for promises within function-based operations (though
431+
please note the risk that the transaction of the batch may expire and
432+
thus its promise resolve before the promises, especially chained promises,
433+
within the function operation can complete).
434+
435+
The second argument is an optional options object. An option,
436+
`parallel` can be set to `true` if the order of operations is
437+
not significant, and another option `extraStores` can be populated
438+
with an array of additional store names to allow in the transaction
439+
(for the sake of function-based operations which reuse the transaction).
440+
441+
The options default to `{extraStores: [], parallel: false}`.
442+
443+
The `batch` method is not available on table objects.
444+
445+
```js
446+
server.batch(
447+
[
448+
// Multiple stores can be modified
449+
{
450+
magazines: [
451+
{ type: 'add', key: 1, value: { name: 'M1', frequency: 12 } },
452+
{ type: 'add', key: 2, value: { name: 'M2', frequency: 24 } },
453+
{ type: 'add', key: 3, value: { name: 'M3', frequency: 6 } },
454+
{ type: 'del', key: 2 }
455+
]
456+
},
457+
{
458+
books: [
459+
{ type: 'put', key: 1, value: { name: 'M1', frequency: 12 } },
460+
{ type: 'move', key: 2, value: 1 },
461+
{ type: 'copy', key: 3, value: 2 }
462+
],
463+
storage: 'clear'
464+
}
465+
function callbackInTransaction (tr, s) {
466+
// Transaction doesn't last long enough to chain these promises/add to separate op functions,
467+
// though we can run an extra operation here (before we return a promise) if timing is not critical
468+
s.magazines
469+
.query()
470+
.only(3)
471+
.modify({modified: true})
472+
.execute();
473+
return s.magazines.put({name: 'M4', frequency: 8});
474+
}
475+
])
476+
.then(function() {
477+
// Continue (the `batch` transaction will have now expired)
478+
});
479+
```
480+
481+
##### `tableBatch`
482+
483+
This method allows batch operations on a single store only using the
484+
formats available to the `batch` method of
485+
[`idb-batch`](https://github.com/brettz9/idb-batch).
486+
487+
The first argument is the array of operations and the second argument
488+
is an optional options object. A single option, `parallel` can be set
489+
to `true` if the order of operations is not significant.
490+
491+
```js
492+
server.people.tableBatch([
493+
{type: 'add', key: 1, value: {name: 'M1', frequency: 12}},
494+
{type: 'add', key: 2, value: {name: 'M2', frequency: 24}},
495+
{type: 'add', value: {id: 3, name: 'M3', frequency: 6}},
496+
{type: 'add', value: {id: 4, name: 'M4', frequency: 52}}
497+
], {parallel: false})
498+
.then(function() {
499+
// Continue (the `tableBatch` transaction will have now expired)
500+
});
501+
```
502+
418503
### Fetching
419504

420505
#### Getting a single object by key
@@ -797,6 +882,8 @@ and `map`.
797882
server.close();
798883
```
799884

885+
This method is not available on table objects.
886+
800887
### Retrieving the `indexedDB.open` result object in use
801888

802889
```js
@@ -833,6 +920,8 @@ server.abort(function (e) {
833920
});
834921
```
835922

923+
These methods are not available on table objects.
924+
836925
See the IndexedDB spec for the [possible exceptions](http://www.w3.org/TR/IndexedDB/#exceptions).
837926

838927
## Deleting a database

0 commit comments

Comments
 (0)