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

Builder class for creating and managing database table schemas. More...

#include <entity_schema.h>

Public Member Functions

 EntitySchema ()=default
 Default constructor (creates empty schema).
 
 EntitySchema (const std::string &entity_name, const std::string &entity_type="base")
 Construct schema with name and type.
 
 EntitySchema (const EntitySchema &)
 
EntitySchemaoperator= (const EntitySchema &)
 
 EntitySchema (EntitySchema &&) noexcept=default
 
EntitySchemaoperator= (EntitySchema &&) noexcept=default
 
 ~EntitySchema ()
 
bool operator== (const EntitySchema &other) const
 Compare two schemas for equality.
 
Entity toEntity () const
 Convert schema to Entity for database operations.
 
std::string id () const
 Get schema unique identifier.
 
std::string name () const
 Get table name.
 
EntitySchemasetName (const std::string &name)
 Set table name (fluent interface).
 
std::string type () const
 Get entity type.
 
EntitySchemasetType (const std::string &type)
 Set entity type (fluent interface).
 
bool hasApi () const
 Check if API endpoints are enabled.
 
EntitySchemasetHasApi (const bool &hasApi)
 Enable/disable API endpoints (fluent interface).
 
bool isSystem () const
 Check if this is a system table.
 
EntitySchemasetSystem (const bool &isSystem)
 Set system table flag (fluent interface).
 
AccessRule listRule () const
 
EntitySchemasetListRule (const AccessRule &listRule)
 
AccessRule getRule () const
 
EntitySchemasetGetRule (const AccessRule &getRule)
 
AccessRule addRule () const
 
EntitySchemasetAddRule (const AccessRule &addRule)
 
AccessRule updateRule () const
 
EntitySchemasetUpdateRule (const AccessRule &updateRule)
 
AccessRule deleteRule () const
 
EntitySchemasetDeleteRule (const AccessRule &deleteRule)
 
std::vector< EntitySchemaFieldfields () const
 Get all field definitions.
 
EntitySchemaaddField (const EntitySchemaField &field)
 Add a field to the schema (fluent interface).
 
bool removeField (const std::string &field_name)
 Remove a field by name.
 
EntitySchemaFieldfield (const std::string &field_name)
 Get field by name (mutable reference).
 
EntitySchemaFieldfieldById (const std::string &field_id)
 Get field by ID (mutable reference).
 
bool hasField (const std::string &field_name) const
 Check if field exists by name.
 
bool hasFieldById (const std::string &field_id) const
 Check if field exists by ID.
 
std::string viewQuery () const
 Get SQL query for view type entities.
 
EntitySchemasetViewQuery (const std::string &viewQuery)
 Set SQL query for view type (fluent interface).
 
void updateWith (const nlohmann::json &new_data)
 Update schema with new JSON data (merges fields).
 
json toJSON () const
 Convert schema to JSON representation.
 
std::string toDDL () const
 Generate SQL DDL (CREATE TABLE) statement.
 
std::string dump () const
 Dump schema as formatted string (for debugging).
 
std::optional< std::string > validate () const
 Validate this schema instance.
 
HandlerFn getOneRouteHandler () const
 
HandlerFn getManyRouteHandler () const
 
HandlerFn postRouteHandler () const
 
HandlerFn patchRouteHandler () const
 
HandlerFn deleteRouteHandler () const
 
void createEntityRoutes () const
 

Static Public Member Functions

static EntitySchema fromSchema (const json &entity_schema)
 Create schema from JSON object.
 
static EntitySchema fromEntity (const Entity &entity)
 Create schema from existing Entity.
 
static std::string toDefaultSqlValue (const std::string &type, const nlohmann::json &v)
 Get default SQL value for a field type.
 
static nlohmann::json listTables (const nlohmann::json &opts=nlohmann::json::object())
 List all tables from database.
 
static nlohmann::json getTable (const std::string &table_id)
 Get table schema by ID from database.
 
static nlohmann::json createTable (const EntitySchema &new_table)
 Create table in database from schema.
 
static nlohmann::json updateTable (const std::string &table_id, const nlohmann::json &new_schema)
 Update existing table schema in database.
 
