From 14cb8715317c93d60f7faa93563eb53edabded5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez?= Date: Tue, 24 Oct 2017 15:59:19 +0200 Subject: [PATCH] Update knex to preserve type information Previously knex would lose type information when callink knex.transaction. --- types/knex/index.d.ts | 38 +++++++++++++++++++------------------- types/knex/knex-tests.ts | 15 ++++++++++++--- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/types/knex/index.d.ts b/types/knex/index.d.ts index 974c3eb0a4..aaee0b27f7 100644 --- a/types/knex/index.d.ts +++ b/types/knex/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for Knex.js // Project: https://github.com/tgriesser/knex -// Definitions by: Qubo , Baronfel +// Definitions by: Qubo , Baronfel , Pablo Rodríguez // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -8,7 +8,7 @@ import events = require("events"); import stream = require ("stream"); -import Promise = require("bluebird"); +import Bluebird = require("bluebird"); type Callback = Function; type Client = Function; @@ -22,9 +22,9 @@ interface Knex extends Knex.QueryInterface { __knex__: string; raw: Knex.RawBuilder; - transaction(transactionScope: (trx: Knex.Transaction) => any): Promise; + transaction(transactionScope: (trx: Knex.Transaction) => Promise | Bluebird | void): Bluebird; destroy(callback: Function): void; - destroy(): Promise; + destroy(): Bluebird; batchInsert(tableName : TableName, data: any[], chunkSize : number) : Knex.QueryBuilder; schema: Knex.SchemaBuilder; queryBuilder(): Knex.QueryBuilder; @@ -351,7 +351,7 @@ declare namespace Knex { and: QueryBuilder; //TODO: Promise? - columnInfo(column?: string): Promise; + columnInfo(column?: string): Bluebird; forUpdate(): QueryBuilder; forShare(): QueryBuilder; @@ -372,18 +372,18 @@ declare namespace Knex { // Chainable interface // - interface ChainableInterface extends Promise { + interface ChainableInterface extends Bluebird { toQuery(): string; options(options: any): QueryBuilder; - stream(callback: (readable: stream.PassThrough) => any): Promise; + stream(callback: (readable: stream.PassThrough) => any): Bluebird; stream(options?: { [key: string]: any }): stream.PassThrough; - stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Promise; + stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Bluebird; pipe(writable: any): stream.PassThrough; exec(callback: Function): QueryBuilder; } interface Transaction extends Knex { - savepoint(transactionScope: (trx: Transaction) => any): Promise; + savepoint(transactionScope: (trx: Transaction) => any): Bluebird; commit(value?: any): QueryBuilder; rollback(error?: any): QueryBuilder; } @@ -392,14 +392,14 @@ declare namespace Knex { // Schema builder // - interface SchemaBuilder extends Promise { + interface SchemaBuilder extends Bluebird { createTable(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder; createTableIfNotExists(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder; - renameTable(oldTableName: string, newTableName: string): Promise; + renameTable(oldTableName: string, newTableName: string): Bluebird; dropTable(tableName: string): SchemaBuilder; - hasTable(tableName: string): Promise; - hasColumn(tableName: string, columnName: string): Promise; - table(tableName: string, callback: (tableBuilder: AlterTableBuilder) => any): Promise; + hasTable(tableName: string): Bluebird; + hasColumn(tableName: string, columnName: string): Bluebird; + table(tableName: string, callback: (tableBuilder: AlterTableBuilder) => any): Bluebird; dropTableIfExists(tableName: string): SchemaBuilder; raw(statement: string): SchemaBuilder; withSchema(schemaName: string): SchemaBuilder; @@ -666,11 +666,11 @@ declare namespace Knex { } interface Migrator { - make(name: string, config?: MigratorConfig): Promise; - latest(config?: MigratorConfig): Promise; - rollback(config?: MigratorConfig): Promise; - status(config?: MigratorConfig): Promise; - currentVersion(config?: MigratorConfig): Promise; + make(name: string, config?: MigratorConfig): Bluebird; + latest(config?: MigratorConfig): Bluebird; + rollback(config?: MigratorConfig): Bluebird; + status(config?: MigratorConfig): Bluebird; + currentVersion(config?: MigratorConfig): Bluebird; } interface FunctionHelper { diff --git a/types/knex/knex-tests.ts b/types/knex/knex-tests.ts index 95d28b1b2b..dec1bf055f 100644 --- a/types/knex/knex-tests.ts +++ b/types/knex/knex-tests.ts @@ -520,7 +520,6 @@ knex.transaction(function(trx) { }) .then(trx.commit) .catch(trx.rollback); - }).then(function() { console.log('Transaction complete.'); }).catch(function(err) { @@ -537,7 +536,17 @@ knex.transaction(function(trx) { .transacting(trx) .forShare() .select('*') -}); +}) + +const transactionReturnValue = knex.transaction(function(trx) { + return knex("table") + .insert({ foo: "bar" }) + .returning(["id"]) + .then(function(result) { return result[0].id as number }) +}) + +// Tests that the transaction has kept the type of its return value by referencing a method of number +transactionReturnValue.then(value => value.toExponential); knex('users').count('active'); @@ -616,7 +625,7 @@ knex.transaction(function(trx) { }); // Using trx as a transaction object: -knex.transaction(function(trx) { +knex.transaction<{ length: number }>(function(trx) { trx.raw('');