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 Type | Description |
---|---|
Standalone | Creates a connection to a database in mongo. |
Multitenant | Creates 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.
"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:
//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.
- Get the Registry DB Info => req.soajs.registry.tenantMetaDB
- Get the Tenant Code => req.soajs.tenant.code
- Build the DB Configuration using => req.soajs.meta.tenantDB
- 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:
//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:
Add Comment