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

mio-mongo

v0.11.4

Published

MongoDB storage plugin for Mio.

Downloads

89

Readme

mio-mongo Build Status Coverage Status NPM version Dependency Status

MongoDB storage plugin for Mio.

Install using npm:

npm install mio-mongo

Usage

var mio = require('mio');
var MongoDB = require('mio-mongo');

var User = mio.Resource.extend({
  attributes: {
    _id: {
      primary: true,
      objectId: true
    }
  }
});

User.use(MongoDB({
  url: 'mongodb://db.example.net:2500',
  collection: 'Users'
}));

User.Collection.get()
  .where({ active: true })
  .sort({ createdAt: 1 })
  .exec(function (err, users) {
    users.at(0).set({ active: false }).patch(function (err) {
      // ...
    });
  });

ObjectIDs

Automatically stringify and cast ObjectId's by setting objectId: true.

var User = mio.Resource.extend({
  attributes: {
    companyId: {
      objectId: true
    }
  }
});

User.find({
  companyId: '547dfc2bdc1e430000ff13b0'
}).exec(function (err, user) {
  console.log(typeof user.companyId); // => "string"
});

Relations

Post.belongsTo('author', {
  target: User,
  foreignKey: 'authorId'
});

User.hasMany('posts', {
  target: Post,
  foreignKey: 'authorId'
});

// fetch posts for user `123`
Post.Collection.get()
  .where({ 'author.id': 123 })
  .exec(function (err, posts) {
    // ...
  });

// fetch users with their posts included
User.Collection.get()
  .withRelated('posts')
  .exec(function (err, users) {
    users.pop().posts;
  });

Aliases

var User = mio.Resource.extend({
  attributes: {
    name: {
      alias: 'fullName'
    }
  }
});

// MongoDB query uses "fullName"
User.find({ name: 'Alex' }).exec(...);

Mongo client

Access the mongo client directly via Resource.options.mongo.db and the resource's collection via Resource.options.mongo.dbCollection.

API Reference

mio-mongo

module.exports(settings) ⇒ function

It is recommended to share the same settings.db object between different resources so they can share the same mongo client and connection pool.

A connection to mongo will be established automatically before any query is run.

If you'd like to use the mongo client directly, the db is available via Resource.options.mongo.db.

Kind: Exported function
Returns: function - returns Mio plugin

| Param | Type | Description | | --- | --- | --- | | settings | Object | | | settings.collection | String | mongodb collection for this resource | | [settings.connectionString] | String | mongodb connection string. required if settings.db is not provided. | | [settings.connectionOptions] | Object | mongodb connection options | | [settings.db] | mongodb.MongoClient.Db | reuse node-mongo-native db connection |

module.exports~prepareResource(resource, attributes) ⇒ Object

Prepare resource attributes.

  • Translate attribute aliases
  • Stringify ObjectIDs
  • Remove undefined attributes

Kind: inner method of module.exports

| Param | Type | | --- | --- | | resource | Resource | | attributes | Object |

"mongodb:query" (query)

Emitted with query argument whenever a query is received and before it is processed, to allow for transformation.

Kind: event emitted by module.exports

| Param | Type | | --- | --- | | query | Object |

"mongodb:collection" (collection)

Emitted whenever a collection of resources is returned. Collections returned by mio-mongo include size and from pagination properties.

Kind: event emitted by module.exports

| Param | Type | | --- | --- | | collection | external:mio.Resource.Collection | | collection.from | Number | | collection.size | Number |

Events

Contributing

Please submit all issues and pull requests to the mio/mongo repository!

Tests

Run tests using npm test or gulp test.

Code coverage

Generate code coverage using gulp coverage and open coverage.html in your web browser.

Support

If you have any problem or suggestion please open an issue here.