architect-mongodb-native

Expose a mongodb client as architect plugin.

Usage no npm install needed!

<script type="module">
  import architectMongodbNative from 'https://cdn.skypack.dev/architect-mongodb-native';
</script>

README

architect-mongodb-native

Expose a mongodb client as architect plugin.

Installation

npm install --save architect-mongodb-native

Config Format

{
  "packagePath": "architect-mongodb-native",
  "url": "mongodb://127.0.0.1:27017/test"
}

Supported config elements

url

MongoDB server url.

config

Additional mongodb config parameters.

Usage

Boot Architect :

var path = require('path');
var architect = require("architect");

var configPath = path.join(__dirname, "config.js");
var config = architect.loadConfig(configPath);

architect.createApp(config, function (err, app) {
    if (err) {
        throw err;
    }
    console.log('application started');
});

Configure mongodb with config.js :

module.exports = [{
    packagePath: "architect-mongodb-native",
    url : 'mongodb://127.0.0.1:27017/test'
}, './routes'];

Consume mongo service in your application :

{
  "name": "routes",
  "version": "0.0.1",
  "main": "index.js",
  "private": true,

  "plugin": {
    "consumes": ["mongo"]
  }
}

Eventually use the mongo service in your app :

module.exports = function setup(options, imports, register) {
    var db = imports.mongo.db;
    db.collection('test').update({hi: 'here'}, {$set: {hi: 'there'}}, {w:1}, function(err) {
      if (err) console.warn(err.message);
      else console.log('successfully updated');
    });
    register();
};

Options

  • url : mongoclient connect url
  • config : additionnal connection parameter
  • config.logger: ['error', 'info', 'debug'] enable mongodb driver logs with the selected level.

multiple connections

Multiple mongo connection are supported. For instance :

module.exports = [{
    packagePath: "architect-mongodb-native",
    first:{
      url : 'mongodb://127.0.0.1:27017/test'
    },
    second:{
      url : 'mongodb://127.0.0.1:27017/other'
    },
    config: {
      readPreference: 'secondaryPreferred',
      replicaSet: 'myreplset'
    }
}, './routes'];