Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit 11a8c0e

Browse files
authored
Merge pull request #36 from orbitdb/feat/put-all-v2
Add `putAll()` function
2 parents eff9c11 + 893ee60 commit 11a8c0e

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/DocumentIndex.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ class DocumentIndex {
1313

1414
updateIndex (oplog, onProgressCallback) {
1515
const reducer = (handled, item, idx) => {
16-
if (handled[item.payload.key] !== true) {
16+
if (item.payload.op === 'PUTALL') {
17+
for (const doc of item.payload.docs) {
18+
if (handled[doc.key] !== true) {
19+
handled[doc.key] = true
20+
this._index[doc.key] = {
21+
op: item.payload.op,
22+
key: doc.key,
23+
value: doc.value
24+
}
25+
}
26+
}
27+
} else if (handled[item.payload.key] !== true) {
1728
handled[item.payload.key] = true
18-
if(item.payload.op === 'PUT') {
29+
if (item.payload.op === 'PUT') {
1930
this._index[item.payload.key] = item
2031
} else if (item.payload.op === 'DEL') {
2132
delete this._index[item.payload.key]

src/DocumentStore.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class DocumentStore extends Store {
6565
}
6666

6767
put (doc, options = {}) {
68-
if (!doc[this.options.indexBy])
69-
throw new Error(`The provided document doesn't contain field '${this.options.indexBy}'`)
68+
if (!doc[this.options.indexBy]) { throw new Error(`The provided document doesn't contain field '${this.options.indexBy}'`) }
7069

7170
return this._addOperation({
7271
op: 'PUT',
@@ -75,9 +74,22 @@ class DocumentStore extends Store {
7574
}, options)
7675
}
7776

77+
putAll (docs, options = {}) {
78+
if (!(Array.isArray(docs))) {
79+
docs = [docs]
80+
}
81+
if (!(docs.every(d => d[this.options.indexBy]))) { throw new Error(`The provided document doesn't contain field '${this.options.indexBy}'`) }
82+
return this._addOperation({
83+
op: 'PUTALL',
84+
docs: docs.map((value) => ({
85+
key: value[this.options.indexBy],
86+
value
87+
}))
88+
}, options)
89+
}
90+
7891
del (key, options = {}) {
79-
if (!this._index.get(key))
80-
throw new Error(`No entry with key '${key}' in the database`)
92+
if (!this._index.get(key)) { throw new Error(`No entry with key '${key}' in the database`) }
8193

8294
return this._addOperation({
8395
op: 'DEL',

0 commit comments

Comments
 (0)