Add support for native sql tagged templates in discover by haydn · Pull Request #39 · ilbertt/bun-sqlgen · GitHub
Skip to content

Add support for native sql tagged templates in discover - #39

Open
haydn wants to merge 1 commit into
ilbertt:mainfrom
haydn:support-native-sql-generics
Open

haydn wants to merge 1 commit into
ilbertt:mainfrom
haydn:support-native-sql-generics

Conversation

@haydn

@haydn haydn commented Sep 20, 2026

Copy link
Copy Markdown

Hey @ilbertt. Love the tool. This PR is a bit of an experiment/proposal for an alternative way of discovering the SQL tagged-templates.

The gist of it is this:

import type { Queries } from '#queries.gen.ts';
import { sql } from 'bun';

const messages = await sql<Queries['ListMessages'][]>`
  SELECT
    id, body, created_at
  FROM
    messages
  ORDER BY created_at DESC
`;

IMO, there are two advantages to this approach:

  1. There's no runtime code needed (bun-sqlgen would be a dev-only dependency).
  2. The sql<Type>`SELECT now()` pattern is better recognised by other tooling (syntax highlighters, formatters, code completions etc).

@haydn
haydn force-pushed the support-native-sql-generics branch from 12e4586 to 2ef3053 Compare September 20, 2026 03:32

@ilbertt ilbertt left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Hey @haydn, thanks for the PR!

I think we should just have one way to type the query to avoid introducing complexity and more documentation. I'm not attached to the current named tag approach, and I really like the zero runtime advantage that your approach would introduce.

What I like about the current named tag approach is that the flow feels more "natural": I assign the name to the query, and the generator picks it up to generate its type. In your approach, the generator creates the type because you referenced it, which feels weird.

Also, I'm a bit scared that we would have to handle many edge cases like type aliases (e.g. import type { Queries as Q }...).

What are your thoughts?


By the way, #40 moved all the advanced cases from the simple (which has also been renamed into "basic" in #42) example to the advanced example, in case you want to update your branch.

@haydn

haydn commented Sep 23, 2026

Copy link
Copy Markdown
Author

@ilbertt

ilbertt commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Thinking about it more, I’m leaning toward your native generics as the single approach, especially for the zero runtime dependencies.

They both feel a bit weird IMO. Either way your code is invalid until the generator runs because whatever you're writing is the canonical reference (slight caveat: in the typed version of this PR the code would be executable, but have invalid types).

Fair point!

Another idea I had (and I'm not sure it's even viable) would be to use the query itself as the way of identifying it (so the developer doesn't need to name it at all). The idea is the types that are generated overload Bun's sql function with signatures based on the actual SQL strings and arguments.

Yeah it would be nice but I think this is not possible with the current version of TypeScript (see microsoft/TypeScript#33304)

Yeah, that's a valid concern. I guess that problem can exist irrespective of the approach though?

Yeah, I should have been more specific: SQL as Blah already works because we resolve Bun’s SQL type semantically, although your example does expose a separate gap with destructured query tags. My concern here is that the PR matches the identifier Queries literally, so import type { Queries as Q } would stop discovery.

Could we handle that import alias and verify discovery still works before the generated file exists? Those are the main cases I’d want to check before moving toward native generics as the single approach.

@haydn

haydn commented Oct 1, 2026

Copy link
Copy Markdown
Author

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants