drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Infer result type from the toSQL method

Open
#2,597 0 comments 2 reactions 0 assignees View on GitHub
enhancement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Currently, the `toSQL` method returns a `Query` object containing the SQL and parameters for a constructed query, but it is not possible to infer the query result from the `Query` type if that SQL were executed.

To enable this, I would like to propose modifying the `Query` interface with an optional type argument that represents the query's result, like this:

```ts
// drizzle-orm/src/sql/sql.ts
export interface Query {
sql: string;
params: unknown[];
}
```

The type representing the query's result can then be passed in to the implementations of `toSQL`, using the already-existing result types present in the objects where `toSQL` is defined (in sqlite-core, it is called `TResult`).

```ts
// drizzle-orm/src/sqlite-core/query-builders/select.ts
toSQL(): Query;
```

This would be a valuable addition for functions that are utilizing `toSQL`, allowing them to access and augment the result type of some SQL. I am willing to help with making this change if you find it acceptable.

Contributor guide

Open the contributing guide

Research direction

Start in drizzle-orm/src/sql/sql.ts and compare the existing TResult types in drizzle-orm/src/sqlite-core/query-builders/select.ts with the other toSQL implementations. Done means Query carries the result type through these implementations and callers can infer it without breaking existing uses.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql, typescript
Domain
backend-api-design, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.