![]() |
MantisBase v0.4.9
|
Builder class for creating and managing database table schemas. More...
#include <entity_schema.h>


Public Member Functions | |
| MbEntitySchema (const MantisBase &app) | |
| Construct an empty schema bound to its application. | |
| MbEntitySchema (const MantisBase &app, const std::string &entity_name, const std::string &entity_type="base") | |
| Construct schema with name and type, bound to its application. | |
| MbEntitySchema (const MbEntitySchema &) | |
| MbEntitySchema & | operator= (const MbEntitySchema &) |
| MbEntitySchema (MbEntitySchema &&) noexcept=default | |
| MbEntitySchema & | operator= (MbEntitySchema &&) noexcept |
| ~MbEntitySchema () | |
| bool | operator== (const MbEntitySchema &other) const |
| Compare two schemas for equality. | |
| MbEntity | toEntity () const |
| Convert schema to MbEntity for database operations. | |
| std::string | id () const |
| Get schema unique identifier. | |
| std::string | name () const |
| Get table name. | |
| MbEntitySchema & | setName (const std::string &name) |
| Set table name (fluent interface). | |
| std::string | type () const |
| Get entity type. | |
| MbEntitySchema & | setType (const std::string &type) |
| Set entity type (fluent interface). | |
| bool | hasApi () const |
| Check if API endpoints are enabled. | |
| MbEntitySchema & | setHasApi (const bool &hasApi) |
| Enable/disable API endpoints (fluent interface). | |
| MbAuthMethods | authMethods () const |
| MbEntitySchema & | setAuthMethods (const MbAuthMethods &methods) |
| bool | isSystem () const |
| Check if this is a system table. | |
| MbEntitySchema & | setSystem (const bool &isSystem) |
| Set system table flag (fluent interface). | |
| MbAccessRule | listRule () const |
| MbEntitySchema & | setListRule (const MbAccessRule &listRule) |
| MbAccessRule | getRule () const |
| MbEntitySchema & | setGetRule (const MbAccessRule &getRule) |
| MbAccessRule | addRule () const |
| MbEntitySchema & | setAddRule (const MbAccessRule &addRule) |
| MbAccessRule | updateRule () const |
| MbEntitySchema & | setUpdateRule (const MbAccessRule &updateRule) |
| MbAccessRule | deleteRule () const |
| MbEntitySchema & | setDeleteRule (const MbAccessRule &deleteRule) |
| std::vector< MbEntitySchemaField > | fields () const |
| Get all field definitions. | |
| MbEntitySchema & | addField (const MbEntitySchemaField &field) |
| Add a field to the schema (fluent interface). | |
| bool | removeField (const std::string &field_name) |
| Remove a field by name. | |
| MbEntitySchemaField & | field (const std::string &field_name) |
| Get field by name (mutable reference). | |
| MbEntitySchemaField & | fieldById (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 |
| MbEntitySchema & | setViewQuery (const std::string &viewQuery) |
| const std::vector< IndexDefinition > & | indexes () const |
| MbEntitySchema & | addIndex (const IndexDefinition &index) |
| bool | removeIndex (const std::string &index_name) |
| 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::vector< std::string > | indexDDL () const |
| std::string | dump () const |
| Dump schema as formatted string (for debugging). | |
| std::optional< std::string > | validate () const |
| Validate this schema instance. | |
| 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 |
Static Public Member Functions | |
| static MbEntitySchema | fromSchema (const MantisBase &app, const json &entity_schema) |
| Create schema from JSON object, bound to an application. | |
| static MbEntitySchema | fromEntity (const MantisBase &app, const MbEntity &entity) |
| Create schema from existing MbEntity, bound to an application. | |
| 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 MantisBase &app, const nlohmann::json &opts=nlohmann::json::object()) |
| List all tables from database. | |
| static nlohmann::json | getTable (const MantisBase &app, const std::string &table_id) |
| Get table schema by ID from database. | |
| static nlohmann::json | createTable (const MbEntitySchema &new_table) |
| Create table in database from schema. | |
| static nlohmann::json | updateTable (const MantisBase &app, const std::string &table_id, const nlohmann::json &new_schema) |
| Update existing table schema in database. | |
| static void | dropTable (const MbEntitySchema &original_table) |
| Drop table from database. | |
| static void | dropTable (const MantisBase &app, const std::string &table_id) |
| Drop table by ID from database. | |
| static bool | tableExists (const MantisBase &app, const std::string &table_name) |
| Check if table exists by name. | |
| static bool | tableExists (const MbEntitySchema &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 MbEntitySchema &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< MbEntitySchemaField > & | defaultBaseFieldsSchema () |
| static const std::vector< MbEntitySchemaField > & | defaultAuthFieldsSchema () |
Builder class for creating and managing database table schemas.
MbEntitySchema provides a fluent interface for defining table structures, fields, access rules, and converting to/from JSON and DDL.
|
explicit |
Construct an empty schema bound to its application.
Used by the fromSchema()/fromEntity() builders before fields are added. The app is injected at construction so DDL/validation can always reach the database without a separate bind step to forget.
| app | Owning application. Stored non-owning; must outlive the schema. |
| mb::MbEntitySchema::MbEntitySchema | ( | const MantisBase & | app, |
| const std::string & | entity_name, | ||
| const std::string & | entity_type = "base" ) |
Construct schema with name and type, bound to its application.
| app | Owning application (see the app-only constructor). |
| entity_name | Table name |
| entity_type | MbEntity type ("base", "auth", or "view"), defaults to "base" |
|
default |
|
defaultnoexcept |
|
default |
| MbEntitySchema & mb::MbEntitySchema::addField | ( | const MbEntitySchemaField & | field | ) |
Add a field to the schema (fluent interface).
| field | Field to add |
| MbEntitySchema & mb::MbEntitySchema::addIndex | ( | const IndexDefinition & | index | ) |
|
nodiscard |
|
nodiscard |
|
static |
Create table in database from schema.
| new_table | Schema to create |
|
static |
|
static |
|
nodiscard |
|
static |
Drop table by ID from database.
| app | A const ref to MantisBase running instance |
| table_id | Table identifier to drop |
|
static |
Drop table from database.
| original_table | Schema of table to drop |
|
nodiscard |
Dump schema as formatted string (for debugging).
| MbEntitySchemaField & mb::MbEntitySchema::field | ( | const std::string & | field_name | ) |
Get field by name (mutable reference).
| field_name | Field name to lookup |
| std::runtime_error | if field not found |
| MbEntitySchemaField & mb::MbEntitySchema::fieldById | ( | const std::string & | field_id | ) |
Get field by ID (mutable reference).
| field_id | Field ID to lookup |
| std::runtime_error | if field not found |
|
nodiscard |
Get all field definitions.
|
static |
Create schema from existing MbEntity, bound to an application.
| app | A const ref to MantisBase running instance |
| entity | MbEntity to convert from |
|
static |
Create schema from JSON object, bound to an application.
| app | Owning application (db access for DDL/validation). |
| entity_schema | JSON schema object |
|
static |
|
nodiscard |
|
static |
Get table schema by ID from database.
| app | Owning application (db access). |
| table_id | Table identifier |
|
nodiscard |
Check if API endpoints are enabled.
|
nodiscard |
Check if field exists by name.
| field_name | Field name to check |
|
nodiscard |
Check if field exists by ID.
| field_id | Field ID to check |
|
nodiscard |
Get schema unique identifier.
|
nodiscard |
|
nodiscard |
|
nodiscard |
Check if this is a system table.
|
static |
Validate this schema name to alphanum and _ only.
|
static |
Validate this schema type to the 3 supported types.
|
nodiscard |
|
static |
List all tables from database.
| app | A const ref to MantisBase running instance |
| opts | Optional parameters (pagination, etc.) |
|
nodiscard |
Get table name.
| MbEntitySchema & mb::MbEntitySchema::operator= | ( | const MbEntitySchema & | other | ) |
|
noexcept |
| bool mb::MbEntitySchema::operator== | ( | const MbEntitySchema & | other | ) | const |
Compare two schemas for equality.
| other | Schema to compare with |
| bool mb::MbEntitySchema::removeField | ( | const std::string & | field_name | ) |
Remove a field by name.
| field_name | Field name to remove |
| bool mb::MbEntitySchema::removeIndex | ( | const std::string & | index_name | ) |
| MbEntitySchema & mb::MbEntitySchema::setAddRule | ( | const MbAccessRule & | addRule | ) |
| MbEntitySchema & mb::MbEntitySchema::setAuthMethods | ( | const MbAuthMethods & | methods | ) |
Replace the stored login methods. Included in JSON only for auth entities.
| MbEntitySchema & mb::MbEntitySchema::setDeleteRule | ( | const MbAccessRule & | deleteRule | ) |
| MbEntitySchema & mb::MbEntitySchema::setGetRule | ( | const MbAccessRule & | getRule | ) |
| MbEntitySchema & mb::MbEntitySchema::setHasApi | ( | const bool & | hasApi | ) |
Enable/disable API endpoints (fluent interface).
| hasApi | API enabled flag |
| MbEntitySchema & mb::MbEntitySchema::setListRule | ( | const MbAccessRule & | listRule | ) |
| MbEntitySchema & mb::MbEntitySchema::setName | ( | const std::string & | name | ) |
Set table name (fluent interface).
| name | New table name |
| MbEntitySchema & mb::MbEntitySchema::setSystem | ( | const bool & | isSystem | ) |
Set system table flag (fluent interface).
| isSystem | System table flag |
| MbEntitySchema & mb::MbEntitySchema::setType | ( | const std::string & | type | ) |
Set entity type (fluent interface).
| type | MbEntity type |
| MbEntitySchema & mb::MbEntitySchema::setUpdateRule | ( | const MbAccessRule & | updateRule | ) |
| MbEntitySchema & mb::MbEntitySchema::setViewQuery | ( | const std::string & | viewQuery | ) |
|
static |
Check if table exists by name.
| app | A const ref to MantisBase running instance |
| table_name | Table name to check |
|
static |
Check if table exists (by schema name).
| table | Schema to check |
|
nodiscard |
Generate SQL DDL (CREATE TABLE) statement.
|
static |
Get default SQL value for a field type.
| type | Field type string |
| v | JSON value (optional) |
|
nodiscard |
|
nodiscard |
Convert schema to JSON representation.
|
nodiscard |
Get entity type.
|
nodiscard |
|
static |
Update existing table schema in database.
| app | A const ref to MantisBase running instance |
| table_id | Table identifier |
| new_schema | Updated schema JSON |
| void mb::MbEntitySchema::updateWith | ( | const nlohmann::json & | new_data | ) |
Update schema with new JSON data (merges fields).
| new_data | JSON object with updates |
|
nodiscard |
Validate this schema instance.
|
static |
Validate schema (static method).
| table_schema | Schema to validate |
|
nodiscard |