Mantra how-to guide

Basic CRUD operations with Mantra data object mapper

To write simple CRUD operations is extraordinary easy with Mantra data models

29-Mar-2022
A  A  A 

Mantra uses a sub project of Mantra team called RedEntities: this is a simple but efficient data object mapper by which you can access to your data entities in your components with simple sentences and chained shorcuts.

Mantra framework has this data solution integrated in its core, but, despite of this, you can use any other data provider by your own.

At the time of writing this, MySql, MariaDB, SQLite, PostgreSQL and Aurora data providers are supported (we are working in adding new ones in the coming months, like Sql Server and Dynamo DB).

Remember that the main purpose of Mantra is to write components as fast as posible, and one of the principles in Mantra development paradigm, is that the components just define simple data models and that they access their data transparently, and they don't worry about how data is persisted and retrieved.

This is a simple list of basic CRUD operations with Mantra, given this sample schema for a component named "users":

const sampleSchema = {
    entities: [
        {
            name : "users",
            fields: [
                { name : "mail", type : "string" },
                { name : "password", type : "string" },
                { name : "created", type : "datetime" }
            ],
            indexes: [ ["mail"], ["created"] ],
            restrictions: {
                unique: [ ["mail"] ]
            }
        }
    ]
}

Select queries ( S() selector )

Select all fields of a single entity given its ID:

const userEntity = await Mantra.model.users.S().SingleById( userId );

Select field name of a single entity given its ID:

const userEntity = await Mantra.model.users.s("mail").SingleById( userId );

Get the number of users entities:

const usersCount = await Mantra.model.users.S().C();

Or...

const usersCount = await Mantra.model.users.S().Count();

Get an user given its mail:

const userEntity = await Mantra.model.users.S().W("mail=?",mail).Single();

Check if an user given its mail exists:

const userExists = await Mantra.model.users.S().W("mail=?",mail).Exists();

Iterate over all user entities:

await Mantra.model.users.S().IA( await (userEntity) => {
    // Do something with userEntity
});

Get last user created according to created field:

const lastUserCreated = await Mantra.model.users.S().OB('created').T(1).R();

Get older user created according to created field:

const olderUserCreated = await Mantra.model.users.S().OB('created',false).T(1).R();

Get users entities paginated from position 10 and get 20:

const userEntities = await Mantra.model.users.S().L(10,20).R();

Insert queries ( I() selector )

Insert a new user entity:

const newUserId = await Mantra.model.users.I().V( { mail: 'mantrarocks@mantrajs.com', password: 'fooIlovePizza' } ).R();

Insert a number of users entities:

const newUsersId = await Mantra.model.users.I().V( 
    [{ mail: 'mantrarocks@mantrajs.com', password: 'fooIlovePizza' },
     { mail: 'mantrarocks2@mantrajs.com', password: 'btcrocks' }] ).R();

Update queries ( S() selector )

Update mail field of an user given its ID:

await Mantra.model.users.U().W('ID=?',userId).V( { mail: 'newmail@joo.com' }).R();

Delete queries ( D() selector )

Delete an user entity given its id:

await Mantra.model.users.D().DeleteById( userId );

Delete an user given its mail:

await Mantra.model.users.D().W("mail=?",userMailToDelete).R();