knex/src/interface.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
import * as helpers from './helpers';
import { isEmpty, isArray, map, clone, each } from 'lodash'
2016-03-02 17:07:05 +01:00
export default function(Target) {
2016-03-02 17:07:05 +01:00
Target.prototype.toQuery = function(tz) {
let data = this.toSQL(this._method, tz);
2016-03-02 17:07:05 +01:00
if (!isArray(data)) data = [data];
return map(data, (statement) => {
return this.client._formatQuery(statement.sql, statement.bindings, tz);
2016-03-02 17:07:05 +01:00
}).join(';\n');
};
// Create a new instance of the `Runner`, passing in the current object.
Target.prototype.then = function(/* onFulfilled, onRejected */) {
const result = this.client.runner(this).run()
2016-03-02 17:07:05 +01:00
return result.then.apply(result, arguments);
};
// Add additional "options" to the builder. Typically used for client specific
// items, like the `mysql` and `sqlite3` drivers.
Target.prototype.options = function(opts) {
this._options = this._options || [];
this._options.push(clone(opts) || {});
return this;
};
// Sets an explicit "connnection" we wish to use for this query.
Target.prototype.connection = function(connection) {
this._connection = connection;
return this;
};
// Set a debug flag for the current schema query stack.
Target.prototype.debug = function(enabled) {
this._debug = arguments.length ? enabled : true;
return this;
};
// Set the transaction object for this query.
Target.prototype.transacting = function(t) {
if (t && t.client) {
if (!t.client.transacting) {
helpers.warn(`Invalid transaction value: ${t.client}`)
2016-03-02 17:07:05 +01:00
} else {
this.client = t.client
}
}
if (isEmpty(t)) {
helpers.error('Invalid value on transacting call, potential bug')
throw Error('Invalid transacting value (null, undefined or empty object)')
}
2016-03-02 17:07:05 +01:00
return this;
};
// Initializes a stream.
Target.prototype.stream = function(options) {
return this.client.runner(this).stream(options);
};
// Initialize a stream & pipe automatically.
Target.prototype.pipe = function(writable, options) {
return this.client.runner(this).pipe(writable, options);
};
// Creates a method which "coerces" to a promise, by calling a
// "then" method on the current `Target`
each(['bind', 'catch', 'finally', 'asCallback',
'spread', 'map', 'reduce', 'tap', 'thenReturn',
'return', 'yield', 'ensure', 'reflect',
'get', 'mapSeries', 'delay'], function(method) {
2016-03-02 17:07:05 +01:00
Target.prototype[method] = function() {
const promise = this.then();
return promise[method].apply(promise, arguments);
2016-03-02 17:07:05 +01:00
};
});
}