Mantis App v0.2.8
Loading...
Searching...
No Matches
models.h
Go to the documentation of this file.
1//
2// Created by allan on 08/05/2025.
3//
4
5#ifndef MODELS_H
6#define MODELS_H
7
8#include <optional>
9#include <soci/soci.h>
10#include <nlohmann/json.hpp>
11
12namespace mantis
13{
14 using json = nlohmann::json;
15
16 class MantisApp;
17
19 {
20 std::unordered_map<std::string, json> m_validators;
21
22 public:
23 Validator();
24
25 std::optional<json> find(const std::string& key);
26
27 json validate(const std::string& key, const std::string& value);
28 };
29
30
31 // Enum of the table type created,
32 // base table types provide `index`, `created`, `updated`
33 // auth table type provide `base` type + `email`, `password`, `name`
34 // view table type provide readonly `sql`
35 typedef enum class TableType
36 {
37 Base = 1,
38 Auth,
39 View
41
43 {TableType::Base, "base"},
44 {TableType::Auth, "auth"},
45 {TableType::View, "view"}
46 })
47
48 typedef enum class FieldTypeDecl
49 {
50 XML = soci::db_xml,
51 STRING = soci::db_string,
52 DOUBLE = soci::db_double,
53 DATE = soci::db_date,
54 INT8 = soci::db_int8,
55 UINT8 = soci::db_uint8,
56 INT16 = soci::db_int16,
57 UINT16 = soci::db_uint16,
58 INT32 = soci::db_int32,
59 UINT32 = soci::db_uint32,
60 INT64 = soci::db_int64,
61 UINT64 = soci::db_uint64,
62 BLOB = soci::db_blob,
63 // User defined types
64 JSON,
65 BOOL,
66 FILE, // Hold file name
67 FILES // Hold an array of file
69
71 { FieldType::XML, "xml" },
72 { FieldType::STRING, "string" },
73 { FieldType::DOUBLE, "double" },
74 { FieldType::DATE, "date" },
75 { FieldType::INT8, "int8" },
76 { FieldType::UINT8, "uint8" },
77 { FieldType::INT16, "int16" },
78 { FieldType::UINT16, "uint16" },
79 { FieldType::INT32, "int32" },
80 { FieldType::UINT32, "uint32" },
81 { FieldType::INT64, "int64" },
82 { FieldType::UINT64, "uint64" },
83 { FieldType::BLOB, "blob" },
84 { FieldType::JSON, "json" },
85 { FieldType::BOOL, "bool" },
86 { FieldType::FILE, "file" },
87 { FieldType::FILES, "files" },
88 })
89
90 const std::vector<std::string> baseFields = {"id", "created", "updated"};
91 const std::vector<std::string> authFields = {"id", "created", "updated", "name", "email", "password"};
92
93 std::optional<FieldType> getFieldType(const std::string& fieldName);
94
95 bool fieldExists(const TableType& type, const std::string& fieldName);
96
97 bool isValidFieldType(const std::string& fieldType);
98
99 // Access rule expression
100 typedef std::string Rule;
101
102 // Field definition
103 struct Field
104 {
105 std::string name;
107
108 bool required = false;
109 bool primaryKey = false;
110 bool system = false;
111
112 std::optional<std::string> defaultValue; // as string, parse based on type
113 std::optional<std::string> regexPattern;
114 std::optional<double> minValue;
115 std::optional<double> maxValue;
116 bool isUnique = false;
117 std::optional<std::string> validator;
118 std::optional<std::string> autoGeneratePattern; // regex for auto-gen strings
119
120 // Convenience constructor
121 Field(std::string n, FieldType t, bool req = false, bool pk = false, bool sys = false,
122 json opts = json::object());
123
124 [[nodiscard]]
125 json to_json() const;
126
127 [[nodiscard]]
128 soci::db_type toSociType() const;
129
130 [[nodiscard]]
131 static soci::db_type toSociType(const FieldType& f_type);
132 };
133
134 // Represents a generic table in the system
135 struct Table
136 {
137 virtual ~Table() = default;
138 std::string id;
139 std::string name;
141 bool system = false;
142 bool has_api = true;
143
144 std::vector<Field> fields;
145
151
152 // Constructor
153 Table() = default;
154
155 [[nodiscard]]
156 virtual json to_json() const;
157
158 [[nodiscard]]
159 virtual std::string to_sql() const;
160 };
161
162 // Specific model for Base table (user-defined)
164 {
165 bool enableSync = true;
166
167 BaseTable();
168 };
169
170 // Specific model for Auth table
172 {
173 std::string usernameField = "email";
174 std::string passwordField = "password";
175 bool enableSync = true;
176
177 // Member Functions
178 AuthTable();
179 ~AuthTable() override = default;
180 };
181
182 // Specific model for View table
184 {
185 std::string sourceSQL;
186 bool enableSync = false;
187
188 ViewTable();
189 };
190
191 struct SystemTable final : BaseTable
192 {
193 bool enableSync = true;
194
195 SystemTable();
196 ~SystemTable() override = default;
197 };
198
199 struct AdminTable final : AuthTable
200 {
201 bool enableSync = true;
202
203 AdminTable();
204 };
205}
206
207#endif //MODELS_H
Definition models.h:19
json validate(const std::string &key, const std::string &value)
Definition models.cpp:37
Validator()
Definition models.cpp:8
std::optional< json > find(const std::string &key)
Definition models.cpp:27
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:31
nlohmann::json json
Shorten JSON namespace.
Definition crud.h:14
FieldType
Definition models.h:68
bool fieldExists(const TableType &type, const std::string &fieldName)
Definition models.cpp:92
bool isValidFieldType(const std::string &fieldType)
Definition models.cpp:115
std::string Rule
Definition models.h:100
std::optional< FieldType > getFieldType(const std::string &fieldName)
Definition models.cpp:70
TableType
Definition models.h:36
const std::vector< std::string > authFields
Definition models.h:91
NLOHMANN_JSON_SERIALIZE_ENUM(TableType, { {TableType::Base, "base"}, {TableType::Auth, "auth"}, {TableType::View, "view"} }) typedef enum class FieldTypeDecl
Definition models.h:42
Definition models.h:200
AdminTable()
Definition models.cpp:350
bool enableSync
Definition models.h:201
Definition models.h:172
std::string usernameField
Definition models.h:173
bool enableSync
Definition models.h:175
std::string passwordField
Definition models.h:174
~AuthTable() override=default
AuthTable()
Definition models.cpp:318
Definition models.h:164
bool enableSync
Definition models.h:165
BaseTable()
Definition models.cpp:308
Definition models.h:104
bool isUnique
Definition models.h:116
std::string name
Definition models.h:105
json to_json() const
Definition models.cpp:189
soci::db_type toSociType() const
Definition models.cpp:206
std::optional< std::string > defaultValue
Definition models.h:112
std::optional< std::string > autoGeneratePattern
Definition models.h:118
std::optional< std::string > regexPattern
Definition models.h:113
bool required
Definition models.h:108
std::optional< double > minValue
Definition models.h:114
std::optional< std::string > validator
Definition models.h:117
FieldType type
Definition models.h:106
bool primaryKey
Definition models.h:109
std::optional< double > maxValue
Definition models.h:115
bool system
Definition models.h:110
Definition models.h:192
bool enableSync
Definition models.h:193
~SystemTable() override=default
SystemTable()
Definition models.cpp:339
Definition models.h:136
std::vector< Field > fields
Definition models.h:144
bool has_api
Definition models.h:142
std::string name
Definition models.h:139
Table()=default
virtual ~Table()=default
Rule getRule
Definition models.h:147
Rule deleteRule
Definition models.h:150
virtual std::string to_sql() const
Definition models.cpp:254
bool system
Definition models.h:141
TableType type
Definition models.h:140
std::string id
Definition models.h:138
virtual json to_json() const
Definition models.cpp:233
Rule addRule
Definition models.h:148
Rule updateRule
Definition models.h:149
Rule listRule
Definition models.h:146
Definition models.h:184
std::string sourceSQL
Definition models.h:185
ViewTable()
Definition models.cpp:334
bool enableSync
Definition models.h:186