static void dropTable (const EntitySchema &original_table)
 Drop table from database.
 
static void dropTable (const std::string &table_id)
 Drop table by ID from database.
 
static bool tableExists (const std::string &table_name)
 Check if table exists by name.
 
static bool tableExists (const EntitySchema &table)
 Check if table exists (by schema name).
 
static std::string genEntityId (const std::string &entity_name)
 Generate unique entity ID from name.
 
static std::optional< std::string > validate (const EntitySchema &table_schema)
 Validate schema (static method).
 
static bool isValidEntityName (const std::string &name)
 Validate this schema name to alphanum and _ only.
 
static bool isValidEntityType (const std::string &type)
 Validate this schema type to the 3 supported types.
 
static const std::vector< EntitySchemaField > & defaultBaseFieldsSchema ()
 
static const std::vector< EntitySchemaField > & defaultAuthFieldsSchema ()
 

Detailed Description

Builder class for creating and managing database table schemas.

EntitySchema provides a fluent interface for defining table structures, fields, access rules, and converting to/from JSON and DDL.

// Create a new schema
EntitySchema schema("users", "base");
schema.addField(EntitySchemaField("name", "string").setRequired(true));
schema.addField(EntitySchemaField("email", "string").setIsUnique(true));
schema.setListRule(AccessRule("custom", "auth.id != \"\""));
// Convert to Entity for database operations
Entity entity = schema.toEntity();
Access control rule for entity permissions.
Definition access_rules.h:31
Represents a single field in a database table schema.
Definition entity_schema_field.h:36
Builder class for creating and managing database table schemas.
Definition entity_schema.h:38
Represents a database table/entity with schema and CRUD operations.
Definition entity.h:39

Constructor & Destructor Documentation

◆ EntitySchema() [1/4]

mb::EntitySchema::EntitySchema ( )
default

Default constructor (creates empty schema).

◆ EntitySchema() [2/4]

mb::EntitySchema::EntitySchema ( const std::string &  entity_name,
const std::string &  entity_type = "base" 
)
explicit

Construct schema with name and type.

Parameters
entity_nameTable name
entity_typeEntity type ("base", "auth", or "view"), defaults to "base"

◆ EntitySchema() [3/4]

mb::EntitySchema::EntitySchema ( const EntitySchema )
default

◆ EntitySchema() [4/4]

mb::EntitySchema::EntitySchema ( EntitySchema &&  )
defaultnoexcept

◆ ~EntitySchema()

mb::EntitySchema::~EntitySchema ( )
default

Member Function Documentation

◆ addField()

EntitySchema & mb::EntitySchema::addField ( const EntitySchemaField field)

Add a field to the schema (fluent interface).

Parameters
fieldField to add
Returns
Reference to self for chaining

◆ addRule()

AccessRule mb::EntitySchema::addRule ( ) const

◆ createEntityRoutes()

void mb::EntitySchema::createEntityRoutes ( ) const

◆ createTable()

nlohmann::json mb::EntitySchema::createTable ( const EntitySchema new_table)
static

Create table in database from schema.

Parameters
new_tableSchema to create
Returns
JSON object with created table data

◆ defaultAuthFieldsSchema()

const std::vector< EntitySchemaField > & mb::EntitySchema::defaultAuthFieldsSchema ( )
static

◆ defaultBaseFieldsSchema()

const std::vector< EntitySchemaField > & mb::EntitySchema::defaultBaseFieldsSchema ( )
static

◆ deleteRouteHandler()

HandlerFn mb::EntitySchema::deleteRouteHandler ( ) const

◆ deleteRule()

AccessRule mb::EntitySchema::deleteRule ( ) const

◆ dropTable() [1/2]

void mb::EntitySchema::dropTable ( const EntitySchema original_table)
static

Drop table from database.

Parameters
original_tableSchema of table to drop

◆ dropTable() [2/2]

void mb::EntitySchema::dropTable ( const std::string &  table_id)
static

Drop table by ID from database.

Parameters
table_idTable identifier to drop

◆ dump()

std::string mb::EntitySchema::dump ( ) const

