Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge pouchdb-hoodie-api and pouchdb-hoodie-sync #153

Merged
merged 20 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 35 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module.exports = Store

var EventEmitter = require('events').EventEmitter

var merge = require('lodash/merge')
var assign = require('lodash/assign')

var subscribeToSyncEvents = require('./lib/subscribe-to-sync-events')
var isPersistent = require('./lib/is-persistent')
var startListenToChanges = require('./lib/helpers/start-listen-to-changes')

function Store (dbName, options) {
if (!(this instanceof Store)) return new Store(dbName, options)
Expand All @@ -25,51 +24,45 @@ function Store (dbName, options) {
}
}

// plugins are directly applied to `options.PouchDB`
options.PouchDB
.plugin(require('pouchdb-hoodie-api'))
.plugin(require('pouchdb-hoodie-sync'))

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

var state = {
db: db
db: db,
dbName: dbName,
PouchDB: options.PouchDB,
emitter: emitter,
remote: options.remote
}

var api = merge(
{
db: storeApi.db,
add: storeApi.add,
find: storeApi.find,
findAll: storeApi.findAll,
findOrAdd: storeApi.findOrAdd,
update: storeApi.update,
updateOrAdd: storeApi.updateOrAdd,
updateAll: storeApi.updateAll,
remove: storeApi.remove,
removeAll: storeApi.removeAll,
on: storeApi.on,
one: storeApi.one,
off: storeApi.off,
withIdPrefix: storeApi.withIdPrefix
},
{
push: syncApi.push,
pull: syncApi.pull,
sync: syncApi.sync,
connect: syncApi.connect,
disconnect: syncApi.disconnect,
isConnected: syncApi.isConnected,
isPersistent: isPersistent.bind(null, state)
}
)

api.reset = require('./lib/reset').bind(null, dbName, options, state, api, storeApi.clear, emitter)
state.emitter.once('newListener', startListenToChanges.bind(null, state))

var api = {
db: state.db,
isPersistent: require('./lib/is-persistent').bind(null, state),
add: require('./lib/add').bind(null, state, null),
find: require('./lib/find').bind(null, state, null),
findAll: require('./lib/find-all').bind(null, state, null),
findOrAdd: require('./lib/find-or-add').bind(null, state, null),
update: require('./lib/update').bind(null, state, null),
updateOrAdd: require('./lib/update-or-add').bind(null, state, null),
updateAll: require('./lib/update-all').bind(null, state, null),
remove: require('./lib/remove').bind(null, state, null),
removeAll: require('./lib/remove-all').bind(null, state, null),
withIdPrefix: require('./lib/with-id-prefix').bind(null, state),
on: require('./lib/on').bind(null, state),
one: require('./lib/one').bind(null, state),
off: require('./lib/off').bind(null, state),
pull: require('./lib/pull').bind(null, state),
push: require('./lib/push').bind(null, state),
sync: require('./lib/sync').bind(null, state),
connect: require('./lib/connect').bind(null, state),
disconnect: require('./lib/disconnect').bind(null, state),
isConnected: require('./lib/is-connected').bind(null, state),
reset: require('./lib/reset').bind(null, state)
}

subscribeToSyncEvents(syncApi, emitter)
state.api = api

return api
}
Expand All @@ -79,7 +72,7 @@ Store.defaults = function (defaultOpts) {
if (typeof dbName !== 'string') throw new Error('Must be a valid string.')
options = options || {}

options = merge({}, defaultOpts, options)
options = assign({}, defaultOpts, options)

return new Store(dbName, options)
}
Expand Down
18 changes: 18 additions & 0 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var addOne = require('./helpers/add-one')
var addMany = require('./helpers/add-many')

module.exports = add

/**
* adds one or multiple objects to local database
*
* @param {String} prefix optional id prefix
* @param {Object|Object[]} properties Properties of one or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You named it properties but the param is named objects. Should we choose the same term to prevent confusion?

* multiple objects
* @return {Promise}
*/
function add (state, prefix, properties) {
return Array.isArray(properties)
? addMany(state, properties, prefix)
: addOne(state, properties, prefix)
}
36 changes: 36 additions & 0 deletions lib/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = connect

var Promise = require('lie')

/**
* connects local and remote database
*
* @return {Promise}
*/
function connect (state) {
return Promise.resolve(state.remote)

.then(function (remote) {
if (state.replication) {
return
}

state.replication = state.db.sync(remote, {
create_target: true,
live: true,
retry: true
})

state.replication.on('error', function (error) {
state.emitter.emit('error', error)
})

state.replication.on('change', function (change) {
for (var i = 0; i < change.change.docs.length; i++) {
state.emitter.emit(change.direction, change.change.docs[i])
}
})

state.emitter.emit('connect')
})
}
18 changes: 18 additions & 0 deletions lib/disconnect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = disconnect

var Promise = require('lie')

/**
* disconnects local and remote database
*
* @return {Promise}
*/
function disconnect (state) {
if (state.replication) {
state.replication.cancel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afaik .cancel() is finished when the complete event is emitted by PouchDB. Is it sensible to wait for this event before resolving and/or emitting the disconnect event?

Copy link
Member Author

@gr2m gr2m Jun 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! This didn’t cause problems so far, so I don’t think it’s critical, but I created a follow up issues for it so we don’t forget to fix it: #155

delete state.replication
state.emitter.emit('disconnect')
}

return Promise.resolve()
}
37 changes: 37 additions & 0 deletions lib/find-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var isntDesignDoc = require('./utils/isnt-design-doc')

module.exports = findAll

/**
* finds all existing objects in local database.
*
* @param {String} prefix optional id prefix
* @param {Function} [filter] Function returning `true` for any object
* to be returned.
* @return {Promise}
*/

