Skip to content

Commit

Permalink
Merge 1f8934e into 12d2c5e
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Apr 19, 2015
2 parents 12d2c5e + 1f8934e commit 5301bd9
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 130 deletions.
104 changes: 101 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,108 @@
# pouchdb-hoodie-api

> PouchDB plugin that provides Hoodie's store API
[![Build Status](https://travis-ci.org/boennemann/pouchdb-hoodie-api.svg?branch=master)](https://travis-ci.org/boennemann/pouchdb-hoodie-api)
[![Coverage Status](https://coveralls.io/repos/boennemann/pouchdb-hoodie-api/badge.svg)](https://coveralls.io/r/boennemann/pouchdb-hoodie-api)
[![Dependency Status](https://david-dm.org/boennemann/pouchdb-hoodie-api.svg)](https://david-dm.org/boennemann/pouchdb-hoodie-api)
[![NPM](https://nodei.co/npm/pouchdb-hoodie-api.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/pouchdb-hoodie-api/)

This plugin provides simple methods to add, find, update and remove data.

## Usage

```js
PouchDB.plugin(hoodieApi)
var db = new PouchDB('dbname')
var api = db.hoodieApi()

// all methods return promises
api.add(object)
api.add([object1, object2])
api.find(id)
api.find(object) // with id property
api.findOrAdd(id, object)
api.findOrAdd(object)
api.findOrAdd([object1, object2])
api.findAll()
api.findAll(filterFunction)
api.update(id, changedProperties)
api.update(id, updateFunction)
api.update(object)
api.update([object1, object2])
api.updateOrAdd(id, object)
api.updateOrAdd(object)
api.updateOrAdd([object1, object2])
api.updateAll(changedProperties)
api.updateAll(updateFunction)
api.remove(id)
api.remove(object)
api.removeAll()
api.removeAll(filterFunction)
api.clear()
```

Find the full API documentation at **TO BE DONE**


## Installation

### In the browser

To use this plugin, include it after `pouchdb.js` in your HTML page:

```html
<script src="pouchdb.js"></script>
<script src="pouchdb.hoodie-api.js"></script>
```

This plugin is also available from Bower:

```
bower install pouchdb
bower install pouchdb-hoodie-api
```

### In Node.js

Install via npm

```
npm install pouchdb
npm install pouchdb-hoodie-api
```

And then attach it to the `PouchDB` object

```js
var PouchDB = require('pouchdb')
PouchDB(require('pouchdb-hoodie-api'))
```


## Testing

[![Coverage Status](https://coveralls.io/repos/boennemann/pouchdb-hoodie-api/badge.svg)](https://coveralls.io/r/boennemann/pouchdb-hoodie-api)
[![devDependency Status](https://david-dm.org/boennemann/pouchdb-hoodie-api/dev-status.svg)](https://david-dm.org/boennemann/pouchdb-hoodie-api#info=devDependencies)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/db-pouch.svg)](https://saucelabs.com/u/db-pouch)

[![Sauce Test Status](https://saucelabs.com/browser-matrix/hoodie-pouch.svg)](https://saucelabs.com/u/hoodie-pouch)
### In Node.js

[![NPM](https://nodei.co/npm/pouchdb-hoodie-api.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/pouchdb-hoodie-api/)
Run all tests and validates JavaScript Code Style using [standard](https://www.npmjs.com/package/standard)

```
npm test
```

To run only the tests

```
npm run test:node
```

### In the browser

```
test:browser:local
```

This will start a local server. All tests and coverage will be run at http://localhost:8080/__zuul
32 changes: 18 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
'use strict'

module.exports = Store
module.exports = { hoodieApi: hoodieApi }

function Store (db) {
if (!(this instanceof Store)) return new Store(db)
if (typeof db !== 'object' || typeof db.adapter !== 'string') throw new Error('Must pass a PouchDB')
function hoodieApi () {
var api = {
db: this,
utils: {
Promise: this.constructor.utils.Promise,
Errors: this.constructor.Errors
}
}

this.db = db
this.PouchDB = db.constructor
api.add = require('./lib/add').bind(api)
api.find = require('./lib/find').bind(api)
api.findAll = require('./lib/find-all').bind(api)
api.findOrAdd = require('./lib/find-or-add').bind(api)
api.update = require('./lib/update').bind(api)
api.updateOrAdd = require('./lib/update-or-add').bind(api)
api.updateAll = require('./lib/update-all').bind(api)
api.remove = require('./lib/remove').bind(api)

this.add = require('./lib/add').bind(this)
this.find = require('./lib/find').bind(this)
this.findAll = require('./lib/find-all').bind(this)
this.findOrAdd = require('./lib/find-or-add').bind(this)
this.update = require('./lib/update').bind(this)
this.updateOrAdd = require('./lib/update-or-add').bind(this)
this.updateAll = require('./lib/update-all').bind(this)
this.remove = require('./lib/remove').bind(this)
return api
}
4 changes: 2 additions & 2 deletions lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = function add (objects) {
var isArray = Array.isArray(objects)
var state = {
db: this.db,
Promise: this.PouchDB.utils.Promise,
errors: this.PouchDB.Errors
Promise: this.utils.Promise,
errors: this.utils.Errors
}

return isArray ? addMany(state, objects) : addOne(state, objects)
Expand Down
4 changes: 2 additions & 2 deletions lib/find-or-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var findOrAddMany = require('./utils/find-or-add-many')
module.exports = function findOrAdd (idOrObjectOrArray, newObject) {
var state = {
db: this.db,
Promise: this.PouchDB.utils.Promise,
errors: this.PouchDB.Errors
Promise: this.utils.Promise,
errors: this.utils.Errors
}
var isArray = Array.isArray(idOrObjectOrArray)

Expand Down
2 changes: 1 addition & 1 deletion lib/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var findMany = require('./utils/find-many')
module.exports = function find (objectsOrIds) {
var state = {
db: this.db,
errors: this.PouchDB.Errors
errors: this.utils.Errors
}
var isArray = Array.isArray(objectsOrIds)

Expand Down
4 changes: 2 additions & 2 deletions lib/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var updateMany = require('./utils/update-many')
module.exports = function remove (objectsOrIds) {
var state = {
db: this.db,
Promise: this.PouchDB.utils.Promise,
errors: this.PouchDB.Errors
Promise: this.utils.Promise,
errors: this.utils.Errors
}
var isArray = Array.isArray(objectsOrIds)

Expand Down
2 changes: 1 addition & 1 deletion lib/update-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var extend = require('pouchdb-extend')

module.exports = function updateAll (changedProperties) {
var db = this.db
var Promise = this.PouchDB.utils.Promise
var Promise = this.utils.Promise

var type = typeof changedProperties

Expand Down
4 changes: 2 additions & 2 deletions lib/update-or-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var updateOrAddMany = require('./utils/update-or-add-many')
module.exports = function updateOrAdd (idOrObjectOrArray, newObject) {
var state = {
db: this.db,
Promise: this.PouchDB.utils.Promise,
errors: this.PouchDB.Errors
Promise: this.utils.Promise,
errors: this.utils.Errors
}
var isArray = Array.isArray(idOrObjectOrArray)

Expand Down
4 changes: 2 additions & 2 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var updateOne = require('./utils/update-one')
var updateMany = require('./utils/update-many')

module.exports = function update (objectsOrIds, change) {
var Promise = this.PouchDB.utils.Promise
var state = { db: this.db, Promise: Promise, errors: this.PouchDB.Errors }
var Promise = this.utils.Promise
var state = { db: this.db, Promise: Promise, errors: this.utils.Errors }
var isArray = Array.isArray(objectsOrIds)

if (typeof objectsOrIds !== 'object' && !change) {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

require('./specs/constructor')
require('./specs/factory')
require('./specs/add')
require('./specs/find')
require('./specs/find-all')
Expand Down
13 changes: 6 additions & 7 deletions tests/specs/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
var test = require('tape')

var dbFactory = require('../utils/db')
var Store = require('../../')

test('has "add" method', function (t) {
t.plan(1)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

t.is(typeof store.add, 'function', 'has method')
})
Expand All @@ -18,7 +17,7 @@ test('adds object to db', function (t) {
t.plan(5)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.add({
foo: 'bar'
Expand All @@ -44,7 +43,7 @@ test('adds object with id to db', function (t) {
t.plan(2)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.add({
id: 'baz',
Expand All @@ -65,7 +64,7 @@ test('fails for invalid object', function (t) {
t.plan(2)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.add()

Expand All @@ -79,7 +78,7 @@ test('fails for existing object', function (t) {
t.plan(2)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.add({id: 'foo', foo: 'bar'})

Expand All @@ -97,7 +96,7 @@ test('adds multiple objects to db', function (t) {
t.plan(8)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.add({
id: 'foo',
Expand Down
35 changes: 0 additions & 35 deletions tests/specs/constructor.js

This file was deleted.

24 changes: 24 additions & 0 deletions tests/specs/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

var test = require('tape')
var dbFactory = require('../utils/db')

test('returns hoodie.store-inspired API', function (t) {
t.plan(2)

var db = dbFactory()
var store = db.hoodieApi()

t.is(typeof store, 'object', 'is object')
t.is(store.db, db, 'exposes db')
})

test('constructs a store object w/o new', function (t) {
t.plan(2)

var db = dbFactory()
var store = db.hoodieApi()

t.is(typeof store, 'object', 'is object')
t.is(store.db, db, 'exposes db')
})
7 changes: 3 additions & 4 deletions tests/specs/find-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
var test = require('tape')

var dbFactory = require('../utils/db')
var Store = require('../../')

test('store.findAll exists', function (t) {
t.plan(1)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

t.is(typeof store.findAll, 'function', 'has method')
})
Expand All @@ -18,7 +17,7 @@ test('store.findAll()', function (t) {
t.plan(3)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.findAll()

Expand Down Expand Up @@ -47,7 +46,7 @@ test('store.findAll(filterFunction)', function (t) {
t.plan(1)

var db = dbFactory()
var store = new Store(db)
var store = db.hoodieApi()

store.add([{
foo: 0
Expand Down
Loading

0 comments on commit 5301bd9

Please sign in to comment.