Skip to content

Commit

Permalink
feat: bulk method
Browse files Browse the repository at this point in the history
  • Loading branch information
ewnd9 committed Nov 3, 2016
1 parent e8d2fa6 commit 699b99c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -107,6 +107,11 @@ Returns the `_id` field in `data` or new id generated by a function provided in

Overwrites the previous object if existed with a correct `_rev` or insert a new doc

## `#bulk(data: Object[])`

Returns an array of the same length containing corresponding doc (has `_id` and `_rev`)
or error (has `error`, `name`, `status` and, in case of validation error - `err`)

### `#sync(db: PouchDB | String, options: Object?, notify: Function)`

## License
Expand Down
55 changes: 55 additions & 0 deletions src/index.js
Expand Up @@ -146,6 +146,61 @@ Model.prototype.update = function(data) {
);
};

Model.prototype.bulk = function(docs) {
const errors = [];
const correct = [];

return Promise
.all(
docs.map((doc, index) => this.validate(doc).then(
doc => correct.push({ doc, index }),
err => errors.push({
err: {
error: true,
name: 'validation error',
err
},
index
})
))
)
.then(() => {
const docs = correct.map(item => {
item.doc._id = this.createId(item.doc);
return item.doc;
});
return this.db.bulkDocs(docs);
})
.then(dbDocs => {
let correctIndex = 0;
let errorsIndex = 0;
let dbIndex = 0;

return docs.map((doc, index) => {
let dbDoc;

if (correct[correctIndex] && correct[correctIndex].index === index) {
dbDoc = dbDocs[dbIndex];
correctIndex++;
dbIndex++;
} else if (errors[errorsIndex] && errors[errorsIndex].index === index) {
dbDoc = errors[errorsIndex].err;
errorsIndex++;
}

if (dbDoc.ok === true) {
return {
...docs[index],
_id: dbDoc.id,
_rev: dbDoc.rev
};
}

return dbDoc;
});
});
};

/*
* https://pouchdb.com/api.html#sync
*/
Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Expand Up @@ -142,6 +142,33 @@ test('#update', async t => {
await model.update({ name: 'test' });
});

test('#bulk', async t => {
const db = new PouchDB('bulk', { adapter: 'memory' });

const validate = doc => new Promise((resolve, reject) => {
return doc.name !== 'test-4' ? resolve(doc) : reject(new Error('name === test-4'));
});

const model = new Model(db, {
createId: ({ name }) => `item:${name}`,
}, validate);

const result0 = await model.bulk([{ name: 'test-1' }, { name: 'test-2' }]);
const result1 = await model.bulk([{ name: 'test-1' }, { name: 'test-2' }, { name: 'test-3' }]);

t.truthy(result0.filter(_ => _._rev).length === 2);
t.truthy(result1.filter(_ => _._rev).length === 1);

const result2 = await model.bulk([{ name: 'test-1' }, { name: 'test-2' }, { name: 'test-3' }, { name: 'test-4' }]);
t.truthy(result2.filter(_ => _.rev).length === 0);
t.deepEqual(result2.filter(_ => _.error).map(_ => _.name), [
'conflict',
'conflict',
'conflict',
'validation error'
]);
});

test('#ensureIndexes', async t => {
const db = new PouchDB('ensureIndexes', { adapter: 'memory' });

Expand Down

0 comments on commit 699b99c

Please sign in to comment.