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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A bedrock module that creates a simple MongoDB database and provides an easy API for creating and working with its collections.
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);
;
For documentation on database configuration, see config.js.
An object whose keys are the names of the collections that have been
opened via openCollections
.
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.
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
.
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.
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 = [];