Skip to content

Commit

Permalink
feat: .withIdPrefix(prefix)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 4, 2017
1 parent 1a2e774 commit 3880604
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 27 deletions.
21 changes: 19 additions & 2 deletions helpers/add-many.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
'use strict'

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

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

module.exports = function addMany (objects) {
module.exports = function addMany (objects, prefix) {
objects.forEach(addTimestamps)
return this.bulkDocs(objects.map(toDoc))

if (prefix) {
objects.forEach(function (object) {
if (!object.id) {
object.id = uuid()
}

if (prefix) {
object.id = prefix + object.id
}
})
}

var docs = objects.map(toDoc)

return this.bulkDocs(docs)

.then(function (responses) {
return responses.map(function (response, i) {
Expand Down
13 changes: 10 additions & 3 deletions helpers/add-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

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

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

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

var method = object.id ? 'put' : 'post'
if (!object.id) {
object.id = uuid()
}

if (prefix) {
object.id = prefix + object.id
}

return this[method](toDoc(addTimestamps(object)))
return this.put(toDoc(addTimestamps(object)))

.then(function (response) {
object.id = response.id
Expand Down
8 changes: 7 additions & 1 deletion helpers/find-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
var toId = require('../utils/to-id')
var toObject = require('../utils/to-object')

module.exports = function findMany (idsOrObjects) {
module.exports = function findMany (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 this.allDocs({keys: ids, include_docs: true})

.then(function (response) {
Expand Down
7 changes: 6 additions & 1 deletion helpers/find-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
var toId = require('../utils/to-id')
var toObject = require('../utils/to-object')

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

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

return this.get(id)

.then(toObject)
Expand Down
16 changes: 11 additions & 5 deletions helpers/find-or-add-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ var toId = require('../utils/to-id')
var findMany = require('./find-many')
var addMany = require('./add-many')

module.exports = function findOrAddMany (state, passedObjects) {
module.exports = function findOrAddMany (state, passedObjects, prefix) {
var self = this
var foundObjects
var passedObjectIds = passedObjects.map(toId)

return findMany.call(this, passedObjectIds)
if (prefix) {
passedObjectIds = passedObjectIds.map(function (id) {
return prefix + id
})
}

return findMany.call(this, passedObjectIds, prefix)

.then(function (_foundObjects) {
foundObjects = _foundObjects

var foundObjectIds = foundObjects.map(toId)
var notFoundObjects = passedObjects.reduce(function (notFoundObjects, passedObject) {
if (foundObjectIds.indexOf(passedObject.id) === -1) {
if (foundObjectIds.indexOf((prefix || '') + passedObject.id) === -1) {
notFoundObjects.push(passedObject)
}
return notFoundObjects
}, [])

if (state) {
return addMany.call(self, notFoundObjects)
return addMany.call(self, notFoundObjects, prefix)
}

return addMany.call(self, notFoundObjects)
return addMany.call(self, notFoundObjects, prefix)
})

.then(function (addedObjects) {
Expand Down
6 changes: 3 additions & 3 deletions helpers/find-or-add-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var toId = require('../utils/to-id')
var findOne = require('./find-one')
var addOne = require('./add-one')

function findOrAddOne (state, idOrObject, newObject) {
function findOrAddOne (state, idOrObject, newObject, prefix) {
var self = this
var id = toId(idOrObject)

Expand All @@ -19,7 +19,7 @@ function findOrAddOne (state, idOrObject, newObject) {
return Promise.reject(PouchDBErrors.MISSING_ID)
}

return findOne.call(this, id)
return findOne.call(this, id, prefix)

.catch(function (/* error */) {
if (typeof newObject === 'object') {
Expand All @@ -29,7 +29,7 @@ function findOrAddOne (state, idOrObject, newObject) {
}

if (state) {
return addOne.call(self, newObject)
return addOne.call(self, newObject, prefix)
}

return addOne.call(self, newObject)
Expand Down
4 changes: 2 additions & 2 deletions helpers/update-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ var toId = require('../utils/to-id')

var findMany = require('./find-many')

module.exports = function updateMany (array, change) {
module.exports = function updateMany (array, change, prefix) {
var self = this
var objects
var ids = array.map(toId)

return findMany.call(this, array)
return findMany.call(this, array, prefix)

.then(function (objects) {
if (change) {
Expand Down
6 changes: 3 additions & 3 deletions helpers/update-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ var addTimestamps = require('../utils/add-timestamps')

var findOne = require('./find-one')

module.exports = function updateOne (idOrObject, change) {
module.exports = function updateOne (idOrObject, change, prefix) {
var self = this
var object

if (typeof idOrObject === 'string' && !change) {
return Promise.reject(PouchDBErrors.NOT_AN_OBJECT)
}

return findOne.call(this, idOrObject)
return findOne.call(this, idOrObject, prefix)

.then(function (object) {
if (!change) {
return extend(object, idOrObject)
return extend(object, idOrObject, {id: object.id, _rev: object._rev})
}
return changeObject(change, object)
})
Expand Down
13 changes: 10 additions & 3 deletions helpers/update-or-add-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ var toId = require('../utils/to-id')
var addMany = require('./add-many')
var updateMany = require('./update-many')

module.exports = function updateOrAddMany (passedObjects) {
module.exports = function updateOrAddMany (passedObjects, prefix) {
var self = this
var addedObjects
var passedObjectIds = passedObjects.map(toId)

return addMany.call(this, passedObjects)
if (prefix) {
passedObjectIds = passedObjectIds.map(function (id) {
return prefix + id
})
}

return addMany.call(this, passedObjects, prefix)

.then(function (_addedObjectsAndErrors) {
addedObjects = _addedObjectsAndErrors

var conflicting = passedObjects.reduce(function (array, passedObject, i) {
var objectOrError = _addedObjectsAndErrors[i]
var isConflictError = objectOrError instanceof Error && objectOrError.status === 409
Expand All @@ -21,7 +28,7 @@ module.exports = function updateOrAddMany (passedObjects) {
return array
}, [])

return updateMany.call(self, conflicting)
return updateMany.call(self, conflicting, null, prefix)
})

.then(function (updatedObjects) {
Expand Down
8 changes: 4 additions & 4 deletions helpers/update-or-add-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ var toId = require('../utils/to-id')
var addOne = require('./add-one')
var updateOne = require('./update-one')

module.exports = function updateOrAddOne (idOrObject, newObject) {
module.exports = function updateOrAddOne (idOrObject, newObject, prefix) {
var self = this
return updateOne.call(this, idOrObject, newObject)
return updateOne.call(this, idOrObject, newObject, prefix)

.catch(function (error) {
if (error.status !== 404) {
Expand All @@ -13,9 +13,9 @@ module.exports = function updateOrAddOne (idOrObject, newObject) {

if (newObject) {
newObject.id = toId(idOrObject)
return addOne.call(self, newObject)
return addOne.call(self, newObject, prefix)
}

return addOne.call(self, idOrObject)
return addOne.call(self, idOrObject, prefix)
})
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function hoodieApi (options) {
updateAll: require('./update-all').bind(this),
remove: require('./remove').bind(this),
removeAll: require('./remove-all').bind(this),
withIdPrefix: require('./with-id-prefix').bind(this, state),
on: require('./lib/on').bind(this, state),
one: require('./lib/one').bind(this, state),
off: require('./lib/off').bind(this, state),
Expand Down

0 comments on commit 3880604

Please sign in to comment.