MantisBase v0.4.9
Loading...
Searching...
No Matches
mb::MbEntity Class Reference

Represents a database table/entity with schema and CRUD operations. More...

#include <entity.h>

Inheritance diagram for mb::MbEntity:
Collaboration diagram for mb::MbEntity:

Public Member Functions

 MbEntity (const MantisBase &app, const nlohmann::json &schema)
 Construct entity from schema JSON object, bound to its app.
 MbEntity (const MantisBase &app, const std::string &name, const std::string &type)
 Construct entity with name and type, bound to its app.
 MbEntity (const MbEntity &)=default
 MbEntity (MbEntity &&)=default
std::string id () const
 Get entity unique identifier.
std::string name () const
 Get entity table name.
std::string type () const
 Get entity type ("base", "auth", or "view").
bool isSystem () const
 Check if entity is a system table.
bool hasApi () const
 Check if entity has API endpoints enabled.
MbAuthMethods authMethods () const
 Login methods for an auth entity.
std::string authMethodDisabledReason (std::string_view method) const
 Empty when method is enabled, otherwise the client-facing reason.
std::string viewQuery () const
 Get SQL query for view type entities.
const std::vector< json > & fields () const
 Get all field definitions.
std::optional< json > field (const std::string &field_name) const
 Get field definition by name.
std::optional< json > hasField (const std::string &field_name) const
 Check if field exists by name.
const json & rules () const
 Get all access rules.
MbAccessRule listRule () const
 Get list access rule (for GET /api/v1/entities/{table}).
MbAccessRule getRule () const
 Get read access rule (for GET /api/v1/entities/{table}/:id).
MbAccessRule addRule () const
 Get create access rule (for POST /api/v1/entities/{table}).
MbAccessRule updateRule () const
 Get update access rule (for PATCH /api/v1/entities/{table}/:id).
MbAccessRule deleteRule () const
 Get delete access rule (for DELETE /api/v1/entities/{table}/:id).
Record create (const json &record, const json &opts=json::object()) const
 Create a new record in the entity table.
EntityListPage listPage (const json &opts=json::object()) const
 List entity records with cursor pagination (always ordered by id ASC).
Records list (const json &opts=json::object()) const
 List records (items only; see listPage for pagination metadata).
std::optional< Record > read (const std::string &id, const json &opts=json::object()) const
 Read a single record by ID.
Record update (const std::string &id, const json &data, const json &opts=json::object()) const
 Update an existing record by ID.
void remove (const std::string &id) const
 Delete a record by ID.
const json & schema () const
 Get complete entity schema JSON.
int countRecords (const json &filter=json::object()) const
 Count records in the entity table, optionally matching a filter.
bool isEmpty () const
 Check if entity table is empty.
bool recordExists (const std::string &id) const
 Check if a record exists by ID.
std::optional< json > findField (const std::string &field_name) const
 Find field definition by name (alias for field()).
std::optional< json > queryFromCols (const std::string &value, const std::vector< std::string > &columns) const
 Query records by searching across multiple columns.
Public Member Functions inherited from mb::MbAppMixin
 MbAppMixin (const MantisBase &app)
 Constrictor for instantiating this interface.
 MbAppMixin (MbAppMixin &&)=default
MbAppMixin & operator= (MbAppMixin &&)=delete
 MbAppMixin (const MbAppMixin &)=default
MbAppMixin & operator= (const MbAppMixin &)=delete
const MantisBase & mbApp () const
 Application that owns this service or request context.
const MbLogger & logger () const

Detailed Description

Represents a database table/entity with schema and CRUD operations.

MbEntity provides methods to interact with database tables including creating, reading, updating, and deleting records. It encapsulates schema information and access rules.

auto app = MantisBase::create();
json schema = {{"name", "users"}, {"type", "base"}, {"fields", json::array()}};
MbEntity entity(*app, schema);
// Or by name after the schema exists in the database:
MbEntity users = app->entity("users");
static std::unique_ptr< MantisBase > create(int argc, char **argv)
Create class instance given cmd args passed in.
Definition mantisbase.cpp:75
const json & schema() const
Get complete entity schema JSON.
Definition entity.cpp:176
MbEntity(const MantisBase &app, const nlohmann::json &schema)
Construct entity from schema JSON object, bound to its app.
Definition entity.cpp:8
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36

Constructor & Destructor Documentation

◆ MbEntity() [1/4]

mb::MbEntity::MbEntity ( const MantisBase & app,
const nlohmann::json & schema )

Construct entity from schema JSON object, bound to its app.

The app is injected at construction so every MbEntity is always bound to the services (database, realtime) its CRUD operations need — there is no separate bind step to forget. Copies carry the binding.

Parameters
appOwning application. Stored non-owning; must outlive the entity.
schemaJSON object containing table schema (name, type, fields, rules, etc.)

◆ MbEntity() [2/4]

mb::MbEntity::MbEntity ( const MantisBase & app,
const std::string & name,
const std::string & type )

Construct entity with name and type, bound to its app.

Parameters
appOwning application (see the schema constructor).
nameTable name
typeMbEntity type ("base", "auth", or "view")

◆ MbEntity() [3/4]

mb::MbEntity::MbEntity ( const MbEntity & )
default

◆ MbEntity() [4/4]

mb::MbEntity::MbEntity ( MbEntity && )
default

Member Function Documentation

◆ addRule()

MbAccessRule mb::MbEntity::addRule ( ) const
nodiscard

Get create access rule (for POST /api/v1/entities/{table}).

Returns
MbAccessRule for creating records

◆ authMethodDisabledReason()

std::string mb::MbEntity::authMethodDisabledReason ( std::string_view method) const
nodiscard

Empty when method is enabled, otherwise the client-facing reason.

Parameters
methodpassword, otp, or oauth

◆ authMethods()

MbAuthMethods mb::MbEntity::authMethods ( ) const
nodiscard

Login methods for an auth entity.

Non-auth entities report every method as disabled. Auth entities with no stored auth object use the defaults (password on, OTP and OAuth off).

◆ countRecords()

int mb::MbEntity::countRecords ( const json & filter = json::object()) const
nodiscard

Count records in the entity table, optionally matching a filter.

Parameters
filterJSON object of {field: value} equality filters (same shape as list filter)
Returns
Number of matching records

◆ create()

Record mb::MbEntity::create ( const json & record,
const json & opts = json::object() ) const
nodiscard

Create a new record in the entity table.

Parameters
recordJSON object with field values
optsOptional parameters (currently unused)
Returns
Created record with generated ID and timestamps
auto users = app.entity("users");
json newUser = {{"name", "John"}, {"email", "john@example.com"}};
Record created = users.create(newUser);
nlohmann::json Record
Single database record as JSON object.
Definition entity.h:25

◆ deleteRule()

MbAccessRule mb::MbEntity::deleteRule ( ) const
nodiscard

Get delete access rule (for DELETE /api/v1/entities/{table}/:id).

Returns
MbAccessRule for deleting records

◆ field()

std::optional< json > mb::MbEntity::field ( const std::string & field_name) const
nodiscard

Get field definition by name.

Parameters
field_nameField name to lookup
Returns
Optional field JSON object if found

◆ fields()

const std::vector< json > & mb::MbEntity::fields ( ) const
nodiscard

Get all field definitions.

Returns
Reference to vector of field JSON objects

◆ findField()

std::optional< json > mb::MbEntity::findField ( const std::string & field_name) const
nodiscard

Find field definition by name (alias for field()).

Parameters
field_nameField name to find
Returns
Optional field JSON if found

◆ getRule()

MbAccessRule mb::MbEntity::getRule ( ) const
nodiscard

Get read access rule (for GET /api/v1/entities/{table}/:id).

Returns
MbAccessRule for reading a record

◆ hasApi()

bool mb::MbEntity::hasApi ( ) const
nodiscard

Check if entity has API endpoints enabled.

Returns
true if API enabled, false otherwise

◆ hasField()

std::optional< json > mb::MbEntity::hasField ( const std::string & field_name) const
nodiscard

Check if field exists by name.

Parameters
field_nameField name to check
Returns
Optional field JSON if exists, nullopt otherwise

◆ id()

std::string mb::MbEntity::id ( ) const
nodiscard

Get entity unique identifier.

Returns
MbEntity ID string

◆ isEmpty()

bool mb::MbEntity::isEmpty ( ) const
nodiscard

Check if entity table is empty.

Returns
true if no records exist, false otherwise

◆ isSystem()

bool mb::MbEntity::isSystem ( ) const
nodiscard

Check if entity is a system table.

Returns
true if system table, false otherwise

◆ list()

Records mb::MbEntity::list ( const json & opts = json::object()) const
nodiscard

List records (items only; see listPage for pagination metadata).

Parameters
optsOptional parameters passed to listPage
Returns
Vector of record JSON objects

◆ listPage()

EntityListPage mb::MbEntity::listPage ( const json & opts = json::object()) const
nodiscard

List entity records with cursor pagination (always ordered by id ASC).

Supported opts keys:

  • pagination.limit — page size (1–500, default 50)
  • pagination.after — cursor (id of last item from previous page)
  • filter — JSON object or JSON string of {field: value} equality filters
Parameters
optsOptional list parameters
Returns
Page of records and whether more rows exist

◆ listRule()

MbAccessRule mb::MbEntity::listRule ( ) const
nodiscard

Get list access rule (for GET /api/v1/entities/{table}).

Returns
MbAccessRule for listing records

◆ name()

std::string mb::MbEntity::name ( ) const
nodiscard

Get entity table name.

Returns
Table name

◆ queryFromCols()

std::optional< json > mb::MbEntity::queryFromCols ( const std::string & value,
const std::vector< std::string > & columns ) const
nodiscard

Query records by searching across multiple columns.

Parameters
valueValue to search for
columnsColumn names to search in
Returns
Optional record JSON if found

◆ read()

std::optional< Record > mb::MbEntity::read ( const std::string & id,
const json & opts = json::object() ) const
nodiscard

Read a single record by ID.

Parameters
idRecord identifier
optsOptional parameters (currently unused)
Returns
Optional record JSON if found, nullopt otherwise

◆ recordExists()

bool mb::MbEntity::recordExists ( const std::string & id) const
nodiscard

Check if a record exists by ID.

Parameters
idRecord identifier to check
Returns
true if record exists, false otherwise

◆ remove()

void mb::MbEntity::remove ( const std::string & id) const

Delete a record by ID.

Parameters
idRecord identifier to delete

◆ rules()

const json & mb::MbEntity::rules ( ) const
nodiscard

Get all access rules.

Returns
Reference to rules JSON object

◆ schema()

const json & mb::MbEntity::schema ( ) const
nodiscard

Get complete entity schema JSON.

Returns
Reference to schema JSON object

◆ type()

std::string mb::MbEntity::type ( ) const
nodiscard

Get entity type ("base", "auth", or "view").

Returns
MbEntity type string

◆ update()

Record mb::MbEntity::update ( const std::string & id,
const json & data,
const json & opts = json::object() ) const
nodiscard

Update an existing record by ID.

Parameters
idRecord identifier
dataJSON object with fields to update
optsOptional parameters (currently unused)
Returns
Updated record JSON

◆ updateRule()

MbAccessRule mb::MbEntity::updateRule ( ) const
nodiscard

Get update access rule (for PATCH /api/v1/entities/{table}/:id).

Returns
MbAccessRule for updating records

◆ viewQuery()

std::string mb::MbEntity::viewQuery ( ) const
nodiscard

Get SQL query for view type entities.

Returns
View SQL query string
Exceptions
MbExceptionif entity is not a view type

The documentation for this class was generated from the following files:
  • /home/runner/work/mantisbase/mantisbase/include/mantisbase/core/models/entity.h
  • /home/runner/work/mantisbase/mantisbase/src/core/models/entity.cpp
  • /home/runner/work/mantisbase/mantisbase/src/core/models/entity_crud.cpp