Created
June 21, 2017 14:27
-
-
Save evanxg852000/3dcc14829eb4432a968f23506d2a08ce to your computer and use it in GitHub Desktop.
Utils to access couchDB using nano
This file contains hidden or 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 Promise from 'bluebird' | |
import nano from 'nano' | |
import {log} from './utils' | |
const dbService = 1//nano(process.env.DB_SERVICE_URL) | |
export const getDbService = () => { | |
Promise.promisifyAll(dbService.db) | |
return Promise.promisifyAll(dbService) | |
} | |
export const getDb = (dbName) => { | |
return Promise.promisifyAll(dbService.db.use(dbName)) | |
} | |
export const save = (db, doc) => { | |
return db.insert(doc).then((res) => { | |
return res | |
}).catch((err) => { | |
log('Error while saving the doc', err) | |
}) | |
} | |
export const bulkSave = (db, docs) => { | |
// TODO | |
return db.bulk(docs, [params]).then((res) =>{ | |
return res | |
}).catch((err) => { | |
log('Error while saving the docs', err) | |
}) | |
} | |
export const getView = (db, designName, viewName, params = null) => { | |
return db.view(designName, viewName, (params || { include_docs: true })) | |
.then((resultSet) => { | |
return resultSet.map((row) => { | |
return row | |
}).filter((row) => { | |
return row !== null | |
}) | |
}) | |
} | |
const using = (dbName) => { | |
return { | |
save: (doc) => { | |
return getDb(dbName).then((db) =>{ | |
return save(db, doc) | |
}) | |
} | |
} | |
} | |
const CouchDbService = getDbService() | |
export default CouchDbService |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment