MantisBase v0.4.9
Loading...
Searching...
No Matches
entity_schema.h
Go to the documentation of this file.
1
8
9#ifndef MANTISBASE_ENTITY_SCHEMA_H
10#define MANTISBASE_ENTITY_SCHEMA_H
11
12#include <vector>
13#include <ranges>
14#include <algorithm>
15
16#include "entity.h"
17#include "entity_schema_field.h"
18#include "access_rules.h"
19#include "auth_methods.h"
21
22namespace mb {
40 public:
50 explicit MbEntitySchema(const MantisBase &app);
51
58 MbEntitySchema(const MantisBase &app, const std::string &entity_name, const std::string &entity_type = "base");
59
60 // Allow copy constructors & assignment ops for easy cloning
63
64 // Move operators (move ctor can copy the app ref; move assignment
65 // keeps the bound app and moves the rest, mirroring copy assignment)
66 MbEntitySchema(MbEntitySchema &&) noexcept = default;
67 MbEntitySchema &operator=(MbEntitySchema &&) noexcept;
68
70
76 bool operator==(const MbEntitySchema &other) const;
77
84 static MbEntitySchema fromSchema(const MantisBase &app, const json &entity_schema);
85
92 static MbEntitySchema fromEntity(const MantisBase &app, const MbEntity &entity);
93
98 [[nodiscard]] MbEntity toEntity() const;
99
100 // ----------- SCHEMA METHODS ----------- //
105 [[nodiscard]] std::string id() const;
106
111 [[nodiscard]] std::string name() const;
112
118 MbEntitySchema &setName(const std::string &name);
119
124 [[nodiscard]] std::string type() const;
125
131 MbEntitySchema &setType(const std::string &type);
132
137 [[nodiscard]] bool hasApi() const;
138
144 MbEntitySchema &setHasApi(const bool &hasApi);
145
147 [[nodiscard]] MbAuthMethods authMethods() const;
148
151
156 [[nodiscard]] bool isSystem() const;
157
163 MbEntitySchema &setSystem(const bool &isSystem);
164
166 [[nodiscard]] MbAccessRule listRule() const;
167
169
171 [[nodiscard]] MbAccessRule getRule() const;
172
174
176 [[nodiscard]] MbAccessRule addRule() const;
177
179
181 [[nodiscard]] MbAccessRule updateRule() const;
182
184
186 [[nodiscard]] MbAccessRule deleteRule() const;
187
189
194 [[nodiscard]] std::vector<MbEntitySchemaField> fields() const;
195
202
208 bool removeField(const std::string &field_name);
209
216 MbEntitySchemaField &field(const std::string &field_name);
217
224 MbEntitySchemaField &fieldById(const std::string &field_id);
225
231 [[nodiscard]] bool hasField(const std::string &field_name) const;
232
238 [[nodiscard]] bool hasFieldById(const std::string &field_id) const;
239
241 [[nodiscard]] std::string viewQuery() const;
242
243 MbEntitySchema &setViewQuery(const std::string &viewQuery);
244
246 [[nodiscard]] const std::vector<IndexDefinition> &indexes() const;
247
249
250 bool removeIndex(const std::string &index_name);
251
256 void updateWith(const nlohmann::json &new_data);
257
258 // ----------- SCHEMA CONVERSION ----------- //
263 [[nodiscard]] json toJSON() const;
264
269 [[nodiscard]] std::string toDDL() const;
270
272 [[nodiscard]] std::vector<std::string> indexDDL() const;
273
280 static std::string toDefaultSqlValue(const std::string &type, const nlohmann::json &v);
281
286 [[nodiscard]] std::string dump() const;
287
288 // ----------- SCHEMA CRUD ----------- //
295 static nlohmann::json listTables(const MantisBase &app, const nlohmann::json &opts = nlohmann::json::object());
296
303 static nlohmann::json getTable(const MantisBase &app, const std::string &table_id);
304
310 static nlohmann::json createTable(const MbEntitySchema &new_table);
311
319 static nlohmann::json updateTable(const MantisBase &app, const std::string &table_id, const nlohmann::json &new_schema);
320
325 static void dropTable(const MbEntitySchema &original_table);
326
332 static void dropTable(const MantisBase &app, const std::string &table_id);
333
340 static bool tableExists(const MantisBase &app, const std::string &table_name);
341
347 static bool tableExists(const MbEntitySchema &table);
348
354 static std::string genEntityId(const std::string &entity_name);
355
361 static std::optional<std::string> validate(const MbEntitySchema &table_schema);
362
367 [[nodiscard]] std::optional<std::string> validate() const;
368
373 static bool isValidEntityName(const std::string& name);
374
379 static bool isValidEntityType(const std::string& type);
380
382 static const std::vector<MbEntitySchemaField> &defaultBaseFieldsSchema();
383
385 static const std::vector<MbEntitySchemaField> &defaultAuthFieldsSchema();
386
387 private:
388 static std::string getFieldType(const std::string &type, std::shared_ptr<soci::session> sql,
389 MbIntPrecision precision = MbIntPrecision::I32);
390
391 void addFieldsIfNotExist(const std::string &type);
392
393 std::string m_name;
394 std::string m_type;
395 std::string m_viewSqlQuery;
396 bool m_isSystem = false;
397 bool m_hasApi = true;
398 MbAuthMethods m_authMethods;
399 std::vector<MbEntitySchemaField> m_fields;
400 std::vector<IndexDefinition> m_indexes;
401 MbAccessRule m_listRule, m_getRule, m_addRule, m_updateRule, m_deleteRule;
402 };
403} // mb
404
405#endif //MANTISBASE_ENTITY_SCHEMA_H
Access rule definition and shared evaluation for entity permissions.
Per-entity login method flags (password, OTP, OAuth).
MantisBase entry point.
Definition mantisbase.h:54
Access control rule for entity permissions.
Definition access_rules.h:45
MbAppMixin(const MantisBase &app)
Constrictor for instantiating this interface.
Definition types.cpp:5
Definition entity_schema_field.h:34
MbEntitySchema(MbEntitySchema &&) noexcept=default
MbEntitySchemaField & field(const std::string &field_name)
Get field by name (mutable reference).
Definition entity_schema.cpp:220
bool hasFieldById(const std::string &field_id) const
Check if field exists by ID.
Definition entity_schema.cpp:664
std::string dump() const
Dump schema as formatted string (for debugging).
Definition entity_schema.cpp:737
json toJSON() const
Convert schema to JSON representation.
Definition entity_schema.cpp:543
std::string toDDL() const
Generate SQL DDL (CREATE TABLE) statement.
Definition entity_schema.cpp:580
bool hasApi() const
Check if API endpoints are enabled.
Definition entity_schema.cpp:463
MbEntitySchema & addField(const MbEntitySchemaField &field)
Add a field to the schema (fluent interface).
Definition entity_schema.cpp:521
MbAccessRule deleteRule() const
Definition entity_schema.cpp:512
MbEntitySchemaField & fieldById(const std::string &field_id)
Get field by ID (mutable reference).
Definition entity_schema.cpp:233
MbEntitySchema & setDeleteRule(const MbAccessRule &deleteRule)
Definition entity_schema.cpp:514
const std::vector< IndexDefinition > & indexes() const
Definition entity_schema.cpp:285
MbEntitySchema & setAuthMethods(const MbAuthMethods &methods)
Definition entity_schema.cpp:467
static nlohmann::json getTable(const MantisBase &app, const std::string &table_id)
Get table schema by ID from database.
Definition entity_schema_crud.cpp:37
MbEntitySchema & setHasApi(const bool &hasApi)
Enable/disable API endpoints (fluent interface).
Definition entity_schema.cpp:472
static nlohmann::json createTable(const MbEntitySchema &new_table)
Create table in database from schema.
Definition entity_schema_crud.cpp:62
void updateWith(const nlohmann::json &new_data)
Update schema with new JSON data (merges fields).
Definition entity_schema.cpp:307
std::string type() const
Get entity type.
Definition entity_schema.cpp:442
static const std::vector< MbEntitySchemaField > & defaultBaseFieldsSchema()
Definition entity_schema_base_fields.cpp:9
MbEntitySchema & setUpdateRule(const MbAccessRule &updateRule)
Definition entity_schema.cpp:507
static std::string toDefaultSqlValue(const std::string &type, const nlohmann::json &v)
Get default SQL value for a field type.
Definition entity_schema.cpp:673
bool hasField(const std::string &field_name) const
Check if field exists by name.
Definition entity_schema.cpp:656
MbEntitySchema & setName(const std::string &name)
Set table name (fluent interface).
Definition entity_schema.cpp:437
MbEntitySchema & operator=(const MbEntitySchema &)
Definition entity_schema.cpp:27
MbAuthMethods authMethods() const
Definition entity_schema.cpp:465
MbEntitySchema & setListRule(const MbAccessRule &listRule)
Definition entity_schema.cpp:486
std::string name() const
Get table name.
Definition entity_schema.cpp:435
MbEntitySchema & setAddRule(const MbAccessRule &addRule)
Definition entity_schema.cpp:500
MbEntity toEntity() const
Convert schema to MbEntity for database operations.
Definition entity_schema.cpp:211
bool removeField(const std::string &field_name)
Remove a field by name.
Definition entity_schema.cpp:531
bool removeIndex(const std::string &index_name)
Definition entity_schema.cpp:298
static MbEntitySchema fromSchema(const MantisBase &app, const json &entity_schema)
Create schema from JSON object, bound to an application.
Definition entity_schema.cpp:71
static std::string genEntityId(const std::string &entity_name)
Generate unique entity ID from name.
Definition entity_schema.cpp:772
std::vector< std::string > indexDDL() const
Definition entity_schema.cpp:639
MbEntitySchema & setType(const std::string &type)
Set entity type (fluent interface).
Definition entity_schema.cpp:444
MbAccessRule listRule() const
Definition entity_schema.cpp:484
MbEntitySchema(const MantisBase &app)
Construct an empty schema bound to its application.
Definition entity_schema.cpp:9
bool isSystem() const
Check if this is a system table.
Definition entity_schema.cpp:477
static std::optional< std::string > validate(const MbEntitySchema &table_schema)
Validate schema (static method).
Definition entity_schema.cpp:776
static void dropTable(const MbEntitySchema &original_table)
Drop table from database.
Definition entity_schema_crud.cpp:757
MbEntitySchema & setViewQuery(const std::string &viewQuery)
Definition entity_schema.cpp:277
static const std::vector< MbEntitySchemaField > & defaultAuthFieldsSchema()
Definition entity_schema_base_fields.cpp:69
static bool isValidEntityType(const std::string &type)
Validate this schema type to the 3 supported types.
Definition entity_schema.cpp:872
static MbEntitySchema fromEntity(const MantisBase &app, const MbEntity &entity)
Create schema from existing MbEntity, bound to an application.
Definition entity_schema.cpp:162
MbAccessRule updateRule() const
Definition entity_schema.cpp:505
MbEntitySchema & addIndex(const IndexDefinition &index)
Definition entity_schema.cpp:289
MbEntitySchema & setGetRule(const MbAccessRule &getRule)
Definition entity_schema.cpp:493
static bool tableExists(const MantisBase &app, const std::string &table_name)
Check if table exists by name.
Definition entity_schema_crud.cpp:811
std::string viewQuery() const
Definition entity_schema.cpp:246
static nlohmann::json updateTable(const MantisBase &app, const std::string &table_id, const nlohmann::json &new_schema)
Update existing table schema in database.
Definition entity_schema_crud.cpp:135
static nlohmann::json listTables(const MantisBase &app, const nlohmann::json &opts=nlohmann::json::object())
List all tables from database.
Definition entity_schema_crud.cpp:14
MbAccessRule addRule() const
Definition entity_schema.cpp:498
static bool isValidEntityName(const std::string &name)
Validate this schema name to alphanum and _ only.
Definition entity_schema.cpp:879
std::vector< MbEntitySchemaField > fields() const
Get all field definitions.
Definition entity_schema.cpp:519
MbEntitySchema & setSystem(const bool &isSystem)
Set system table flag (fluent interface).
Definition entity_schema.cpp:479
MbAccessRule getRule() const
Definition entity_schema.cpp:491
MbEntitySchema(const MbEntitySchema &)
Represents a database table/entity with schema and CRUD operations.
Definition entity.h:55
MbEntity class for database table operations and CRUD functionality.
MbEntity schema field definition and validation.
#define MANTISBASE_API
Definition export.h:31
Definition api_keys.h:35
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
MbIntPrecision
Definition int_precision.h:24
Definition log_database.h:21
Definition utils.h:35
Definition entity_schema_field.h:22
Definition auth_methods.h:21
Core type aliases, handler signatures, and the MbAppMixin DI mixin.