digitalbazaar/bedrock-mongodb

Name: bedrock-mongodb

Owner: Digital Bazaar, Inc.

Description: Bedrock mongodb module

Created: 2015-01-01 06:57:01.0

Updated: 2016-02-05 22:44:03.0

Pushed: 2017-12-17 21:14:56.0

Homepage: null

Size: 115

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

bedrock-mongodb

A bedrock module that creates a simple MongoDB database and provides an easy API for creating and working with its collections.

Requirements
Quick Examples
install bedrock-mongodb

Below is an example that simply opens a collection when the database is ready and then runs a query and prints the result. A more common use case for a module that uses bedrock-mongodb would be to expose its own API that hides the details of using whatever collections it has opened.

bedrock = require('bedrock');
database = require('bedrock-mongodb');

ustom configuration
ock.config.mongodb.name = 'my_project_dev'; // default: bedrock_dev
ock.config.mongodb.host = 'localhost';      // default: localhost
ock.config.mongodb.port = 27017;            // default: 27017
ock.config.mongodb.username = 'my_project'; // default: bedrock
ock.config.mongodb.password = 'password';   // default: password

he mongodb database 'my_project_dev' and the 'my_project' user will
e created on start up following a prompt for the admin user credentials

lternatively, use `mongodb` URL format:
ock.config.mongodb.url = 'mongodb://localhost:27017/my_project_dev';

nable local collection if a local database is available
he local database has similar options to primary database
ee lib/config.js for details
edrock.config.mongodb.local.enable = true; // default: false

pen some collections once the database is ready
ock.events.on('bedrock-mongodb.ready', function(callback) {
tabase.openCollections(['collection1', 'collection2'], function(err) {
if(err) {
  return callback(err);
}
// do something with the open collection(s)
database.collections.collection1.find({id: 'foo'}, function(err, result) {
  if(err) {
    return callback(err);
  }
  console.log('result', result);
  callback();
});
;


ock.start();

Below is an example demonstrating the use of a distributed ID generator.

database = require('bedrock-mongodb');

base.getDistributedIdGenerator('mynamespace', function(err, idGenerator) {
(err) {
console.error('Error', err);
return;

Generator.generateId(function(err, id) {
if(err) {
  console.error('Error', err);
  return;
}
console.log('ID generated', identifier);
;

Configuration

For documentation on database configuration, see config.js.

Requirements
Setup
  1. Ensure an admin user is set up on mongodb. To do so, follow the instructions at mongodb.org for your version of MongoDB. Version 3.x is currently supported.
  2. [optional] Tweak your project's configuration settings; see Configuration or Quick Examples.
API
collections

An object whose keys are the names of the collections that have been opened via openCollections.

openCollections(collections, callback)

Opens a set of collections (creating them if necessary), if they aren't already open. Once all of the collections are open, callback is called. If an error occurs, callback is called immediately with the error. If no error occurs, then once the callback has been called, collections object will have keys that match the collection names and values that are instances of mongodb-native Collection.

getDistributedIdGenerator(namespace, callback)

Gets the DistributedIdGenerator for the given namespace. If the DistributedIdGenerator does not exist, it will be created. The callback will be passed an error if one occurred, otherwise it will be passed null for the error and the DistributedIdGenerator instance. A DistributedIdGenerator can be used to quickly generate unique identifiers in a safe and distributed manner.

The underlying assumption that prevents identifier collisions is that there is a shared collection (amongst all machines) with synchronized write access. To ensure identifiers can be generated without waiting for a system-wide lock, this collection is only hit once a local identifier namespace is exhausted, which should be very rare.

A distributed ID looks like:

sion>.<globalId>.<localId>.<currentId>

Where '.' is the reserved separator character. The globalId is stored in a shared database and can only be updated atomically.

The version is hardcoded to 1. The localId can be any combination of alphanumeric characters not including .. The . character was chosen instead of - or _ because those characters are used in URL-safe base64 encodings. This allows globalId and localId parts to be encoded in base64, however, they are encoded in hex in the current implementation as is the currentId.

DistributedIdGenerator.generateId(callback)

Generates a new unique, URL-safe identifier. The identifier is guaranteed not to conflict with any other identifier generated using the same namespace, regardless of the machine used to generate it (provided that the machines share the same database). If an error occurs, the callback will be called with the error, otherwise, it will be called with null for the error and the identifier.

Test Mode
Drop Collections on Initialization

When doing testing, it is often desirable to have empty collections at the beginning of test operations. This may be accomplished by the following configuration parameters IN ADDITION to specifying the test parameter on the command line. The test configuration in a project should ALWAYS specify a UNIQUE mongodb database.

lways specify a unique mongodb database for testing
ock.config.mongodb.name = 'my_project_test';
ock.config.mongodb.host = 'localhost';
ock.config.mongodb.port = 27017;
ock.config.mongodb.username = 'test'; // default: bedrock
ock.config.mongodb.password = 'password';
rop collections on initialization
ock.config.mongodb.dropCollections.onInit = true;
f 'onInit' is specified, 'collections' must also be specified
f collections is an empty array, ALL collections will be dropped
ock.config.mongodb.dropCollections.collections = [];

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.