27 : storagePath(path) {}
35 printf(
"Failed to get JsonService\n");
40 printf(
"[FrameworkModel] No storage path set, cannot load data\n");
73 std::vector<nlohmann::json> items;
76 TRACE(
"[FrameworkModel::all] Item: %s\n", item.dump(4).c_str());
77 items.push_back(item);
88 if (item.contains(idField) && item[idField] == id)
100 if (!item.contains(idField))
102 std::string
id = item[idField];
115 if (item.contains(idField) && item[idField] == id)
130 if (it->contains(idField) && (*it)[idField] == id)
142 auto result =
find(
id);
143 return result ? *result : nlohmann::json(
nullptr);
153 if (item.contains(idField) && item[idField] == id)
169 if (!obj.contains(idField))
171 return save(obj[idField], obj);
180 if (item.contains(idField) && item[idField] == id)
182 for (
auto &el : updates.items())
184 item[el.key()] = el.value();
186 return save(
id, item);
198 if (it->contains(idField) && (*it)[idField] == id)
200 nlohmann::json removed = *it;
202 return saveAll() ? removed :
nullptr;
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
#define TRACE(...)
Default trace (INFO level).
Base model class for persistent JSON collections.
Abstract interface for file and directory storage backends.
static constexpr std::uintptr_t getTypeKey()
Provides a basic JSON-backed record model.
bool load()
Loads the JSON collection from storage.
bool createFromJson(const nlohmann::json &obj)
nlohmann::json deleteAsJson(const std::string &id)
bool updateFromJson(const std::string &id, const nlohmann::json &updates)
virtual std::string getIdField() const
Returns the JSON key used as the record ID.
std::optional< nlohmann::json > find(const std::string &id) const
Finds an item by ID.
nlohmann::json collection
In-memory array of records.
JsonService * jsonService
Underlying JSON persistence layer
bool create(const nlohmann::json &item)
Adds a new item to the collection.
bool update(const std::string &id, const nlohmann::json &updatedItem)
Updates an item by ID.
nlohmann::json findAsJson(const std::string &id) const
Finds a single item by ID and returns it as JSON or null.
bool save()
Saves the current collection to storage.
std::string storagePath
File path for this model.
std::vector< nlohmann::json > all() const
Returns all items in the collection.
bool remove(const std::string &id)
Removes an item by ID.
nlohmann::json & data()
Access the internal JSON object.
bool save(const std::string &path) const
Save the current JSON data to storage.
bool load(const std::string &path)
Load a JSON file from storage.
Delegates to user or system configuration.