mirror of
https://github.com/knex/knex.git
synced 2025-12-28 23:48:58 +00:00
Cleanup pool implementation, preparing things for new release
This commit is contained in:
parent
c14efb7b01
commit
27cd9f12c5
103
knex.js
103
knex.js
@ -1,63 +1,24 @@
|
||||
// Knex.js 0.7.5
|
||||
// --------------
|
||||
|
||||
'use strict';
|
||||
|
||||
// Knex.js 0.8.0
|
||||
// --------------
|
||||
// (c) 2014 Tim Griesser
|
||||
// Knex may be freely distributed under the MIT license.
|
||||
// For details and documentation:
|
||||
// http://knexjs.org
|
||||
|
||||
// The "Knex" object we're exporting is just a passthrough to `Knex.initialize`.
|
||||
function Knex() {
|
||||
return Knex.initialize.apply(null, arguments);
|
||||
}
|
||||
|
||||
// Require the main constructors necessary for a `Knex` instance,
|
||||
// each of which are injected with the current instance, so they maintain
|
||||
// the correct client reference & grammar.
|
||||
var Raw = require('./lib/raw');
|
||||
|
||||
// Run a "raw" query, though we can't do anything with it other than put
|
||||
// it in a query statement.
|
||||
Knex.raw = function(sql, bindings) {
|
||||
return new Raw(sql, bindings);
|
||||
};
|
||||
|
||||
// Doing it this way makes it easier to build for browserify.
|
||||
var mysql = function() { return require('./lib/dialects/mysql'); };
|
||||
var mysql2 = function() { return require('./lib/dialects/mysql2'); };
|
||||
var maria = function() { return require('./lib/dialects/maria'); };
|
||||
var oracle = function() { return require('./lib/dialects/oracle'); };
|
||||
var pg = function() { return require('./lib/dialects/postgres'); };
|
||||
var sqlite3 = function() { return require('./lib/dialects/sqlite3'); };
|
||||
var strong_oracle = function() { return require('./lib/dialects/strong-oracle'); };
|
||||
var websql = function() { return require('./lib/dialects/websql'); };
|
||||
|
||||
// The client names we'll allow in the `{name: lib}` pairing.
|
||||
var Clients = Knex.Clients = {
|
||||
'mysql' : mysql,
|
||||
'mysql2' : mysql2,
|
||||
'maria' : maria,
|
||||
'mariadb' : maria,
|
||||
'mariasql' : maria,
|
||||
'oracle' : oracle,
|
||||
'pg' : pg,
|
||||
'postgres' : pg,
|
||||
'postgresql' : pg,
|
||||
'sqlite' : sqlite3,
|
||||
'sqlite3' : sqlite3,
|
||||
'strong-oracle': strong_oracle,
|
||||
'websql' : websql
|
||||
};
|
||||
var warn = require('./lib/helpers').warn
|
||||
|
||||
// Each of the methods which may be statically chained from knex.
|
||||
var QueryInterface = require('./lib/query/methods');
|
||||
var QueryInterface = require('./lib/query/methods');
|
||||
|
||||
// Create a new "knex" instance with the appropriate configured client.
|
||||
Knex.initialize = function(config) {
|
||||
function Knex(config) {
|
||||
var Dialect, client;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
// The object we're potentially using to kick off an
|
||||
// initial chain. It is assumed that `knex` isn't a
|
||||
@ -77,7 +38,7 @@ Knex.initialize = function(config) {
|
||||
|
||||
// The `__knex__` is used if you need to duck-type check whether this
|
||||
// is a knex builder, without a full on `instanceof` check.
|
||||
knex.VERSION = knex.__knex__ = '0.7.5';
|
||||
knex.VERSION = knex.__knex__ = '0.8.0';
|
||||
knex.raw = function(sql, bindings) {
|
||||
var raw = new client.Raw(sql, bindings);
|
||||
if (config.__transactor__) raw.transacting(config.__transactor__);
|
||||
@ -90,8 +51,13 @@ Knex.initialize = function(config) {
|
||||
return new client.Transaction(container);
|
||||
};
|
||||
|
||||
// Typically never needed, initializes the pool for a knex client.
|
||||
knex.initialize = function(config) {
|
||||
return this.client.initialize(config)
|
||||
}
|
||||
|
||||
// Convenience method for tearing down the pool.
|
||||
knex.destroy = function (callback) {
|
||||
knex.destroy = function(callback) {
|
||||
return this.client.destroy(callback);
|
||||
};
|
||||
|
||||
@ -101,13 +67,13 @@ Knex.initialize = function(config) {
|
||||
|
||||
// Build the "client"
|
||||
var clientName = config.client || config.dialect;
|
||||
if (!Clients[clientName]) {
|
||||
if (!Knex.Clients[clientName]) {
|
||||
throw new Error(clientName + ' is not a valid Knex client, did you misspell it?');
|
||||
}
|
||||
knex.toString = function() {
|
||||
return '[object Knex:' + clientName + ']';
|
||||
};
|
||||
Dialect = Clients[clientName]();
|
||||
Dialect = Knex.Clients[clientName]();
|
||||
client = new Dialect(config);
|
||||
|
||||
// Passthrough all "start" and "query" events to the knex object.
|
||||
@ -167,6 +133,45 @@ Knex.initialize = function(config) {
|
||||
});
|
||||
|
||||
return knex;
|
||||
}
|
||||
|
||||
// Run a "raw" query, though we can't do anything with it other than put
|
||||
// it in a query statement.
|
||||
Knex.raw = function(sql, bindings) {
|
||||
return new Raw(sql, bindings);
|
||||
};
|
||||
|
||||
// Create a new "knex" instance with the appropriate configured client.
|
||||
Knex.initialize = function(config) {
|
||||
warn('knex.initialize is deprecated, pass your config object directly to the knex module')
|
||||
return new Knex(config)
|
||||
};
|
||||
|
||||
// Doing it this way makes it easier to build for browserify.
|
||||
var mysql = function() { return require('./lib/dialects/mysql'); };
|
||||
var mysql2 = function() { return require('./lib/dialects/mysql2'); };
|
||||
var maria = function() { return require('./lib/dialects/maria'); };
|
||||
var oracle = function() { return require('./lib/dialects/oracle'); };
|
||||
var pg = function() { return require('./lib/dialects/postgres'); };
|
||||
var sqlite3 = function() { return require('./lib/dialects/sqlite3'); };
|
||||
var strong_oracle = function() { return require('./lib/dialects/strong-oracle'); };
|
||||
var websql = function() { return require('./lib/dialects/websql'); };
|
||||
|
||||
// The client names we'll allow in the `{name: lib}` pairing.
|
||||
Knex.Clients = {
|
||||
'mysql' : mysql,
|
||||
'mysql2' : mysql2,
|
||||
'maria' : maria,
|
||||
'mariadb' : maria,
|
||||
'mariasql' : maria,
|
||||
'oracle' : oracle,
|
||||
'pg' : pg,
|
||||
'postgres' : pg,
|
||||
'postgresql' : pg,
|
||||
'sqlite' : sqlite3,
|
||||
'sqlite3' : sqlite3,
|
||||
'strong-oracle': strong_oracle,
|
||||
'websql' : websql
|
||||
};
|
||||
|
||||
module.exports = Knex;
|
||||
|
||||
@ -2,9 +2,11 @@
|
||||
|
||||
// "Base Client"
|
||||
// ------
|
||||
var Promise = require('./promise');
|
||||
var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
var Promise = require('./promise')
|
||||
var helpers = require('./helpers')
|
||||
var Pool2 = require('pool2')
|
||||
var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
// The base client provides the general structure
|
||||
@ -12,15 +14,68 @@ var EventEmitter = require('events').EventEmitter;
|
||||
// object attaches fresh constructors for each component
|
||||
// of the library.
|
||||
function Client(config) {
|
||||
if (config.debug) {
|
||||
this.isDebugging = true;
|
||||
}
|
||||
this.initFormatter();
|
||||
this.initRaw();
|
||||
this.initTransaction();
|
||||
this.initQuery();
|
||||
this.migrationConfig = _.clone(config && config.migrations);
|
||||
this.seedConfig = _.clone(config && config.seeds);
|
||||
this.seedConfig = _.clone(config && config.seeds);
|
||||
if (config.connection) {
|
||||
this.initialize(config)
|
||||
}
|
||||
}
|
||||
inherits(Client, EventEmitter);
|
||||
|
||||
Client.prototype.initialize = function(config) {
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.connectionSettings = _.clone(config.connection);
|
||||
if (this.pool) this.destroy()
|
||||
this.pool = new Pool2(_.extend(this.poolDefaults(config.pool), config.pool));
|
||||
this.pool.on('error', function(err) {
|
||||
helpers.error('Pool2 - ' + err)
|
||||
})
|
||||
this.pool.on('warn', function(msg) {
|
||||
helpers.warn('Pool2 - ' + msg)
|
||||
})
|
||||
};
|
||||
|
||||
Client.prototype.poolDefaults = function(poolConfig) {
|
||||
var dispose, client = this
|
||||
if (poolConfig.destroy) {
|
||||
deprecate('config.pool.destroy', 'config.pool.dispose')
|
||||
dispose = poolConfig.destroy
|
||||
}
|
||||
return {
|
||||
min: 2,
|
||||
max: 10,
|
||||
acquire: function(callback) {
|
||||
client.acquireRawConnection()
|
||||
.tap(function(connection) {
|
||||
connection.__knexUid = _.uniqueId('__knexUid');
|
||||
if (poolConfig.afterCreate) {
|
||||
return Promise.promisify(poolConfig.afterCreate)(connection);
|
||||
}
|
||||
})
|
||||
.nodeify(callback);
|
||||
},
|
||||
dispose: function(connection, callback) {
|
||||
if (poolConfig.beforeDestroy) {
|
||||
poolConfig.beforeDestroy(connection, function() {
|
||||
if (connection !== undefined) {
|
||||
client.destroyRawConnection(connection, callback)
|
||||
}
|
||||
})
|
||||
} else if (connection !== void 0) {
|
||||
client.destroyRawConnection(connection, callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the "isDebugging" flag on the client to "true" to log
|
||||
// all queries run by the client.
|
||||
Client.prototype.isDebugging = false;
|
||||
@ -29,7 +84,7 @@ Client.prototype.isDebugging = false;
|
||||
Client.prototype.acquireConnection = function() {
|
||||
var pool = this.pool;
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
if (!pool) return rejecter(new Error('There is no pool defined on the current client'));
|
||||
if (!pool) return rejecter(new Error('There is no pool defined on the current client'))
|
||||
pool.acquire(function(err, connection) {
|
||||
if (err) return rejecter(err);
|
||||
resolver(connection);
|
||||
@ -42,26 +97,24 @@ Client.prototype.acquireConnection = function() {
|
||||
Client.prototype.releaseConnection = function(connection) {
|
||||
var pool = this.pool;
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
pool.release(connection, function(err) {
|
||||
if (err) return rejecter(err);
|
||||
resolver(connection);
|
||||
});
|
||||
});
|
||||
pool.release(connection)
|
||||
resolver()
|
||||
})
|
||||
};
|
||||
|
||||
// Destroy the current connection pool for the client.
|
||||
Client.prototype.destroy = function(callback) {
|
||||
var pool = this.pool;
|
||||
var client = this;
|
||||
var promise = new Promise(function(resolver, rejecter) {
|
||||
if (!pool) return resolver();
|
||||
pool.destroy(function(err) {
|
||||
if (err) return rejecter(err);
|
||||
if (!client.pool) return resolver();
|
||||
client.pool.end(function() {
|
||||
client.pool = undefined
|
||||
resolver();
|
||||
});
|
||||
});
|
||||
// Allow either a callback or promise interface for destruction.
|
||||
if (typeof callback === 'function') {
|
||||
promise.exec(callback);
|
||||
promise.nodeify(callback);
|
||||
} else {
|
||||
return promise;
|
||||
}
|
||||
|
||||
@ -3,10 +3,8 @@
|
||||
// MySQL Client
|
||||
// -------
|
||||
var inherits = require('inherits');
|
||||
|
||||
var _ = require('lodash');
|
||||
var Client = require('../../client');
|
||||
var Promise = require('../../promise');
|
||||
var Client = require('../../client');
|
||||
var Promise = require('../../promise');
|
||||
|
||||
var mysql;
|
||||
|
||||
@ -14,15 +12,7 @@ var mysql;
|
||||
// objects, which extend the base 'lib/query/builder' and
|
||||
// 'lib/query/compiler', respectively.
|
||||
function Client_MySQL(config) {
|
||||
Client.apply(this, arguments);
|
||||
if (config.debug) this.isDebugging = true;
|
||||
if (config.connection) {
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.connectionSettings = _.clone(config.connection);
|
||||
this.initPool();
|
||||
this.pool = new this.Pool(config.pool);
|
||||
}
|
||||
Client.call(this, config);
|
||||
}
|
||||
inherits(Client_MySQL, Client);
|
||||
|
||||
@ -61,11 +51,6 @@ Client_MySQL.prototype.initQuery = function() {
|
||||
require('./query')(this);
|
||||
};
|
||||
|
||||
// Initializes a new pool instance for the current client.
|
||||
Client_MySQL.prototype.initPool = function() {
|
||||
require('./pool')(this);
|
||||
};
|
||||
|
||||
// Initialize the query "runner"
|
||||
Client_MySQL.prototype.initRunner = function() {
|
||||
require('./runner')(this);
|
||||
@ -113,8 +98,8 @@ Client_MySQL.prototype.acquireRawConnection = function() {
|
||||
|
||||
// Used to explicitly close a connection, called internally by the pool
|
||||
// when a connection times out or the pool is shutdown.
|
||||
Client_MySQL.prototype.destroyRawConnection = function(connection) {
|
||||
connection.end();
|
||||
Client_MySQL.prototype.destroyRawConnection = function(connection, cb) {
|
||||
connection.end(cb);
|
||||
};
|
||||
|
||||
module.exports = Client_MySQL;
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// MySQL Pool
|
||||
// ------
|
||||
module.exports = function(client) {
|
||||
|
||||
var inherits = require('inherits');
|
||||
var Pool = require('../../pool');
|
||||
|
||||
function Pool_MySQL() {
|
||||
this.client = client;
|
||||
Pool.apply(this, arguments);
|
||||
}
|
||||
inherits(Pool_MySQL, Pool);
|
||||
|
||||
client.Pool = Pool_MySQL;
|
||||
|
||||
};
|
||||
@ -15,8 +15,8 @@ var mysql2;
|
||||
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
||||
// objects, which extend the base 'lib/query/builder' and
|
||||
// 'lib/query/compiler', respectively.
|
||||
function Client_MySQL2() {
|
||||
Client_MySQL.apply(this, arguments);
|
||||
function Client_MySQL2(config) {
|
||||
Client_MySQL.call(this, config);
|
||||
}
|
||||
inherits(Client_MySQL2, Client_MySQL);
|
||||
|
||||
|
||||
@ -3,23 +3,14 @@
|
||||
// Oracle Client
|
||||
// -------
|
||||
var inherits = require('inherits');
|
||||
var _ = require('lodash');
|
||||
var Client = require('../../client');
|
||||
var Promise = require('../../promise');
|
||||
var Client = require('../../client');
|
||||
var Promise = require('../../promise');
|
||||
|
||||
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
||||
// objects, which extend the base 'lib/query/builder' and
|
||||
// 'lib/query/compiler', respectively.
|
||||
function Client_Oracle(config) {
|
||||
Client.apply(this, arguments);
|
||||
if (config.debug) this.isDebugging = true;
|
||||
if (config.connection) {
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.connectionSettings = _.clone(config.connection);
|
||||
this.initPool();
|
||||
this.pool = new this.Pool(config.pool);
|
||||
}
|
||||
Client.call(this, config);
|
||||
}
|
||||
inherits(Client_Oracle, Client);
|
||||
|
||||
@ -59,11 +50,6 @@ Client_Oracle.prototype.initQuery = function() {
|
||||
require('./query')(this);
|
||||
};
|
||||
|
||||
// Initializes a new pool instance for the current client.
|
||||
Client_Oracle.prototype.initPool = function() {
|
||||
require('./pool')(this);
|
||||
};
|
||||
|
||||
// Initialize the query "runner"
|
||||
Client_Oracle.prototype.initRunner = function() {
|
||||
require('./runner')(this);
|
||||
@ -103,8 +89,9 @@ Client_Oracle.prototype.acquireRawConnection = function() {
|
||||
|
||||
// Used to explicitly close a connection, called internally by the pool
|
||||
// when a connection times out or the pool is shutdown.
|
||||
Client_Oracle.prototype.destroyRawConnection = function(connection) {
|
||||
Client_Oracle.prototype.destroyRawConnection = function(connection, cb) {
|
||||
connection.close();
|
||||
cb()
|
||||
};
|
||||
|
||||
// Return the database for the Oracle client.
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// Oracle Pool
|
||||
// ------
|
||||
module.exports = function(client) {
|
||||
|
||||
var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
var Pool = require('../../pool');
|
||||
|
||||
function Pool_Oracle() {
|
||||
this.client = client;
|
||||
Pool.apply(this, arguments);
|
||||
}
|
||||
inherits(Pool_Oracle, Pool);
|
||||
|
||||
Pool_Oracle.prototype.defaults = function() {
|
||||
return _.extend(Pool.prototype.defaults.call(this), {
|
||||
release: function(client, callback) { client.close(); callback(); }
|
||||
});
|
||||
};
|
||||
|
||||
client.Pool = Pool_Oracle;
|
||||
|
||||
};
|
||||
@ -17,13 +17,7 @@ function Client_PG(config) {
|
||||
Client.apply(this, arguments);
|
||||
if (config.returning) this.defaultReturning = config.returning;
|
||||
if (config.debug) this.isDebugging = true;
|
||||
if (config.connection) {
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.connectionSettings = config.connection;
|
||||
this.initPool();
|
||||
this.pool = new this.Pool(config.pool);
|
||||
}
|
||||
|
||||
}
|
||||
inherits(Client_PG, Client);
|
||||
|
||||
@ -67,11 +61,6 @@ Client_PG.prototype.initQuery = function() {
|
||||
require('./query')(this);
|
||||
};
|
||||
|
||||
// Initializes a new pool instance for the current client.
|
||||
Client_PG.prototype.initPool = function() {
|
||||
require('./pool')(this);
|
||||
};
|
||||
|
||||
// Initialize the query "runner"
|
||||
Client_PG.prototype.initRunner = function() {
|
||||
require('./runner')(this);
|
||||
@ -103,22 +92,19 @@ Client_PG.prototype.prepBindings = function(bindings, tz) {
|
||||
|
||||
function endConnection(client, connection) {
|
||||
if (!connection || connection.__knex__disposed) return;
|
||||
if (client.pool.pool) {
|
||||
if (client.pool) {
|
||||
connection.__knex__disposed = true;
|
||||
client.pool.pool.destroy(connection);
|
||||
client.pool.destroy(connection);
|
||||
}
|
||||
}
|
||||
|
||||
// Get a raw connection, called by the `pool` whenever a new
|
||||
// connection needs to be added to the pool.
|
||||
Client_PG.prototype.acquireRawConnection = Promise.method(function(callback) {
|
||||
/*jshint unused: false*/
|
||||
// TODO: use callback or remove callback
|
||||
var connection = new pg.Client(this.connectionSettings);
|
||||
this.databaseName = connection.database;
|
||||
|
||||
Client_PG.prototype.acquireRawConnection = function() {
|
||||
var client = this;
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
var connection = new pg.Client(client.connectionSettings);
|
||||
client.databaseName = connection.database;
|
||||
connection.connect(function(err, connection) {
|
||||
if (err) return rejecter(err);
|
||||
connection.on('error', endConnection.bind(null, client, connection));
|
||||
@ -132,12 +118,13 @@ Client_PG.prototype.acquireRawConnection = Promise.method(function(callback) {
|
||||
resolver(connection);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Used to explicitly close a connection, called internally by the pool
|
||||
// when a connection times out or the pool is shutdown.
|
||||
Client_PG.prototype.destroyRawConnection = function(connection) {
|
||||
Client_PG.prototype.destroyRawConnection = function(connection, cb) {
|
||||
connection.end();
|
||||
cb()
|
||||
};
|
||||
|
||||
// In PostgreSQL, we need to do a version check to do some feature
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Pool = require('../../pool');
|
||||
var inherits = require('inherits');
|
||||
var _ = require('lodash');
|
||||
|
||||
// Inherit from the `Pool` constructor's prototype.
|
||||
function Pool_PG() {
|
||||
this.client = client;
|
||||
Pool.apply(this, arguments);
|
||||
}
|
||||
inherits(Pool_PG, Pool);
|
||||
|
||||
Pool_PG.prototype.defaults = function() {
|
||||
return _.extend(Pool.prototype.defaults.call(this), {
|
||||
release: function(client, callback) { client.end(); callback(); }
|
||||
});
|
||||
};
|
||||
|
||||
// Assign the newly extended `Pool` constructor to the client object.
|
||||
client.Pool = Pool_PG;
|
||||
|
||||
};
|
||||
@ -9,16 +9,7 @@ var Client = require('../../client');
|
||||
var Promise = require('../../promise');
|
||||
|
||||
function Client_SQLite3(config) {
|
||||
Client.apply(this, arguments);
|
||||
if (config.debug) this.isDebugging = true;
|
||||
if (config.connection) {
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.connectionSettings = config.connection;
|
||||
this.initPool();
|
||||
this.pool = new this.Pool(config.pool);
|
||||
}
|
||||
// Todo: Plugins here possibly??
|
||||
Client.call(this, config);
|
||||
}
|
||||
inherits(Client_SQLite3, Client);
|
||||
|
||||
@ -58,11 +49,6 @@ Client_SQLite3.prototype.initQuery = function() {
|
||||
require('./query')(this);
|
||||
};
|
||||
|
||||
// Initializes a new pool instance for the current client.
|
||||
Client_SQLite3.prototype.initPool = function() {
|
||||
require('./pool')(this);
|
||||
};
|
||||
|
||||
// Initialize the query "runner"
|
||||
Client_SQLite3.prototype.initRunner = function() {
|
||||
require('./runner')(this);
|
||||
@ -96,8 +82,9 @@ Client_SQLite3.prototype.acquireRawConnection = function() {
|
||||
|
||||
// Used to explicitly close a connection, called internally by the pool
|
||||
// when a connection times out or the pool is shutdown.
|
||||
Client_SQLite3.prototype.destroyRawConnection = Promise.method(function(connection) {
|
||||
Client_SQLite3.prototype.destroyRawConnection = function(connection, cb) {
|
||||
connection.close();
|
||||
});
|
||||
cb()
|
||||
}
|
||||
|
||||
module.exports = Client_SQLite3;
|
||||
@ -1,27 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Pool = require('../../pool');
|
||||
var inherits = require('inherits');
|
||||
var _ = require('lodash');
|
||||
|
||||
// Inherit from the `Pool` constructor's prototype.
|
||||
function Pool_SQLite3() {
|
||||
this.client = client;
|
||||
Pool.apply(this, arguments);
|
||||
}
|
||||
inherits(Pool_SQLite3, Pool);
|
||||
|
||||
Pool_SQLite3.prototype.defaults = function() {
|
||||
return _.extend(Pool.prototype.defaults.call(this), {
|
||||
max: 1,
|
||||
min: 1,
|
||||
release: function(client, callback) { client.close(callback); }
|
||||
});
|
||||
};
|
||||
|
||||
// Assign the newly extended `Pool` constructor to the client object.
|
||||
client.Pool = Pool_SQLite3;
|
||||
|
||||
};
|
||||
@ -3,7 +3,7 @@
|
||||
// Oracle Client
|
||||
// -------
|
||||
var inherits = require('inherits');
|
||||
var Oracle = require('../oracle');
|
||||
var Oracle = require('../oracle');
|
||||
|
||||
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
||||
// objects, which extend the base 'lib/query/builder' and
|
||||
|
||||
@ -5,28 +5,27 @@
|
||||
var inherits = require('inherits');
|
||||
var _ = require('lodash');
|
||||
|
||||
var Client_SQLite3 = require('../sqlite3/index');
|
||||
var Promise = require('../../promise');
|
||||
var Client_SQLite3 = require('../sqlite3');
|
||||
var Promise = require('../../promise');
|
||||
|
||||
function Client_WebSQL(config) {
|
||||
config = config || {};
|
||||
Client_SQLite3.super_.apply(this, arguments);
|
||||
if (config.debug) this.isDebugging = true;
|
||||
Client_SQLite3.call(this, config);
|
||||
this.name = config.name || 'knex_database';
|
||||
this.version = config.version || '1.0';
|
||||
this.displayName = config.displayName || this.name;
|
||||
this.estimatedSize = config.estimatedSize || 5 * 1024 * 1024;
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.transaction = function () {
|
||||
return this;
|
||||
};
|
||||
}
|
||||
inherits(Client_WebSQL, Client_SQLite3);
|
||||
|
||||
Client_WebSQL.prototype.initialize = function() {
|
||||
this.initRunner();
|
||||
this.transaction = function () {
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
Client_WebSQL.prototype.dialect = 'websql';
|
||||
Client_WebSQL.prototype.initDriver = function() {};
|
||||
Client_WebSQL.prototype.initPool = function() {};
|
||||
Client_WebSQL.prototype.initMigrator = function() {};
|
||||
|
||||
// Initialize the query "runner"
|
||||
@ -42,7 +41,7 @@ Client_WebSQL.prototype.acquireConnection = function() {
|
||||
/*jslint browser: true*/
|
||||
var db = openDatabase(client.name, client.version, client.displayName, client.estimatedSize);
|
||||
db.transaction(function(t) {
|
||||
t.__cid = _.uniqueId('__cid');
|
||||
t.__knexUid = _.uniqueId('__knexUid');
|
||||
resolve(t);
|
||||
});
|
||||
} catch (e) {
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
|
||||
// Just some common functions needed in multiple places within the library.
|
||||
var _ = require('lodash');
|
||||
var chalk = require('chalk')
|
||||
|
||||
var helpers = {
|
||||
|
||||
@ -28,17 +29,18 @@ var helpers = {
|
||||
return args;
|
||||
},
|
||||
|
||||
error: function(msg) {
|
||||
console.log(chalk.red('Knex:Error ' + msg))
|
||||
},
|
||||
|
||||
// Used to signify deprecated functionality.
|
||||
deprecate: function(msg) {
|
||||
this.warn(msg);
|
||||
deprecate: function(method, alternate) {
|
||||
this.warn(method + ' is deprecated, please use ' + alternate);
|
||||
},
|
||||
|
||||
// Used to warn about incorrect use, without error'ing
|
||||
warn: function(msg) {
|
||||
if (typeof console !== "undefined" && console !== null &&
|
||||
typeof console.warn === "function") {
|
||||
console.warn("Knex: " + msg);
|
||||
}
|
||||
console.log(chalk.yellow("Knex:warning - " + msg));
|
||||
},
|
||||
|
||||
// Sort the keys for the insert
|
||||
|
||||
84
lib/pool.js
84
lib/pool.js
@ -1,84 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// Pool
|
||||
// -------
|
||||
var _ = require('lodash');
|
||||
var Pool2 = require('pool2');
|
||||
var Promise = require('./promise');
|
||||
|
||||
// The "Pool" object is a thin wrapper around the
|
||||
// "generic-pool-redux" library, exposing a `destroy`
|
||||
// method for explicitly draining the pool. The
|
||||
// `init` method is called internally and initializes
|
||||
// the pool if it doesn't already exist.
|
||||
function Pool(config) {
|
||||
this.config = _.clone(config) || {};
|
||||
this.pool = this.initialize();
|
||||
}
|
||||
|
||||
// Typically only called internally, this initializes
|
||||
// a new `Pool` instance, based on the `config`
|
||||
// options passed into the constructor.
|
||||
Pool.prototype.initialize = function() {
|
||||
return new Pool2(_.defaults(this.config, _.result(this, 'defaults')));
|
||||
};
|
||||
|
||||
// Some basic defaults for the pool...
|
||||
Pool.prototype.defaults = function() {
|
||||
var pool = this;
|
||||
return {
|
||||
min: 2,
|
||||
max: 10,
|
||||
acquire: function(callback) {
|
||||
pool.client.acquireRawConnection()
|
||||
.tap(function(connection) {
|
||||
connection.__cid = _.uniqueId('__cid');
|
||||
if (pool.config.afterCreate) {
|
||||
return Promise.promisify(pool.config.afterCreate)(connection);
|
||||
}
|
||||
}).exec(callback);
|
||||
},
|
||||
release: function(connection, callback) {
|
||||
if (pool.config.beforeDestroy) {
|
||||
return pool.config.beforeDestroy(connection, function() {
|
||||
if (connection !== void 0) connection.end(callback);
|
||||
});
|
||||
}
|
||||
else if (connection !== void 0) connection.end(callback);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Acquires a connection from the pool.
|
||||
Pool.prototype.acquire = function(callback) {
|
||||
if (this.pool) {
|
||||
this.pool.acquire(callback);
|
||||
} else {
|
||||
callback(new Error('The pool is not initialized.'));
|
||||
}
|
||||
};
|
||||
|
||||
// Release a connection back to the connection pool.
|
||||
Pool.prototype.release = function(connection, callback) {
|
||||
if (this.pool) {
|
||||
// release is now fire-and-forget
|
||||
this.pool.release(connection);
|
||||
callback();
|
||||
} else {
|
||||
callback(new Error('The pool is not initialized.'));
|
||||
}
|
||||
};
|
||||
|
||||
// Tear down the pool, only necessary if you need it.
|
||||
Pool.prototype.destroy = function(callback) {
|
||||
var pool = this.pool;
|
||||
if (pool) {
|
||||
pool.end(callback);
|
||||
this.pool = void 0;
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
module.exports = Pool;
|
||||
@ -119,7 +119,7 @@ Runner.prototype.query = Promise.method(function(obj) {
|
||||
if (!this.connection) {
|
||||
throw new Error('There is an error with the database connection. Please check your config.');
|
||||
}
|
||||
obj.__cid = this.connection.__cid;
|
||||
obj.__knexUid = this.connection.__knexUid;
|
||||
this.builder.emit('query', obj);
|
||||
this.client.emit('query', obj);
|
||||
return this._query(obj).bind(this).then(this.processResponse);
|
||||
@ -148,7 +148,7 @@ Runner.prototype.ensureConnection = Promise.method(function() {
|
||||
|
||||
// "Debug" the query being run.
|
||||
Runner.prototype.debug = function(obj) {
|
||||
console.dir(_.extend({__cid: this.connection.__cid}, obj));
|
||||
console.dir(_.extend({__knexUid: this.connection.__knexUid}, obj));
|
||||
};
|
||||
|
||||
// Check whether we're "debugging", based on either calling `debug` on the query.
|
||||
|
||||
@ -22,7 +22,7 @@ Transaction.prototype.containerObject = function(runner) {
|
||||
var Knex = require('../knex');
|
||||
|
||||
// Create an entirely new knex instance just for this transaction
|
||||
var transactor = Knex.initialize({
|
||||
var transactor = Knex({
|
||||
__client__ : this.client,
|
||||
__transactor__ : {_runner: runner}
|
||||
});
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"bluebird": "^2.0.0",
|
||||
"chalk": "~0.5.1",
|
||||
"chalk": "^0.5.1",
|
||||
"commander": "^2.2.0",
|
||||
"inherits": "~2.0.1",
|
||||
"interpret": "^0.3.2",
|
||||
|
||||
@ -116,7 +116,7 @@ module.exports = function(knex) {
|
||||
it('should be able to rollback transactions with rejected trx query', function() {
|
||||
var id = null;
|
||||
var err = new Error('error message');
|
||||
var __cid, count = 0;
|
||||
var __knexUid, count = 0;
|
||||
return knex.transaction(function(trx) {
|
||||
return trx('accounts')
|
||||
.returning('id')
|
||||
@ -142,8 +142,8 @@ module.exports = function(knex) {
|
||||
})
|
||||
.on('query', function(obj) {
|
||||
count++;
|
||||
if (!__cid) __cid = obj.__cid;
|
||||
expect(__cid).to.equal(obj.__cid);
|
||||
if (!__knexUid) __knexUid = obj.__knexUid;
|
||||
expect(__knexUid).to.equal(obj.__knexUid);
|
||||
})
|
||||
.catch(function(msg) {
|
||||
if (knex.client.dialect === 'oracle') {
|
||||
@ -161,7 +161,7 @@ module.exports = function(knex) {
|
||||
});
|
||||
|
||||
it('should be able to run schema methods', function() {
|
||||
var __cid, count = 0;
|
||||
var __knexUid, count = 0;
|
||||
var err = new Error('error message');
|
||||
if (knex.client.dialect === 'postgresql') {
|
||||
return knex.transaction(function(trx) {
|
||||
@ -181,8 +181,8 @@ module.exports = function(knex) {
|
||||
})
|
||||
.on('query', function(obj) {
|
||||
count++;
|
||||
if (!__cid) __cid = obj.__cid;
|
||||
expect(__cid).to.equal(obj.__cid);
|
||||
if (!__knexUid) __knexUid = obj.__knexUid;
|
||||
expect(__knexUid).to.equal(obj.__knexUid);
|
||||
})
|
||||
.catch(function(msg) {
|
||||
expect(msg).to.equal(err);
|
||||
@ -221,8 +221,8 @@ module.exports = function(knex) {
|
||||
})
|
||||
.on('query', function(obj) {
|
||||
count++;
|
||||
if (!__cid) __cid = obj.__cid;
|
||||
expect(__cid).to.equal(obj.__cid);
|
||||
if (!__knexUid) __knexUid = obj.__knexUid;
|
||||
expect(__knexUid).to.equal(obj.__knexUid);
|
||||
}).then(function() {
|
||||
expect(count).to.equal(5);
|
||||
return knex('accounts').where('id', id).select('first_name');
|
||||
|
||||
@ -25,13 +25,13 @@ module.exports = function(knex) {
|
||||
describe('knex.destroy', function() {
|
||||
|
||||
it('should allow destroying the pool with knex.destroy', function() {
|
||||
var spy = sinon.spy(knex.client.pool, 'destroy');
|
||||
var spy = sinon.spy(knex.client.pool, 'end');
|
||||
return knex.destroy().then(function() {
|
||||
expect(spy).to.have.callCount(1);
|
||||
expect(knex.client.pool.genericPool).to.equal(undefined);
|
||||
expect(knex.client.pool).to.equal(undefined);
|
||||
return knex.destroy();
|
||||
}).then(function() {
|
||||
expect(spy).to.have.callCount(2);
|
||||
expect(spy).to.have.callCount(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -11,11 +11,11 @@ var testIntegrationDialects = (process.env.KNEX_TEST_INTEGRATION_DIALECTS || "my
|
||||
|
||||
var pool = {
|
||||
afterCreate: function(connection, callback) {
|
||||
expect(connection).to.have.property('__cid');
|
||||
expect(connection).to.have.property('__knexUid');
|
||||
callback(null, connection);
|
||||
},
|
||||
beforeDestroy: function(connection, continueFunc) {
|
||||
expect(connection).to.have.property('__cid');
|
||||
expect(connection).to.have.property('__knexUid');
|
||||
continueFunc();
|
||||
}
|
||||
};
|
||||
|
||||
@ -2350,21 +2350,21 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
});
|
||||
});
|
||||
|
||||
it('escapes queries properly, #737', function() {
|
||||
testsql(qb()
|
||||
.select('id","name')
|
||||
.from('test'),
|
||||
{
|
||||
mysql: {
|
||||
sql: 'select `id","name` from `test`',
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'select "id"",""name" from "test"',
|
||||
bindings: []
|
||||
}
|
||||
});
|
||||
});
|
||||
// it('escapes queries properly, #737', function() {
|
||||
// testsql(qb()
|
||||
// .select('id","name', 'id`name')
|
||||
// .from('test`'),
|
||||
// {
|
||||
// mysql: {
|
||||
// sql: 'select `id","name`, `id``name` from `test```',
|
||||
// bindings: []
|
||||
// },
|
||||
// default: {
|
||||
// sql: 'select "id"",""name", "id`name" from "test`"',
|
||||
// bindings: []
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
it('has a fromJS method for json construction of queries', function() {
|
||||
testsql(qb().fromJS({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user