A singleton class that acts as the entry point for all actions performed in patio.

Example
var patio = require("patio");

patio.createConnection(....);

patio.camelize = true;
patio.quoteIdentifiers=false;
patio.parseInt8=false
patio.defaultPrimaryKeyType = "integer" //"bigint"

patio.createModel("my_table");


 //CHANGING IDENTIFIER INPUT METHOD


 //use whatever is passed in
  patio.identifierInputMethod = null;
 //convert to uppercase
 patio.identifierInputMethod = "toUpperCase";
 //convert to camelCase
 patio.identifierInputMethod = "camelize";
 //convert to underscore
 patio.identifierInputMethod = "underscore";


 //CHANGING IDENTIFIER OUTPUT METHOD

 //use whatever the db returns
  patio.identifierOutputMethod = null;
 //convert to uppercase
 patio.identifierOutputMethod = "toUpperCase";
 //convert to camelCase
 patio.identifierOutputMethod = "camelize";
 //convert to underscore
 patio.identifierOutputMethod = "underscore";

 //TURN QUOTING OFF
  patio.quoteIdentifiers = false
            
Extends Static Properties
PropertyTypeDefault ValueDescription
patio.Time

Mixin that provides time formatting/coversion functions.

Instance Properties
PropertyTypeDefault ValueDescription
DATABASESpatio.Database[] []

An array of databases that are currently connected.

LOGGERLogger comb.logger("patio")

Returns the root comb logger using this logger you can set the levels add appenders etc.

camelizefunction

Sets the whether or not to camelize identifiers coming from the database and to underscore identifiers when sending identifiers to the database. Setting this property to true has the same effect as:

    patio.identifierOutputMethod = "camelize";
    patio.identifierInputMethod = "underscore";
defaultDatabasepatio.Database null

Returns the default database. This is the first database created using patio#connect.

defaultPrimaryKeyTypeString

Set the default primary key type when not specified for all databases by default. By default, patio uses "integer".

identifierInputMethodString

Set the method to call on identifiers going into the database. This affects how identifiers are sent to the database. So if you use camelCased and the db identifiers are all underscored use camelize. The method can include

  • toUpperCase
  • toLowerCase
  • camelize
  • underscore
  • Other String instance method names.

patio uses toUpperCase identifiers in all SQL strings for most databases.

identifierOutputMethodString

Set the method to call on identifiers coming out of the database. This affects the how identifiers are represented by calling the method on them. The method can include

  • toUpperCase
  • toLowerCase
  • camelize
  • underscore
  • Other String instance method names.
most database implementations in patio use toLowerCase

parseInt8Boolean

Sets whether bigint types should be parsed to a number. An error will be thrown if set and the number is set and the number is greater than 2^53 or less than -2^53.

quoteIdentifiersBoolean

Set whether to quote identifiers for all databases by default. By default, patio quotes identifiers in all SQL strings.

underscorefunction

Sets the whether or not to underscore identifiers coming from the database and to camelize identifiers when sending identifiers to the database. Setting this property to true has the same effect as:

    patio.identifierOutputMethod = "underscore";
    patio.identifierInputMethod = "camelize";

Constructor

Defined index.js Source
function (){
   this._super(arguments);
   var constants = SQL.Constants;
   for (var i in constants) {
       this[i] = constants[i];
   }
           
}
            

addModel Function Public


Defined index.js

This method is used to create a patio.Model object.

Example
var Flight = patio.addModel("flight", {
     instance:{
             toObject:function () {
                 var obj = this._super(arguments);
                 obj.weekdays = this.weekdaysArray;
                 obj.legs = this.legs.map(function (l) {
                     return l.toObject();
                 });
                 return obj;
             },

             _setWeekdays:function (weekdays) {
                 this.weekdaysArray = weekdays.split(",");
                 return weekdays;
             }
     },

     static:{

         init:function () {
             this.oneToMany("legs", {
                 model:"flightLeg",
                 orderBy:"scheduledDepartureTime",
                 fetchType:this.fetchType.EAGER
             });
         },

         byAirline:function (airline) {
             return this.filter({airline:airline}).all();
         },

         arrivesAt:function (airportCode) {
             return this.join(this.flightLeg.select("flightId").filter({arrivalCode:airportCode}).distinct(), {flightId:"id"}).all();
         },

         departsFrom:function (airportCode) {
             return this.join(this.flightLeg.select("flightId").filter({departureCode:airportCode}).distinct(), {flightId:"id"}).all();
         },

         getters:{
             flightLeg:function () {
                 if (!this.__flightLeg) {
                     this.__flightLeg = this.patio.getModel("flightLeg");
                 }
                 return this.__flightLeg;
             }
         }
     }
 });
        
