Mantis App v0.1.13
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
32 // Enum of the table type created,
33 // base table types provide `index`, `created`, `updated`
34 // auth table type provide `base` type + `email`, `password`, `name`
35 // view table type provide readonly `sql`
36 typedef enum class TableType
37 {
38 Base = 1,
39 Auth,
40 View
42
44 {TableType::Base, "base"},
45 {TableType::Auth, "auth"},
46 {TableType::View, "view"}
47 })
48
49 typedef enum class FieldTypeDecl {
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
67
69 { FieldType::XML, "xml" },
70 { FieldType::STRING, "string" },
71 { FieldType::DOUBLE, "double" },
72 { FieldType::DATE, "date" },
73 { FieldType::INT8, "int8" },
74 { FieldType::UINT8, "uint8" },
75 { FieldType::INT16, "int16" },
76 { FieldType::UINT16, "uint16" },
77 { FieldType::INT32, "int32" },
78 { FieldType::UINT32, "uint32" },
79 { FieldType::INT64, "int64" },
80 { FieldType::UINT64, "uint64" },
81 { FieldType::BLOB, "blob" },
82 { FieldType::JSON, "json" },
83 { FieldType::BOOL, "bool" },
84 })
85
86 const std::vector<std::string> baseFields = {"id", "created", "updated"};
87 const std::vector<std::string> authFields = {"id", "created", "updated", "name", "email", "password"};
88
89 std::optional<FieldType> getFieldType(const std::string& fieldName);
90
91 bool fieldExists(const TableType& type, const std::string& fieldName);
92
93 bool isValidFieldType(const std::string& fieldType);
94
95 // Access rule expression
96 typedef std::string Rule;
97
98 // Field definition
99 struct Field {
100 std::string name;
102
103 bool required = false;
104 bool primaryKey = false;
105 bool system = false;
106
107 std::optional<std::string> defaultValue; // as string, parse based on type
108 std::optional<std::string> regexPattern;
109 std::optional<double> minValue;
110 std::optional<double> maxValue;
111 bool isUnique = false;
112 std::optional<std::string> validator;
113 std::optional<std::string> autoGeneratePattern; // regex for auto-gen strings
114
115 // Convenience constructor
116 Field(std::string n, FieldType t, bool req = false, bool pk = false, bool sys = false, json opts = json::object());
117
118 [[nodiscard]]
119 json to_json() const;
120
121 [[nodiscard]]
122 soci::db_type toSociType() const;
123
124 [[nodiscard]]
125 static soci::db_type toSociType(const FieldType& f_type);
126 };
127
128 // Represents a generic table in the system
129 struct Table {
130 virtual ~Table() = default;
131 std::string id;
132 std::string name;
134 bool system = false;
135 bool has_api = true;
136
137 std::vector<Field> fields;
138
144
145 // Constructor
146 Table() = default;
147
148 [[nodiscard]]
149 virtual json to_json() const;
150
151 [[nodiscard]]
152 virtual std::string to_sql() const;
153 };
154
155 // Specific model for Base table (user-defined)
156 struct BaseTable : Table {
157 bool enableSync = true;
158
159 BaseTable();
160 };
161
162 // Specific model for Auth table
164 {
165 std::string usernameField = "email";
166 std::string passwordField = "password";
167 bool enableSync = true;
168
169 // Member Functions
170 AuthTable();
171 ~AuthTable() override = default;
172 };
173
174 // Specific model for View table
175 struct ViewTable : Table {
176 std::string sourceSQL;
177 bool enableSync = false;
178
179 ViewTable();
180 };
181
182 struct SystemTable final : BaseTable {
183 bool enableSync = true;
184
185 SystemTable();
186 ~SystemTable() override = default;
187 };
188
189 struct AdminTable final : AuthTable {
190 bool enableSync = true;
191
192 AdminTable();
193 };
194}
195
196#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:30
nlohmann::json json
Shorten JSON namespace.
Definition crud.h:14
FieldType
Definition models.h:66
bool fieldExists(const TableType &type, const std::string &fieldName)
Definition models.cpp:91
bool isValidFieldType(const std::string &fieldType)
Definition models.cpp:114
std::string Rule
Definition models.h:96
std::optional< FieldType > getFieldType(const std::string &fieldName)
Definition models.cpp:70
TableType
Definition models.h:37
const std::vector< std::string > authFields
Definition models.h:87
NLOHMANN_JSON_SERIALIZE_ENUM(TableType, { {TableType::Base, "base"}, {TableType::Auth, "auth"}, {TableType::View, "view"} }) typedef enum class FieldTypeDecl
Definition models.h:43
Definition models.h:189
AdminTable()
Definition models.cpp:319
bool enableSync
Definition models.h:190
Definition models.h:164
std::string usernameField
Definition models.h:165
bool enableSync
Definition models.h:167
std::string passwordField
Definition models.h:166
~AuthTable() override=default
AuthTable()
Definition models.cpp:287
Definition models.h:156
bool enableSync
Definition models.h:157
BaseTable()
Definition models.cpp:277
Definition models.h:99
bool isUnique
Definition models.h:111
std::string name
Definition models.h:100
json to_json() const
Definition models.cpp:186
soci::db_type toSociType() const
Definition models.cpp:203
std::optional< std::string > defaultValue
Definition models.h:107
std::optional< std::string > autoGeneratePattern
Definition models.h:113
std::optional< std::string > regexPattern
Definition models.h:108
bool required
Definition models.h:103
std::optional< double > minValue
Definition models.h:109
std::optional< std::string > validator
Definition models.h:112
FieldType type
Definition models.h:101
bool primaryKey
Definition models.h:104
std::optional< double > maxValue
Definition models.h:110
bool system
Definition models.h:105
Definition models.h:182
bool enableSync
Definition models.h:183
~SystemTable() override=default
SystemTable()
Definition models.cpp:308
Definition models.h:129
std::vector< Field > fields
Definition models.h:137
bool has_api
Definition models.h:135
std::string name
Definition models.h:132
Table()=default
virtual ~Table()=default
Rule getRule
Definition models.h:140
Rule deleteRule
Definition models.h:143
virtual std::string to_sql() const
Definition models.cpp:248
bool system
Definition models.h:134
TableType type
Definition models.h:133
std::string id
Definition models.h:131
virtual json to_json() const
Definition models.cpp:227
Rule addRule
Definition models.h:141
Rule updateRule
Definition models.h:142
Rule listRule
Definition models.h:139
Definition models.h:175
std::string sourceSQL
Definition models.h:176
ViewTable()
Definition models.cpp:303
bool enableSync
Definition models.h:177