Mantra how-to guide

Build a new Mantra project from scratch

Build a new Mantra project is easy and requires minimal stuff to start with

04-Mar-2022
A  A  A 

A Mantra project is composed by a simple folder and files structure:

  • A folder to locate the components of the project.
  • A mantraconfig.json file with project configuration.
  • ... nothing else. mantraconfig.json is the configuration file that Mantra expects to find in the project root folder, as you can read in the official documentation. Is could have lots of properties, but only a few are minimal to start with, like this example:
{
  "CurrentVersion": "1",
  "ComponentsLocations": [ "components" ],
  "Apps": {
    "myapp": { }
  },
  "ComponentsConfig": {
    "core": {
      "croncleanupevent": "* */5 * * * *",
      "cronbackupevent": "0 */5 * * * *",
    }
  },
  "DefaultComponents": ["tick"],
  "Entities": {
    "default": {
      "provider": "sqlite",
      "databasepath": "./tickdb.db"
    }
  },
}

With this simple configuration file, you can start a project, given a component named as "tick". To run the project, simple write:

$ mantrad startapp

The component should be located at /components folder, according to "ComponentsLocations" property of the configuration. The minimal code for a component in Mantra involves two files:

  • tick.js module as the entry point of the component (to define hooks, if needed, between other things).
  • mantra.json component configuration file For tick.js file, this is a minimal code:
"use strict";

module.exports = () => {
    return {
        Start: {
             onStart: async (Mantra) => console.log("Tick component started!");
        }
    };
}

And the content of mantra.json for this simple component, this is the minimal json file:

{
    "name": "tick",
    "version": "1.0.0"
}

And that's all! So, finally, the project structure should be like this:

`-- components
    `-- tick
        |-- tick.js
        |-- mantra.json
|-- mantraconfig.json

From this minimal structure, you can start to add functionality, frontends, more components, etc. to your project.