Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Connecting to CUBRID through Node.js Driver


Once everything is in place, let’s see how we write a simple program which does only one thing – connects to a CUBRID database. For simplicity, let’s assume we want to connect to a local CUBRID installation, in particular to the well-known demodb database.

Callback pattern

And this is how you do it, using the node-cubrid driver:

var CUBRID = require('node-cubrid');
var CUBRIDClient = CUBRID.createCUBRIDConnection('localhost', 33000, 'public', '', 'demodb');

CUBRIDClient.connect(function (err) {
  if (err) {
    errorHandler(err);
  } else {
    CUBRIDClient.close(function (err) {
      if (err) {
        errorHandler(err);
      }
    });
  }
});

Note how the database connect information is being initialized within the new call.

Events pattern

If you prefer using events, this is how you can achieve the same results as above:

var CUBRID = require('node-cubrid');
var CUBRIDClient = CUBRID.createCUBRIDConnection('localhost', 33000, 'public', '', 'demodb');

CUBRIDClient.connect();

CUBRIDClient.on(CUBRIDClient.EVENT_ERROR, function (err) {
  throw err.message;
});

CUBRIDClient.on(CUBRIDClient.EVENT_CONNECTED, function () {
  console.log('Connection opened.');
  CUBRIDClient.query('select * from game');
});

CUBRIDClient.on(CUBRIDClient.EVENT_QUERY_DATA_AVAILABLE, function (result, queryHandle) {
  CUBRIDClient.closeQuery(queryHandle, function () {
    
  });
});

CUBRIDClient.on(CUBRIDClient.EVENT_QUERY_CLOSED, function (queryHandle) {
  CUBRIDClient.close();
});

CUBRIDClient.on(CUBRIDClient.EVENT_CONNECTION_CLOSED, function () {
  console.log('Connection closed');
});

As you can see, it is very easy to work with the driver functions model – they follow the standard database logic model you know from other database drivers. The only particular thing, which is specific to node.js, is the callbacks/events model. If you need or want to read more about it, we recommend starting with the following online resources:

comments powered by Disqus
Page info
viewed 1444 times
translations en
Author
posted 2 years ago by
Esen Sagynov
Contributors
updated 2 years ago by
View revisions
tagged
Share this article