Skip to content

Latest commit

 

History

History
97 lines (66 loc) · 3.08 KB

naomi.md

File metadata and controls

97 lines (66 loc) · 3.08 KB

Naomi singleton

Naomi itself is a singleton. It's what you get when you load "naomi" into Node.js, i.e.

var naomi = require('naomi');

Naomi is extensible. It allows registering external database engines to handle different database types, e.g.

var naomi = require('naomi');
var mysql = require('naomi-mysql');

naomi.register('mysql', mysql);

Once you have a database engine registered, you may use it to create a database instance, e.g.

var db = naomi.create('mysql', {
  // connection properties
});

Table of Contents

Methods

#register(identifier, engine)

Registers the supplied database engine under the designated identifier.

Arguments
  1. identifier (string) database engine identifier, e.g. "mysql", "postgres".
  2. engine (Class<Database>) a Naomi Database.
Example
var naomi = require('naomi');
var mysql = require('naomi-mysql');

// register mysql database class under "foobar"
naomi.register('foobar', mysql);
Note

The engine id is merely a string; you can call it whatever you want, e.g. "foobar" or "quux". It makes more sense, however, to name it according to the underlying database engine, e.g. "mysql" for MySQL.

#create(identifier, [connectionProperties]) -> Database

Creates and returns a new Database instance of the designated engine.

Arguments
  1. identifier (string) database engine identifier, e.g. "mysql", "postgres".
  2. connectionProperties (Object) connection properties specific to the database engine (optional).
MySQL-specific arguments
  • connectionProperties.database (string) the name of the database schema
  • connectionProperties.host (string) the hostname of the database server (optional, defaults to "localhost")
  • connectionProperties.port (number) the port of the database server (optional, defaults to 3306)
  • connectionProperties.user (string) the user to access the database (optional, defaults to "root")
  • connectionProperties.password (string) ] the password to access the database (optional, defaults to "" - empty string)
  • connectionProperties.connectionLimit (number) maximum number of connections to maintain in the internal pool (optional, defaults to 10)
Returns

Returns a new Database instance.

Example
var naomi = require('naomi');
var mysql = require('naomi-mysql');

naomi.register('mysql', mysql);

var db = naomi.create('mysql', {
  host: process.env.MYSQL_HOST,
  port: parseInt(process.env.MYSQL_PORT, 10),
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASSWORD,
  database: process.env.MYSQL_DATABASE
});

#database(identifier, [connectionProperties]) -> Database

Alias of naomi#create().