an extremely thin mongo native driver wrapper that eliminates a surprising amount of boilerplate
An extremely thin mongo native driver wrapper that eliminates a surprising amount of boilerplate
npm install meerkat --save
The Meerkat Express middleware enhances the Express request object with a reference to the current Meerkat Connection and an alias to the Connection's
Initialize Connection and Middleware:
express = require 'express'meerkat = require 'meerkat'app = expressappconfigure ->appuse expressstatic public_dir# ...appuse meerkatmiddlewareapplocals# ...appuse expressrouter# ...configmongodbmeerkatconnect applocalsoptionsuri->applisten processenvPORT || 3000
Use Meerkat Within Express Middleware/Routes:
# access the meerkat connection within express middlwarereqmeerkatcollection'users'find_one id: reqparamsid# do something with the users
Or Better Yet...
# access meerkat collections within express middlwarereqcollection'users'find_one id: reqparamsid# do something with the users
configmongodbmeerkatconnect optionsuri#work with the connection
Already have a native connection:
connection = meerkatwrapper native# work with the connection
connectioncollection'users'find_one id: reqparamsid# do something with the user
connectioncollection 'users'Usersfind_one id: reqparamsidUsersfindid: $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.
#nativeconnectioncollection 'users'throw err if err?UsersfindOne id: reqparamsidthrow err if err?Usersfindid: $in: userfriendstoArraythrow err if err?# do something with the user's friends#meerkat with custom failure handler=consolelog errthrow errconnectioncollection 'users'failureUsersfind_one id: reqparamsidfailureUsersfindid: $in: userfriendsall failure# do something with the user's friends#meerkat with default error handler (common case)connectioncollection 'users'Usersfind_one id: reqparamsidUsersfindid: $in: userfriendsall# do something with the user's friends
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 userscursor = connectioncollection'users'find active: true
A Merkat Cursor supports the following usage patterns:
# get a count of the resultscursorcount
# access all of the cursor's results as an arraycursorall# do something with the active users
# iterate over each of the cursor's results one at a timecursoreach# perform an operation on each user
# sort the users by last name and then access a specific slice of the resultscursorsort'name.last'limit10skip10all# do something with the users
# sort the users by last and first and use the pagination helper to access the second page of resultscursorsort'name.last'paginate 210###pagination metadata:pagination.total --> the total number of results matching the querypagination.pages --> the total number of pages in the cursor given the current results returned per pagepagination.per --> the maxiumum number of results per pagepagination.page --> the page number of the current page counting from 1#### do something with the users
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.