0.0.2 • Published 7 years ago

inmemory-mongo v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

INMEMORY-MONGO

Build Status

Spins up an actual mongodb instance programmatically from node for testing or mocking during development.

Works on all platforms which is due to the awesome mongodb-prebuilt package.

Installation

The library has only been tested with Node 8.

npm install inmemory-mongo

Usage

Require inmemory-mongo, create an instance and start the server.

const MongoInstance from 'inmemory-mongo';

const mongoInstance = new MongoInstance();
mongoInstance.start()
  .then(config => {
    console.log("HOST " + config.host);
    console.log("PORT " + config.port);
  });

//Do something with your mongo instance

mongoInstance.stop()
    .then(() => {
        //Instance has now stopped
    });

The library is built around Promises, and as such can also take advantage of async/await semantics.

const MongoInstance from 'inmemory-mongo';

//Inside an async function
const mongoInstance = new MongoInstance();
const config = await mongoInstance.start();
console.log("HOST " + config.host);
console.log("PORT " + config.port);

//Do something with your mongo instance

await mongoInstance.stop();

Methods

constructor(PORT)

If no PORT is specified the default value is 27017

start()

Starts an instance of mongodb Returns a Promise on startup containing the configuration of the new instance - host and port

stop()

Stops the instance Returns an empty Promise when the instance has terminated

getUri()

Returns a URI that can be used to connect to the running instance

getConnection(DATABASE_NAME)

Returns a Promise of a connection object from the official MongoDB driver for Node.js

getDocument(DATABASE_NAME, COLLECTION_NAME, DOCUMENT_ID)

Returns a Promise containing the desired document

addDocument(DATABASE_NAME, COLLECTION_NAME, DOCUMENT_TO_ADD)

Adds a document to a collection of a database Returns a Promise of the actual object that was placed in the collection

addDocuments(DATABASE_NAME, COLLECTIONS_PATH)

Adds an entire directory to the database. The directory must contains sub-directories named after each collection name. Each collection directory must then contain the documents that must be added. Check out the test named addDirectoryOfCollections() within the file tests/test.js. Returns a Promise of the exact objects that were placed in mongodb

Contributions

This project is a fork of mongo-in-memory by Giorgio Zamparelli.

License

The MIT License (MIT)

Copyright (c) 2017 James Sawle

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.