Mantra how-to guide

Iterate over all entities of a model

With simple Mantra object mapper you can code tasks like iterating over all entities of a component model is done in one line of code

22-Mar-2022
A  A  A 

Consider this simple data model defined by a component:

{
    "entities" : [
        {
            "name" : "articles",
            "fields": [
                { "name" : "userid", "type" : "string" },
                { "name" : "title", "type": "string" },
                { "name" : "subtitle", "type" : "string" },
                { "name" : "type", "type": "string" },
                { "name" : "content", "type": "longtext" },
                { "name" : "tags", "type": "string" },
                { "name" : "published", "type": "boolean" },
                { "name" : "created", "type" : "datetime" }
            ],
            "indexes": [ ["userid"], ["created"], ["type"], ["published","created"] ]
        }
    ]
}

With Mantra object mapper (RedEntities subproject), to iterate over all entities of "articles", is a simple task that can be done with IA() shorcut (equal to IterateAll() ):

await Mantra.dal.articles.IA( Mantra, async (entity) => {
   // entity is row in "articles" table
});

IA or IterateAll method, expect as second parameter an async function which will be called once by each entity in the model.

Mantra data model, based on RedEntities, has been designed so that basic but powefull CRUD operations can be written easily and fast.