Skip to content

Instantly share code, notes, and snippets.

@pablohpsilva
Created July 9, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablohpsilva/a8a5df1bae91b48d67fce61846063e3c to your computer and use it in GitHub Desktop.
Save pablohpsilva/a8a5df1bae91b48d67fce61846063e3c to your computer and use it in GitHub Desktop.
/*
// Ideia de uso:
const fmongo = FunctionalMongo('mongodb://0.0.0.0:27017/yhub')
.collect('User')
.byId({ id })
*/
import { curry, objOf } from 'ramda'
import { MongoClient } from 'mongodb'
const _connect = curry((uri, options = { useNewUrlParser: true }) => MongoClient.connect(uri, options))
const _collection = curry((connection, collectionName) => connection.collection(collectionName))
const _FunctionalMongo = async (uri, options) => {
const client = await _connect(uri, options);
const connection = client.db();
return objOf(
'collect',
(collectionName) => {
const collection = await _collection(connection, collectionName)
return objOf(
'byId',
(identifier) => collection.find(identifier)
)
}
)
}
export default curry(_FunctionalMongo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment