Mantra how-to guide

Set a specific data provider for a component

With Mantra, you can set differents data providers for the components within the same project

15-Mar-2022
A  A  A 

This is one of the main features of Mantra: components doesn't know how data is persisted (they just use RedEntities object mapper), and within the same project, you set by configuration a number of different providers or differents instances for each component or groups of them.

Why?

In software develpment, we need to follow this principle: data is persisted according to how data is consumed and components doesn't need to know about how their data models are persisted.

This is, if the data entities of a component need to be written as fast as posible, or if they need to be read extremely fast, or it is not important the latency when writting new data to database, etc., then those cases determine the data providers you need.

In the time of write this, this data providers are supported by Mantra: MySql, MariaDB, Amazon Aurora, PostgreSql, Sqlite. In the coming months, Sql Server, Oracle, Redis and MongoDB will be supported as well.

You can any number of data providers instances with the same project, and this feature of Mantra gives you a great scalability for your project.

Let's see it with an example.

Given a component named as "eventlogger", we want to use a specific instance of PostgreSQL of it.

We can configure this easily with the property "Entities" of the configuration file mantraconfig.json, just with:

"Entities": {
   "default": {
      "provider": "mysql",
      "host": "localhost",
      "database": "yourdatabase",
      "user": "mysqluseruser",
      "password": "password"
   },
   "logger": {
      "provider": "postgresql",
      "host": "localhost",
      "database": "eventloggerdatabase",
      "user": "postgreuser",
      "password": "password",
      "components": ["eventlogger"]
   }
}

With this configuration, all data for all components apart from "eventlogger", will be persisted in the "default" configuration (an instance of MySql), and the data model of "eventlogger" will be persisted with "logger" configuration (an instance of PostgreSQL).

You don't need to do anything else.

Usually, in the development environment, you use a different "Entities" configuration than in production environment. Indeed, during development, you can use Sqlite as data provider for the project with no need to install Sqlite in your development machine, because Mantra install an in-built version of Sqlite3 to reduce external dependencies.