2013-09-03 23:02:23 -04:00
|
|
|
var _ = require('underscore');
|
2013-08-06 11:29:46 -04:00
|
|
|
var whenfn = require('when/function');
|
2013-05-08 20:16:39 -04:00
|
|
|
var nodefn = require('when/node/function');
|
2013-05-02 00:21:49 -04:00
|
|
|
|
|
|
|
// 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-07-17 08:38:33 -04:00
|
|
|
var poolInstance = this.pool = require('generic-pool').Pool(_.extend({
|
2013-05-02 00:21:49 -04:00
|
|
|
name: 'pool-' + name,
|
2013-05-03 00:20:51 -04:00
|
|
|
min: 2,
|
|
|
|
max: 10,
|
|
|
|
log: false,
|
|
|
|
idleTimeoutMillis: 30000,
|
2013-05-02 00:21:49 -04:00
|
|
|
create: function(callback) {
|
2013-06-09 16:57:31 -04:00
|
|
|
var pool = this;
|
|
|
|
instance.getRawConnection(function(err, conn) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
conn.__cid = _.uniqueId('__cid');
|
|
|
|
if (pool.afterCreate) {
|
|
|
|
pool.afterCreate(conn, function(err) {
|
|
|
|
callback(err, conn);
|
|
|
|
});
|
|
|
|
} else {
|
2013-06-09 13:50:43 -04:00
|
|
|
callback(null, conn);
|
2013-06-09 16:57:31 -04:00
|
|
|
}
|
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
},
|
2013-06-09 13:50:43 -04:00
|
|
|
destroy: function(conn) {
|
|
|
|
if (this.beforeDestroy) {
|
|
|
|
this.beforeDestroy(conn, function() {
|
|
|
|
conn.end();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
conn.end();
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 00:21:49 -04:00
|
|
|
}, this.poolDefaults, options.pool));
|
2013-07-17 05:03:37 -04:00
|
|
|
|
|
|
|
// Default to draining on exit.
|
2013-07-17 08:38:33 -04:00
|
|
|
if (poolInstance.drainOnExit !== false && typeof process === 'object') {
|
2013-07-17 05:03:37 -04:00
|
|
|
process.on('exit', function() {
|
2013-07-17 08:38:33 -04:00
|
|
|
poolInstance.drain(function() {
|
|
|
|
poolInstance.destroyAllNow();
|
2013-07-17 05:03:37 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-05-02 00:21:49 -04:00
|
|
|
};
|
|
|
|
|
2013-05-04 14:36:41 -04:00
|
|
|
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});
|
|
|
|
};
|
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
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-08-06 11:29:46 -04:00
|
|
|
return whenfn.call(this.pool.release, conn);
|
2013-05-03 00:20:51 -04:00
|
|
|
},
|
|
|
|
|
2013-05-02 00:21:49 -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-18 00:07:28 -04:00
|
|
|
return nodefn.call(connection.query.bind(connection), 'begin;', []).then(function() {
|
2013-05-03 00:20:51 -04:00
|
|
|
return connection;
|
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-09-05 16:36:49 -04:00
|
|
|
finishTransaction: function(type, transaction, msg) {
|
2013-05-18 00:07:28 -04:00
|
|
|
var ctx = this;
|
2013-09-05 16:36:49 -04:00
|
|
|
var dfd = transaction.dfd;
|
2013-05-18 00:07:28 -04:00
|
|
|
nodefn.call(trans.connection.query.bind(trans.connection), type + ';', []).then(function(resp) {
|
2013-07-04 13:56:47 -04:00
|
|
|
if (type === 'commit') dfd.resolve(msg || resp);
|
|
|
|
if (type === 'rollback') dfd.reject(msg || resp);
|
2013-08-29 09:21:05 +02:00
|
|
|
}, function (err) {
|
|
|
|
dfd.reject(err);
|
2013-05-08 20:16:39 -04:00
|
|
|
}).ensure(function() {
|
2013-07-18 00:28:17 -04:00
|
|
|
return ctx.releaseConnection(trans.connection).then(function() {
|
|
|
|
trans.connection = null;
|
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-09-03 22:01:31 -04:00
|
|
|
};
|