knex/src/client.js

321 lines
8.8 KiB
JavaScript
Raw Normal View History

import Promise from 'bluebird';
import * as helpers from './helpers';
2016-03-02 17:07:05 +01:00
import Raw from './raw';
import Runner from './runner';
import Formatter from './formatter';
import Transaction from './transaction';
2016-03-02 17:07:05 +01:00
import QueryBuilder from './query/builder';
import QueryCompiler from './query/compiler';
2016-03-02 17:07:05 +01:00
import SchemaBuilder from './schema/builder';
import SchemaCompiler from './schema/compiler';
import TableBuilder from './schema/tablebuilder';
import TableCompiler from './schema/tablecompiler';
import ColumnBuilder from './schema/columnbuilder';
import ColumnCompiler from './schema/columncompiler';
2016-03-02 17:07:05 +01:00
import * as genericPool from 'generic-pool';
import * as genericPoolErrors from 'generic-pool/lib/errors'
import inherits from 'inherits';
import { EventEmitter } from 'events';
2016-03-02 17:07:05 +01:00
import { makeEscape } from './query/string'
import { assign, uniqueId, cloneDeep, defaults, get } from 'lodash'
2016-03-02 17:07:05 +01:00
const debug = require('debug')('knex:client')
const debugQuery = require('debug')('knex:query')
2016-09-16 16:04:11 -04:00
const debugBindings = require('debug')('knex:bindings')
2016-09-13 18:12:23 -04:00
let id = 0
function clientId() {
return `client${id++}`
}
2016-03-02 17:07:05 +01:00
// The base client provides the general structure
// for a dialect specific client object.
function Client(config = {}) {
this.config = config
//Client is a required field, so throw error if it's not supplied.
//If 'this.dialect' is set, then this is a 'super()' call, in which case
//'client' does not have to be set as it's already assigned on the client prototype.
if(!this.config.client && !this.dialect) {
throw new Error(`knex: Required configuration option 'client' is missing.`)
}
2016-03-02 17:07:05 +01:00
this.connectionSettings = cloneDeep(config.connection || {})
if (this.driverName && config.connection) {
this.initializeDriver()
if (!config.pool || (config.pool && config.pool.max !== 0)) {
2016-09-13 18:12:23 -04:00
this.__cid = clientId()
2016-03-02 17:07:05 +01:00
this.initializePool(config)
}
}
this.valueForUndefined = this.raw('DEFAULT');
if (config.useNullAsDefault) {
this.valueForUndefined = null
}
}
inherits(Client, EventEmitter)
assign(Client.prototype, {
formatter() {
2016-09-12 18:26:49 -04:00
return new Formatter(this)
2016-03-02 17:07:05 +01:00
},
queryBuilder() {
2016-09-12 18:45:35 -04:00
return new QueryBuilder(this)
2016-03-02 17:07:05 +01:00
},
queryCompiler(builder) {
2016-09-12 18:45:35 -04:00
return new QueryCompiler(this, builder)
2016-03-02 17:07:05 +01:00
},
schemaBuilder() {
2016-09-12 18:45:35 -04:00
return new SchemaBuilder(this)
2016-03-02 17:07:05 +01:00
},
schemaCompiler(builder) {
2016-09-12 18:45:35 -04:00
return new SchemaCompiler(this, builder)
2016-03-02 17:07:05 +01:00
},
tableBuilder(type, tableName, fn) {
2016-09-12 18:45:35 -04:00
return new TableBuilder(this, type, tableName, fn)
2016-03-02 17:07:05 +01:00
},
tableCompiler(tableBuilder) {
2016-09-12 18:45:35 -04:00
return new TableCompiler(this, tableBuilder)
2016-03-02 17:07:05 +01:00
},
columnBuilder(tableBuilder, type, args) {
2016-09-12 18:45:35 -04:00
return new ColumnBuilder(this, tableBuilder, type, args)
2016-03-02 17:07:05 +01:00
},
columnCompiler(tableBuilder, columnBuilder) {
2016-09-12 18:45:35 -04:00
return new ColumnCompiler(this, tableBuilder, columnBuilder)
2016-03-02 17:07:05 +01:00
},
2017-05-16 18:17:19 +08:00
runner(builder) {
return new Runner(this, builder)
2016-03-02 17:07:05 +01:00
},
transaction(container, config, outerTx) {
2016-09-12 18:45:35 -04:00
return new Transaction(this, container, config, outerTx)
2016-03-02 17:07:05 +01:00
},
raw() {
2016-09-12 18:45:35 -04:00
return new Raw(this).set(...arguments)
2016-03-02 17:07:05 +01:00
},
_formatQuery(sql, bindings, timeZone) {
bindings = bindings == null ? [] : [].concat(bindings);
let index = 0;
return sql.replace(/\\?\?/g, (match) => {
if (match === '\\?') {
return '?'
}
if (index === bindings.length) {
return match
}
const value = bindings[index++];
return this._escapeBinding(value, {timeZone})
})
},
_escapeBinding: makeEscape({
escapeString(str) {
return `'${str.replace(/'/g, "''")}'`
}
}),
query(connection, obj) {
2016-03-02 17:07:05 +01:00
if (typeof obj === 'string') obj = {sql: obj}
obj.bindings = this.prepBindings(obj.bindings)
2016-03-02 17:07:05 +01:00
debugQuery(obj.sql)
this.emit('query', assign({__knexUid: connection.__knexUid}, obj))
2016-09-16 16:04:11 -04:00
debugBindings(obj.bindings)
return this._query(connection, obj).catch((err) => {
err.message = this._formatQuery(obj.sql, obj.bindings) + ' - ' + err.message
this.emit('query-error', err, assign({__knexUid: connection.__knexUid}, obj))
2016-03-02 17:07:05 +01:00
throw err
})
},
stream(connection, obj, stream, options) {
2016-03-02 17:07:05 +01:00
if (typeof obj === 'string') obj = {sql: obj}
this.emit('query', assign({__knexUid: connection.__knexUid}, obj))
debugQuery(obj.sql)
obj.bindings = this.prepBindings(obj.bindings)
2016-09-16 16:04:11 -04:00
debugBindings(obj.bindings)
return this._stream(connection, obj, stream, options)
2016-03-02 17:07:05 +01:00
},
prepBindings(bindings) {
return bindings;
2016-03-02 17:07:05 +01:00
},
wrapIdentifier(value) {
if (this.config.wrapIdentifier) {
return this.config.wrapIdentifier(value, this.wrapIdentifierImpl);
}
return this.wrapIdentifierImpl(value);
},
wrapIdentifierImpl(value) {
return (value !== '*' ? `"${value.replace(/"/g, '""')}"` : '*')
2016-03-02 17:07:05 +01:00
},
initializeDriver() {
2016-03-02 17:07:05 +01:00
try {
this.driver = this._driver()
} catch (e) {
helpers.exit(`Knex: run\n$ npm install ${this.driverName} --save\n${e.stack}`)
2016-03-02 17:07:05 +01:00
}
},
poolDefaults() {
return {min: 2, max: 10, Promise}
},
getPoolSettings(poolConfig) {
poolConfig = defaults({}, poolConfig, this.poolDefaults());
const timeoutValidator = (config, path) => {
let timeout = get(config, path)
if (timeout !== undefined) {
timeout = parseInt(timeout, 10)
if (isNaN(timeout) || timeout <= 0) {
throw new Error(`${path} must be a positive int`)
2016-09-13 18:12:23 -04:00
}
}
return timeout
}
// acquire connection timeout can be set on config or config.pool
// choose the smallest, positive timeout setting and set on poolConfig
const timeouts = [
timeoutValidator(this.config, 'acquireConnectionTimeout') || 60000,
timeoutValidator({pool: poolConfig}, 'pool.acquireTimeoutMillis')
].filter(timeout => timeout !== undefined)
poolConfig.acquireTimeoutMillis = Math.min(...timeouts);
return {
config: poolConfig,
factory: {
create: () => {
return this.acquireRawConnection()
.tap(function(connection) {
connection.__knexUid = uniqueId('__knexUid')
if (poolConfig.afterCreate) {
return Promise.promisify(poolConfig.afterCreate)(connection)
}
});
},
destroy: (connection) => {
if (poolConfig.beforeDestroy) {
helpers.warn(`
2016-09-13 18:12:23 -04:00
beforeDestroy is deprecated, please open an issue if you use this
to discuss alternative apis
`)
poolConfig.beforeDestroy(connection, function() {})
}
if (connection !== void 0) {
return this.destroyRawConnection(connection)
}
return Promise.resolve();
},
validate: (connection) => {
if (connection.__knex__disposed) {
helpers.warn(`Connection Error: ${connection.__knex__disposed}`)
return Promise.resolve(false);
}
return this.validateConnection(connection)
2016-03-02 17:07:05 +01:00
}
},
2016-03-02 17:07:05 +01:00
}
},
2016-09-13 18:12:23 -04:00
initializePool(config) {
if (this.pool) {
helpers.warn('The pool has already been initialized')
return
}
const poolSettings = this.getPoolSettings(config.pool);
this.pool = genericPool.createPool(poolSettings.factory, poolSettings.config)
2016-09-13 18:12:23 -04:00
},
validateConnection(connection) {
return Promise.resolve(true);
2016-09-13 18:12:23 -04:00
},
2016-03-02 17:07:05 +01:00
// Acquire a connection from the pool.
acquireConnection() {
if (!this.pool) {
return Promise.reject(new Error('Unable to acquire a connection'))
}
return this.pool.acquire()
.tap(connection => {
debug('acquired connection from pool: %s', connection.__knexUid)
})
.catch(genericPoolErrors.TimeoutError, () => {
throw new Promise.TimeoutError(
2016-10-09 14:00:55 -04:00
'Knex: Timeout acquiring a connection. The pool is probably full. ' +
'Are you missing a .transacting(trx) call?'
)
});
2016-03-02 17:07:05 +01:00
},
// Releases a connection back to the connection pool,
// returning a promise resolved when the connection is released.
releaseConnection(connection) {
debug('releasing connection to pool: %s', connection.__knexUid)
return this.pool.release(connection).catch(() => {
debug('pool refused connection: %s', connection.__knexUid)
2016-03-02 17:07:05 +01:00
})
},
// Destroy the current connection pool for the client.
destroy(callback) {
return Promise.resolve(
this.pool &&
this.pool.drain()
.then(() => this.pool.clear())
.then(() => {
this.pool = void 0
if(typeof callback === 'function') {
callback();
}
2016-09-13 18:12:23 -04:00
})
);
2016-03-02 17:07:05 +01:00
},
// Return the database being used by this client.
database() {
2016-03-02 17:07:05 +01:00
return this.connectionSettings.database
},
toString() {
2016-03-02 17:07:05 +01:00
return '[object KnexClient]'
2016-05-26 11:06:33 -07:00
},
canCancelQuery: false,
assertCanCancelQuery() {
if (!this.canCancelQuery) {
throw new Error("Query cancelling not supported for this dialect");
}
},
2016-05-26 11:06:33 -07:00
cancelQuery() {
throw new Error("Query cancelling not supported for this dialect")
2016-03-02 17:07:05 +01:00
}
})
export default Client