Skip to content

Commit 1fe7362

Browse files
authored
Updates to neon-http for @neondatabase/serverless@1.0.0, when released (#4237)
* Updates for @neondatabase/serverless@1.0.0 compatibility * Clearer comments * Linting
1 parent 53f578c commit 1fe7362

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

drizzle-orm/src/neon-http/session.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const queryConfig = {
2626

2727
export class NeonHttpPreparedQuery<T extends PreparedQueryConfig> extends PgPreparedQuery<T> {
2828
static override readonly [entityKind]: string = 'NeonHttpPreparedQuery';
29+
private clientQuery: (sql: string, params: any[], opts: Record<string, any>) => NeonQueryPromise<any, any>;
2930

3031
constructor(
3132
private client: NeonHttpClient,
@@ -36,6 +37,10 @@ export class NeonHttpPreparedQuery<T extends PreparedQueryConfig> extends PgPrep
3637
private customResultMapper?: (rows: unknown[][]) => T['execute'],
3738
) {
3839
super(query);
40+
// `client.query` is for @neondatabase/serverless v1.0.0 and up, where the
41+
// root query function `client` is only usable as a template function;
42+
// `client` is a fallback for earlier versions
43+
this.clientQuery = (client as any).query ?? client as any;
3944
}
4045

4146
async execute(placeholderValues: Record<string, unknown> | undefined): Promise<T['execute']>;
@@ -50,10 +55,10 @@ export class NeonHttpPreparedQuery<T extends PreparedQueryConfig> extends PgPrep
5055

5156
this.logger.logQuery(this.query.sql, params);
5257

53-
const { fields, client, query, customResultMapper } = this;
58+
const { fields, clientQuery, query, customResultMapper } = this;
5459

5560
if (!fields && !customResultMapper) {
56-
return client(
61+
return clientQuery(
5762
query.sql,
5863
params,
5964
token === undefined
@@ -65,7 +70,7 @@ export class NeonHttpPreparedQuery<T extends PreparedQueryConfig> extends PgPrep
6570
);
6671
}
6772

68-
const result = await client(
73+
const result = await clientQuery(
6974
query.sql,
7075
params,
7176
token === undefined
@@ -96,7 +101,7 @@ export class NeonHttpPreparedQuery<T extends PreparedQueryConfig> extends PgPrep
96101
all(placeholderValues: Record<string, unknown> | undefined = {}): Promise<T['all']> {
97102
const params = fillPlaceholders(this.query.params, placeholderValues);
98103
this.logger.logQuery(this.query.sql, params);
99-
return this.client(
104+
return this.clientQuery(
100105
this.query.sql,
101106
params,
102107
this.authToken === undefined ? rawQueryConfig : {
@@ -113,7 +118,7 @@ export class NeonHttpPreparedQuery<T extends PreparedQueryConfig> extends PgPrep
113118
values(placeholderValues: Record<string, unknown> | undefined = {}, token?: NeonAuthToken): Promise<T['values']> {
114119
const params = fillPlaceholders(this.query.params, placeholderValues);
115120
this.logger.logQuery(this.query.sql, params);
116-
return this.client(this.query.sql, params, { arrayMode: true, fullResults: true, authToken: token }).then((
121+
return this.clientQuery(this.query.sql, params, { arrayMode: true, fullResults: true, authToken: token }).then((
117122
result,
118123
) => result.rows);
119124
}
@@ -134,6 +139,7 @@ export class NeonHttpSession<
134139
> extends PgSession<NeonHttpQueryResultHKT, TFullSchema, TSchema> {
135140
static override readonly [entityKind]: string = 'NeonHttpSession';
136141

142+
private clientQuery: (sql: string, params: any[], opts: Record<string, any>) => NeonQueryPromise<any, any>;
137143
private logger: Logger;
138144

139145
constructor(
@@ -143,6 +149,10 @@ export class NeonHttpSession<
143149
private options: NeonHttpSessionOptions = {},
144150
) {
145151
super(dialect);
152+
// `client.query` is for @neondatabase/serverless v1.0.0 and up, where the
153+
// root query function `client` is only usable as a template function;
154+
// `client` is a fallback for earlier versions
155+
this.clientQuery = (client as any).query ?? client as any;
146156
this.logger = options.logger ?? new NoopLogger();
147157
}
148158

@@ -168,13 +178,12 @@ export class NeonHttpSession<
168178
) {
169179
const preparedQueries: PreparedQuery[] = [];
170180
const builtQueries: NeonQueryPromise<any, true>[] = [];
171-
172181
for (const query of queries) {
173182
const preparedQuery = query._prepare();
174183
const builtQuery = preparedQuery.getQuery();
175184
preparedQueries.push(preparedQuery);
176185
builtQueries.push(
177-
this.client(builtQuery.sql, builtQuery.params, {
186+
this.clientQuery(builtQuery.sql, builtQuery.params, {
178187
fullResults: true,
179188
arrayMode: preparedQuery.isResponseInArrayMode(),
180189
}),
@@ -189,7 +198,7 @@ export class NeonHttpSession<
189198
// change return type to QueryRows<true>
190199
async query(query: string, params: unknown[]): Promise<FullQueryResults<true>> {
191200
this.logger.logQuery(query, params);
192-
const result = await this.client(query, params, { arrayMode: true, fullResults: true });
201+
const result = await this.clientQuery(query, params, { arrayMode: true, fullResults: true });
193202
return result;
194203
}
195204

@@ -198,7 +207,7 @@ export class NeonHttpSession<
198207
query: string,
199208
params: unknown[],
200209
): Promise<FullQueryResults<false>> {
201-
return this.client(query, params, { arrayMode: false, fullResults: true });
210+
return this.clientQuery(query, params, { arrayMode: false, fullResults: true });
202211
}
203212

204213
override async count(sql: SQL): Promise<number>;

0 commit comments

Comments
 (0)