Skip to content

Connecting to Database

Diogo Resende edited this page Jul 30, 2013 · 16 revisions

To connect to a database, use .connect. You can pass a URL string where the scheme is a supported driver or you can pass an Object with the parameters of the connection.

var orm = require("orm");

orm.connect("mysql://root:password@localhost/test", function (err, db) {
    if (err) {
        console.log("Something is wrong with the connection", err);
        return;
    }

    // connected!
});

The callback is only called when the connection is done successfully (or unsuccessfully). You can avoid passing a callback and listen for the connect event if you prefer.

var orm = require("orm");
var db  = orm.connect("mysql://root:password@localhost/test");

orm.on("connect", function (err) {
    if (err) {
        console.log("Something is wrong with the connection", err);
        return;
    }

    // connected!
});

The connection URL has the following syntax: driver://username:password@hostname/database?option1=value1&option2=value2..

Valid options are:

  • debug (default: false): prints queries to console;
  • pool (default: false): manages a connection pool (only for mysql and postgres) using built-in driver pool;
  • strdates (default: false): saves dates as strings (only for sqlite).

Clone this wiki locally