knex/src/client.js

312 lines
8.3 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
2016-09-13 18:12:23 -04:00
import { Pool } from 'generic-pool';
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 } 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
const debugPool = require('debug')('knex:pool')
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) {
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(poolConfig) {
2016-09-13 18:12:23 -04:00
const name = this.dialect + ':' + this.driverName + ':' + this.__cid
2016-03-02 17:07:05 +01:00
return {
min: 2,
max: 10,
2016-09-13 18:12:23 -04:00
name: name,
log(str, level) {
if (level === 'info') {
debugPool(level.toUpperCase() + ' pool ' + name + ' - ' + str)
}
},
create: (callback) => {
this.acquireRawConnection()
2016-03-02 17:07:05 +01:00
.tap(function(connection) {
connection.__knexUid = uniqueId('__knexUid')
if (poolConfig.afterCreate) {
return Promise.promisify(poolConfig.afterCreate)(connection)
}
})
.asCallback(callback)
2016-03-02 17:07:05 +01:00
},
2016-09-13 18:12:23 -04:00
destroy: (connection) => {
2016-03-02 17:07:05 +01:00
if (poolConfig.beforeDestroy) {
2016-09-13 18:12:23 -04:00
helpers.warn(`
beforeDestroy is deprecated, please open an issue if you use this
to discuss alternative apis
`)
poolConfig.beforeDestroy(connection, function() {})
}
if (connection !== void 0) {
this.destroyRawConnection(connection)
2016-03-02 17:07:05 +01:00
}
},
2016-09-13 18:12:23 -04:00
validate: (connection) => {
if (connection.__knex__disposed) {
helpers.warn(`Connection Error: ${connection.__knex__disposed}`)
return false
}
return this.validateConnection(connection)
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
}
this.pool = new Pool(assign(this.poolDefaults(config.pool || {}), config.pool))
},
validateConnection(connection) {
return true
},
2016-03-02 17:07:05 +01:00
// Acquire a connection from the pool.
acquireConnection() {
2016-10-09 14:00:55 -04:00
return new Promise((resolver, rejecter) => {
2016-09-13 18:12:23 -04:00
if (!this.pool) {
return rejecter(new Error('Unable to acquire a connection'))
2016-03-02 17:07:05 +01:00
}
2016-10-09 14:00:55 -04:00
let wasRejected = false
const t = setTimeout(() => {
wasRejected = true
rejecter(new Promise.TimeoutError(
'Knex: Timeout acquiring a connection. The pool is probably full. ' +
'Are you missing a .transacting(trx) call?'
))
}, this.config.acquireConnectionTimeout || 60000)
this.pool.acquire((err, connection) => {
clearTimeout(t)
2016-10-09 14:00:55 -04:00
if (err) {
return rejecter(err)
}
if (wasRejected) {
this.pool.release(connection)
} else {
debug('acquired connection from pool: %s', connection.__knexUid)
resolver(connection)
}
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) {
2016-09-13 18:12:23 -04:00
return new Promise((resolver) => {
2016-03-02 17:07:05 +01:00
debug('releasing connection to pool: %s', connection.__knexUid)
2016-09-13 18:12:23 -04:00
this.pool.release(connection)
2016-03-02 17:07:05 +01:00
resolver()
})
},
// Destroy the current connection pool for the client.
destroy(callback) {
2016-09-13 18:12:23 -04:00
const promise = new Promise((resolver) => {
if (!this.pool) {
return resolver()
}
this.pool.drain(() => {
this.pool.destroyAllNow(() => {
this.pool = undefined
resolver()
})
2016-03-02 17:07:05 +01:00
})
})
2016-09-13 18:12:23 -04:00
2016-03-02 17:07:05 +01:00
// Allow either a callback or promise interface for destruction.
if (typeof callback === 'function') {
promise.asCallback(callback)
2016-03-02 17:07:05 +01:00
} else {
return promise
}
},
// 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