Dump schema as formatted string (for debugging).

Returns
Formatted schema string

◆ field()

EntitySchemaField & mb::EntitySchema::field ( const std::string &  field_name)

Get field by name (mutable reference).

Parameters
field_nameField name to lookup
Returns
Reference to EntitySchemaField
Exceptions
std::runtime_errorif field not found

◆ fieldById()

EntitySchemaField & mb::EntitySchema::fieldById ( const std::string &  field_id)

Get field by ID (mutable reference).

Parameters
field_idField ID to lookup
Returns
Reference to EntitySchemaField
Exceptions
std::runtime_errorif field not found

◆ fields()

std::vector< EntitySchemaField > mb::EntitySchema::fields ( ) const

Get all field definitions.

Returns
Vector of EntitySchemaField objects

◆ fromEntity()

EntitySchema mb::EntitySchema::fromEntity ( const Entity entity)
static

Create schema from existing Entity.

Parameters
entityEntity to convert from
Returns
EntitySchema instance

◆ fromSchema()

EntitySchema mb::EntitySchema::fromSchema ( const json entity_schema)
static

Create schema from JSON object.

Parameters
entity_schemaJSON schema object
Returns
EntitySchema instance

◆ genEntityId()

std::string mb::EntitySchema::genEntityId ( const std::string &  entity_name)
static

Generate unique entity ID from name.

Parameters
entity_nameEntity name
Returns
Generated ID string

◆ getManyRouteHandler()

HandlerFn mb::EntitySchema::getManyRouteHandler ( ) const

◆ getOneRouteHandler()

HandlerFn mb::EntitySchema::getOneRouteHandler ( ) const

◆ getRule()

AccessRule mb::EntitySchema::getRule ( ) const

◆ getTable()

nlohmann::json mb::EntitySchema::getTable ( const std::string &  table_id)
static

Get table schema by ID from database.

Parameters
table_idTable identifier
Returns
JSON schema object

◆ hasApi()

bool mb::EntitySchema::hasApi ( ) const

Check if API endpoints are enabled.

Returns
true if API enabled

◆ hasField()

bool mb::EntitySchema::hasField ( const std::string &  field_name) const

Check if field exists by name.

Parameters
field_nameField name to check
Returns
true if field exists

◆ hasFieldById()

bool mb::EntitySchema::hasFieldById ( const std::string &  field_id) const

Check if field exists by ID.

Parameters
field_idField ID to check
Returns
true if field exists

◆ id()

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

Get schema unique identifier.

Returns
Schema ID string

◆ isSystem()

bool mb::EntitySchema::isSystem ( ) const

Check if this is a system table.

Returns
true if system table

◆ isValidEntityName()

bool mb::EntitySchema::isValidEntityName ( const std::string &  name)
static

Validate this schema name to alphanum and _ only.

Returns
Boolean true if all checks out

◆ isValidEntityType()

bool mb::EntitySchema::isValidEntityType ( const std::string &  type)
static

Validate this schema type to the 3 supported types.

Returns
Boolean true if all checks out

◆ listRule()

AccessRule mb::EntitySchema::listRule ( ) const

◆ listTables()

nlohmann::json mb::EntitySchema::listTables ( const nlohmann::json &  opts = nlohmann::json::object())
static

List all tables from database.

Parameters
optsOptional parameters (pagination, etc.)
Returns
JSON array of table schemas

◆ name()

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

Get table name.

Returns
Table name

◆ operator=() [1/2]

EntitySchema & mb::EntitySchema::operator= ( const EntitySchema other)

◆ operator=() [2/2]

EntitySchema & mb::EntitySchema::operator= ( EntitySchema &&  )
defaultnoexcept

◆ operator==()

bool mb::EntitySchema::operator== ( const EntitySchema other) const

Compare two schemas for equality.

Parameters
otherSchema to compare with
Returns
true if schemas are equal

◆ patchRouteHandler()

HandlerFn mb::EntitySchema::patchRouteHandler ( ) const

◆ postRouteHandler()

HandlerFn mb::EntitySchema::postRouteHandler ( ) const

◆ removeField()

bool mb::EntitySchema::removeField ( const std::string &  field_name)

