Node.js example
This example shows a small node.js program which defines two classes, product and purchase. You can add objects to the database by running it from the command line with different options. The example shows how to create a server, user and database, and how to run the Node.js code. It also shows how to use the dboo command line tool to run search queries on the database.
Code
1const dboo = require('dboo');
2
3let host = "localhost";
4let port = 8822;
5let database = "example";
6let user = "me";
7let password = "123";
8
9// An example class product, which defines purchasable products.
10class product
11{
12 _name;
13
14 constructor(name = "") {
15 this._name = name;
16 }
17};
18dboo.class(product, [{_name: dboo.string}]);
19
20// An example class purchase, which defines actual purchased of a product.
21class purchase
22{
23 constructor(prod, quantity) {
24 this._product = prod;
25 this._quantity = quantity;
26 }
27
28 _product;
29 _quantity;
30};
31
32dboo.class(purchase, [{_product: product},{_quantity: dboo.double}]);
33
34dboo.init();
35
36const args = process.argv;
37
38if ( args.length == 2 ) {
39 console.log("Usage: ");
40 console.log(" shopping init");
41 console.log(" shopping product <product name>");
42 console.log(" shopping purchase <product name> <quantity>");
43 return 0;
44}
45
46
47try {
48
49 let odb = new dboo.ODB();
50 odb.connect(host, port, database, user, password);
51
52 if ( (args.length == 3) && args[2] == "init") {
53 // First argument == "init":
54
55 // Define product and purchase classes for dboo.
56 // This creates indices and allows for searching on the classes
57 odb.define(product);
58 odb.define(purchase);
59
60 } else if (args.length == 4 && args[2] == "product") {
61 // First argument == "product", create a product:
62
63 let products = [];
64 let prod_name = args[3];
65
66 // First check if product exist, search on product name
67 odb.query(products, "select<product>(eq(_name, '" + prod_name + "'))");
68
69 if (products.length == 0) {
70 // No results from search, so we can add this product
71
72 let prod = new product(prod_name);
73
74 // Commit it the new product to the database
75 odb.commit(prod);
76
77 } else {
78 console.log("Product " + prod_name + " already exist!");
79 }
80
81 } else if (args.length == 5 && args[2] == "purchase") {
82 // First argument == purchase, create a purchase object
83
84 // Using unique_ptr here, however that is not always possible, depending on
85 // how other objects links to this and what life span is expected.
86 let products = [];
87 let prod_name = args[3];
88
89 // First check if product exist, search on product name
90 odb.query(products, "select<product>(eq(_name, '" + prod_name + "'));");
91
92 if (products.length != 0) {
93 // Product exist, so we can purchase items
94
95 // convert last argument to double (no checking here, just assume it works...)
96 let quantity = parseFloat(args[4]);
97
98 // Make a purchase
99 let trade = new purchase(products[0], quantity);
100
101 // Commit it the new trade to the database
102 odb.commit(trade);
103
104 } else {
105 console.log("No product named " + prod_name);
106 }
107 }
108
109} catch (e) {
110 console.log(e);
111}
Set up environment
Ensure the dboo node module is installed:
> npm init
> npm install dboo
Running DBOO
DBOO is a server/client application that can manage any number of databases. You will need to create a server, start the server, create a database. dbood is the server process, normally run in daemon mode. dboo is a management tool with which you create users, databases etc.
# Create a server in your home directory (~/.dboo/dboo.cfg and server directory ~/.dboo/server)
> dbood create
Creating server
Base directory for databases: /Users/tomas/.dboo/databases
Log directory: /Users/tomas/.dboo/log
Server directory: /Users/tomas/.dboo/server
Host name: localhost
Listening port: 8822
Server started from '/Users/tomas/.dboo/server': localhost:8822.
Shutting down, tidying up.
Shutting down, exiting.
# Start the server in daemon mode
> dbood -d
Next step is to create a database and user. This is shown here using interactive mode:
# Create a database
> dboo -u=root -p=
> dboo root@dboo::server> create database example --groups=example
Created database 'example'
dboo root@dboo::server>
# Create a user
dboo root@dboo::server> add user me --groups=example --password=123
Created user 'me'
dboo root@dboo::server> exit
Try the example program
To run the example program, first initialize:
> node example.js init
Create some products:
> node example.js product milk
> node example.js product sugar
> node example.js product bread
> node example.js product tea
> node example.js product water
Use the dboo tool to search for data, using batch mode:
> dboo example -u=me -p=123 -- "select<product>()"
{"dboo::objectid" : "3600807991f00014", "dboo::class" : "product", "_name" : "bread"}
{"dboo::objectid" : "5c00807991f80014", "dboo::class" : "product", "_name" : "tea"}
{"dboo::objectid" : "680080798adc0014", "dboo::class" : "product", "_name" : "milk"}
{"dboo::objectid" : "c40080798e580014", "dboo::class" : "product", "_name" : "sugar"}
{"dboo::objectid" : "ca00807b5a900014", "dboo::class" : "product", "_name" : "water"}
Make some purchases:
> node example.js purchase milk 10
> node example.js purchase sugar 2
> node example.js purchase tea 2
> node example.js purchase bread 3
> node example.js purchase bread 2
> node example.js purchase milk 1
> node example.js purchase water 2
> node example.js purchase water 3
Select all purchase objects from the database:
> dboo example -u=me -p=123 -- "select<purchase>()"
{"dboo::objectid" : "5000807b5b340017", "dboo::class" : "purchase", "_product" : "ca00807b5a900014", "_quantity" : 3}
{"dboo::objectid" : "5c00807993840017", "dboo::class" : "purchase", "_product" : "680080798adc0014", "_quantity" : 10}
{"dboo::objectid" : "6b00807993e00017", "dboo::class" : "purchase", "_product" : "680080798adc0014", "_quantity" : 1}
{"dboo::objectid" : "9800807993a40017", "dboo::class" : "purchase", "_product" : "c40080798e580014", "_quantity" : 2}
{"dboo::objectid" : "c400807993cc0017", "dboo::class" : "purchase", "_product" : "3600807991f00014", "_quantity" : 3}
{"dboo::objectid" : "ca00807b5b280017", "dboo::class" : "purchase", "_product" : "ca00807b5a900014", "_quantity" : 2}
{"dboo::objectid" : "d100807993c00017", "dboo::class" : "purchase", "_product" : "5c00807991f80014", "_quantity" : 2}
{"dboo::objectid" : "e000807993d40017", "dboo::class" : "purchase", "_product" : "3600807991f00014", "_quantity" : 2}
Select all purchase objects and include all referenced objects in the result:
> dboo example -u=me -p=123 -- "select<purchase>()" --fetch_referenced_objects
{"dboo::objectid" : "3600807991f00014", "dboo::class" : "product", "_name" : "bread"}
{"dboo::objectid" : "5000807b5b340017", "dboo::class" : "purchase", "_product" : "ca00807b5a900014", "_quantity" : 3}
{"dboo::objectid" : "5c00807991f80014", "dboo::class" : "product", "_name" : "tea"}
{"dboo::objectid" : "5c00807993840017", "dboo::class" : "purchase", "_product" : "680080798adc0014", "_quantity" : 10}
{"dboo::objectid" : "680080798adc0014", "dboo::class" : "product", "_name" : "milk"}
{"dboo::objectid" : "6b00807993e00017", "dboo::class" : "purchase", "_product" : "680080798adc0014", "_quantity" : 1}
{"dboo::objectid" : "9800807993a40017", "dboo::class" : "purchase", "_product" : "c40080798e580014", "_quantity" : 2}
{"dboo::objectid" : "c40080798e580014", "dboo::class" : "product", "_name" : "sugar"}
{"dboo::objectid" : "c400807993cc0017", "dboo::class" : "purchase", "_product" : "3600807991f00014", "_quantity" : 3}
{"dboo::objectid" : "ca00807b5a900014", "dboo::class" : "product", "_name" : "water"}
{"dboo::objectid" : "ca00807b5b280017", "dboo::class" : "purchase", "_product" : "ca00807b5a900014", "_quantity" : 2}
{"dboo::objectid" : "d100807993c00017", "dboo::class" : "purchase", "_product" : "5c00807991f80014", "_quantity" : 2}
{"dboo::objectid" : "e000807993d40017", "dboo::class" : "purchase", "_product" : "3600807991f00014", "_quantity" : 2}
Enter interactive mode and select purchases of product ‘water’:
dboo example -u=me -p=123
dboo me@example> select<purchase>(in(_product, select<product>(eq(_name,'water'))))
{"dboo::objectid" : "5000807b5b340017", "dboo::class" : "purchase", "_product" : "ca00807b5a900014", "_quantity" : 3}
{"dboo::objectid" : "ca00807b5b280017", "dboo::class" : "purchase", "_product" : "ca00807b5a900014", "_quantity" : 2}