Node.js Oriento driver example with error handling

910 views Asked by At

I'm new to node.js and trying to teach myself node with orientdb as the datastore. I'm using oriento as the driver.

I want to be able to handle any connection errors in my code but cannot find any examples of how to do this during the initial connection to orientdb server and connection to my database:

var express = require('express');
var oriento = require('oriento');

var server = oriento({
    host: "localhost",
    port: 2424,
    username: "root",
    password: "test"
});

How do I catch if there is connection error to the server? Later on when I want to work with a specific database in the server:

var db = server.use({
    name: 'blog',
    username: 'admin',
    password: 'admin'
});

How do I catch if there is an error in "use"ing this database?

I can sort of figure out from the oriento documentation how to handle errors during queries etc., but got stuck in these initial steps.

1

There are 1 answers

0
esengineer On

Here is how. Basically, it does auto connect to the server on the first request. For connection errors you need catch on every request.

var Oriento = require('oriento');

var server = Oriento({
    host: 'localhost',
    port: 2424,
    username: 'root',
    password: 'BDFE8AC356595663AF66ADF08E703DE30DF5755F99DE9D329EFF75A5CB8A9CE8'
});

server
    .list()
    .then(function (dbs) {
        console.log('There are ' + dbs.length + ' databases on the server.');

        var firstDB = dbs[0];

        var orientDB = server.use({
            name: firstDB.name,
            username: firstDB.username,
            password: firstDB.password
        });

        console.log('Using database: ' + orientDB.name);
    })
    .catch(function (err) {
        console.log(err);
    });