-
Notifications
You must be signed in to change notification settings - Fork 22
Tutorial
In this tutorial we assume that you already have an understanding about how web sites works, about how javascript works and that you are interested about what and how OrientDB works with nodejs.
In order to complete this tutorial, you need to have installed and working:
- nodejs: check it with the command
node -v
(on my computer it printsv0.8.16
). In order to install nodejs on your computer, follow the instructions. - expressjs: check it with the command
express -V
(on my computer it prints3.0.5
). In order to install express, typenpm install -g express
(may required root access). - OrientDB: we assume that it's running on your computer. In order to install it, follow the instuctions. This tutorial uses version 1.3.0.
First, let's make a bare minimum express js app. Open up a console/terminal and type
express --sessions tutorial
Then
cd tutorial
npm install -d
node app.js
Express will report
Express server listening on port 3000
head over here and see express working as expected.
Create an blog
database on OrientDB: you can do that using OrientDB Studio or using the OrientDB console with the command
create database local:/home/federico/materiale/works_My/orientdb-graphed-1.3.0/databases/blog admin admin local
(don't forget to adjust the path)
Edit file package.json
and add the following snippet to the list of dependencies:
"orientdb": "*"
Edit file app.js
and add this require:
orientdb = require('orientdb')
Right after it, add the following code
// previous code omitted
var dbConfig = {
user_name: 'admin',
user_password: 'admin'
};
var serverConfig = {
host: 'localhost',
port: 2424
};
var server = new orientdb.Server(serverConfig);
var db = new orientdb.GraphDb('blog', server, dbConfig);
db.open(function(err) {
if (err) {
throw err;
}
console.log("Successfully connected to OrientDB");
});
// subsequent code omitted
Go back to the terminal, interrupt node with CTRL+C
, run npm install -d
again to download OrientDB nodejs driver, then restart the app with node app.js
. You'll now see:
Express server listening on port 3000
Successfully connected to OrientDB
Well done!
Right below the console.log
statement in db.open
callback, add this snipped
routes.init(db, function(err) {
if (err) {
throw err;
}
});
Then open index.js
in routes
folder. At its top, add the line module.db = null;
. Then put the following function at the end of the file
exports.init = function(orient, callback) {
module.db = orient;
};
We are going to programmatically add some classes to our blog database, IF they are missing.
Open file index.js
in routes
folder and add the following function
function ensureSchemaIsSetup(db, callback) {
if (db.getClassByName("Post") === null) {
db.createClass("Post", "OGraphVertex", callback);
}
if (db.getClassByName("Tag") === null) {
db.createClass("Tag", "OGraphVertex", callback);
}
if (db.getClassByName("User") === null) {
db.createClass("User", "OGraphVertex", callback);
}
}
Now go the exports.init
function and below module.db
assignement, call that function so that init now looks like this:
exports.init = function(orient, callback) {
module.db = orient;
ensureSchemaIsSetup(orient, callback);
};