npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meerkat

v0.7.2

Published

an extremely thin mongo native driver wrapper that eliminates a surprising amount of boilerplate

Downloads

9

Readme

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 = express()
app.configure ->
  app.use express.static public_dir
  # ...
  app.use meerkat.middleware(app.locals)
  # ...
  app.use express.router
  # ...
{ options, uri } = config.mongodb
    meerkat.connect app.locals, options, uri, ->
      app.listen process.env.PORT || 3000

Use Meerkat Within Express Middleware/Routes:

# access the meerkat connection within express middlware
(req, res, next) ->
  req.meerkat.collection('users').find_one id: req.params.id, (user) ->
    # do something with the users

Or Better Yet...

# access meerkat collections within express middlware
(req, res, next) ->
  req.collection('users').find_one id: req.params.id, (user) ->
    # do something with the users

Connect to Mongo Outside of Express

{ options, uri } = config.mongodb
meerkat.connect options, uri, (connection) ->
  #work with the connection

Already have a native connection:

  connection = meerkat.wrapper native
  # work with the connection

Meerkat Collections

Single Use:

connection.collection('users').find_one id: req.params.id, (user) ->
  # do something with the user

Multi-Use:

connection.collection 'users', (Users) ->
  Users.find_one id: req.params.id, (user) ->
    Users.find(id: $in: user.friends).all (friends) ->
      # 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
connection.collection 'users', (err, Users) ->
  throw err if err?
  Users.findOne id: req.params.id, (err, user) ->
    throw err if err?
    Users.find(id: $in: user.friends).toArray (err, friends) ->
      throw err if err?
      # do something with the user's friends

#meerkat with custom failure handler
failure = (err) -> 
  console.log err
  throw err
connection.collection 'users', failure, (Users) ->
  Users.find_one id: req.params.id, failure, (user) ->
    Users.find(id: $in: user.friends).all failure, (friends) ->
      # do something with the user's friends

#meerkat with default error handler (common case)
connection.collection 'users', (Users) ->
  Users.find_one id: req.params.id, (user) ->
    Users.find(id: $in: user.friends).all (friends) ->
      # 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 = connection.collection('users').find active: true

A Merkat Cursor supports the following usage patterns:

# get a count of the results
cursor.count()

Or:

# access all of the cursor's results as an array
cursor.all (users) ->
  # do something with the active users

Or:

# iterate over each of the cursor's results one at a time
cursor.each (user) ->
  # perform an operation on each user

Or:

# sort the users by last name and then access a specific slice of the results
cursor.sort('name.last').limit(10).skip(10).all (users) ->
  # 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
cursor.sort('name.last').paginate 2, 10, (users, pagination) ->
  ###
  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)