access-mongo
Simple MongoDB wrapper. Will present a consistent promised API for both MongoDB and NEDB databases.
This will be incredibly useful for testing using local files rather than actual MongoDB instances. This will also be very useful for using a MongoDB-like database for work that can use simple flat files. In this case your fixtures can just be the NEDB files, which are an extended form of JSON.
Installation
npm install --save access-mongo
Usage
var AccessMongo = require('access-mongo');
AccessMongo.connect('mongodb://foo:bar@hello.com:1234/mydb').then(function() {
var User = AccessMongo.createModel('users');
User.where({name: 'Matt'}).then(function(user) {
console.log(user);
});
});
AccessMongo.connect({
foo: 'mongodb://foo:bar@hello.com:1234/mydb',
bar: 'mongodb://foo:bar@yoyo.foo.bar:1234/otherdb'
}).then(function() {
var FooUser = AccessMongo.createModel('users', {connection: 'foo'});
var BarUser = AccessMongo.createModel('users', {connection: 'foo'});
});
AccessMongo.connect({
foo: 'mongodb://foo:bar@hello.com:1234/mydb',
bar: 'mongodb://foo:bar@yoyo.foo.bar:1234/otherdb'
}, {
maxPoolSize: 3
});
AccessMongo.connect('test.db').then(function() {
var User = AccessMongo.createModel('users');
User.where({name: 'Matt'}).then(function(user) {
console.log(user);
});
});
Primary API
AccessMongo
AccessMongo
is a singleton that manages the connection state to each database you connect to. You can connect to one or multiple databases. Each connection has a name that you can use to refer to it later. If you don't specify a name the connection name will be default
.
Connecting to a single database
Returns a promise that resolves to the instance of AccessMongo
.
AccessMongo.connect('mongodb://localhost:27017/test-db');
Connecting to a multiple databases
Returns a promise that resolves to the instance of AccessMongo
.
AccessMongo.connect({
default: 'mongodb://localhost:27017/test-db',
foo: 'mongodb://foo:bar@other.db.com:27019/some-db'
});
Disconnecting from all databases
Returns a promise that resolves to the instance of AccessMongo
.
AccessMongo.disconnect();
Creating a model
Returns a Model
object.
var User = AccessMongo.createModel('users');
var User = AccessMongo.createModel('users', {connection: 'foo'});
var User = AccessMongo.createModel('users', {database: 'other-db'});
var User = AccessMongo.createModel('users', {connection: 'foo', database: 'other-db'});
Model
Creating a query
var query = User.where({name: 'Matt Insler'});
query.where({email: 'matt.insler@gmail.com'});
Modifying a query
User.skip(5);
User.where({...}).skip(5);
User.fields({name: 1, address});
User.where({...}).fields({timestamp: 0});
User.sort({name: 1});
User.where({...}).sort({name: -1});
Fetching data from a query
User.first().then(function(user) {
});
User.where({...}).then(function(user) {
});
User.array().then(function(users) {
});
User.where({...}).array().then(function(users) {
});
var cursor = User.cursor();
var cursor = User.where({...}).cursor();
User.count().then(function(count) {
});
User.where({...}).count().then(function(count) {
});
User.distinct('name').then(function(distinctNames) {
});
User.where({...}).distinct('name').then(function(distinctNames) {
});
Inserting/updating documents
User.create({...}).then(function(savedDocument) {
);
User.create([{...}, {...}]).then(function(arrayOfSavedDocuments) {
);
User.save({...}).then(function(savedDocument) {
});
User.update(query, update, options).then(function(numDocumentsChanged, details) {
});
User.where({...}).update(update, options).then(function(numDocumentsChanged, details) {
});
User.where({...}).firstAndUpdate(update, options).then(function(fetchedDocument) {
});
Removing documents
User.remove();
User.remove({ override: true });
User.where({...}).remove().then(function(numDocumentsRemoved) {
});
Lower-level API
AccessMongo
Get the default connection
Returns the default connection object if there is a one or null
.
This is the equivalent of AccessMongo.getConnection('default')
.
var connection = AccessMongo.getDefaultConnection();
Get a connection by name
Returns the connection object by name or null
.
var connection = AccessMongo.getConnection('foo');
Connection
Get the default database
var db = connection.getDefaultConnection();
Get a database by name
var db = connection.getDatabase('my-db');
Disconnect this connection
connection.disconnect();
Database
Get a collection from the database
var collection = db.getCollection('users');
License
Copyright (c) 2015 Matt Insler
Licensed under the MIT license.