Meerkat
An extremely thin mongo native driver wrapper that eliminates a surprising amount of boilerplate
Getting Started
Install Meerkat:
npm install meerkat --save
Examples
Meerkat was written to improve the experience of accessing MongoDB within the context of an Express app written in CoffeeScript. As such, the examples will demonstrate its usage in this context. However, Meerkat is not limited to use within Express and its API should translate well to pure JavaScript.
Express Middleware
The Meerkat Express middleware enhances the Express request object with a reference to the current Meerkat Connection and an alias to the Connection's collection
method.
Initialize Connection and Middleware:
express = require 'express'meerkat = require 'meerkat'app = expressappconfigure -> appuse expressstatic public_dir # ... appuse meerkatmiddlewareapplocals # ... appuse expressrouter # ... configmongodb meerkatconnect applocalsoptionsuri-> applisten processenvPORT || 3000
Use Meerkat Within Express Middleware/Routes:
# access the meerkat connection within express middlware reqmeerkatcollection'users'find_one id: reqparamsid # do something with the users
Or Better Yet...
# access meerkat collections within express middlware reqcollection'users'find_one id: reqparamsid # do something with the users
Connect to Mongo Outside of Express
configmongodbmeerkatconnect optionsuri #work with the connection
Already have a native connection:
connection = meerkatwrapper native # work with the connection
Meerkat Collections
Single Use:
connectioncollection'users'find_one id: reqparamsid # do something with the user
Multi-Use:
connectioncollection 'users' Usersfind_one id: reqparamsid Usersfindid: $in: userfriendsall # do something with the user's friends
Meerkat exposes Collection methods equivalent to the Mongo Native Driver with a snake_case as apposed to camelCase syntax. It also avoids a considerable amount of boilerplate by allowing for the configuration of a default failure callback. In the context of Express Middleware and intelligent default failure behavior is setup for you. Meerkat API calls map directly to Mongo Native Driver calls witht he following translation rules of thumb.
#native connectioncollection 'users' throw err if err? UsersfindOne id: reqparamsid throw err if err? Usersfindid: $in: userfriendstoArray throw err if err? # do something with the user's friends #meerkat with custom failure handler = consolelog err throw errconnectioncollection 'users'failure Usersfind_one id: reqparamsidfailure Usersfindid: $in: userfriendsall failure # do something with the user's friends #meerkat with default error handler (common case) connectioncollection 'users' Usersfind_one id: reqparamsid Usersfindid: $in: userfriendsall # do something with the user's friends
Access a Cursor
The find method of a Meerkat Collection returns a Cursor much like the mongodb native driver with some key enhancements.
# get a cursor over all active users cursor = connectioncollection'users'find active: true
A Merkat Cursor supports the following usage patterns:
# get a count of the results cursorcount
Or:
# access all of the cursor's results as an array cursorall # do something with the active users
Or:
# iterate over each of the cursor's results one at a time cursoreach # perform an operation on each user
Or:
# sort the users by last name and then access a specific slice of the results cursorsort'name.last'limit10skip10all # do something with the users
Or:
# sort the users by last and first and use the pagination helper to access the second page of results cursorsort'name.last'paginate 210 ### pagination metadata: pagination.total --> the total number of results matching the query pagination.pages --> the total number of pages in the cursor given the current results returned per page pagination.per --> the maxiumum number of results per page pagination.page --> the page number of the current page counting from 1 ### # do something with the users
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
Release History
(Nothing yet)