Repository navigation
v3.2.3
DatabaseAPI Library
The DatabaseAPI Library is a C++ library that provides an interface and implementation for a simple key-value database. It is designed to work on the ESP32/Arduino platform, making use of the Non-Volatile Storage (NVS) capabilities for data storage.
Table of Contents
- Introduction
- Features
- Dependencies
- Installation
- Usage
- API
- Example
- License
- Contributions
- Platformio Registry
Introduction
The DatabaseAPI Library is designed to simplify the management of key-value pairs in a non-volatile storage environment. It provides a flexible and easy-to-use interface for interacting with a database.
Features
Key-Value Storage: Store and retrieve key-value pairs.Error Handling: Comprehensive error handling with error codes.Database Delegation: Easily switch between different database implementations.ESP32/Arduino NVS Support: Includes an implementation for the ESP32/Arduino NVS database.
Dependencies
The DatabaseAPI Library has the following dependencies:
- C++ Standard Library
- ESP32/Arduino NVS library (for the NVSDelegate implementation)
Installation
Method 1:
To use the DatabaseAPI library in your PlatformIO project, follow these steps:
-
Open "platformio.ini", a project configuration file located in the root of PlatformIO project.
-
Add the following line to the
lib_depsoption of[env:]section:
ronny-antoon/DatabaseAPI@^3.2.3 -
Build a project, PlatformIO will automatically install dependencies.
Method 2:
To use the DatabaseAPI library in your Arduino project, follow these steps:
-
Download the latest release from the GitHub repository.
-
In the Arduino IDE, click "Sketch" -> "Include Library" -> "Add .ZIP Library" and select the downloaded
.zipfile. -
Make sure to link your project with the necessary ESP-IDF libraries for NVS support.
Usage
- Initialize the database delegate, such as
NVSDelegatefor ESP32/Arduino NVS.
NVSDelegate nvsDelegate("MyPartition");- Create a
DatabaseAPIinstance and pass the delegate to it.
DatabaseAPI database(&nvsDelegate);- Use the API to perform database operations.
char key[] = "my_key";
char value[] = "my_value";
DatabaseError_t result = database.set(key, value);
if (result == DATABASE_OK) {
// Key-Value pair successfully set.
} else {
// Handle the error.
}API
The DatabaseAPI Library provides the following classes and interfaces:
DatabaseDelegateInterface: Interface for database delegate classes.NVSDelegate: Implementation of the DatabaseDelegateInterface for ESP32/Arduino NVS.DatabaseAPIInterface: Interface for the Database API class.DatabaseAPI: Implementation of the DatabaseAPIInterface for interacting with the database.
Detailed documentation and usage examples can be found in the library source code.
Example
Here's a simple example of how to use the DatabaseAPI library to store and retrieve data from the NVS database:
#include <DatabaseAPI.hpp>
#include <NVSDelegate.hpp>
#include <DatabaseError.hpp>
// Initialize the database delegate
NVSDelegate nvsDelegate("MyPartition");
// Initialize the database API class
DatabaseAPI database(&nvsDelegate);
// Set a new key-value pair in database.
DatabaseError_t result = database.set("key", "value");
if (result == DATABASE_OK) {
// Key-Value pair successfully set.
} else {
// Handle the error.
}For a complete list of methods and error codes, refer to the class documentation in the source code.
License
This library is provided under the MIT License. You are free to use, modify, and distribute it as needed.
Contributions
If you'd like to contribute to the Database Library, please follow these guidelines:
- Fork the repository.
- Make your changes and document them.
- Submit a pull request.
Author
This library was created by Ronny Antoon. You can contact me at [ronny.antoon@gmail.com] for any questions or feedback.
Platformio Registry
https://registry.platformio.org/libraries/ronny-antoon/DatabaseAPI
For more information on the ESP-IDF's NVS API, refer to the official documentationy