mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-01 15:50:13 +00:00
Update knex to preserve type information
Previously knex would lose type information when callink knex.transaction.
This commit is contained in:
38
types/knex/index.d.ts
vendored
38
types/knex/index.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
// Type definitions for Knex.js
|
||||
// Project: https://github.com/tgriesser/knex
|
||||
// Definitions by: Qubo <https://github.com/tkQubo>, Baronfel <https://github.com/baronfel>
|
||||
// Definitions by: Qubo <https://github.com/tkQubo>, Baronfel <https://github.com/baronfel>, Pablo Rodríguez <https://github.com/MeLlamoPablo>
|
||||
// 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<any>;
|
||||
transaction<T>(transactionScope: (trx: Knex.Transaction) => Promise<T> | Bluebird<T> | void): Bluebird<T>;
|
||||
destroy(callback: Function): void;
|
||||
destroy(): Promise<void>;
|
||||
destroy(): Bluebird<void>;
|
||||
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>;
|
||||
columnInfo(column?: string): Bluebird<ColumnInfo>;
|
||||
|
||||
forUpdate(): QueryBuilder;
|
||||
forShare(): QueryBuilder;
|
||||
@@ -372,18 +372,18 @@ declare namespace Knex {
|
||||
// Chainable interface
|
||||
//
|
||||
|
||||
interface ChainableInterface extends Promise<any> {
|
||||
interface ChainableInterface extends Bluebird<any> {
|
||||
toQuery(): string;
|
||||
options(options: any): QueryBuilder;
|
||||
stream(callback: (readable: stream.PassThrough) => any): Promise<any>;
|
||||
stream(callback: (readable: stream.PassThrough) => any): Bluebird<any>;
|
||||
stream(options?: { [key: string]: any }): stream.PassThrough;
|
||||
stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Promise<any>;
|
||||
stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Bluebird<any>;
|
||||
pipe(writable: any): stream.PassThrough;
|
||||
exec(callback: Function): QueryBuilder;
|
||||
}
|
||||
|
||||
interface Transaction extends Knex {
|
||||
savepoint(transactionScope: (trx: Transaction) => any): Promise<any>;
|
||||
savepoint(transactionScope: (trx: Transaction) => any): Bluebird<any>;
|
||||
commit(value?: any): QueryBuilder;
|
||||
rollback(error?: any): QueryBuilder;
|
||||
}
|
||||
@@ -392,14 +392,14 @@ declare namespace Knex {
|
||||
// Schema builder
|
||||
//
|
||||
|
||||
interface SchemaBuilder extends Promise<any> {
|
||||
interface SchemaBuilder extends Bluebird<any> {
|
||||
createTable(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder;
|
||||
createTableIfNotExists(tableName: string, callback: (tableBuilder: CreateTableBuilder) => any): SchemaBuilder;
|
||||
renameTable(oldTableName: string, newTableName: string): Promise<void>;
|
||||
renameTable(oldTableName: string, newTableName: string): Bluebird<void>;
|
||||
dropTable(tableName: string): SchemaBuilder;
|
||||
hasTable(tableName: string): Promise<boolean>;
|
||||
hasColumn(tableName: string, columnName: string): Promise<boolean>;
|
||||
table(tableName: string, callback: (tableBuilder: AlterTableBuilder) => any): Promise<void>;
|
||||
hasTable(tableName: string): Bluebird<boolean>;
|
||||
hasColumn(tableName: string, columnName: string): Bluebird<boolean>;
|
||||
table(tableName: string, callback: (tableBuilder: AlterTableBuilder) => any): Bluebird<void>;
|
||||
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<string>;
|
||||
latest(config?: MigratorConfig): Promise<any>;
|
||||
rollback(config?: MigratorConfig): Promise<any>;
|
||||
status(config?: MigratorConfig): Promise<number>;
|
||||
currentVersion(config?: MigratorConfig): Promise<string>;
|
||||
make(name: string, config?: MigratorConfig): Bluebird<string>;
|
||||
latest(config?: MigratorConfig): Bluebird<any>;
|
||||
rollback(config?: MigratorConfig): Bluebird<any>;
|
||||
status(config?: MigratorConfig): Bluebird<number>;
|
||||
currentVersion(config?: MigratorConfig): Bluebird<string>;
|
||||
}
|
||||
|
||||
interface FunctionHelper {
|
||||
|
||||
@@ -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('');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user