Skip to content

Commit

Permalink
Merge f2ca074 into 3f0ccc4
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 9, 2017
2 parents 3f0ccc4 + f2ca074 commit dd5b8ad
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 171 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,42 @@ new Store(dbName, options)
| Argument | Type | Description | Required
| :------- | :--- | :---------- | :-------
| **`dbName`** | String | name of the database | Yes
| **`options.remote`** | String | name or URL of remote database | Yes (unless `remoteBaseUrl` is preset, see [Store.defaults](storedefaults))
| **`options.PouchDB`** | Constructor | [PouchDB custom builds](https://pouchdb.com/custom.html) | Yes (unless preset using [Store.defaults](storedefaults)))
| **`options.remote`** | String | name or URL of remote database | Yes (unless `remoteBaseUrl` is preset, see [Store.defaults](#storedefaults))
| **`options.remote`** | Object | PouchDB instance | Yes (ignores `remoteBaseUrl` from [Store.defaults](#storedefaults))
| **`options.remote`** | Promise | Resolves to either string or PouchDB instance | see above
| **`options.PouchDB`** | Constructor | [PouchDB custom builds](https://pouchdb.com/custom.html) | Yes (unless preset using [Store.defaults](#storedefaults)))

Returns `store` API.

Example

```js
var Store = require('@hoodie/store-client')
var store = new Store('mydb', { remote: 'http://localhost:5984/mydb' })
var store = new Store('mydb', {
PouchDB: PouchDB,
remote: 'http://localhost:5984/mydb'
})
store.sync() // will sync with http://localhost:5984/mydb
```

Example with dynamic remote URL and ajax headers


```js
var loadAccount = require('./load-account')
var store = new Store('mydb', {
PouchDB: PouchDB,
get remote () {
return loadAccount.then(function (account) {
return new PouchDB('http://localhost:5984/' + encodeURIComponent('user/' + account.id), {
ajax: {
headers: {
authorization: 'session ' + account.session.id
}
}
})
})
}
})
store.sync() // will sync with http://localhost:5984/mydb
```

Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Store (dbName, options) {
if (!(this instanceof Store)) return new Store(dbName, options)
if (typeof dbName !== 'string') throw new Error('Must be a valid string.')

if (!options || (!options.remote && !options.remoteBaseUrl)) {
if (!options || (!('remote' in options) && !options.remoteBaseUrl)) {
throw new Error('options.remote or options.remoteBaseUrl is required')
}

Expand All @@ -32,8 +32,7 @@ function Store (dbName, options) {

var db = new options.PouchDB(dbName)
var emitter = new EventEmitter()
var remote = options.remote
var syncApi = db.hoodieSync({remote: remote})
var syncApi = db.hoodieSync(options)
var storeApi = db.hoodieApi({emitter: emitter})

var state = {
Expand Down Expand Up @@ -76,7 +75,7 @@ function Store (dbName, options) {
}
)

api.reset = require('./lib/reset').bind(null, dbName, options.PouchDB, state, api, storeApi.clear, emitter, options.remoteBaseUrl, remote)
api.reset = require('./lib/reset').bind(null, dbName, options, state, api, storeApi.clear, emitter)

subscribeToSyncEvents(syncApi, emitter)

Expand Down
22 changes: 4 additions & 18 deletions lib/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@ var merge = require('lodash/merge')

var subscribeToSyncEvents = require('./subscribe-to-sync-events')

function reset (dbName, CustomPouchDB, state, api, clear, emitter, remoteBaseUrl, remote, options) {
if (options) {
if (options.name) {
dbName = options.name
}

if (options.remote) {
remote = options.remote
} else if (options.name && remoteBaseUrl) {
remote = remoteBaseUrl + '/' + encodeURIComponent(options.name)
}

CustomPouchDB = CustomPouchDB.defaults(options)
}

function reset (dbName, options, state, api, clear, emitter) {
return api.disconnect()

.then(clear)

.then(function () {
var newDB = new CustomPouchDB(dbName)
var syncApi = newDB.hoodieSync({remote: remote})
var newDB = new options.PouchDB(dbName)
var syncApi = newDB.hoodieSync(options)
var storeApi = newDB.hoodieApi({emitter: emitter})

subscribeToSyncEvents(syncApi, emitter)
Expand Down Expand Up @@ -58,6 +44,6 @@ function reset (dbName, CustomPouchDB, state, api, clear, emitter, remoteBaseUrl
}
)

api.reset = reset.bind(null, dbName, CustomPouchDB, state, api, storeApi.clear, emitter, remoteBaseUrl, remote)
api.reset = reset.bind(null, dbName, options, state, api, storeApi.clear, emitter)
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@hoodie/store-client",
"version": "0.0.0-semantically-released",
"description": "Hoodie Client for data persistence & offline sync",
"main": "index.js",
"files": ["index.js", "lib", "dist"],
Expand Down Expand Up @@ -57,7 +58,7 @@
"hoodie-zuul-config": "^2.0.0",
"lodash": "^4.6.0",
"pouchdb-hoodie-api": "^2.0.0",
"pouchdb-hoodie-sync": "^2.0.0"
"pouchdb-hoodie-sync": "^2.1.1"
},
"publishConfig": {
"access": "public"
Expand Down
105 changes: 0 additions & 105 deletions tests/specs/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var simple = require('simple-mock')
var test = require('tape')

var PouchDB = require('../utils/pouchdb.js')
Expand Down Expand Up @@ -125,110 +124,6 @@ test('store.reset creates empty instance of store', function (t) {
.catch(t.fail)
})

test('store.reset creates empty instance of store with new options', function (t) {
t.plan(3)

var store = new Store('test-db-clear', {
PouchDB: PouchDB,
remote: 'test-db-clear'
})
var newOptions = {
name: 'new-test-db-clear',
remote: 'new-test-db-clear'
}
store.on('clear', t.pass.bind(null, '"clear" event emitted'))

// merge in-memory adapter options
store.reset(newOptions)

.then(function () {
return store.findAll()
})

.then(function (result) {
t.deepEqual(result, [], '.findAll() resolves with empty array after .clear()')
t.is(store.db.name, newOptions.name, 'reset store has a new name')
})

.catch(t.fail)
})

test('store.reset creates empty instance of store with new options passed as arguments', function (t) {
t.plan(1)

var reset = require('../../lib/reset')
var clear = function () {
return Promise.resolve()
}

var store = {
disconnect: simple.stub().returnWith(Promise.resolve())
}

reset('new-test-db-clear-arguments', PouchDB, undefined, store, clear, undefined, 'new-test-db-clear-arguments-remote', {})

.then(function () {
t.is(store.db.name, 'new-test-db-clear-arguments', 'reset store has a new name')
})

.catch(t.error)
})

test('store.reset creates empty instance of store with new name and remoteBaseUrl', function (t) {
t.plan(3)

var CustomStore = Store.defaults({
PouchDB: PouchDB,
remoteBaseUrl: 'http://example.com/'
})
var store = new CustomStore('test-db-clear')
var newOptions = {
name: 'new-test-db-clear'
}
store.on('clear', t.pass.bind(null, '"clear" event emitted'))

// merge in-memory adapter options
store.reset(newOptions)

.then(function () {
return store.findAll()
})

.then(function (result) {
t.deepEqual(result, [], '.findAll() resolves with empty array after .clear()')
t.is(store.db.name, 'new-test-db-clear', 'reset store has a new name')
})

.catch(t.fail)
})

test('store.reset creates empty instance of store with new name', function (t) {
t.plan(3)

var store = new Store('test-db-clear', {
PouchDB: PouchDB,
remote: 'test-db-clear'
})
var newOptions = {
name: 'new-test-db-clear'
}
store.on('clear', t.pass.bind(null, '"clear" event emitted'))

// merge in-memory adapter options
store.reset(newOptions)

.then(function () {
return store.findAll()
})

.then(function (result) {
t.deepEqual(result, [], '.findAll() resolves with empty array after .clear()')
t.is(store.db.name, 'new-test-db-clear', 'reset store has a new name')
})

.catch(t.fail)
})

function addEventToArray (array, object) {
if (arguments.length > 2) {
arguments[0].push({
Expand Down
40 changes: 40 additions & 0 deletions tests/specs/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,43 @@ test('Store api exports', function (t) {

t.end()
})

test('new Store(db, options) with options.remote being a PouchDB instance', function (t) {
var store = new Store('test-db', merge({
PouchDB: PouchDB,
remote: new PouchDB('test-db2')
}))

t.ok(store.db, 'sets .db on instance')
t.is(store.db.name, 'test-db', '.db is PouchDB object')

t.end()
})

test('new Store(db, options) with options.remote being a function that returns a PouchDB instance', function (t) {
var store = new Store('test-db', merge({
PouchDB: PouchDB,
remote: function () {
return new PouchDB('test-db2')
}
}))

t.ok(store.db, 'sets .db on instance')
t.is(store.db.name, 'test-db', '.db is PouchDB object')

t.end()
})

test('new Store(db, options) with options.remote being a function that resolves with PouchDB instance', function (t) {
var store = new Store('test-db', merge({
PouchDB: PouchDB,
remote: function () {
return Promise.resolve(new PouchDB('test-db2'))
}
}))

t.ok(store.db, 'sets .db on instance')
t.is(store.db.name, 'test-db', '.db is PouchDB object')

t.end()
})
66 changes: 27 additions & 39 deletions tests/specs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ test('triggers "change" events on pull', function (t) {
})

.then(function () {
t.is(changeEvents.length, 3, '"change" event triggered')
t.is(changeEvents.length, 3, '"change" events triggered')
t.is(changeEvents[0].event, 'add', '"change" triggered with event name')
t.is(changeEvents[0].object.foo, 'bar', '"change" triggered with object')

Expand All @@ -308,44 +308,6 @@ test('triggers "change" events on pull', function (t) {
.catch(t.fail)
})

test('triggers no "change" from remote if only local changes pushed', function (t) {
t.plan(2)

var store = new Store('test-db-remote-changes-local2', {
PouchDB: PouchDB,
remote: 'test-db-remote-changes-remote2'
})
var remoteChangeEvents = []

store.on('change', function (event, object, options) {
if (!options || !options.remote) {
return
}
remoteChangeEvents.push({
event: event,
object: object,
options: options
})
})

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

.then(function () {
return store.push()
})

.then(function () {
t.is(remoteChangeEvents.length, 0, 'no "change" events triggered')
return store.pull()
})

.then(function () {
t.is(remoteChangeEvents.length, 0, 'no "change" events triggered')
})

.catch(t.fail)
})

test('after "clear", store.on("push") for store.push()', function (t) {
t.plan(4)

Expand Down Expand Up @@ -376,3 +338,29 @@ test('after "clear", store.on("push") for store.push()', function (t) {

.catch(t.fail)
})

test('store.sync() with options.remote being a promise', function (t) {
t.plan(3)

var remoteDb = new PouchDB('test-db-sync-promise-remote')
var store = new Store('test-db-sync-promise', {
PouchDB: PouchDB,
remote: Promise.resolve(remoteDb)
})
var obj1 = {_id: 'test1', foo: 'bar1'}
var obj2 = {_id: 'test2', foo: 'bar2'}

store.add([obj1, obj2])

.then(function () {
return store.sync()
})

.then(function (pushedObjs) {
t.is(pushedObjs.length, 2, '2 items returned')
t.is(pushedObjs[0]._id, 'test1', 'returns hoodie object')
t.is(pushedObjs[1].foo, 'bar2', 'returns hoodie object')
})

.catch(t.fail)
})

0 comments on commit dd5b8ad

Please sign in to comment.