Connecting to DB

Omar Sharim edited this page Jun 3, 2015 · 17 revisions

Back to Overview

Connecting to DB

Config parameters

  • object config: can contain any of the following optional properties
    • string driver:
      • default value: null
      • adapter to use when connecting to database server
    • string user:
      • default value: process.env.USER
      • Database user
    • string database:
      • default value: process.env.USER
      • database to use when connecting to database server
    • string password:
      • default value: null
      • user's password for database server
    • number port:
      • default value: depends by adapter
      • port to use when connecting to database server
    • string host:
      • default value: null
      • host address of database server
      • used to initialize underlying net.Stream()
    • bool pool:
      • default value: false
      • used pooling connections to connect to server
    • bool ssl:
      • default value: false
      • whether to try SSL/TLS to connect to server

First, we need to define a connection.

MySQL/MariaDB

For MySQL database need install mysql client. Then:

$ npm install mysql --save
    var caminte = require('caminte'),
    Schema = caminte.Schema,
    config = {
         driver     : "mysql", // mariadb
         host       : "localhost",
         port       : "3306",
         username   : "test",
         password   : "test",
         database   : "test",
         pool       : true // optional for use pool directly 
    };

    var schema = new Schema(config.driver, config);

SQLite 3

For SQLite database need install sqlite3 client. Then:

$ npm install sqlite3 --save
    var caminte = require('caminte'),
    Schema = caminte.Schema,
    config = {
         driver     : "sqlite3",
         database   : "/db/mySite.db"
    };

    var schema = new Schema(config.driver, config);

PostgreSQL

For PostgreSQL database need install postgres client. Then:

$ npm install pg --save
    var caminte = require('caminte'),
    Schema = caminte.Schema,
    config = {
         driver     : "postgres",
         host       : "localhost",
         port       : "5432",
         username   : "test",
         password   : "test",
         database   : "test",
         pool       : true // optional for use pool directly 
    };

    var schema = new Schema(config.driver, config);

Redis

For Redis database need install redis client. Then:

$ npm install redis --save
    var caminte = require('caminte'),
    Schema = caminte.Schema,
    config = {
         driver     : "redis",
         host       : "localhost",
         port       : "6379"
    };

    var schema = new Schema(config.driver, config);