0.11.4 • Published 9 years ago

mio-mongo v0.11.4

Weekly downloads
133
License
-
Repository
github
Last release
9 years ago

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

ParamTypeDescription
settingsObject
settings.collectionStringmongodb collection for this resource
settings.connectionStringStringmongodb connection string. required if settings.db is not provided.
settings.connectionOptionsObjectmongodb connection options
settings.dbmongodb.MongoClient.Dbreuse 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

ParamType
resourceResource
attributesObject

"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

ParamType
queryObject

"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

ParamType
collectionexternal:mio.Resource.Collection
collection.fromNumber
collection.sizeNumber

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.

0.11.4

9 years ago

0.11.3

9 years ago

0.11.2

9 years ago

0.11.1

9 years ago

0.11.0

9 years ago

0.10.2

9 years ago

0.10.1

9 years ago

0.10.0

9 years ago

0.9.11

9 years ago

0.9.10

9 years ago

0.9.9

9 years ago

0.9.8

9 years ago

0.9.7

9 years ago

0.9.6

9 years ago

0.9.5

9 years ago

0.9.4

9 years ago

0.9.3

9 years ago

0.9.2

9 years ago

0.9.1

9 years ago

0.8.1

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.5.0

9 years ago

0.4.0

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago