mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-02 00:00:04 +00:00
fix slonik query type
also add some generics and new options on client configuration
This commit is contained in:
45
types/slonik/index.d.ts
vendored
45
types/slonik/index.d.ts
vendored
@@ -79,18 +79,18 @@ export interface FieldType {
|
||||
}
|
||||
|
||||
export type DatabaseTransactionConnectionType = CommonQueryMethodsType & {
|
||||
transaction: (handler: TransactionFunctionType) => Promise<unknown>;
|
||||
transaction: <T>(handler: TransactionFunctionType<T>) => Promise<T>;
|
||||
};
|
||||
|
||||
export type DatabasePoolConnectionType = CommonQueryMethodsType & {
|
||||
transaction: (handler: TransactionFunctionType) => Promise<unknown>;
|
||||
transaction: <T>(handler: TransactionFunctionType<T>) => Promise<T>;
|
||||
};
|
||||
|
||||
export type ConnectionRoutineType = (connection: DatabasePoolConnectionType) => Promise<unknown>;
|
||||
export type ConnectionRoutineType<T> = (connection: DatabasePoolConnectionType) => Promise<T>;
|
||||
|
||||
export type DatabasePoolType = CommonQueryMethodsType & {
|
||||
connect: (connectionRoutine: ConnectionRoutineType) => Promise<unknown>;
|
||||
transaction: (handler: TransactionFunctionType) => Promise<unknown>;
|
||||
connect: <T>(connectionRoutine: ConnectionRoutineType<T>) => Promise<T>;
|
||||
transaction: <T>(handler: TransactionFunctionType<T>) => Promise<T>;
|
||||
};
|
||||
|
||||
export type DatabaseConfigurationType =
|
||||
@@ -152,7 +152,7 @@ export interface NoticeType {
|
||||
where: string;
|
||||
}
|
||||
|
||||
export type QueryResultType<T> = Readonly<{
|
||||
export interface QueryResultType<T> {
|
||||
command: 'DELETE' | 'INSERT' | 'SELECT' | 'UPDATE';
|
||||
fields: ReadonlyArray<FieldType>;
|
||||
notices: ReadonlyArray<NoticeType>,
|
||||
@@ -160,7 +160,7 @@ export type QueryResultType<T> = Readonly<{
|
||||
rowAsArray: boolean;
|
||||
rowCount: number;
|
||||
rows: ReadonlyArray<T>;
|
||||
}>;
|
||||
}
|
||||
|
||||
export type QueryResultRowColumnType = string | number;
|
||||
export type QueryResultRowType<ColumnName extends string = string> = {
|
||||
@@ -170,7 +170,7 @@ export type QueryResultRowType<ColumnName extends string = string> = {
|
||||
// TODO: Infer column names via generic
|
||||
export type QueryAnyFirstFunctionType = QueryMethodType<QueryResultRowColumnType[]>;
|
||||
export type QueryAnyFunctionType = QueryMethodType<QueryResultRowType[]>;
|
||||
export type QueryFunctionType = QueryMethodType<QueryResultRowType>;
|
||||
export type QueryFunctionType = QueryMethodType<QueryResultType<QueryResultRowType>>;
|
||||
export type QueryManyFirstFunctionType = QueryMethodType<QueryResultRowColumnType[]>;
|
||||
export type QueryManyFunctionType = QueryMethodType<QueryResultRowType[]>;
|
||||
export type QueryMaybeOneFirstFunctionType = QueryMethodType<QueryResultRowColumnType>;
|
||||
@@ -280,9 +280,9 @@ export function createPool(
|
||||
//
|
||||
// TRANSACTION
|
||||
// ----------------------------------------------------------------------
|
||||
export type TransactionFunctionType = (
|
||||
export type TransactionFunctionType<T> = (
|
||||
connection: DatabaseTransactionConnectionType
|
||||
) => Promise<unknown>;
|
||||
) => Promise<T>;
|
||||
|
||||
//
|
||||
// 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<T = unknown> {
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -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<string>
|
||||
// $ExpectType QueryResultType<QueryResultRowType<string>>
|
||||
result;
|
||||
// $ExpectType QueryResultRowType<string>
|
||||
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<QueryResultRowType<string>>
|
||||
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}'`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user