Table of contents
Introduction
Mongoopose is very simple and currently provides by far not all of the mongoose api.
It wraps mongoose and behaves like an adapter, based on functional programming patterns
and Promises to escape the pyramid of doom.
Combining function composition and Promises comes in very handy, when complex database
queries make your head hurt. Your code becomes more readable and easier to reason about,
because you will start to see how your data flows.
Mongoopose itself has only a few very basic wrapper functions like find, findOne, save and remove,
but leaves you with a simple possibility to compose every async task into the pipeline.
Prerequisites
Most likely you would have a basic understanding of functional programming,
but happily you do not need it to just use it, because the implementation is very straight forward.
There is no npm dependency as you can see in package.json, because mongoopose uses
dependency injection.
In a nutshell... it depends on mongoose, which means you also have a MongoDB setup.
Installation
npm install @stejnar/mongoopose
Usage
Enough talking. Here is some code:
const mongoopose = require('mongoopose')
const mongoose = require('mongoose')
const userSchema = require('./schema')
const UserModel = mongoose.model('User', userSchema)
const {compose, Params} = mongoopose
const User = mongoopose.model(UserModel)
const selectJon = params => Params({select: {email: 'jon-snow@iron-throne.com'}})
const updateJon = params => Params({query: {$set: {lifes: 2}}})
const pipeline = compose(
User.findOne(selectJon),
User.update(updateJon)
)
pipeline(Params())
.then(params => console.log(params))
.catch(error => console.error(error))
See functors.test.js for more examples
Documentation
mongoopose
mongoopose.model()
Parameters:
A model that gets returned from mongoose.model()
Returns:
Model
mongoopose.compose()
Parameter:
...function, you should only pass findById(), findOne(), find(), save(), update(), remove() and/or pipe()
Returns:
function(Params), that returns a Promise
mongoopose.Params
Model
- Model.findById()
- Model.findOne()
- Model.find()
- Model.save()
- Model.update()
- Model.remove()
Note: All methods, but pipe(), take the same input and give the same output.
As follows:
Parameter:
function(query = Params => Params), optional and defaults into no Params transformation
Returns:
function(Params), that returns a Promise
Model.pipe()
Parameter:
function(action), action can be any function that gets resolve, reject and params passed into and calls resolve or reject
Returns:
function(Params), that returns a Promise
Params
Params.as
Type: String
Default: mongoose.Model.modelName
This is the key with that a queries result gets assigned to Params.
Params.select
Type: Object
This is the selector that gets passed into mongoose queries.
Params.query
Type: Object
This is the update object for Model.update()
Params.save
Type: Object
This gets passed into the mongoose model constructor for Model.save().
Params.add()
Parameters:
- result any,
- name String, this is the key with that result gets assigned to Params
Returns:
Params
Params.assign()
Parameters:
obj, any
Returns:
Params
Params.toError()
Parameters:
Returns:
Error