MantisBase v0.4.9
Loading...
Searching...
No Matches
entity.h
Go to the documentation of this file.
1
8
9#ifndef MANTISBASE_ENTITY_H
10#define MANTISBASE_ENTITY_H
11
12#include <string>
13#include <string_view>
14#include <memory>
15#include "mantisbase/mantis.h"
18#include "../types.h"
19#include "access_rules.h"
20#include "auth_methods.h"
21
22namespace mb {
23 class MantisBase; // forward declaration; MbEntity holds a non-owning pointer to it
24
25 using Record = nlohmann::json;
26 using Records = std::vector<Record>;
27
33
37 inline constexpr int MAX_LIST_PAGE_SIZE = 500;
38
56 public:
67 MbEntity(const MantisBase &app, const nlohmann::json &schema);
68
75 MbEntity(const MantisBase &app, const std::string &name, const std::string &type);
76
77 MbEntity(const MbEntity&) = default; // default copy constructor
78 MbEntity(MbEntity&&) = default; // default move constructor
79
80 // --------------- DB TABLE OPS ------------------ //
85 [[nodiscard]] std::string id() const;
86
91 [[nodiscard]] std::string name() const;
92
97 [[nodiscard]] std::string type() const;
98
103 [[nodiscard]] bool isSystem() const;
104
109 [[nodiscard]] bool hasApi() const;
110
117 [[nodiscard]] MbAuthMethods authMethods() const;
118
123 [[nodiscard]] std::string authMethodDisabledReason(std::string_view method) const;
124
130 [[nodiscard]] std::string viewQuery() const;
131
136 [[nodiscard]] const std::vector<json> &fields() const;
137
143 [[nodiscard]] std::optional<json> field(const std::string& field_name) const;
144
150 [[nodiscard]] std::optional<json> hasField(const std::string& field_name) const;
151
156 [[nodiscard]] const json& rules() const;
157
162 [[nodiscard]] MbAccessRule listRule() const;
163
168 [[nodiscard]] MbAccessRule getRule() const;
169
174 [[nodiscard]] MbAccessRule addRule() const;
175
180 [[nodiscard]] MbAccessRule updateRule() const;
181
186 [[nodiscard]] MbAccessRule deleteRule() const;
187
188 // --------------- DB CRUD OPS ------------------ //
200 [[nodiscard]] Record create(const json &record, const json &opts = json::object()) const;
201
213 [[nodiscard]] EntityListPage listPage(const json &opts = json::object()) const;
214
220 [[nodiscard]] Records list(const json &opts = json::object()) const;
221
228 [[nodiscard]] std::optional<Record> read(const std::string &id, const json &opts = json::object()) const;
229
237 [[nodiscard]] Record update(const std::string &id, const json &data, const json &opts = json::object()) const;
238
243 void remove(const std::string &id) const;
244
245 // --------------- SCHEMA OPS ------------------ //
250 [[nodiscard]] const json &schema() const;
251
257 [[nodiscard]] int countRecords(const json &filter = json::object()) const;
258
263 [[nodiscard]] bool isEmpty() const;
264
265 // --------------- UTILITY OPS ------------------ //
271 [[nodiscard]] bool recordExists(const std::string &id) const;
272
278 [[nodiscard]] std::optional<json> findField(const std::string &field_name) const;
279
286 [[nodiscard]] std::optional<json> queryFromCols(const std::string &value,
287 const std::vector<std::string> &columns) const;
288
289 private:
290
296 std::shared_ptr<const nlohmann::json> m_schema;
297 };
298} // mb
299
300#endif //MANTISBASE_ENTITY_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
Record create(const json &record, const json &opts=json::object()) const
Create a new record in the entity table.
Definition entity_crud.cpp:115
int countRecords(const json &filter=json::object()) const
Count records in the entity table, optionally matching a filter.
Definition entity_crud.cpp:480
MbEntity(const MbEntity &)=default
std::string name() const
Get entity table name.
Definition entity.cpp:81
const json & schema() const
Get complete entity schema JSON.
Definition entity.cpp:176
void remove(const std::string &id) const
Delete a record by ID.
Definition entity_crud.cpp:427
MbEntity(const MantisBase &app, const nlohmann::json &schema)
Construct entity from schema JSON object, bound to its app.
Definition entity.cpp:8
std::string id() const
Get entity unique identifier.
Definition entity.cpp:77
std::optional< json > hasField(const std::string &field_name) const
Check if field exists by name.
Definition entity.cpp:148
MbAccessRule listRule() const
Get list access rule (for GET /api/v1/entities/{table}).
Definition entity.cpp:156
bool isSystem() const
Check if entity is a system table.
Definition entity.cpp:117
std::optional< json > queryFromCols(const std::string &value, const std::vector< std::string > &columns) const
Query records by searching across multiple columns.
Definition entity_crud.cpp:513
MbAccessRule getRule() const
Get read access rule (for GET /api/v1/entities/{table}/:id).
Definition entity.cpp:160
MbAccessRule deleteRule() const
Get delete access rule (for DELETE /api/v1/entities/{table}/:id).
Definition entity.cpp:172
MbEntity(MbEntity &&)=default
MbAccessRule addRule() const
Get create access rule (for POST /api/v1/entities/{table}).
Definition entity.cpp:164
bool hasApi() const
Check if entity has API endpoints enabled.
Definition entity.cpp:121
std::string viewQuery() const
Get SQL query for view type entities.
Definition entity.cpp:125
Record update(const std::string &id, const json &data, const json &opts=json::object()) const
Update an existing record by ID.
Definition entity_crud.cpp:275
const json & rules() const
Get all access rules.
Definition entity.cpp:152
MbAccessRule updateRule() const
Get update access rule (for PATCH /api/v1/entities/{table}/:id).
Definition entity.cpp:168
std::optional< json > findField(const std::string &field_name) const
Find field definition by name (alias for field()).
Definition entity.cpp:178
std::string authMethodDisabledReason(std::string_view method) const
Empty when method is enabled, otherwise the client-facing reason.
Definition entity.cpp:93
std::string type() const
Get entity type ("base", "auth", or "view").
Definition entity.cpp:113
Records list(const json &opts=json::object()) const
List records (items only; see listPage for pagination metadata).
Definition entity_crud.cpp:246
bool recordExists(const std::string &id) const
Check if a record exists by ID.
Definition entity_crud.cpp:555
EntityListPage listPage(const json &opts=json::object()) const
List entity records with cursor pagination (always ordered by id ASC).
Definition entity_crud.cpp:199
bool isEmpty() const
Check if entity table is empty.
Definition entity_crud.cpp:500
std::optional< json > field(const std::string &field_name) const
Get field definition by name.
Definition entity.cpp:139
MbAuthMethods authMethods() const
Login methods for an auth entity.
Definition entity.cpp:85
std::optional< Record > read(const std::string &id, const json &opts=json::object()) const
Read a single record by ID.
Definition entity_crud.cpp:250
const std::vector< json > & fields() const
Get all field definitions.
Definition entity.cpp:135
Custom exception class for MantisBase errors.
#define MANTISBASE_API
Definition export.h:31
Convenience header that includes all MantisBase public API headers.
Definition api_keys.h:35
constexpr int MAX_LIST_PAGE_SIZE
Definition entity.h:37
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
std::vector< Record > Records
Collection of database records.
Definition entity.h:26
nlohmann::json Record
Single database record as JSON object.
Definition entity.h:25
SOCI database wrapper utilities.
Definition entity.h:29
Records items
Definition entity.h:30
bool has_more
Definition entity.h:31
Definition auth_methods.h:21
Core type aliases, handler signatures, and the MbAppMixin DI mixin.