Skip to content

Commit 9198dca

Browse files
rtolsmaclaude
andcommitted
feat: Add types_only option to generate interfaces without queries
With `types_only: true`, output contains only the Args/Row interfaces, enum literal union types, and override imports — no driver imports, query constants, or query functions. This lets a frontend share the database types generated for a backend without pulling in a database driver dependency. Verified the types-only output compiles standalone under tsc --strict with verbatimModuleSyntax, including enum unions and override imports. Default output is unchanged. Fixes #71. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GFyrFwpJC2eivaBRyNBJqv
1 parent 6d4de97 commit 9198dca

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,23 @@ Note that overrides only change the generated *types* — converting values at
414414
runtime (for example, parsing a `numeric` string into a `Big`) is still up to
415415
your driver configuration.
416416

417+
### Types-only generation
418+
419+
Set `types_only: true` to generate only the `Args`/`Row` interfaces, enum
420+
union types, and override imports — no driver imports, query constants, or
421+
query functions. This is useful for sharing database types with a frontend
422+
that never runs the queries itself.
423+
424+
```yaml
425+
codegen:
426+
- out: src/types/db
427+
plugin: ts
428+
options:
429+
runtime: node
430+
driver: pg
431+
types_only: true
432+
```
433+
417434
## Development
418435

419436
If you want to build and test sqlc-gen-typescript locally, follow these steps:

‎src/app.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface Options {
5151
driver?: string;
5252
mysql2?: Mysql2Options
5353
overrides?: TypeOverride[];
54+
types_only?: boolean;
5455
}
5556

5657
interface Driver {
@@ -132,7 +133,9 @@ function codegen(input: GenerateRequest): GenerateResponse {
132133
}
133134

134135
for (const [filename, queries] of querymap.entries()) {
135-
const nodes = driver.preamble(queries);
136+
// With types_only, emit only interfaces, enums, and override imports —
137+
// no driver imports, query constants, or query functions
138+
const nodes = options.types_only ? [] : driver.preamble(queries);
136139
// Insert custom type imports and enum declarations after the driver's
137140
// own imports, before any other declarations
138141
let insertAt = 0;
@@ -157,13 +160,15 @@ function codegen(input: GenerateRequest): GenerateResponse {
157160
const lowerName = query.name[0].toLowerCase() + query.name.slice(1);
158161
const textName = `${lowerName}Query`;
159162

160-
nodes.push(
161-
queryDecl(
162-
textName,
163-
`-- name: ${query.name} ${query.cmd}
163+
if (!options.types_only) {
164+
nodes.push(
165+
queryDecl(
166+
textName,
167+
`-- name: ${query.name} ${query.cmd}
164168
${query.text}`
165-
)
166-
);
169+
)
170+
);
171+
}
167172

168173
let argIface = undefined;
169174
let returnIface = undefined;
@@ -176,7 +181,7 @@ ${query.text}`
176181
nodes.push(rowDecl(returnIface, driver, query.columns));
177182
}
178183

179-
switch (query.cmd) {
184+
switch (options.types_only ? "" : query.cmd) {
180185
case ":exec": {
181186
nodes.push(
182187
driver.execDecl(lowerName, textName, argIface, query.params)

0 commit comments

Comments
 (0)