-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better docs #25
Comments
hello @paulocoutinhox did you figured it out how to use any of the items above? (btw, também sou BR). |
Hi, Not yet :( No answer until now. |
This comment was marked as spam.
This comment was marked as spam.
Hi @rmszc81, I asked chatgpt 4.5 about it and it generate one example for each item. Only need someone to check the information if is correct or try compile it: ObjectBox C++ ExamplesMore complete and complex examples1. Relation One-to-Many#include "objectbox.hpp"
#include <vector>
class Author {
public:
int64_t id;
std::string name;
OBX_ENTITY_ID(Author)
OBX_RELATION_ONE_TO_MANY(Book, books)
};
class Book {
public:
int64_t id;
std::string title;
OBX_ENTITY_ID(Book)
OBX_RELATION_ONE_TO_MANY(Author, author)
};
void createAuthorWithBooks() {
OBX_store* store = createStore();
Author author;
author.name = "J.K. Rowling";
Book book1;
book1.title = "Harry Potter and the Philosopher's Stone";
Book book2;
book2.title = "Harry Potter and the Chamber of Secrets";
author.books.push_back(book1);
author.books.push_back(book2);
auto authorBox = obx::Box<Author>(store);
authorBox.put(author);
closeStore(store);
} 2. Relation One-to-One#include "objectbox.hpp"
class Address {
public:
int64_t id;
std::string city;
OBX_ENTITY_ID(Address)
};
class Person {
public:
int64_t id;
std::string name;
OBX_ENTITY_ID(Person)
OBX_RELATION_ONE_TO_ONE(Address, address)
};
void createPersonWithAddress() {
OBX_store* store = createStore();
Address address;
address.city = "New York";
Person person;
person.name = "John Doe";
person.address = address;
auto personBox = obx::Box<Person>(store);
personBox.put(person);
closeStore(store);
} 3. Storing Date/Time#include "objectbox.hpp"
#include <chrono>
class Event {
public:
int64_t id;
std::string name;
std::chrono::system_clock::time_point date;
OBX_ENTITY_ID(Event)
};
void createEvent() {
OBX_store* store = createStore();
Event event;
event.name = "Conference";
event.date = std::chrono::system_clock::now();
auto eventBox = obx::Box<Event>(store);
eventBox.put(event);
closeStore(store);
} 4. Storing Float/Double#include "objectbox.hpp"
class Measurement {
public:
int64_t id;
float temperature;
double pressure;
OBX_ENTITY_ID(Measurement)
};
void createMeasurement() {
OBX_store* store = createStore();
Measurement measurement;
measurement.temperature = 23.5f;
measurement.pressure = 1013.25;
auto measurementBox = obx::Box<Measurement>(store);
measurementBox.put(measurement);
closeStore(store);
} 5. More Complex Queries#include "objectbox.hpp"
class Product {
public:
int64_t id;
std::string name;
float price;
OBX_ENTITY_ID(Product)
};
void queryProducts() {
OBX_store* store = createStore();
auto productBox = obx::Box<Product>(store);
// Find products with price between 10 and 50
auto products = productBox.query().between(Product_::price, 10.0f, 50.0f).build().find();
for (const auto& product : products) {
std::cout << "Product: " << product.name << ", Price: " << product.price << std::endl;
}
closeStore(store);
} 6. Indexes#include "objectbox.hpp"
class Customer {
public:
int64_t id;
std::string email;
OBX_ENTITY_ID(Customer)
OBX_PROPERTY(email, IndexType::value)
};
void createCustomer() {
OBX_store* store = createStore();
Customer customer;
customer.email = "[email protected]";
auto customerBox = obx::Box<Customer>(store);
customerBox.put(customer);
closeStore(store);
} 7. Full-Text Search#include "objectbox.hpp"
class Article {
public:
int64_t id;
std::string content;
OBX_ENTITY_ID(Article)
OBX_PROPERTY(content, IndexType::value, OBX_PROPERTY_FLAG_FULLTEXT)
};
void searchArticles() {
OBX_store* store = createStore();
auto articleBox = obx::Box<Article>(store);
// Full-text search for "objectbox"
auto articles = articleBox.query().contains(Article_::content, "objectbox").build().find();
for (const auto& article : articles) {
std::cout << "Article: " << article.content << std::endl;
}
closeStore(store);
} 8. Queries with Date/Time#include "objectbox.hpp"
#include <chrono>
class LogEntry {
public:
int64_t id;
std::string message;
std::chrono::system_clock::time_point timestamp;
OBX_ENTITY_ID(LogEntry)
};
void queryLogEntries() {
OBX_store* store = createStore();
auto logBox = obx::Box<LogEntry>(store);
// Find logs within the last 24 hours
auto now = std::chrono::system_clock::now();
auto one_day_ago = now - std::chrono::hours(24);
auto logs = logBox.query().greater(LogEntry_::timestamp, one_day_ago).build().find();
for (const auto& log : logs) {
std::cout << "Log: " << log.message << std::endl;
}
closeStore(store);
} 9. Date/Time are Stored as UTC Timestamp?Yes, ObjectBox stores date/time as UTC timestamps. 10. Working with Multi-thread#include "objectbox.hpp"
#include <thread>
void writeData(obx::Box<MyEntity>& box) {
MyEntity entity;
entity.name = "Test";
box.put(entity);
}
void multiThreadExample() {
OBX_store* store = createStore();
auto box = obx::Box<MyEntity>(store);
std::thread t1(writeData, std::ref(box));
std::thread t2(writeData, std::ref(box));
t1.join();
t2.join();
closeStore(store);
} 11. Recovery After File Corruption (if it exists)ObjectBox does not have built-in recovery for corrupted files, but you can implement periodic backups and use those for recovery. #include "objectbox.hpp"
#include <filesystem>
#include <iostream>
void backupDatabase() {
std::filesystem::copy("data.mdb", "data_backup.mdb", std::filesystem::copy_options::overwrite_existing);
}
void restoreDatabase() {
std::filesystem::copy("data_backup.mdb", "data.mdb", std::filesystem::copy_options::overwrite_existing);
}
void exampleUsage() {
try {
OBX_store* store = createStore();
// Perform operations
closeStore(store);
} catch (...) {
std::cerr << "Database error occurred. Restoring from backup." << std::endl;
restoreDatabase();
}
} Can you check? |
Hi,
I think that you can improve the docs with these topics:
Thanks.
The text was updated successfully, but these errors were encountered: