Created
September 5, 2016 10:15
-
-
Save fgandellini/aa0d811594287bc4918e371aed4c0f6e to your computer and use it in GitHub Desktop.
Cycle.js PouchDB driver (WIP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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