lib/schema.js
function Schema(name, settings) {
    var schema = this;

    name = name ? name.toLowerCase() : '';
    switch (name) {
        case 'sqlite':
            name = 'sqlite3';
            break;
        case 'mysqldb':
        case 'mariadb':
            name = 'mysql';
            break;
        case 'mongo':
            name = 'mongodb';
            break;
        case 'couchdb':
        case 'couch':
            name = 'nano';
            break;
        case 'rethinkdb':
        case 'rethink':
            name = 'rethinkdb';
            break;
    }

    // just save everything we get
    schema.name = name;
    schema.settings = settings;

    // Disconnected by default
    schema.connected = false;

    // create blank models pool
    schema.models = {};
    schema.definitions = {};

    // define schema types
    schema.Text = function Text() {
    };
    schema.JSON = schema.Json = function JSON() {
    };
    schema.Float = function Float() {
    };
    schema.Real = schema.Double = function Real() {
    };
    schema.Int = schema.Integer = function Integer() {
    };
    schema.UUID = schema.Uuid = function UUID() {
    };
    schema.TimeUUID = schema.TimeUuid = function TimeUUID() {
    };
    schema.CounterColumn = function CounterColumn() {
    };
    schema.Blob = schema.Bytes = function Blob() {
    };

    schema.Date = schema.Timestamp = Date;
    schema.Boolean = schema.Tinyint = Boolean;
    schema.Number = Number;
    schema.String = schema.Varchar = String;
    // and initialize schema using adapter
    // this is only one initialization entry point of adapter
    // this module should define `adapter` member of `this` (schema)
    var adapter;
    if (typeof name === 'object') {
        adapter = name;
        schema.name = adapter.name;
    } else if (name.match(/^\//)) {
        // try absolute path
        adapter = require(name);
    } else if (existsSync(__dirname + '/adapters/' + name + '.js')) {
        // try built-in adapter
        adapter = require('./adapters/' + name);
    } else {
        try {
            adapter = require('caminte-' + name);
        } catch (e) {
            throw new Error('Adapter ' + name + ' is not defined, try\n  npm install ' + name);
        }
    }

    adapter.initialize(schema, function () {

        // we have an adaper now?
        if (!schema.adapter) {
            throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema');
        }

        schema.adapter.log = function (query, start) {
            schema.log(query, start);
        };

        schema.adapter.logger = function (query) {
            'use strict';
            var t1 = Date.now();
            var log = schema.log;
            return function (q) {
                log(q || query, t1);
            };
        };
        var res = getState(schema);
        if (util.isError(res)) {
            schema.emit('error', res);
        } else {
            schema.connected = true;
            schema.emit('connected');
        }
    }.bind(schema));
}
function Schema(name, settings) {
    var schema = this;

    name = name ? name.toLowerCase() : '';
    switch (name) {
        case 'sqlite':
            name = 'sqlite3';
            break;
        case 'mysqldb':
        case 'mariadb':
            name = 'mysql';
            break;
        case 'mongo':
            name = 'mongodb';
            break;
        case 'couchdb':
        case 'couch':
            name = 'nano';
            break;
        case 'rethinkdb':
        case 'rethink':
            name = 'rethinkdb';
            break;
    }

    // just save everything we get
    schema.name = name;
    schema.settings = settings;

    // Disconnected by default
    schema.connected = false;

    // create blank models pool
    schema.models = {};
    schema.definitions = {};

    // define schema types
    schema.Text = function Text() {
    };
    schema.JSON = schema.Json = function JSON() {
    };
    schema.Float = function Float() {
    };
    schema.Real = schema.Double = function Real() {
    };
    schema.Int = schema.Integer = function Integer() {
    };
    schema.UUID = schema.Uuid = function UUID() {
    };
    schema.TimeUUID = schema.TimeUuid = function TimeUUID() {
    };
    schema.CounterColumn = function CounterColumn() {
    };
    schema.Blob = schema.Bytes = function Blob() {
    };

    schema.Date = schema.Timestamp = Date;
    schema.Boolean = schema.Tinyint = Boolean;
    schema.Number = Number;
    schema.String = schema.Varchar = String;
    // and initialize schema using adapter
    // this is only one initialization entry point of adapter
    // this module should define `adapter` member of `this` (schema)
    var adapter;
    if (typeof name === 'object') {
        adapter = name;
        schema.name = adapter.name;
    } else if (name.match(/^\//)) {
        // try absolute path
        adapter = require(name);
    } else if (existsSync(__dirname + '/adapters/' + name + '.js')) {
        // try built-in adapter
        adapter = require('./adapters/' + name);
    } else {
        try {
            adapter = require('caminte-' + name);
        } catch (e) {
            throw new Error('Adapter ' + name + ' is not defined, try\n  npm install ' + name);
        }
    }

    adapter.initialize(schema, function () {

        // we have an adaper now?
        if (!schema.adapter) {
            throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema');
        }

        schema.adapter.log = function (query, start) {
            schema.log(query, start);
        };

        schema.adapter.logger = function (query) {
            'use strict';
            var t1 = Date.now();
            var log = schema.log;
            return function (q) {
                log(q || query, t1);
            };
        };
        var res = getState(schema);
        if (util.isError(res)) {
            schema.emit('error', res);
        } else {
            schema.connected = true;
            schema.emit('connected');
        }
    }.bind(schema));
}
function Schema ( name , settings ) { var schema = this ; name = name ? name . toLowerCase ( ) : '' ; switch ( name ) { case 'sqlite' : name = 'sqlite3' ; break ; case 'mysqldb' : case 'mariadb' : name = 'mysql' ; break ; case 'mongo' : name = 'mongodb' ; break ; case 'couchdb' : case 'couch' : name = 'nano' ; break ; case 'rethinkdb' : case 'rethink' : name = 'rethinkdb' ; break ; } // just save everything we get schema . name = name ; schema . settings = settings ; // Disconnected by default schema . connected = false ; // create blank models pool schema . models = { } ; schema . definitions = { } ; // define schema types schema . Text = function Text ( ) { } ; schema . JSON = schema . Json = function JSON ( ) { } ; schema . Float = function Float ( ) { } ; schema . Real = schema . Double = function Real ( ) { } ; schema . Int = schema . Integer = function Integer ( ) { } ; schema . UUID = schema . Uuid = function UUID ( ) { } ; schema . TimeUUID = schema . TimeUuid = function TimeUUID ( ) { } ; schema . CounterColumn = function CounterColumn ( ) { } ; schema . Blob = schema . Bytes = function Blob ( ) { } ; schema . Date = schema . Timestamp = Date ; schema . Boolean = schema . Tinyint = Boolean ; schema . Number = Number ; schema . String = schema . Varchar = String ; // and initialize schema using adapter // this is only one initialization entry point of adapter // this module should define `adapter` member of `this` (schema) var adapter ; if ( typeof name === 'object' ) { adapter = name ; schema . name = adapter . name ; } else if ( name . match ( / ^\/ / ) ) { // try absolute path adapter = require ( name ) ; } else if ( existsSync ( __dirname + '/adapters/' + name + '.js' ) ) { // try built-in adapter adapter = require ( './adapters/' + name ) ; } else { try { adapter = require ( 'caminte-' + name ) ; } catch ( e ) { throw new Error ( 'Adapter ' + name + ' is not defined, try\n  npm install ' + name ) ; } } adapter . initialize ( schema , function ( ) { // we have an adaper now? if ( ! schema . adapter ) { throw new Error ( 'Adapter is not defined correctly: it should create `adapter` member of schema' ) ; } schema . adapter . log = function ( query , start ) { schema . log ( query , start ) ; } ; schema . adapter . logger = function ( query ) { 'use strict' ; var t1 = Date . now ( ) ; var log = schema . log ; return function ( q ) { log ( q || query , t1 ) ; } ; } ; var res = getState ( schema ) ; if ( util . isError ( res ) ) { schema . emit ( 'error' , res ) ; } else { schema . connected = true ; schema . emit ( 'connected' ) ; } } . bind ( schema ) ) ; }
Schema - adapter-specific classes factory.

All classes in single schema shares same adapter type and
one database connection

@param name - type of schema adapter (mysql, mongoose, sequelize, redis)
@param settings - any database-specific settings which we need to
establish connection (of course it depends on specific adapter)

- host
- port
- username
- password
- database
- debug {Boolean} = false

@example Schema creation, waiting for connection callback
```
var schema = new Schema('mysql', { database: 'myapp_test' });
schema.define(...);
schema.on('connected', function () {
// work with database
});
```
Schema - adapter - specific classes factory .