MantisBase v0.3.4
Loading...
Searching...
No Matches
mb::Entity Class Reference

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

#include <entity.h>

Public Member Functions

 Entity (const nlohmann::json &schema)
 Construct entity from schema JSON object.
 
 Entity (const std::string &name, const std::string &type)
 Construct entity with name and type.
 
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.
 
std::string viewQuery () const
 Get SQL query for view type entities.
 
const std::vector< json > & fields () const
 Get all field definitions.
 
std::optional< jsonfield (const std::string &field_name) const
 Get field definition by name.
 
std::optional< jsonhasField (const std::string &field_name) const
 Check if field exists by name.
 
const jsonrules () const
 Get all access rules.
 
AccessRule listRule () const
 Get list access rule (for GET /api/v1/entities/{table}).
 
AccessRule getRule () const
 Get read access rule (for GET /api/v1/entities/{table}/:id).
 
AccessRule addRule () const
 Get create access rule (for POST /api/v1/entities/{table}).
 
AccessRule updateRule () const
 Get update access rule (for PATCH /api/v1/entities/{table}/:id).
 
AccessRule 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.
 
Records list (const json &opts=json::object()) const
 List all records in the entity table.
 
std::optional< Recordread (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 jsonschema () const
 Get complete entity schema JSON.
 
int countRecords () const
 Count total records in the entity table.
 
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< jsonfindField (const std::string &field_name) const
 Find field definition by name (alias for field()).
 
std::optional< jsonqueryFromCols (const std::string &value, const std::vector< std::string > &columns) const
 Query records by searching across multiple columns.
 
HandlerFn getOneRouteHandler () const
 Get route handler for GET /api/v1/entities/{table}/:id.
 
HandlerFn getManyRouteHandler () const
 Get route handler for GET /api/v1/entities/{table}.
 
HandlerWithContentReaderFn postRouteHandler () const
 Get route handler for POST /api/v1/entities/{table}.
 
HandlerWithContentReaderFn patchRouteHandler () const
 Get route handler for PATCH /api/v1/entities/{table}/:id.
 
HandlerFn deleteRouteHandler () const
 Get route handler for DELETE /api/v1/entities/{table}/:id.
 
void createEntityRoutes () const
 Register all CRUD routes for this entity with the router.
 

Detailed Description

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

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

// Create entity from schema JSON
json schema = {{"name", "users"}, {"type", "base"}, {"fields", ...}};
Entity entity(schema);
// Create entity with name and type
Entity entity("posts", "base");
const json & schema() const
Get complete entity schema JSON.
Definition entity.cpp:472
Entity(const nlohmann::json &schema)
Construct entity from schema JSON object.
Definition entity.cpp:8
nlohmann::json json
Shorten JSON namespace.
Definition context_store.h:18

Constructor & Destructor Documentation

◆ Entity() [1/2]

mb::Entity::Entity ( const nlohmann::json &  schema)
explicit

Construct entity from schema JSON object.

Parameters
schemaJSON object containing table schema (name, type, fields, rules, etc.)

◆ Entity() [2/2]

mb::Entity::Entity ( const std::string &  name,
const std::string &  type 
)
explicit

Construct entity with name and type.

Parameters
nameTable name
typeEntity type ("base", "auth", or "view")

Member Function Documentation

◆ addRule()

AccessRule mb::Entity::addRule ( ) const

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

Returns
AccessRule for creating records

◆ countRecords()

int mb::Entity::countRecords ( ) const

Count total records in the entity table.

Returns
Number of records

◆ create()

Record mb::Entity::create ( const json record,
const json opts = json::object() 
) const

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
json newUser = {{"name", "John"}, {"email", "john@example.com"}};
Record created = entity.create(newUser);
nlohmann::json Record
Single database record as JSON object.
Definition entity.h:20

◆ createEntityRoutes()

void mb::Entity::createEntityRoutes ( ) const

Register all CRUD routes for this entity with the router.

◆ deleteRouteHandler()

HandlerFn mb::Entity::deleteRouteHandler ( ) const

Get route handler for DELETE /api/v1/entities/{table}/:id.

Returns
Handler function for record deletion

◆ deleteRule()

AccessRule mb::Entity::deleteRule ( ) const

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

Returns
AccessRule for deleting records

◆ field()

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

Get field definition by name.

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

◆ fields()

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

Get all field definitions.

Returns
Reference to vector of field JSON objects

◆ findField()

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

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

Parameters
field_nameField name to find
Returns
Optional field JSON if found

◆ getManyRouteHandler()

HandlerFn mb::Entity::getManyRouteHandler ( ) const

Get route handler for GET /api/v1/entities/{table}.

Returns
Handler function for listing records

◆ getOneRouteHandler()

HandlerFn mb::Entity::getOneRouteHandler ( ) const

Get route handler for GET /api/v1/entities/{table}/:id.

Returns
Handler function for single record retrieval

◆ getRule()

AccessRule mb::Entity::getRule ( ) const

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

Returns
AccessRule for reading a record

◆ hasApi()

bool mb::Entity::hasApi ( ) const

Check if entity has API endpoints enabled.

Returns
true if API enabled, false otherwise

◆ hasField()

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

Check if field exists by name.

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

◆ id()

std::string mb::Entity::id ( ) const

Get entity unique identifier.

Returns
Entity ID string

◆ isEmpty()

bool mb::Entity::isEmpty ( ) const

Check if entity table is empty.

Returns
true if no records exist, false otherwise

◆ isSystem()

bool mb::Entity::isSystem ( ) const

Check if entity is a system table.

Returns
true if system table, false otherwise

◆ list()

Records mb::Entity::list ( const json opts = json::object()) const

List all records in the entity table.

Parameters
optsOptional parameters (currently unused)
Returns
Vector of record JSON objects

◆ listRule()

AccessRule mb::Entity::listRule ( ) const

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

Returns
AccessRule for listing records

◆ name()

std::string mb::Entity::name ( ) const

Get entity table name.

Returns
Table name

◆ patchRouteHandler()

HandlerWithContentReaderFn mb::Entity::patchRouteHandler ( ) const

Get route handler for PATCH /api/v1/entities/{table}/:id.

Returns
Handler function with content reader for record updates

◆ postRouteHandler()

HandlerWithContentReaderFn mb::Entity::postRouteHandler ( ) const

Get route handler for POST /api/v1/entities/{table}.

Returns
Handler function with content reader for record creation

◆ queryFromCols()

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

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::Entity::read ( const std::string &  id,
const json opts = json::object() 
) const

Read a single record by ID.

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

◆ recordExists()

bool mb::Entity::recordExists ( const std::string &  id) const

Check if a record exists by ID.

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

◆ remove()

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

Delete a record by ID.

Parameters
idRecord identifier to delete

◆ rules()

const json & mb::Entity::rules ( ) const

Get all access rules.

Returns
Reference to rules JSON object

◆ schema()

const json & mb::Entity::schema ( ) const

Get complete entity schema JSON.

Returns
Reference to schema JSON object

◆ type()

std::string mb::Entity::type ( ) const

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

Returns
Entity type string

◆ update()

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

Update an existing record by ID.

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

◆ updateRule()

AccessRule mb::Entity::updateRule ( ) const

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

Returns
AccessRule for updating records

◆ viewQuery()

std::string mb::Entity::viewQuery ( ) const

Get SQL query for view type entities.

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

The documentation for this class was generated from the following files: