Skip to content
A simple package for MongoDB integration, in a format similar to mongo-map (enmap provided by a mongo store).
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE.md
README.md
connectDB.js
createDB.js
db.js
deleteItem.js
detectType.js
dropDB.js
getAll.js
getItem.js
package-lock.json
package.json
setItem.js

README.md

Version License Author

Description

A simple module to wrap the mongodb package, making it perform similar to mongo-map (enmap with a mongo store) but without the in-memory storage.

Installation

It's simple, just install it from npm. No external dependencies, should automatically install any additional npm packages.

npm i mongodb-multidriver

How to use

Here's an example snippet below for how you could use mongodb-multidriver

const DBDriver = require("mongodb-multidriver");
const dbo = new DBDriver({
  database: "myDatabase",
  collection: "myCollection",
  url: "mongodb://localhost/"
});

This is the minimum set required to setup and use the DB driver. You can pass an additional, optional, parameter with the options to automatically return data set as their originating class (see below, Structure Exporting).

In methods that include an optional callback, they can operate in promise and callback mode. In callback mode, await is not required - nothing is directly returned. If no callback is supplied, a promise is returned which must be await'd. Callback is optional. If the row does not exist, 'null' is returned. Data is returned in the format (err, result) for callback mode, and only the result for promise mode. You need to setup a .catch() to catch any errors on the promise if using the promises mode. Note that in callback mode, no structure exporting takes place - the raw data from the database is returned. In promise mode, structure exporting and some data sanitisation takes place (returning only the result.value of an object if it exists, instead of the whole object). Some of the basic commands:

(await) dbo.get(id, [callback])

Gets a row with the specified ID from the collection.

(await) dbo.set(id, value, [callback])

Sets the specified value into the collection at the specified ID.

(await) dbo.delete(id, [callback])

Deletes the row with the specified ID from the collection.

(await) dbo.drop([callback])

Drops the entire collection.

dbo.close([callback])

Closes the database/collection connection. Await/promise mode not available.

(await) dbo.forEach(action)

Gets every row from the collection, and performs the specified action with the retrieved rows. Returns a promise, resolves with no data, but will reject any errors so it should still be caught.

(await) dbo.map(action)

Gets every row from the collection, and maps them using the specified action. Returns a promise, resolves with the mapped result.

(await) dbo.filter(action)

Gets every row from the collection, and filters them according to the specified action. Returns a promise, resolves with the filtered result.

(await) dbo.all([callback])

Just gets every row in the collection.

Structure Exporting

In the options of the driver constructor, you can supply the property "structures" as an object, specifying a key/pair match of class identifiers and their corresponding classes. Using this object, the driver will attempt to return data from the collection that matches the class identifier as it's original class type, instead of a generic object type. For example, see the code snippet below:

const DBDriver = require("mongodb-multidriver");

class MyClassOne {
    constructor(previous, propOne, propTwo){
        this.propOne = (previous && previous.propOne) || propOne;
        this.propTwo = (previous && previous.propTwo) || propTwo;
        this.identifier = "MyClassOne";
    }
}

class dbo = new DBDriver({
    database: "myDatabase",
    collection: "myCollection",
    url: "mongodb://localhost/",
    structures: {
        "MyClassOne": MyClassOne
    }
});

(async ()=>{
    await dbo.set("row1", new MyClassOne(null, "Property 1", 2));
    console.log(await dbo.get("row1")); // returns an instance of MyClassOne, with propOne and propTwo set to the original values
})();

Note that the key for the structures in the options must match the identifier set in the classes - the identifier can technically be any equatable datatype, although I would recommend using either strings, integers, or hexadecimal types. A reference to the class must be supplied as the value to the key in the structures option.

For classes to work with this functionality, they must have logic in place to take the previous object as it's first parameter, assigning the values to the class where needed. This module will be updated in future to provide a helper to assist in easier creation of conforming classes.

You can’t perform that action at this time.