Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@
"bentocache": "patches/bentocache.patch",
"@graphql-codegen/schema-ast": "patches/@graphql-codegen__schema-ast.patch",
"@fastify/vite": "patches/@fastify__vite.patch",
"@slonik/pg-driver": "patches/@slonik__pg-driver.patch",
"slonik": "patches/slonik.patch"
"@slonik/pg-driver": "patches/@slonik__pg-driver.patch"
},
"onlyBuiltDependencies": [
"msw"
Expand Down
6 changes: 3 additions & 3 deletions packages/internal/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
},
"dependencies": {
"@standard-schema/spec": "1.0.0",
"slonik": "48.13.2",
"slonik-interceptor-query-logging": "48.13.2",
"slonik-sql-tag-raw": "48.13.2",
"slonik": "49.10.7",
"slonik-interceptor-query-logging": "49.10.7",
"slonik-sql-tag-raw": "49.10.7",
"zod": "3.25.76"
},
"devDependencies": {
Expand Down
23 changes: 14 additions & 9 deletions packages/internal/postgres/src/postgres-database-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { PgPoolBridge } from './pg-pool-bridge';

const tracer = trace.getTracer('storage');

type InferColumnValue<T extends StandardSchemaV1> =
StandardSchemaV1.InferOutput<T> extends Record<string, unknown>
? StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]
: unknown;
Comment on lines +19 to +22

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If StandardSchemaV1.InferOutput<T> is a union type (e.g., { id: string } | null), the conditional type StandardSchemaV1.InferOutput<T> extends Record<string, unknown> is not distributive because StandardSchemaV1.InferOutput<T> is not a naked type parameter. This causes the type to resolve to unknown instead of distributing over the union (e.g., string | null).

Using a distributive helper type resolves this issue and preserves type safety for union or nullable schema outputs.

type DistributiveInferColumnValue<O> = O extends Record<string, unknown>
  ? O[keyof O]
  : O extends null | undefined
    ? O
    : unknown;

type InferColumnValue<T extends StandardSchemaV1> = DistributiveInferColumnValue<
  StandardSchemaV1.InferOutput<T>
>;


export interface CommonQueryMethods {
exists<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
Expand All @@ -33,19 +38,19 @@ export interface CommonQueryMethods {
oneFirst<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]>;
): Promise<InferColumnValue<T>>;
one<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<StandardSchemaV1.InferOutput<T>>;
anyFirst<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<ReadonlyArray<StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]>>;
): Promise<ReadonlyArray<InferColumnValue<T>>>;
maybeOneFirst<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<null | StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]>;
): Promise<null | InferColumnValue<T>>;
}

export class PostgresDatabasePool implements CommonQueryMethods {
Expand Down Expand Up @@ -88,14 +93,14 @@ export class PostgresDatabasePool implements CommonQueryMethods {
async oneFirst<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]> {
): Promise<InferColumnValue<T>> {
return await this.pool.oneFirst(sql, values);
}

async maybeOneFirst<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<null | StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]> {
): Promise<null | InferColumnValue<T>> {
return await this.pool.maybeOneFirst(sql, values);
}

Expand All @@ -109,9 +114,7 @@ export class PostgresDatabasePool implements CommonQueryMethods {
async anyFirst<T extends StandardSchemaV1>(
sql: QuerySqlToken<T>,
values?: PrimitiveValueExpression[],
): Promise<
ReadonlyArray<StandardSchemaV1.InferOutput<T>[keyof StandardSchemaV1.InferOutput<T>]>
> {
): Promise<ReadonlyArray<InferColumnValue<T>>> {
return await this.pool.anyFirst(sql, values);
}

Expand Down Expand Up @@ -190,9 +193,11 @@ export async function createPostgresDatabasePool(args: {
interceptors: dbInterceptors.concat(args.additionalInterceptors ?? []),
typeParsers,
captureStackTrace: false,
maximumPoolSize: args.maximumPoolSize,
maxPoolSize: args.maximumPoolSize,
idleTimeout: 30000,
statementTimeout: args.statementTimeout,
// we have our own tracing setup that is less verbose
tracing: false,
});

function interceptError<K extends Exclude<keyof SlonikCommonQueryMethods, 'transaction'>>(
Expand Down
242 changes: 0 additions & 242 deletions patches/slonik.patch

This file was deleted.

Loading
Loading