Remove a field by name.

Parameters
field_nameField name to remove
Returns
true if field was removed, false if not found

◆ setAddRule()

EntitySchema & mb::EntitySchema::setAddRule ( const AccessRule addRule)

◆ setDeleteRule()

EntitySchema & mb::EntitySchema::setDeleteRule ( const AccessRule deleteRule)

◆ setGetRule()

EntitySchema & mb::EntitySchema::setGetRule ( const AccessRule getRule)

◆ setHasApi()

EntitySchema & mb::EntitySchema::setHasApi ( const bool &  hasApi)

Enable/disable API endpoints (fluent interface).

Parameters
hasApiAPI enabled flag
Returns
Reference to self for chaining

◆ setListRule()

EntitySchema & mb::EntitySchema::setListRule ( const AccessRule listRule)

◆ setName()

EntitySchema & mb::EntitySchema::setName ( const std::string &  name)

Set table name (fluent interface).

Parameters
nameNew table name
Returns
Reference to self for chaining

◆ setSystem()

EntitySchema & mb::EntitySchema::setSystem ( const bool &  isSystem)

Set system table flag (fluent interface).

Parameters
isSystemSystem table flag
Returns
Reference to self for chaining

◆ setType()

EntitySchema & mb::EntitySchema::setType ( const std::string &  type)

Set entity type (fluent interface).

Parameters
typeEntity type
Returns
Reference to self for chaining

◆ setUpdateRule()

EntitySchema & mb::EntitySchema::setUpdateRule ( const AccessRule updateRule)

◆ setViewQuery()

EntitySchema & mb::EntitySchema::setViewQuery ( const std::string &  viewQuery)

Set SQL query for view type (fluent interface).

Parameters
viewQuerySQL SELECT query string
Returns
Reference to self for chaining

◆ tableExists() [1/2]

bool mb::EntitySchema::tableExists ( const EntitySchema table)
static

Check if table exists (by schema name).

Parameters
tableSchema to check
Returns
true if table exists

◆ tableExists() [2/2]

bool mb::EntitySchema::tableExists ( const std::string &  table_name)
static

Check if table exists by name.

Parameters
table_nameTable name to check
Returns
true if table exists

◆ toDDL()

std::string mb::EntitySchema::toDDL ( ) const

Generate SQL DDL (CREATE TABLE) statement.

Returns
SQL DDL string

◆ toDefaultSqlValue()

std::string mb::EntitySchema::toDefaultSqlValue ( const std::string &  type,
const nlohmann::json &  v 
)
static

Get default SQL value for a field type.

Parameters
typeField type string
vJSON value (optional)
Returns
Default SQL value string

◆ toEntity()

Entity mb::EntitySchema::toEntity ( ) const

Convert schema to Entity for database operations.

Returns
Entity instance

◆ toJSON()

nlohmann::json mb::EntitySchema::toJSON ( ) const

Convert schema to JSON representation.

Returns
JSON object with schema data

◆ type()

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

Get entity type.

Returns
Entity type ("base", "auth", or "view")

◆ updateRule()

AccessRule mb::EntitySchema::updateRule ( ) const

◆ updateTable()

nlohmann::json mb::EntitySchema::updateTable ( const std::string &  table_id,
const nlohmann::json &  new_schema 
)
static

Update existing table schema in database.

Parameters
table_idTable identifier
new_schemaUpdated schema JSON
Returns
JSON object with updated table data

◆ updateWith()

void mb::EntitySchema::updateWith ( const nlohmann::json &  new_data)

Update schema with new JSON data (merges fields).

Parameters
new_dataJSON object with updates

◆ validate() [1/2]

std::optional< std::string > mb::EntitySchema::validate ( ) const

Validate this schema instance.

Returns
Optional error message if validation fails

◆ validate() [2/2]

std::optional< std::string > mb::EntitySchema::validate ( const EntitySchema table_schema)
static

Validate schema (static method).

Parameters
table_schemaSchema to validate
Returns
Optional error message if validation fails

◆ viewQuery()

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

Get SQL query for view type entities.

Returns
View SQL query string

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