function findAll (state, prefix, filter) {
var options = {
include_docs: true
}

if (prefix) {
options.startkey = prefix
options.endkey = prefix + '\uffff'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this \uffff character?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it’s the highest Unicode character, so it makes sure it gives us all keys which start with the prefix string, see http://docs.couchdb.org/en/2.0.0/couchapp/views/collation.html#string-ranges (I don’t know why they use ufff0 instead of uffff in the example though)

}

return state.db.allDocs(options)

.then(function (res) {
var objects = res.rows
.filter(isntDesignDoc)
.map(function (row) {
return row.doc
})

return typeof filter === 'function'
? objects.filter(filter)
: objects
})
}
20 changes: 20 additions & 0 deletions lib/find-or-add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var findOrAddOne = require('./helpers/find-or-add-one')
var findOrAddMany = require('./helpers/find-or-add-many')

module.exports = findOrAdd

/**
* tries to find object in local database, otherwise creates new one
* with passed properties.
*
* @param {String} prefix optional id prefix
* @param {String|Object|Object[]} idOrObject id or object with `._id` property
* @param {Object} [properties] Optional properties if id passed
* as first option
* @return {Promise}
*/
function findOrAdd (state, prefix, idOrObjectOrArray, properties) {
return Array.isArray(idOrObjectOrArray)
? findOrAddMany(state, idOrObjectOrArray, prefix)
: findOrAddOne(state, idOrObjectOrArray, properties, prefix)
}
18 changes: 18 additions & 0 deletions lib/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var findOne = require('./helpers/find-one')
var findMany = require('./helpers/find-many')

module.exports = find

/**
* finds existing object in local database
*
* @param {String} prefix optional id prefix
* @param {String|Object|Object[]} objectsOrIds Id of object or object with
* `._id` property
* @return {Promise}
*/
function find (state, prefix, objectsOrIds) {
return Array.isArray(objectsOrIds)
? findMany(state, objectsOrIds, prefix)
: findOne(state, objectsOrIds, prefix)
}
39 changes: 39 additions & 0 deletions lib/helpers/add-many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var clone = require('lodash/clone')
var uuid = require('pouchdb-utils').uuid

var addTimestamps = require('../utils/add-timestamps')

module.exports = function addMany (state, docs, prefix) {
docs = docs.map(function (doc) {
doc = clone(doc)
delete doc.hoodie
return addTimestamps(doc)
})

if (prefix) {
docs.forEach(function (doc) {
doc._id = prefix + (doc._id || uuid())
})
}

return state.db.bulkDocs(docs)

.then(function (responses) {
return responses.map(function (response, i) {
if (response instanceof Error) {
if (response.status === 409) {
var conflict = new Error('Object with id "' + docs[i]._id + '" already exists')
conflict.name = 'Conflict'
conflict.status = 409
return conflict
} else {
return response
}
}

docs[i]._id = response.id
docs[i]._rev = response.rev
return docs[i]
})
})
}
43 changes: 43 additions & 0 deletions lib/helpers/add-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var clone = require('lodash/clone')
var PouchDBErrors = require('pouchdb-errors')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pouchdb-errors is also missing in the dependencies. See my comment from above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks 👍

var Promise = require('lie')
var uuid = require('pouchdb-utils').uuid

var addTimestamps = require('../utils/add-timestamps')

module.exports = function addOne (state, doc, prefix) {
if (typeof doc !== 'object') {
return Promise.reject(PouchDBErrors.NOT_AN_OBJECT)
}

doc = clone(doc)

if (!doc._id) {
doc._id = uuid()
}

if (prefix) {
doc._id = prefix + doc._id
}

delete doc.hoodie

return state.db.put(addTimestamps(doc))

.then(function (response) {
doc._id = response.id
doc._rev = response.rev
return doc
})

.catch(function (error) {
if (error.status === 409) {
var conflict = new Error('Object with id "' + doc._id + '" already exists')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are constructing this Error object at different places. Might it be sensible to extract it to a helper function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we had the idea a while ago to have a central repository where we keep all our errors, in an effort to normalise them across all repositories and also client & server: gr2m/milestones#95

I think for now we can keep it as is, it’s just two instances of a missing and conflict error respectively

conflict.name = 'Conflict'
conflict.status = 409
throw conflict
} else {
throw error
}
})
}
33 changes: 33 additions & 0 deletions lib/helpers/find-many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var toId = require('../utils/to-id')

module.exports = function findMany (state, idsOrObjects, prefix) {
var ids = idsOrObjects.map(toId)

if (prefix) {
ids = ids.map(function (id) {
return id.substr(0, prefix.length) === prefix ? id : prefix + id
})
}

return state.db.allDocs({keys: ids, include_docs: true})

.then(function (response) {
var foundMap = response.rows.reduce(function (map, row) {
map[row.id] = row.doc
return map
}, {})
var docs = ids.map(function (id) {
var doc = foundMap[id]
if (doc) {
return doc
}

var missing = new Error('Object with id "' + id + '" is missing')
missing.name = 'Not found'
missing.status = 404
return missing
})

return docs
})
}
22 changes: 22 additions & 0 deletions lib/helpers/find-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var toId = require('../utils/to-id')

module.exports = function findOne (state, idOrObject, prefix) {
var id = toId(idOrObject)

if (prefix && id.substr(0, prefix.length) !== prefix) {
id = prefix + id
}

return state.db.get(id)

.catch(function (error) {
if (error.status === 404) {
var missing = new Error('Object with id "' + id + '" is missing')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding my comment about error construction. Might this also be sensible to extract as a helper function if we need it at different places?

missing.name = 'Not found'
missing.status = 404
throw missing
} else {
throw error
}
})
}
Loading