Skip to content

Commit 8f02237

Browse files
committed
Implemented bulk imports. Fixes #3.
1 parent 40f2747 commit 8f02237

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,43 @@ Deletes the collection from the database.
386386

387387
### Manipulating documents
388388

389+
#### collection.import(data, [opts,] callback)
390+
391+
Bulk imports the given *data* into the collection.
392+
393+
The *data* can be an array of documents:
394+
395+
```js
396+
[
397+
{key1: value1, key2: value2}, // document 1
398+
{key1: value1, key2: value2}, // document 2
399+
...
400+
]
401+
```
402+
403+
Or it can be an array of value arrays following an array of keys.
404+
405+
```js
406+
[
407+
['key1', 'key2'], // key names
408+
[value1, value2], // document 1
409+
[value1, value2], // document 2
410+
...
411+
]
412+
```
413+
414+
If *opts* is set, it must be an object with any of the following properties:
415+
416+
* *waitForSync*: Wait until the documents have been synced to disk. Default: *false*.
417+
* *details*: Whether the response should contain additional details about documents that could not be imported. Default: *false*.
418+
389419
#### collection.replace(documentHandle, data, [opts,] callback)
390420

391421
Replaces the content of the document with the given *documentHandle* with the given *data*.
392422

393423
If *opts* is set, it must be an object with any of the following properties:
394424

395-
* *waitForSync*: Wait until document has been synced to disk. Default: `false`.
425+
* *waitForSync*: Wait until the document has been synced to disk. Default: `false`.
396426
* *rev*: Only replace the document if it matches this revision. Optional.
397427
* *policy*: Determines the behaviour when the revision is not matched:
398428
* if *policy* is set to `"last"`, the document will be replaced regardless of the revision.

lib/collection.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extend(BaseCollection.prototype, {
105105
var self = this;
106106
self._api.delete('collection/' + self.name, function (err, body) {
107107
if (err) callback(err);
108-
else callback(null);
108+
else callback(null, body);
109109
});
110110
},
111111
replace: function (documentHandle, data, opts, callback) {
@@ -141,7 +141,7 @@ extend(BaseCollection.prototype, {
141141
opts = extend({}, opts, {collection: this.name});
142142
this._api.delete(this._documentPath(documentHandle), opts, function (err, body) {
143143
if (err) callback(err);
144-
else callback(null);
144+
else callback(null, body);
145145
});
146146
},
147147
all: function (type, callback) {
@@ -157,6 +157,19 @@ extend(BaseCollection.prototype, {
157157
if (err) callback(err);
158158
else callback(null, body.documents);
159159
});
160+
},
161+
import: function (data, opts, callback) {
162+
if (typeof opts === 'function') {
163+
callback = opts;
164+
opts = undefined;
165+
}
166+
if (!callback) callback = noop;
167+
this._api.post('import', data, extend({}, opts, {
168+
collection: this.name
169+
}), function (err, body) {
170+
if (err) callback(err);
171+
else callback(null, body);
172+
});
160173
}
161174
});
162175

0 commit comments

Comments
 (0)