MantisBase v0.3.4
Loading...
Searching...
No Matches
entity.h
Go to the documentation of this file.
1
9#ifndef MANTISBASE_ENTITY_H
10#define MANTISBASE_ENTITY_H
11
12#include <string>
13#include "mantisbase/mantis.h"
16#include "../types.h"
17#include "access_rules.h"
18
19namespace mb {
20 using Record = nlohmann::json;
21 using Records = std::vector<Record>;
22
39 class Entity {
40 public:
45 explicit Entity(const nlohmann::json &schema);
46
52 explicit Entity(const std::string &name, const std::string& type);
53
54 // --------------- DB TABLE OPS ------------------ //
59 [[nodiscard]] std::string id() const;
60
65 [[nodiscard]] std::string name() const;
66
71 [[nodiscard]] std::string type() const;
72
77 [[nodiscard]] bool isSystem() const;
78
83 [[nodiscard]] bool hasApi() const;
84
90 [[nodiscard]] std::string viewQuery() const;
91
96 [[nodiscard]] const std::vector<json> &fields() const;
97
103 [[nodiscard]] std::optional<json> field(const std::string& field_name) const;
104
110 [[nodiscard]] std::optional<json> hasField(const std::string& field_name) const;
111
116 [[nodiscard]] const json& rules() const;
117
122 [[nodiscard]] AccessRule listRule() const;
123
128 [[nodiscard]] AccessRule getRule() const;
129
134 [[nodiscard]] AccessRule addRule() const;
135
140 [[nodiscard]] AccessRule updateRule() const;
141
146 [[nodiscard]] AccessRule deleteRule() const;
147
148 // --------------- DB CRUD OPS ------------------ //
159 [[nodiscard]] Record create(const json &record, const json &opts = json::object()) const;
160
166 [[nodiscard]] Records list(const json &opts = json::object()) const;
167
174 [[nodiscard]] std::optional<Record> read(const std::string &id, const json &opts = json::object()) const;
175
183 [[nodiscard]] Record update(const std::string &id, const json &data, const json &opts = json::object()) const;
184
189 void remove(const std::string &id) const;
190
191 // --------------- SCHEMA OPS ------------------ //
196 [[nodiscard]] const json &schema() const;
197
202 [[nodiscard]] int countRecords() const;
203
208 [[nodiscard]] bool isEmpty() const;
209
210 // --------------- UTILITY OPS ------------------ //
216 [[nodiscard]] bool recordExists(const std::string &id) const;
217
223 [[nodiscard]] std::optional<json> findField(const std::string &field_name) const;
224
231 [[nodiscard]] std::optional<json> queryFromCols(const std::string &value,
232 const std::vector<std::string> &columns) const;
233
234 // --------------- SCHEMA ROUTING ------------------ //
239 [[nodiscard]] HandlerFn getOneRouteHandler() const;
240
245 [[nodiscard]] HandlerFn getManyRouteHandler() const;
246
251 [[nodiscard]] HandlerWithContentReaderFn postRouteHandler() const;
252
257 [[nodiscard]] HandlerWithContentReaderFn patchRouteHandler() const;
258
263 [[nodiscard]] HandlerFn deleteRouteHandler() const;
264
268 void createEntityRoutes() const;
269
270 private:
271 nlohmann::json m_schema;
272 };
273} // mb
274
275#endif //MANTISBASE_ENTITY_H
Access rule definition for entity permissions.
Access control rule for entity permissions.
Definition access_rules.h:31
Represents a database table/entity with schema and CRUD operations.
Definition entity.h:39
const std::vector< json > & fields() const
Get all field definitions.
Definition entity.cpp:101
HandlerWithContentReaderFn postRouteHandler() const
Get route handler for POST /api/v1/entities/{table}.
Definition entity_routes_handlers.cpp:133
Record create(const json &record, const json &opts=json::object()) const
Create a new record in the entity table.
Definition entity.cpp:146
std::string viewQuery() const
Get SQL query for view type entities.
Definition entity.cpp:91
bool hasApi() const
Check if entity has API endpoints enabled.
Definition entity.cpp:87
int countRecords() const
Count total records in the entity table.
Definition entity.cpp:474
bool recordExists(const std::string &id) const
Check if a record exists by ID.
Definition entity.cpp:499
HandlerFn deleteRouteHandler() const
Get route handler for DELETE /api/v1/entities/{table}/:id.
Definition entity_routes_handlers.cpp:270
HandlerFn getManyRouteHandler() const
Get route handler for GET /api/v1/entities/{table}.
Definition entity_routes_handlers.cpp:59
Records list(const json &opts=json::object()) const
List all records in the entity table.
Definition entity.cpp:222
HandlerFn getOneRouteHandler() const
Get route handler for GET /api/v1/entities/{table}/:id.
Definition entity_routes_handlers.cpp:7
Record update(const std::string &id, const json &data, const json &opts=json::object()) const
Update an existing record by ID.
Definition entity.cpp:283
std::string type() const
Get entity type ("base", "auth", or "view").
Definition entity.cpp:79
HandlerWithContentReaderFn patchRouteHandler() const
Get route handler for PATCH /api/v1/entities/{table}/:id.
Definition entity_routes_handlers.cpp:202
bool isSystem() const
Check if entity is a system table.
Definition entity.cpp:83
AccessRule updateRule() const
Get update access rule (for PATCH /api/v1/entities/{table}/:id).
Definition entity.cpp:134
const json & schema() const
Get complete entity schema JSON.
Definition entity.cpp:472
void createEntityRoutes() const
Register all CRUD routes for this entity with the router.
Definition entity_routes_handlers.cpp:305
AccessRule addRule() const
Get create access rule (for POST /api/v1/entities/{table}).
Definition entity.cpp:130
std::optional< json > field(const std::string &field_name) const
Get field definition by name.
Definition entity.cpp:105
const json & rules() const
Get all access rules.
Definition entity.cpp:118
AccessRule getRule() const
Get read access rule (for GET /api/v1/entities/{table}/:id).
Definition entity.cpp:126
std::optional< Record > read(const std::string &id, const json &opts=json::object()) const
Read a single record by ID.
Definition entity.cpp:258
std::optional< json > findField(const std::string &field_name) const
Find field definition by name (alias for field()).
Definition entity.cpp:512
AccessRule deleteRule() const
Get delete access rule (for DELETE /api/v1/entities/{table}/:id).
Definition entity.cpp:138
std::optional< json > queryFromCols(const std::string &value, const std::vector< std::string > &columns) const
Query records by searching across multiple columns.
Definition entity.cpp:522
std::string id() const
Get entity unique identifier.
Definition entity.cpp:71
std::optional< json > hasField(const std::string &field_name) const
Check if field exists by name.
Definition entity.cpp:114
bool isEmpty() const
Check if entity table is empty.
Definition entity.cpp:486
std::string name() const
Get entity table name.
Definition entity.cpp:75
AccessRule listRule() const
Get list access rule (for GET /api/v1/entities/{table}).
Definition entity.cpp:122
void remove(const std::string &id) const
Delete a record by ID.
Definition entity.cpp:426
Custom exception class for MantisBase errors.
Convenience header that includes all MantisBase public API headers.
router.h
Definition auth.h:15
std::vector< Record > Records
Collection of database records.
Definition entity.h:21
nlohmann::json Record
Single database record as JSON object.
Definition entity.h:20
std::function< void(MantisRequest &, MantisResponse &, MantisContentReader &)> HandlerWithContentReaderFn
‍Route Handler function with content reader shorthand
Definition types.h:40
std::function< void(MantisRequest &, MantisResponse &)> HandlerFn
‍Route Handler function shorthand
Definition types.h:36
nlohmann::json json
Shorten JSON namespace.
Definition context_store.h:18
SOCI database wrapper utilities.