Skip to content

Commit d7104a8

Browse files
committed
feat: make list queries included by default
Remove the conditional inclusion check for list queries in the SQL template, making them always generated. Update tests to reflect that ListUsers and other list queries are now part of the default generated queries rather than opt-in. Signed-off-by: Svetlin Ralchev <iamralch@users.noreply.github.com>
1 parent 53771ba commit d7104a8

5 files changed

Lines changed: 21 additions & 22 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ A CLI tool that generates [sqlc](https://sqlc.dev)-compatible SQL queries from y
1212
## Features
1313

1414
- Generate CRUD queries (`SELECT`, `INSERT`, `UPDATE`, `DELETE`) from a database schema catalog
15-
- Primary key CRUD operations generated by default — no configuration needed
16-
- Opt-in to additional queries (List, Copy, FK joins, non-unique index queries) via `queries` allowlist
15+
- Primary key CRUD and List operations generated by default — no configuration needed
16+
- Opt-in to additional queries (Copy, FK joins, non-unique index queries) via `queries` allowlist
1717
- Configurable via YAML — shares the same `sqlc.yaml` configuration file
1818
- Works as a standalone CLI or as part of a CI/CD pipeline
1919
- Supports custom query templates
@@ -61,20 +61,21 @@ sql:
6161
out: "ent/query"
6262
options:
6363
queries:
64-
- "ListUsers"
6564
- "CopyUsers"
6665
- "GetUserWithPost"
6766
```
6867
6968
### Default queries (always generated)
7069
71-
Primary key CRUD operations and their Exec/Batch variants are generated
72-
automatically for every table:
70+
Primary key CRUD operations, List queries, and their Exec/Batch variants are
71+
generated automatically for every table:
7372
7473
| Query | Description |
7574
| ------------------------- | ------------------------------------------ |
7675
| `Get<Table>` | Select a row by primary key |
7776
| `BatchGet<Tables>` | Batch select rows by primary key |
77+
| `List<Tables>` | Paginated list with filtering |
78+
| `List<Tables>By<Columns>` | Paginated list by non-unique index |
7879
| `Insert<Table>` | Insert a row |
7980
| `ExecInsert<Table>` | Insert a row (exec, returns affected rows) |
8081
| `BatchInsert<Tables>` | Batch insert rows |
@@ -94,12 +95,10 @@ These queries are only generated when explicitly listed in `options.queries`:
9495

9596
| Query | Description |
9697
| ------------------------------- | ---------------------------------------- |
97-
| `List<Tables>` | Paginated list with filtering |
9898
| `Copy<Tables>` | Bulk insert via PostgreSQL COPY protocol |
9999
| `Get<Table>With<Related>` | Select with FK join |
100100
| `BatchGet<Tables>With<Related>` | Batch select with FK join |
101101
| `Get<Table>By<Columns>` | Select by non-PK unique index |
102-
| `List<Tables>By<Columns>` | Paginated list by non-unique index |
103102
| `Update<Tables>By<Columns>` | Update by non-unique index |
104103
| `Delete<Tables>By<Columns>` | Delete by non-unique index |
105104

internal/sqlc/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var _ = Describe("Config", func() {
2424
Expect(config.SQL[0].Codegen[0].Plugin).To(Equal("gen-queries"))
2525
Expect(config.SQL[0].Codegen[0].Out).To(Equal("ent/query"))
2626
opts := config.SQL[0].GetOptions()
27-
Expect(opts.Queries).To(HaveLen(3))
28-
Expect(opts.Queries).To(ContainElements("ListUsers", "CopyUsers", "GetUserByEmail"))
27+
Expect(opts.Queries).To(HaveLen(2))
28+
Expect(opts.Queries).To(ContainElements("CopyUsers", "GetUserByEmail"))
2929
})
3030

3131
When("the file does not exist", func() {

internal/sqlc/config_test_exclude.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ sql:
1212
out: "ent/query"
1313
options:
1414
queries:
15-
- "ListUsers"
1615
- "CopyUsers"
1716
- "GetUserByEmail"

internal/sqlc/generator_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ var _ = Describe("Generator", func() {
6565
Expect(string(content)).To(ContainSubstring("name: UpdateUser :one"))
6666
Expect(string(content)).To(ContainSubstring("name: DeleteUser :one"))
6767

68+
// Default List queries are present
69+
Expect(string(content)).To(ContainSubstring("name: ListUsers :many"))
70+
6871
// Opt-in queries are not present without config
69-
Expect(string(content)).NotTo(ContainSubstring("name: ListUsers :many"))
7072
Expect(string(content)).NotTo(ContainSubstring("name: CopyUsers :copyfrom"))
7173
}
7274
})
@@ -103,7 +105,6 @@ var _ = Describe("Generator", func() {
103105
Out: dir,
104106
Options: sqlc.CodegenOptions{
105107
Queries: []string{
106-
"ListUsers",
107108
"CopyUsers",
108109
},
109110
},
@@ -130,8 +131,10 @@ var _ = Describe("Generator", func() {
130131
Expect(string(content)).To(ContainSubstring("name: UpdateUser :one"))
131132
Expect(string(content)).To(ContainSubstring("name: DeleteUser :one"))
132133

133-
// Opt-in queries that were listed are present
134+
// Default List queries are present
134135
Expect(string(content)).To(ContainSubstring("name: ListUsers :many"))
136+
137+
// Opt-in queries that were listed are present
135138
Expect(string(content)).To(ContainSubstring("name: CopyUsers :copyfrom"))
136139
}
137140
})
@@ -150,8 +153,10 @@ var _ = Describe("Generator", func() {
150153
Expect(string(content)).To(ContainSubstring("name: GetUser :one"))
151154
Expect(string(content)).To(ContainSubstring("name: InsertUser :one"))
152155

156+
// Default List queries are present
157+
Expect(string(content)).To(ContainSubstring("name: ListUsers :many"))
158+
153159
// Opt-in queries are not present
154-
Expect(string(content)).NotTo(ContainSubstring("name: ListUsers :many"))
155160
Expect(string(content)).NotTo(ContainSubstring("name: CopyUsers :copyfrom"))
156161
}
157162
})
@@ -170,8 +175,8 @@ var _ = Describe("Generator", func() {
170175
Expect(string(content)).To(ContainSubstring("name: GetUser :one"))
171176
Expect(string(content)).To(ContainSubstring("name: DeleteUser :one"))
172177

173-
// Opt-in queries are not present
174-
Expect(string(content)).NotTo(ContainSubstring("name: ListUsers :many"))
178+
// Default List queries are present
179+
Expect(string(content)).To(ContainSubstring("name: ListUsers :many"))
175180
}
176181
})
177182

@@ -189,8 +194,8 @@ var _ = Describe("Generator", func() {
189194
Expect(string(content)).To(ContainSubstring("name: GetUser :one"))
190195
Expect(string(content)).To(ContainSubstring("name: InsertUser :one"))
191196

192-
// Non-existent opt-in queries don't cause errors
193-
Expect(string(content)).NotTo(ContainSubstring("name: ListUsers :many"))
197+
// Default List queries are still present
198+
Expect(string(content)).To(ContainSubstring("name: ListUsers :many"))
194199
}
195200
})
196201
})

internal/sqlc/template/template.sql.tmpl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ INSERT INTO {{.Table.Name}} (
264264
);
265265
{{- end}}
266266
{{- $query_name := printf "List%s" (table_name .Table.Name "many")}}
267-
{{- if should_include . $query_name}}
268267

269268
-- List{{table_name .Table.Name "many"}} retrieves a paginated list of rows from '{{$.Table.Name}}'.
270269
--
@@ -298,10 +297,8 @@ LIMIT
298297
sqlc.narg(page_limit)::int
299298
OFFSET
300299
sqlc.narg(page_offset)::int;
301-
{{- end}}
302300

303301
{{range $idx, $key := .Table.GetNonUniqueIndexes}}{{- $query_name := printf "List%s%s" (table_name $.Table.Name "many") (query_index $key)}}
304-
{{- if should_include $ $query_name}}
305302

306303
-- List{{table_name $.Table.Name "many"}}{{query_index $key}} retrieves a paginated list of rows from '{{$.Table.Name}}'{{if $key.Name}} filtered by {{$key.Name}}{{end}}.
307304
--
@@ -341,7 +338,6 @@ LIMIT
341338
sqlc.narg(page_limit)::int
342339
OFFSET
343340
sqlc.narg(page_offset)::int;
344-
{{- end}}
345341

346342
{{- $query_name := printf "Update%s%s" (table_name $.Table.Name "many") (query_index $key)}}
347343
{{- if should_include $ $query_name}}

0 commit comments

Comments
 (0)