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

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

#include <entity_schema.h>

Inheritance diagram for mb::MbEntitySchema:
Collaboration diagram for mb::MbEntitySchema:

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 ()

Detailed Description

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.

auto app = MantisBase::create();
MbEntitySchema schema(*app, "users", "base");
schema.addField(MbEntitySchemaField("name", "string").setRequired(true));
schema.addField(MbEntitySchemaField("email", "string").setIsUnique(true));
schema.setListRule(MbAccessRule("auth", "", "users"));
MbEntity entity = schema.toEntity();
static std::unique_ptr< MantisBase > create(int argc, char **argv)
Create class instance given cmd args passed in.
Definition mantisbase.cpp:75
Access control rule for entity permissions.
Definition access_rules.h:45
Definition entity_schema_field.h:34
MbEntitySchema(const MantisBase &app)
Construct an empty schema bound to its application.
Definition entity_schema.cpp:9
Represents a database table/entity with schema and CRUD operations.
Definition entity.h:55

Constructor & Destructor Documentation

◆ MbEntitySchema() [1/4]

mb::MbEntitySchema::MbEntitySchema ( const MantisBase & app)
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.

Parameters
appOwning application. Stored non-owning; must outlive the schema.

◆ MbEntitySchema() [2/4]

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.

Parameters
appOwning application (see the app-only constructor).
entity_nameTable name
entity_typeMbEntity type ("base", "auth", or "view"), defaults to "base"

◆ MbEntitySchema() [3/4]

mb::MbEntitySchema::MbEntitySchema ( const MbEntitySchema & )
default

◆ MbEntitySchema() [4/4]

mb::MbEntitySchema::MbEntitySchema ( MbEntitySchema && )
defaultnoexcept

◆ ~MbEntitySchema()

mb::MbEntitySchema::~MbEntitySchema ( )
default

Member Function Documentation

◆ addField()

MbEntitySchema & mb::MbEntitySchema::addField ( const MbEntitySchemaField & field)

Add a field to the schema (fluent interface).

Parameters
fieldField to add
Returns
Reference to self for chaining

◆ addIndex()

MbEntitySchema & mb::MbEntitySchema::addIndex ( const IndexDefinition & index)

◆ addRule()

MbAccessRule mb::MbEntitySchema::addRule ( ) const
nodiscard
Returns
Create access rule.

◆ authMethods()

MbAuthMethods mb::MbEntitySchema::authMethods ( ) const
nodiscard
Returns
Login methods stored for an auth entity.

◆ createTable()

