From a4bfee77513d000cf0fddf130a370071c240f93d Mon Sep 17 00:00:00 2001 From: wubzz Date: Sun, 11 Dec 2016 17:23:44 +0100 Subject: [PATCH] Ensure that 'client' is provided in knex config object (#1822) * Ensure that 'client' is provided in knex config object * sqlite3 columncompiler: Assign 'modifiers' --after-- super() call. * oracle columncompiler: Assign 'modifiers' --after-- super() call. --- src/client.js | 8 ++++++++ src/dialects/oracle/schema/columncompiler.js | 2 +- src/dialects/sqlite3/schema/columncompiler.js | 2 +- src/schema/columncompiler.js | 1 + test/tape/knex.js | 10 ++++++++++ test/tape/query-builder.js | 4 ++-- test/tape/raw.js | 2 +- 7 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/client.js b/src/client.js index 17370c3306..a8344959f8 100644 --- a/src/client.js +++ b/src/client.js @@ -37,6 +37,14 @@ function clientId() { // 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.`) + } + this.connectionSettings = cloneDeep(config.connection || {}) if (this.driverName && config.connection) { this.initializeDriver() diff --git a/src/dialects/oracle/schema/columncompiler.js b/src/dialects/oracle/schema/columncompiler.js index 465b1ce98f..07fe1c7ac1 100644 --- a/src/dialects/oracle/schema/columncompiler.js +++ b/src/dialects/oracle/schema/columncompiler.js @@ -9,8 +9,8 @@ import Trigger from './trigger'; // ------- function ColumnCompiler_Oracle() { - this.modifiers = ['defaultTo', 'checkIn', 'nullable', 'comment']; ColumnCompiler.apply(this, arguments); + this.modifiers = ['defaultTo', 'checkIn', 'nullable', 'comment']; } inherits(ColumnCompiler_Oracle, ColumnCompiler); diff --git a/src/dialects/sqlite3/schema/columncompiler.js b/src/dialects/sqlite3/schema/columncompiler.js index 190f914eec..fd5b355b51 100644 --- a/src/dialects/sqlite3/schema/columncompiler.js +++ b/src/dialects/sqlite3/schema/columncompiler.js @@ -6,8 +6,8 @@ import ColumnCompiler from '../../../schema/columncompiler'; // ------- function ColumnCompiler_SQLite3() { - this.modifiers = ['nullable', 'defaultTo']; ColumnCompiler.apply(this, arguments); + this.modifiers = ['nullable', 'defaultTo']; } inherits(ColumnCompiler_SQLite3, ColumnCompiler); diff --git a/src/schema/columncompiler.js b/src/schema/columncompiler.js index 96b98fd860..be009e9baa 100644 --- a/src/schema/columncompiler.js +++ b/src/schema/columncompiler.js @@ -18,6 +18,7 @@ function ColumnCompiler(client, tableCompiler, columnBuilder) { this.isIncrements = (this.type.indexOf('increments') !== -1); this.formatter = client.formatter(); this.sequence = []; + this.modifiers = []; } ColumnCompiler.prototype.pushQuery = helpers.pushQuery diff --git a/test/tape/knex.js b/test/tape/knex.js index 1c0d5e57d4..050273330d 100644 --- a/test/tape/knex.js +++ b/test/tape/knex.js @@ -65,3 +65,13 @@ test('it should use knex supported dialect', function(t) { }) knexObj.destroy() }) + +test('it should throw error if client is omitted in config', function(t) { + t.plan(1); + try { + var knexObj = knex({}); + t.deepEqual(true, false); //Don't reach this point + } catch(error) { + t.deepEqual(error.message, 'knex: Required configuration option \'client\' is missing.'); + } +}) diff --git a/test/tape/query-builder.js b/test/tape/query-builder.js index 3a56c57e03..dddcd4c8b6 100644 --- a/test/tape/query-builder.js +++ b/test/tape/query-builder.js @@ -14,7 +14,7 @@ tape('accumulates multiple update calls #647', function(t) { tape('allows for object syntax in join', function(t) { t.plan(1) - var qb = new QueryBuilder(new Client()) + var qb = new QueryBuilder(new Client({client: 'mysql'})) var sql = qb.table('users').innerJoin('accounts', { 'accounts.id': 'users.account_id', 'accounts.owner_id': 'users.id' @@ -24,7 +24,7 @@ tape('allows for object syntax in join', function(t) { }) tape('clones correctly', function(t) { - var qb = new QueryBuilder(new Client()) + var qb = new QueryBuilder(new Client({client: 'mysql'})) var original = qb.table('users').debug().innerJoin('accounts', { 'accounts.id': 'users.account_id', 'accounts.owner_id': 'users.id' diff --git a/test/tape/raw.js b/test/tape/raw.js index f0ded60586..ae063f7922 100644 --- a/test/tape/raw.js +++ b/test/tape/raw.js @@ -5,7 +5,7 @@ var Client = require('../../lib/client') var test = require('tape') var _ = require('lodash'); -var client = new Client() +var client = new Client({client: 'mysql'}) function raw(sql, bindings) { return new Raw(client).set(sql, bindings) }