Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Objective


SOAJS provides a MongoDB connector to communicate with the database. The Connector is capable of connecting to Static and Multitenant Databases and offers a simple interface to execute DB operations.

Connection TypeDescription
StandaloneCreates a connection to a database in mongo.
MultitenantCreates a connection to a database in mongo based on the tenant code retrieved from the req.soajs.tenant.code. More information on request at this page.


Connecting to a Database


The database configuration is located in the registry and can be retrieved via req.soajs.registry.

This information is then passed on to the Connector so that the later can communicate with Mongo DB.

The following snippet shows the location of both static and multitenant database configurations inside the registry.

Registry section for Database Configuration
"coreDB": {
    "staticdb": {
      "prefix": "",
      "credentials": { /*...*/ },
      "servers": [ /*...*/ ],
      "URLParam": { /*...*/ },
      "extraParam": { /*...*/ },
      "name": "staticdb"
    }
},
"tenantMetaDB": {
    "multitenantdb": {
      "prefix": "",
      "credentials": { /*...*/ },
      "servers": [ /*...*/ ],
      "URLParam": { /*...*/ },
      "extraParam": { /*...*/ },
      "name": "#TENANT_NAME#_multitenantdb"
    }
}


Standalone Databases


The below examples shows how to connect to staticdb who is a static database from the list above, and retrieve some records. The configuration of this database is located under coreDB object:

Connecting to a Standalone Database
//require soajs and initialize a service
var soajs = require('soajs');
var soajsModules = require("soajs.core.module");

var service = new soajs.server.service({...});
service.init(function(){

    //add an api
    service.get("/someAPIroute", function(req, res){

        //fetch the db configuraiton
        var dbConfig = req.soajs.registry.coreDB.staticdb;

        //create a new connection
        var mongo_connector = new soajsModules.mongo(dbConfig);

        //get records from collection
        mongo_connector.find('mycollection', {}, function(error, records){ ..... });

    });

    service.start();
});


Multitenant Databases


Multitenant databases names are dependent on the tenant code: %TENANT_CODE% + dbName.
Therefore additional steps are required to establish a connection to these databases.

  1. Get the Registry DB Info => req.soajs.registry.tenantMetaDB
  2. Get the Tenant Code => req.soajs.tenant.code
  3. Build the DB Configuration using => req.soajs.meta.tenantDB
  4. Connect to Mongo by providing the DB Configuration

The below examples shows how to connect to a multitenant database multitenantdb from the list above, and retrieve some records:

Connecting to a Multitenant Database
//require soajs and initialize a service
var soajs = require('soajs');
var soajsModules = require("soajs.core.module");

var service = new soajs.server.service({...});
service.init(function(){

    //add an api
    service.get("/someAPIroute", function(req, res){

        //fetch the registry info
        var dbRegistry = req.soajs.registry.tenantMetaDB;

        //fetch the tenant code
        var tenantCode = req.soajs.tenant.code;

        //build the db configuration for multitenant databases
        var dbConfig = req.soajs.meta.tenantDB(dbRegistry, "multitenantdb", tenantCode);

        //create a new connection
        var mongo_connector = new soajsModules.mongo(dbConfig);

        //get records from collection
        mongo_connector.find('mycollection', {}, function(error, records){ ..... });

    });

    service.start();
});


Database Operations


SOAJS Mongo Driver allows you to perform database operations to store, retrieve and remove information from the database.
The below table list the available operations and their usage:

  • No labels