knex/clients/base.js

131 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-05-08 20:16:39 -04:00
var When = require('when');
var nodefn = require('when/node/function');
var _ = require('underscore');
// Setup is called with the context of the current client.
exports.setup = function(Client, name, options) {
if (!options.connection) {
throw new Error('The database connection properties must be specified.');
}
this.name = name;
this.debug = options.debug;
this.connectionSettings = options.connection;
this.grammar = Client.grammar;
this.schemaGrammar = Client.schemaGrammar;
// Extend the genericPool with the options
// passed into the init under the "pool" option.
var instance = this;
2013-05-03 00:20:51 -04:00
this.pool = require('generic-pool').Pool(_.extend({
name: 'pool-' + name,
2013-05-03 00:20:51 -04:00
min: 2,
max: 10,
log: false,
idleTimeoutMillis: 30000,
create: function(callback) {
2013-05-03 00:20:51 -04:00
var conn = instance.getRawConnection();
conn.__cid = _.uniqueId('__cid');
callback(null, conn);
},
2013-05-03 00:20:51 -04:00
destroy: function(client) { client.end(); }
}, this.poolDefaults, options.pool));
};
exports.skim = function(data) {
return _.map(data, function(obj) {
return _.pick(obj, _.keys(obj));
});
};
exports.debug = function(builder, conn) {
console.log({sql: builder.sql, bindings: builder.bindings, __cid: conn.__cid});
};
exports.protoProps = {
2013-05-03 00:20:51 -04:00
// Retrieves a connection from the connection pool,
// returning a promise.
getConnection: function() {
2013-05-08 20:16:39 -04:00
return nodefn.call(this.pool.acquire);
2013-05-03 00:20:51 -04:00
},
// Releases a connection from the connection pool,
// returning a promise.
releaseConnection: function(conn) {
2013-05-08 20:16:39 -04:00
return nodefn.call(this.pool.release, conn);
2013-05-03 00:20:51 -04:00
},
// Begins a transaction statement on the instance,
// resolving with the connection of the current transaction.
startTransaction: function() {
2013-05-03 00:20:51 -04:00
return this.getConnection().then(function(connection) {
2013-05-08 20:16:39 -04:00
return nodefn.call(connection.query, 'begin;', []).then(function() {
2013-05-03 00:20:51 -04:00
return connection;
});
});
},
2013-05-03 00:20:51 -04:00
finishTransaction: function(type, trans, dfd) {
2013-05-08 20:16:39 -04:00
nodefn.call(trans.connection.query, type + ';', []).then(function() {
2013-05-03 00:20:51 -04:00
if (type === 'commit') dfd.resolve(resp);
if (type === 'rollback') dfd.reject(resp);
2013-05-08 20:16:39 -04:00
}).ensure(function() {
trans.connection.end();
trans.connection = null;
});
}
2013-05-03 10:03:44 -04:00
};
exports.grammar = {};
2013-05-03 10:03:44 -04:00
exports.schemaGrammar = {
// Compile a create table command.
compileCreateTable: function(blueprint, command) {
var columns = this.getColumns(blueprint).join(', ');
return 'create table ' + this.wrapTable(blueprint) + ' (' + columns + ')';
},
2013-05-03 10:03:44 -04:00
// Compile a drop table command.
compileDropTable: function(blueprint, command) {
return 'drop table ' + this.wrapTable(blueprint);
},
// Compile a drop table (if exists) command.
compileDropTableIfExists: function(blueprint, command) {
return 'drop table if exists ' + this.wrapTable(blueprint);
},
// Compile a drop index command.
compileDropIndex: function(blueprint, command) {
return 'drop index ' + command.index;
},
// Create the column definition for a tiny integer type.
typeTinyInteger: function() {
return 'tinyint';
},
// Create the column definition for a time type.
typeTime: function(column) {
return 'time';
},
// Create the column definition for a date type.
typeDate: function(column) {
return 'date';
},
// Create the column definition for a binary type.
typeBinary: function(column) {
return 'blob';
},
// Get the SQL for a nullable column modifier.
modifyNullable: function(blueprint, column) {
return column.isNullable ? ' null' : ' not null';
2013-05-03 10:03:44 -04:00
}
};