Skip to content

Commit c1de3d8

Browse files
authored
Fix and improve sql() helper types (#338)
1 parent e0483a5 commit c1de3d8

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

types/index.d.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ interface BaseOptions<T extends JSToPostgresTypeMap> {
112112
*/
113113
publications: string
114114
onclose: (connId: number) => void;
115-
backoff: boolean | ((attemptNum:number) => number);
115+
backoff: boolean | ((attemptNum: number) => number);
116116
max_lifetime: number | null;
117117
keep_alive: number | null;
118118
}
@@ -157,7 +157,7 @@ type Keys = string
157157

158158
type SerializableObject<T, K extends any[]> =
159159
number extends K['length'] ? {} :
160-
Record<Keys & (keyof T) & (K['length'] extends 0 ? string : K[number]), postgres.SerializableParameter>
160+
(Record<Keys & (keyof T) & (K['length'] extends 0 ? string : K[number]), postgres.SerializableParameter | postgres.JSONValue> & Record<string, any>)
161161

162162
type First<T, K extends any[]> =
163163
// Tagged template string call
@@ -167,9 +167,9 @@ type First<T, K extends any[]> =
167167
// Dynamic values helper (depth 2)
168168
T extends readonly any[][] ? postgres.EscapableArray[] :
169169
// Insert/update helper (depth 2)
170-
T extends (object & infer R)[] ? SerializableObject<R, K>[] :
171-
// Dynamic values helper (depth 1)
172-
T extends readonly any[] ? postgres.EscapableArray :
170+
T extends readonly (object & infer R)[] ? (R extends postgres.SerializableParameter ? readonly postgres.SerializableParameter[] : readonly SerializableObject<R, K>[]) :
171+
// Dynamic values/ANY helper (depth 1)
172+
T extends readonly any[] ? (readonly postgres.SerializableParameter[]) :
173173
// Insert/update helper (depth 1)
174174
T extends object ? SerializableObject<T, K> :
175175
// Unexpected type
@@ -179,7 +179,7 @@ type Rest<T> =
179179
T extends TemplateStringsArray ? never : // force fallback to the tagged template function overload
180180
T extends string ? string[] :
181181
T extends readonly any[][] ? [] :
182-
T extends (object & infer R)[] ? (Keys & keyof R)[] :
182+
T extends readonly (object & infer R)[] ? (Keys & keyof R)[] :
183183
T extends readonly any[] ? [] :
184184
T extends object ? (Keys & keyof T)[] :
185185
any
@@ -454,10 +454,10 @@ declare namespace postgres {
454454
| null
455455
| boolean
456456
| number
457+
| bigint // weak: require the `postgres.BigInt` type
457458
| string
458459
| Date
459-
| Uint8Array
460-
| bigint;
460+
| Uint8Array;
461461

462462
type SerializableParameter = never
463463
| Serializable
@@ -466,6 +466,20 @@ declare namespace postgres {
466466
| ArrayParameter
467467
| readonly SerializableParameter[];
468468

469+
type JSONValue = // using a dedicated type to detect symbols, bigints, and other non serializable types
470+
| null
471+
| string
472+
| number
473+
| Date // serialized as `string`
474+
| JSONValue[]
475+
| { toJSON(): any } // `toJSON` called by `JSON.stringify`; not typing the return type, typings is strict enough anyway
476+
| {
477+
[prop: string | number]:
478+
| undefined
479+
| JSONValue
480+
| ((...args: any) => any) // serialized as `undefined`
481+
};
482+
469483
interface Row {
470484
[column: string]: any;
471485
}
@@ -574,7 +588,7 @@ declare namespace postgres {
574588
* @param parameters Interpoled values of the template string
575589
* @returns A promise resolving to the result of your query
576590
*/
577-
<T extends readonly any[] = Row[]>(template: TemplateStringsArray, ...parameters: (SerializableParameter | PendingQuery<any>)[]): PendingQuery<AsRowList<T>>;
591+
<T extends readonly object[] = Row[]>(template: TemplateStringsArray, ...parameters: (SerializableParameter | PendingQuery<any>)[]): PendingQuery<AsRowList<T>>;
578592

579593
CLOSE: {};
580594
END: this['CLOSE'];
@@ -604,7 +618,7 @@ declare namespace postgres {
604618
array<T extends SerializableParameter[] = SerializableParameter[]>(value: T, type?: number): ArrayParameter<T>;
605619
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, options?: { cache?: boolean }): PendingQuery<AsRowList<T>>;
606620
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, args: SerializableParameter[], options?: { cache?: boolean }): PendingQuery<AsRowList<T>>;
607-
json(value: any): Parameter;
621+
json(value: JSONValue): Parameter;
608622
}
609623

610624
interface UnsafeQueryOptions {

0 commit comments

Comments
 (0)