Overview
Steps to use DBOO:
→ Declare class members and inheritance structures (C++ cls, cls::member(), cls::base(), Node.js dboo.class()).
→ Call dboo::init() to initialize the DBOO API.
→ Call odb::connect() to connect to a database.
→ Call odb::define() to let the database know about your classes (once).
→ Store and retrieve objects (odb::commit(), odb.select() etc).
1. Declare class members
Depending on the programming language, the method of registering the classes is differs. C++ requires pointer to member and the name, while Node.js needs name and data type.
#include "dboo/rfx.h"
#include <string>
using namespace std;
class AddressCard
{
public:
string Name;
string Street;
int Zip;
string Country;
time_t DateOfBirth;
public:
AddressCard() : Zip(0) {}
static void dboo_class_init(dboo::cls<AddressCard>& c) { 1)
c.member(&AddressCard::Name, "Name");
c.member(&AddressCard::Street, "Street");
c.member(&AddressCard::Zip, "Zip");
c.member(&AddressCard::Country, "Country");
c.member(&AddressCard::DateOfBirth, "DateOfBirth");
}
static inline dboo::cls<AddressCard> cAddressCard("AddressCard"); 2)
};
const dboo = require('dboo');
class AddressCard {
Name = "";
Street = "";
Zip = 0;
Country = "";
DateOfBirth = "";
constructor() {
this.Name = "";
this.Street = "";
this.Zip = 0;
this.Country = "";
this.DateOfBirth = "";
}
};
dboo.class(AddressCard, 3)
[{"Name": dboo.string},
{"Street": dboo.string},
{"Zip": dboo.int64},
{"Country": dboo.string},
{"DateOfBirth": dboo.int64},
]
);
Notes
- 1
Class initialization function; used for declaring members and base classes.
- 2
Class object declaration.
- 3
In Node.js are both class and member declaration done with the
dboo.class()
function.
The supported data types are listed in Datatypes with examples.
2. Call dboo::init()
The dboo::init() function must be called before a client can connect to a server. This will register all meembers and the inheritance structure of all classes.
#include "AddressCard.h"
#include "AddressBook.h"
#include <dboo/rfx.h>
int main() {
dboo::init();
}
const dboo = require('dboo');
const card = require('./AddressCard.js');
const book = require('./AddressBook.js');
// Must be called after all types and dboo.class functions been called:
dboo.init();
3. Call odb.connect()
The dboo::odb class is the main interface to DBOO. You connect to a database with the dboo::odb::constructor or by calling the dboo::odb::connect function:
#include "AddressCard.h"
#include "AddressBook.h"
#include <dboo/odb.h>
void init_db_classes() {
dboo::odb db{host, port, user, pwd, dbname};
}
const dboo = require('dboo');
const card = require('./AddressCard.js');
const book = require('./AddressBook.js');
function init_db_classes() {
let db = new dboo.odb();
db.connect(host, port, user, pwd, dbname);
}
4. Call odb.define()
The call to dboo::define() is only needed when declaring the classes for DBOO, typically only once in an initialization routine, and when changes to the classes have been made.
#include "AddressCard.h"
#include "AddressBook.h"
#include <dboo/odb.h>
void init_db_classes() {
dboo::odb db{host, port, user, pwd, dbname};
db.define<AddressCard>():
db.define<AddressBook>():
}
const dboo = require('dboo');
const card = require('./AddressCard.js');
const book = require('./AddressBook.js');
function init_db_classes() {
let db = new dboo.odb();
db.connect(host, port, user, pwd, dbname);
db.define(AddressCard):
db.define(AddressBook):
}
5. Store and retrieve objects
Use the functions dboo::odb::commit(), dboo::odb::select() and dboo::odb::query() to store and retrieve objects. For query API, see Query language.
#include "AddressCard.h"
#include "AddressBook.h"
#include <dboo/odb.h>
void init_db_classes() {
dboo::odb db{host, port, user, pwd, dbname};
vector<AddressCard*> cards;
vector<AddressBook*> books;
db.select<AddressCard>(cards);
db.select<AddressBook>(books);
}
const dboo = require('dboo');
const card = require('./AddressCard.js');
const book = require('./AddressBook.js');
function init_db_classes() {
let db = new dboo.odb();
db.connect(host, port, user, pwd, dbname);
let cards = [];
let books = [];
db.query(cards, "select<AddressCard>()");
db.query(books, "select<AddressBook>()");
}