Skip to content

Commit

Permalink
breaking: remove unnecessary separation to id and data in put and upd…
Browse files Browse the repository at this point in the history
…ate, update docs
  • Loading branch information
ewnd9 committed Nov 3, 2016
1 parent efdc15c commit e8d2fa6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
26 changes: 24 additions & 2 deletions README.md
Expand Up @@ -3,9 +3,9 @@
[![Build Status](https://travis-ci.org/ewnd9/pouchdb-model.svg?branch=master)](https://travis-ci.org/ewnd9/pouchdb-model)
[![Coverage Status](https://coveralls.io/repos/github/ewnd9/pouchdb-model/badge.svg?branch=master)](https://coveralls.io/github/ewnd9/pouchdb-model?branch=master)

Convenient wrapper for pouchdb
<!-- TOC depthFrom:1 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->

See [index.js](src/index.js) for the available methods (proper docs are coming)
Convenient wrapper for pouchdb

## Install

Expand Down Expand Up @@ -87,6 +87,28 @@ function validationFactory(schema) {
}
```

## Methods

### `#createId(data: Object)`

Returns the `_id` field in `data` or new id generated by a function provided in an initializer

### `#findById(id: String)`

### `#findOne(data: Object)`

### `#findAll(options: Object?)`

### `#findByIndex(options: Object?)`

### `#put(data: Object)`

### `#update(data: Object)`

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

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

## License

MIT © [ewnd9](http://ewnd9.com)
19 changes: 8 additions & 11 deletions src/index.js
Expand Up @@ -13,7 +13,7 @@ function Model(db, { createId, indexes, migrations }, validate) {
}

Model.prototype.createId = function(data) {
if (typeof data === 'object' && data._id) {
if (data._id) {
return data._id;
} else {
return this._createId(data);
Expand Down Expand Up @@ -108,16 +108,14 @@ Model.prototype.normalizeAllDocsOptions = function(options) {
};
};

Model.prototype.put = function(id, _data) {
const data = _data || id;

Model.prototype.put = function(data) {
if ('_key' in data) {
delete data._key;
}

const doc = {
...data,
_id: this.createId(id),
_id: this.createId(data),
updatedAt: new Date().toISOString()
};

Expand All @@ -130,21 +128,20 @@ Model.prototype.put = function(id, _data) {
});
};

Model.prototype.update = function(id, _data) {
const data = _data || id;
Model.prototype.update = function(data) {
data._id = this.createId(data);
data.updatedAt = new Date().toISOString();

return this
.findOne(id)
.findOne(data)
.then(
dbData => {
const obj = { ...dbData, ...data };
obj._rev = dbData._rev;

return this.put(id, obj);
return this.put(obj);
},
err => this.onNotFound(err, () => {
return this.put(id, data);
return this.put(data);
})
);
};
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -18,7 +18,7 @@ test('Create correct subtitles model', async t => {

let item = { name: 'test' };
item = await model.update(item);
t.deepEqual(Object.keys(item), ['name', 'updatedAt', '_id', '_rev']);
t.deepEqual(Object.keys(item), ['name', '_id', 'updatedAt', '_rev']);

item = await model.update(item);
t.truthy(item._rev.indexOf('2-') === 0);
Expand Down

0 comments on commit e8d2fa6

Please sign in to comment.