nlohmann::json mb::MbEntitySchema::createTable ( const MbEntitySchema & 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< MbEntitySchemaField > & mb::MbEntitySchema::defaultAuthFieldsSchema ( )
static
Returns
Default field objects injected into auth entity schemas.

◆ defaultBaseFieldsSchema()

const std::vector< MbEntitySchemaField > & mb::MbEntitySchema::defaultBaseFieldsSchema ( )
static
Returns
Default field objects injected into base entity schemas.

◆ deleteRule()

MbAccessRule mb::MbEntitySchema::deleteRule ( ) const
nodiscard
Returns
Delete access rule.

◆ dropTable() [1/2]

void mb::MbEntitySchema::dropTable ( const MantisBase & app,
const std::string & table_id )
static

Drop table by ID from database.

Parameters
appA const ref to MantisBase running instance
table_idTable identifier to drop

◆ dropTable() [2/2]

void mb::MbEntitySchema::dropTable ( const MbEntitySchema & original_table)
static

Drop table from database.

Parameters
original_tableSchema of table to drop

◆ dump()

std::string mb::MbEntitySchema::dump ( ) const
nodiscard

Dump schema as formatted string (for debugging).

Returns
Formatted schema string

◆ field()

MbEntitySchemaField & mb::MbEntitySchema::field ( const std::string & field_name)

Get field by name (mutable reference).

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

◆ fieldById()

MbEntitySchemaField & mb::MbEntitySchema::fieldById ( const std::string & field_id)

Get field by ID (mutable reference).

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

◆ fields()

std::vector< MbEntitySchemaField > mb::MbEntitySchema::fields ( ) const
nodiscard

Get all field definitions.

Returns
Vector of MbEntitySchemaField objects

◆ fromEntity()

MbEntitySchema mb::MbEntitySchema::fromEntity ( const MantisBase & app,
const MbEntity & entity )
static

Create schema from existing MbEntity, bound to an application.

Parameters
appA const ref to MantisBase running instance
entityMbEntity to convert from
Returns
MbEntitySchema instance

◆ fromSchema()

MbEntitySchema mb::MbEntitySchema::fromSchema ( const MantisBase & app,
const json & entity_schema )
static

Create schema from JSON object, bound to an application.

Parameters
appOwning application (db access for DDL/validation).
entity_schemaJSON schema object
Returns
MbEntitySchema instance

◆ genEntityId()

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

Generate unique entity ID from name.

Parameters
entity_nameMbEntity name
Returns
Generated ID string

◆ getRule()

MbAccessRule mb::MbEntitySchema::getRule ( ) const
nodiscard
Returns
Get (single-record) access rule.

◆ getTable()

nlohmann::json mb::MbEntitySchema::getTable ( const MantisBase & app,
const std::string & table_id )
static

Get table schema by ID from database.

Parameters
appOwning application (db access).
table_idTable identifier
Returns
JSON schema object

◆ hasApi()

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

Check if API endpoints are enabled.

Returns
true if API enabled

◆ hasField()

bool mb::MbEntitySchema::hasField ( const std::string & field_name) const
nodiscard

Check if field exists by name.

Parameters
field_nameField name to check
Returns
true if field exists

◆ hasFieldById()

bool mb::MbEntitySchema::hasFieldById ( const std::string & field_id) const
nodiscard

Check if field exists by ID.

Parameters
field_idField ID to check
Returns
true if field exists

◆ id()

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

Get schema unique identifier.

Returns
Schema ID string

◆ indexDDL()

std::vector< std::string > mb::MbEntitySchema::indexDDL ( ) const
nodiscard
Returns
CREATE INDEX statements for declared secondary indexes.

◆ indexes()

const std::vector< IndexDefinition > & mb::MbEntitySchema::indexes ( ) const
nodiscard
Returns
Index definitions declared on this schema.

◆ isSystem()

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

Check if this is a system table.

Returns
true if system table

◆ isValidEntityName()

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

Validate this schema name to alphanum and _ only.

Returns
Boolean true if all checks out

◆ isValidEntityType()

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

Validate this schema type to the 3 supported types.

Returns
Boolean true if all checks out

◆ listRule()

MbAccessRule mb::MbEntitySchema::listRule ( ) const
nodiscard
Returns
List (collection) access rule for this schema.

◆ listTables()

nlohmann::json mb::MbEntitySchema::listTables ( const MantisBase & app,
const nlohmann::json & opts = nlohmann::json::object() )
static

List all tables from database.

Parameters
appA const ref to MantisBase running instance
optsOptional parameters (pagination, etc.)
Returns
JSON array of table schemas

◆ name()

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

Get table name.

Returns
Table name

◆ operator=() [1/2]

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

◆ operator=() [2/2]

MbEntitySchema & mb::MbEntitySchema::operator= ( MbEntitySchema && other)
noexcept

◆ operator==()

bool mb::MbEntitySchema::operator== ( const MbEntitySchema & other) const

Compare two schemas for equality.

Parameters
otherSchema to compare with
Returns
true if schemas are equal

◆ removeField()

bool mb::MbEntitySchema::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

◆ removeIndex()

bool mb::MbEntitySchema::removeIndex ( const std::string & index_name)

◆ setAddRule()

MbEntitySchema & mb::MbEntitySchema::setAddRule ( const MbAccessRule & addRule)

◆ setAuthMethods()

MbEntitySchema & mb::MbEntitySchema::setAuthMethods ( const MbAuthMethods & methods)

Replace the stored login methods. Included in JSON only for auth entities.

◆ setDeleteRule()

MbEntitySchema & mb::MbEntitySchema::setDeleteRule ( const MbAccessRule & deleteRule)

◆ setGetRule()

MbEntitySchema & mb::MbEntitySchema::setGetRule ( const MbAccessRule & getRule)

◆ setHasApi()

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

Enable/disable API endpoints (fluent interface).

Parameters
hasApiAPI enabled flag
Returns
Reference to self for chaining

◆ setListRule()

MbEntitySchema & mb::MbEntitySchema::setListRule ( const MbAccessRule & listRule)

◆ setName()

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

Set table name (fluent interface).

Parameters
nameNew table name
Returns
Reference to self for chaining

◆ setSystem()

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

Set system table flag (fluent interface).

Parameters
isSystemSystem table flag
Returns
Reference to self for chaining

◆ setType()

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

Set entity type (fluent interface).

Parameters
typeMbEntity type
Returns
Reference to self for chaining

◆ setUpdateRule()

MbEntitySchema & mb::MbEntitySchema::setUpdateRule ( const MbAccessRule & updateRule)

◆ setViewQuery()

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

◆ tableExists() [1/2]

bool mb::MbEntitySchema::tableExists ( const MantisBase & app,
const std::string & table_name )
static

Check if table exists by name.

Parameters
appA const ref to MantisBase running instance
table_nameTable name to check
Returns
true if table exists

◆ tableExists() [2/2]

bool mb::MbEntitySchema::tableExists ( const MbEntitySchema & table)
static

Check if table exists (by schema name).

Parameters
tableSchema to check
Returns
true if table exists

◆ toDDL()

std::string mb::MbEntitySchema::toDDL ( ) const
nodiscard

Generate SQL DDL (CREATE TABLE) statement.

Returns
SQL DDL string

◆ toDefaultSqlValue()

std::string mb::MbEntitySchema::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()

MbEntity mb::MbEntitySchema::toEntity ( ) const
nodiscard

Convert schema to MbEntity for database operations.

Returns
MbEntity instance

◆ toJSON()

nlohmann::json mb::MbEntitySchema::toJSON ( ) const
nodiscard

Convert schema to JSON representation.

Returns
JSON object with schema data

◆ type()

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

Get entity type.

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

◆ updateRule()

MbAccessRule mb::MbEntitySchema::updateRule ( ) const
nodiscard
Returns
Update access rule.

◆ updateTable()

nlohmann::json mb::MbEntitySchema::updateTable ( const MantisBase & app,
const std::string & table_id,
const nlohmann::json & new_schema )
static

Update existing table schema in database.

Parameters
appA const ref to MantisBase running instance
table_idTable identifier
new_schemaUpdated schema JSON
Returns
JSON object with updated table data

◆ updateWith()

void mb::MbEntitySchema::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::MbEntitySchema::validate ( ) const
nodiscard

Validate this schema instance.

Returns
Optional error message if validation fails

◆ validate() [2/2]

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

Validate schema (static method).

Parameters
table_schemaSchema to validate
Returns
Optional error message if validation fails

◆ viewQuery()

std::string mb::MbEntitySchema::viewQuery ( ) const
nodiscard
Returns
SQL SELECT for view-type entities.

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