Arguments Source
function (table,supers,proto){
   return model.create.apply(model, arguments);
           
}
    

configureLogging Function Public


Defined index.js

This can be used to configure logging. If a options hash is passed in then it will passed to the comb.logging.PropertyConfigurator. If the options are omitted then a ConsoleAppender will be added and the level will be set to info.

Example
var config = {
    "patio" : {
        level : "INFO",
        appenders : [
            {
                type : "RollingFileAppender",
                file : "/var/log/patio.log",
            },
            {
                type : "RollingFileAppender",
                file : "/var/log/patio-error.log",
                name : "errorFileAppender",
                level : "ERROR"
            }
        ]
};

patio.configureLogging(config);
        
Arguments Source
function (opts){
   comb.logger.configure(opts);
   if (!opts) {
       LOGGER.level = "info";
   }
           
}
    

connect Function Public


Defined index.js

Source
function (){
   return this.createConnection.apply(this, arguments);
           
}
    

createConnection Function Public


Defined index.js

Returns a patio.Database object that can be used to for querying.

This method is the entry point for all interactions with a database including getting patio.Datasets for creating queries(see patio.Database#from).

The patio.Database returned can also be used to create( patio.Database#createTable), alter(@link patio.Database#alterTable}), rename( patio.Database#renameTable), and drop( patio.Database#dropTable) as well as many other patio.Database actions.

Example
//connect using an object
var DB = patio.createConnection({
             host : "127.0.0.1",
             port : 3306,
             type : "mysql",
             maxConnections : 1,
             minConnections : 1,
             user : "test",
             password : "testpass",
             database : 'test'
});
//connect using a connection string
var CONNECT_STRING = "mysql://test:testpass@localhost:3306/test?maxConnections=1&minConnections=1";
var DB = patio.createConnection(CONNECT_STRING);

//...do something
DB.createTable("myTable", function(){
   this.name("text");
   this.value("integer");
}).chain(function(){
    //tables created!!!
});
        
Arguments Source
function (options){
   var ret = Database.connect(options);
   this.emit("connect", ret);
   return ret;
           
}
    

disconnect Function Public


Defined index.js

Disconnects all databases in use.

Arguments Returns Source
function (cb){
   var ret = Database.disconnect(cb), self = this;
   ret.classic(function (err) {
       if (err) {
           self.emit("error", err);
       } else {
           self.emit("disconnect");
       }
   });
   return ret.promise();
           
}
    

getModel Function Public


Defined index.js

Returns a model from the name of the table for which the model was created.


var TestModel = patio.addModel("test_model").sync(function(err){
     if(err){
        console.log(err.stack);
     }else{
           var TestModel = patio.getModel("test_model");
     }
});

If you have two tables with the same name in different databases then you can use the db parameter also.



var DB1 = patio.createConnection("mysql://test:testpass@localhost:3306/test_1");
var DB2 = patio.createConnection("mysql://test:testpass@localhost:3306/test_2");
var Test1 = patio.addModel(DB1.from("test");
var Test2 = patio.addModel(DB2.from("test");

//sync the models
patio.syncModels().chain(function(){
     //now you can use them
     var test1Model = new Test1();
     var test2Model = new Test2();
});

Arguments Source
function (name,db){
   return model.getModel(name, db);
           
}
    

logDebug Function Public


Defined index.js

Logs a DEBUG level message to the "patio" logger.

Source
function (){
   if (LOGGER.isDebug) {
       LOGGER.debug.apply(LOGGER, arguments);
   }
           
}
    

logError Function Public


Defined index.js

Logs an ERROR level message to the "patio" logger.

Source
function (){
   if (LOGGER.isError) {
       LOGGER.error.apply(LOGGER, arguments);
   }
           
}
    

logFatal Function Public


Defined index.js

Logs a FATAL level message to the "patio" logger.

Source
function (){
   if (LOGGER.isFatal) {
       LOGGER.fatal.apply(LOGGER, arguments);
   }
           
}
    

logInfo Function Public


Defined index.js

Logs an INFO level message to the "patio" logger.

Source
function (){
   if (LOGGER.isInfo) {
       LOGGER.info.apply(LOGGER, arguments);
   }
           
}
    

logTrace Function Public


Defined index.js

Logs a TRACE level message to the "patio" logger.

Source
function (){
   if (LOGGER.isTrace) {
       LOGGER.trace.apply(LOGGER, arguments);
   }
           
}
    

logWarn Function Public


Defined index.js

Logs a WARN level message to the "patio" logger.

Source
function (){
   if (LOGGER.isWarn) {
       LOGGER.warn.apply(LOGGER, arguments);
   }
           
}
    

migrate Function Public


Defined index.js

Migrates the database using migration files found in the supplied directory.

Integer Migrations

Integer migrations are the simpler of the two migrations but are less flexible than timestamp based migrations. In order for patio to determine which versions to use the file names must end in .js where versionNumber is a integer value representing the version number. NOTE:With integer migrations missing versions are not allowed.

An example directory structure might look like the following:
-migrations
     - createFirstTables.0.js
     - shortDescription.1.js
     - another.2.js
     .
     .
     .
     -lastMigration.n.js
     
In order to easily identify where certain schema alterations have taken place it is a good idea to provide a brief but meaningful migration name.
         createEmployee.0.js
         alterEmployeeNameColumn.1.js
     

Timestamp Migrations

Timestamp migrations are the more complex of the two migrations but offer greater flexibility especially with development teams. This is because Timestamp migrations do not require consecutive version numbers, ,allow for duplicate version numbers(but this should be avoided), keeps track of all currently applied migrations, and it will merge missing migrations. In order for patio to determine the order of the migration files the file names must end in .js where the timestamp can be any form of a time stamp.
//yyyyMMdd
20110131
//yyyyMMddHHmmss
20110131123940
//unix epoch timestamp
1328035161
     
as long as it is greater than 20000101 other wise it will be assumed to be part of an integer migration.

An example directory structure might look like the following:
-migrations
     - createFirstTables.1328035161.js
     - shortDescription.1328035360.js
     - another.1328035376.js
     .
     .
     .
     -lastMigration.n.js
     
In order to easily identify where certain schema alterations have taken place it is a good idea to provide a brief but meaningful migration name.
         createEmployee.1328035161.js
         alterEmployeeNameColumn.1328035360.js
     

NOTE:If you start with IntegerBased migrations and decide to transition to Timestamp migrations the patio will attempt the migrate the current schema to the timestamp based migration schema.

In order to run a migraton all one has to do is call patio.migrate(DB, directory, options);
 var DB = patio.connect("my://connection/string");
 patio.migrate(DB, __dirname + "/migrations").chain(function(){
     console.log("migrations finished");
 });
Example migration file

//Up function used to migrate up a version
exports.up = function(db) {
  //create a new table
  db.createTable("company", function() {
      this.primaryKey("id");
      this.companyName(String, {size : 20, allowNull : false});
  });
  db.createTable("employee", function(table) {
      this.primaryKey("id");
      this.firstName(String);
      this.lastName(String);
      this.middleInitial("char", {size : 1});
  });
};

//Down function used to migrate down version
exports.down = function(db) {
   db.dropTable("employee", "company");
};

Arguments Returns Source
function (db){
   db = isString(db) ? this.connect(db) : db;
   var args = argsToArray(arguments);
   args.splice(0, 1);
   return migrate.run.apply(migrate, [db].concat(args));
           
}
    

syncModels Function Public


Defined index.js

Helper method to sync all models at once.

Example
var User = patio.addModel("user");
var Blog = patio.addModel("blog");

//using promise api
patio.syncModels().chain(function(){
    var user = new User();
}, function(error){
   console.log(err);
});

//using a callback

 patio.syncModels(function(err){
     if(err){
         console.log(err);
     }else{
         var user = new User();
     }
 });
        
Arguments Returns Source
function (cb){
   return model.syncModels(cb);
           
}
    

Documentation generated using coddoc.