From 2cbd45cb2adc73eecdd4a4423b2824493ae6d8a1 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Mon, 10 Sep 2018 15:41:16 -0600 Subject: [PATCH] [@types/knex] Update ChainableInterface. (#28716) Fixes #28679. As I [noted in a comment](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/28679#issuecomment-419265731) on the original issue, the class-like objects [`Raw`](https://github.com/tgriesser/knex/blob/master/src/raw.js#L191-L193), [`QueryBuilder`](https://github.com/tgriesser/knex/blob/2183a278268dc7aedf5edeccecb99176b9827f27/src/query/builder.js#L1074), and [`SchemaBuilder`](https://github.com/tgriesser/knex/blob/232fe9f1517dba927f6a3a1fb1b8842d7c0a4007/src/schema/builder.js#L68) use a [utility file](https://github.com/tgriesser/knex/blob/master/src/interface.js) to extend a handful of methods onto them. The contents of this file are represented in the `ChainableInterface` interface. Changes made: - Updated `SchemaBuilder` to extend `ChainableInterface`. - Moved `debug`, `transacting`, and `connection` from `QueryInterface` to `ChainableInterface`. - Removed `exec` from `ChainableInterface` as it was [removed 0.12.0](https://github.com/tgriesser/knex/blob/master/CHANGELOG.md#0120---13-sep-2016). --- types/knex/index.d.ts | 18 ++++++++---------- types/knex/knex-tests.ts | 12 ++---------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/types/knex/index.d.ts b/types/knex/index.d.ts index b400af2497..329bf438b2 100644 --- a/types/knex/index.d.ts +++ b/types/knex/index.d.ts @@ -156,7 +156,6 @@ declare namespace Knex { // Others first: Select; - debug(enabled?: boolean): QueryBuilder; pluck(column: string): QueryBuilder; insert(data: any, returning?: string | string[]): QueryBuilder; @@ -169,9 +168,6 @@ declare namespace Knex { delete(returning?: string | string[]): QueryBuilder; truncate(): QueryBuilder; - transacting(trx?: Transaction): QueryBuilder; - connection(connection: any): QueryBuilder; - clone(): QueryBuilder; } @@ -390,12 +386,14 @@ declare namespace Knex { interface ChainableInterface extends Bluebird { toQuery(): string; - options(options: any): QueryBuilder; - stream(callback: (readable: stream.PassThrough) => any): Bluebird; + options(options: { [key: string]: any }): this; + connection(connection: any): this; + debug(enabled: boolean): this; + transacting(trx: Transaction): this; + stream(handler: (readable: stream.PassThrough) => any): Bluebird; + stream(options: { [key: string]: any }, handler: (readable: stream.PassThrough) => any): Bluebird; stream(options?: { [key: string]: any }): stream.PassThrough; - stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Bluebird; - pipe(writable: any): stream.PassThrough; - exec(callback: Function): QueryBuilder; + pipe(writable: T, options?: { [key: string]: any }): stream.PassThrough; } interface Transaction extends Knex { @@ -408,7 +406,7 @@ declare namespace Knex { // Schema builder // - interface SchemaBuilder extends Bluebird { + interface SchemaBuilder extends ChainableInterface { createTable(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder; createTableIfNotExists(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder; alterTable(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder; diff --git a/types/knex/knex-tests.ts b/types/knex/knex-tests.ts index e7fb3e1b81..461e1738fe 100644 --- a/types/knex/knex-tests.ts +++ b/types/knex/knex-tests.ts @@ -982,19 +982,11 @@ knex.select('name').from('users') .where('id', '>', 20) .andWhere('id', '<', 200) .limit(10) - .offset(x) - .exec(function(err: any, rows: any[]) { - if (err) return console.error(err); - knex.select('id').from('nicknames').whereIn('nickname', rows.map((r: any) => r.name)) - .exec(function(err: any, rows: any[]) { - if (err) return console.error(err); - console.log(rows); - }); - }); + .offset(x); // Retrieve the stream: var stream = knex.select('*').from('users').stream(); -var writableStream: any; +var writableStream: NodeJS.WritableStream; stream.pipe(writableStream); // With options: