joebalancio / mongo

MongoDB storage plugin for Mio.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

MongoDB storage plugin for Mio.

License:Other


Languages

Language:JavaScript 97.7%Language:Handlebars 2.3%