Skip to content

Commit

Permalink
Give ...First functions more intuitive typeargs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Oct 20, 2020
1 parent 40f8dde commit afe34f9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const getFooIdByBar = async (connection: DatabaseConnectionType, bar: string): P

```js
const getFooIdByBar = (connection: DatabaseConnectionType, bar: string): Promise<DatabaseRecordIdType> => {
return connection.oneFirst<DatabaseRecordType>(sql`
return connection.oneFirst<number>(sql`
SELECT id
FROM foo
WHERE bar = ${bar}
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,15 @@ export type QueryMethodType<RowType, Result> = (
) => Promise<Result>;
export type QueryMethodParams<T> = Parameters<QueryMethodType<T, never>>;

export type QueryAnyFirstFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<Array<T[keyof T]>>;
export type QueryAnyFirstFunctionType = <T = QueryResultRowColumnType, Row = Record<string, T>>(...args: QueryMethodParams<Row>) => Promise<Array<Row[keyof Row]>>;
export type QueryAnyFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<readonly T[]>;
export type QueryExistsFunctionType = (...args: QueryMethodParams<unknown>) => Promise<boolean>;
export type QueryFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<QueryResultType<T>>;
export type QueryManyFirstFunctionType = QueryAnyFirstFunctionType;
export type QueryManyFunctionType = QueryAnyFunctionType;
export type QueryMaybeOneFirstFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<T[keyof T] | null>;
export type QueryMaybeOneFirstFunctionType = <T = QueryResultRowColumnType, Row = Record<string, T>>(...args: QueryMethodParams<Row>) => Promise<Row[keyof Row] | null>;
export type QueryMaybeOneFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<T | null>;
export type QueryOneFirstFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<T[keyof T]>;
export type QueryOneFirstFunctionType = <T = QueryResultRowColumnType, Row = Record<string, T>>(...args: QueryMethodParams<Row>) => Promise<Row[keyof Row]>;
export type QueryOneFunctionType = <T = ExternalQueryResultRowType>(...args: QueryMethodParams<T>) => Promise<T>;

export type InterceptorType = {
Expand Down
4 changes: 4 additions & 0 deletions test/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ const poolTypes = () => {
expectTypeOf(await pool.maybeOne(getFooBarQuery(10))).toEqualTypeOf<FooBar | null>();

expectTypeOf(await pool.any(getFooBarQuery(10))).toEqualTypeOf<readonly FooBar[]>();

expectTypeOf(await pool.anyFirst(getFooQuery(10))).toEqualTypeOf<string[]>();

expectTypeOf(await pool.anyFirst(getFooBarQuery(10))).toEqualTypeOf<Array<string | number>>();
};

createPool('postgres://localhost', {
Expand Down
21 changes: 15 additions & 6 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ export default async () => {
const oneFirst = await client.oneFirst(sql`select true`);
expectTypeOf(oneFirst).toEqualTypeOf<PrimitiveValueExpressionType>();

const oneFirstTyped = await client.oneFirst<Row>(sql`select true`);
expectTypeOf(oneFirstTyped).toEqualTypeOf<string | boolean>();
const oneFirstTyped = await client.oneFirst<boolean>(sql`select true`);
expectTypeOf(oneFirstTyped).toEqualTypeOf<boolean>();

const oneFirstTypedQuery = await client.oneFirst(sql<Row>`select true`);
expectTypeOf(oneFirstTypedQuery).toEqualTypeOf<string | boolean>();

const maybeOneFirst = await client.maybeOneFirst(sql`select true`);
expectTypeOf(maybeOneFirst).toEqualTypeOf<PrimitiveValueExpressionType>();

const maybeOneFirstTyped = await client.maybeOneFirst<Row>(sql`select true`);
expectTypeOf(maybeOneFirstTyped).toEqualTypeOf<string | boolean | null>();
const maybeOneFirstTyped = await client.maybeOneFirst<boolean>(sql`select true`);
expectTypeOf(maybeOneFirstTyped).toEqualTypeOf<boolean | null>();

const maybeOneFirstTypedQuery = await client.maybeOneFirst(sql<Row>`select true`);
expectTypeOf(maybeOneFirstTypedQuery).toEqualTypeOf<string | boolean | null>();

const many = await client.many(sql`select true`);
expectTypeOf(many).toEqualTypeOf<readonly Record<string, PrimitiveValueExpressionType>[]>();
Expand All @@ -55,8 +61,11 @@ export default async () => {
const manyFirst = await client.manyFirst(sql`select true`);
expectTypeOf(manyFirst).toEqualTypeOf<PrimitiveValueExpressionType[]>();

const manyFirstTyped = await client.manyFirst<Row>(sql`select true`);
expectTypeOf(manyFirstTyped).toEqualTypeOf<Array<string | boolean>>();
const manyFirstTyped = await client.manyFirst<boolean>(sql`select true`);
expectTypeOf(manyFirstTyped).toEqualTypeOf<boolean[]>();

const manyFirstTypedQuery = await client.manyFirst(sql<Row>`select true`);
expectTypeOf(manyFirstTypedQuery).toEqualTypeOf<Array<string | boolean>>();

expectTypeOf(client.any).toEqualTypeOf(client.many);
expectTypeOf(client.anyFirst).toEqualTypeOf(client.manyFirst);
Expand Down

0 comments on commit afe34f9

Please sign in to comment.