From 92252e694e751c983d0daf5c72c6bcbd12efd006 Mon Sep 17 00:00:00 2001 From: mmkal Date: Tue, 9 Apr 2019 10:34:25 -0400 Subject: [PATCH] fix slonik query type also add some generics and new options on client configuration --- types/slonik/index.d.ts | 45 ++++++++++++++++++++---------------- types/slonik/slonik-tests.ts | 14 ++++++++++- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/types/slonik/index.d.ts b/types/slonik/index.d.ts index 228d6a12d7..7ab870b80a 100644 --- a/types/slonik/index.d.ts +++ b/types/slonik/index.d.ts @@ -79,18 +79,18 @@ export interface FieldType { } export type DatabaseTransactionConnectionType = CommonQueryMethodsType & { - transaction: (handler: TransactionFunctionType) => Promise; + transaction: (handler: TransactionFunctionType) => Promise; }; export type DatabasePoolConnectionType = CommonQueryMethodsType & { - transaction: (handler: TransactionFunctionType) => Promise; + transaction: (handler: TransactionFunctionType) => Promise; }; -export type ConnectionRoutineType = (connection: DatabasePoolConnectionType) => Promise; +export type ConnectionRoutineType = (connection: DatabasePoolConnectionType) => Promise; export type DatabasePoolType = CommonQueryMethodsType & { - connect: (connectionRoutine: ConnectionRoutineType) => Promise; - transaction: (handler: TransactionFunctionType) => Promise; + connect: (connectionRoutine: ConnectionRoutineType) => Promise; + transaction: (handler: TransactionFunctionType) => Promise; }; export type DatabaseConfigurationType = @@ -152,7 +152,7 @@ export interface NoticeType { where: string; } -export type QueryResultType = Readonly<{ +export interface QueryResultType { command: 'DELETE' | 'INSERT' | 'SELECT' | 'UPDATE'; fields: ReadonlyArray; notices: ReadonlyArray, @@ -160,7 +160,7 @@ export type QueryResultType = Readonly<{ rowAsArray: boolean; rowCount: number; rows: ReadonlyArray; -}>; +} export type QueryResultRowColumnType = string | number; export type QueryResultRowType = { @@ -170,7 +170,7 @@ export type QueryResultRowType = { // TODO: Infer column names via generic export type QueryAnyFirstFunctionType = QueryMethodType; export type QueryAnyFunctionType = QueryMethodType; -export type QueryFunctionType = QueryMethodType; +export type QueryFunctionType = QueryMethodType>; export type QueryManyFirstFunctionType = QueryMethodType; export type QueryManyFunctionType = QueryMethodType; export type QueryMaybeOneFirstFunctionType = QueryMethodType; @@ -280,9 +280,9 @@ export function createPool( // // TRANSACTION // ---------------------------------------------------------------------- -export type TransactionFunctionType = ( +export type TransactionFunctionType = ( connection: DatabaseTransactionConnectionType -) => Promise; +) => Promise; // // INTERCEPTOR @@ -361,7 +361,21 @@ export function createTypeParserPreset(): TypeParserType[]; // CLIENT // ---------------------------------------------------------------------- export interface ClientConfigurationType { + /** Dictates whether to capture stack trace before executing query. Middlewares access stack trace through query execution context. (Default: true) */ captureStackTrace?: boolean; + + /** Timeout (in milliseconds) after which an error is raised if cannot cannot be established. (Default: 5000) */ + connectionTimeout?: number; + + /** Timeout (in milliseconds) after which idle clients are closed. (Default: 5000) */ + idleTimeout?: number; + + /** Do not allow more than this many connections. (Default: 10) */ + maximumPoolSize?: number; + + /** Add more server connections to pool if below this number. (Default: 1) */ + minimumPoolSize?: number; + /** * An array of [Slonik interceptors](https://github.com/gajus/slonik#slonik-interceptors) */ @@ -372,16 +386,7 @@ export interface ClientConfigurationType { typeParsers?: TypeParserType[]; } -export interface ClientUserConfigurationType { - /** - * An array of [Slonik interceptors](https://github.com/gajus/slonik#slonik-interceptors) - */ - interceptors?: InterceptorType[]; - /** - * An array of [Slonik type parsers](https://github.com/gajus/slonik#slonik-type-parsers) - */ - typeParsers?: TypeParserType[]; -} +export interface ClientUserConfigurationType extends ClientConfigurationType {} // // ERRORS diff --git a/types/slonik/slonik-tests.ts b/types/slonik/slonik-tests.ts index 30caf65f33..aa2b73932b 100644 --- a/types/slonik/slonik-tests.ts +++ b/types/slonik/slonik-tests.ts @@ -32,10 +32,13 @@ const VALUE = 'foo'; // ---------------------------------------------------------------------- const pool = createPool('postgres://localhost'); +// $ExpectType Promise<{ connectResult: string; }> pool.connect(async (connection) => { const result = await connection.query(sql`SELECT 1`); - // $ExpectType QueryResultRowType + // $ExpectType QueryResultType> result; + // $ExpectType QueryResultRowType + result.rows[0]; connection.query(sql` SELECT 1 @@ -53,11 +56,18 @@ pool.connect(async (connection) => { await connection.one(sql`SELECT foo`); await connection.oneFirst(sql`SELECT foo`); + // Disallow raw strings + // $ExpectError + await connection.query(`SELECT foo`); + + // $ExpectType { transactionResult: string; } await connection.transaction(async (transactionConnection) => { await transactionConnection.query(sql`INSERT INTO foo (bar) VALUES ('baz')`); await transactionConnection.query(sql`INSERT INTO qux (quux) VALUES ('corge')`); + return { transactionResult: 'foo' }; }); + // $ExpectType QueryResultType> await connection.transaction(async (t1) => { await t1.query(sql`INSERT INTO foo (bar) VALUES ('baz')`); @@ -66,6 +76,7 @@ pool.connect(async (connection) => { }); }); + // $ExpectType void await connection.transaction(async (t1) => { await t1.query(sql`INSERT INTO foo (bar) VALUES ('baz')`); @@ -77,6 +88,7 @@ pool.connect(async (connection) => { }); } catch (error) { /* empty */ } }); + return { connectResult: 'foo' }; }); pool.query(sql`SELECT * FROM table WHERE name = '${VALUE}'`);