Skip to content

Instantly share code, notes, and snippets.

@fgandellini
Created September 5, 2016 10:15
Show Gist options
  • Save fgandellini/aa0d811594287bc4918e371aed4c0f6e to your computer and use it in GitHub Desktop.
Save fgandellini/aa0d811594287bc4918e371aed4c0f6e to your computer and use it in GitHub Desktop.
Cycle.js PouchDB driver (WIP)
import xs from 'xstream'
import XStreamAdapter from '@cycle/xstream-adapter'
export function makePouchDBDriver(PouchDB, dbName, dbOptions) {
PouchDB.plugin(require('relational-pouch'))
const db = new PouchDB(dbName, dbOptions)
const api = ['put', 'get', 'allDocs' , 'destroy', 'setSchema', 'rel.save', 'rel.find', 'rel.del']
return (sinks, streamAdapter) =>
Object.assign({},
...api
.filter(m => !m.includes('.'))
.map(method => ({
[method]() {
const data$ = xs.fromPromise(db[method].apply(db, arguments))
return streamAdapter.adapt(data$, XStreamAdapter.streamSubscribe)
}
})),
...api
.filter(m => m.includes('.'))
.map(objMethod => {
const [obj, method] = objMethod.split('.')
return {
[obj]: {
[method]() {
const data$ = xs.fromPromise(db[method].apply(db, arguments))
return streamAdapter.adapt(data$, XStreamAdapter.streamSubscribe)
}
}
}
})
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment