Open paulocoutinhox opened 2 years ago
hello @paulocoutinhox
did you figured it out how to use any of the items above? I just discovered object-box so, I'm trying to find it out yet the best practices. for C# and .NET, I'm using LiteDB, from @mbdavid
(btw, também sou BR).
Hi,
Not yet :(
No answer until now.
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:
#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);
}
#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);
}
#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);
}
#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);
}
#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);
}
#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 = "customer@example.com";
auto customerBox = obx::Box<Customer>(store);
customerBox.put(customer);
closeStore(store);
}
#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);
}
#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);
}
Yes, ObjectBox stores date/time as UTC timestamps.
#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);
}
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.