Skip to content

Commit d712f96

Browse files
committed
Document type overrides and rename support
1 parent fcef3e6 commit d712f96

File tree

1 file changed

+89
-18
lines changed

1 file changed

+89
-18
lines changed

README.md

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
> 🚨
22
>
3-
> sqlc is **very new** and under rapid development.
3+
> sqlc is **new** and under rapid development.
44
>
5-
> The code it generates is correct and safe for production use, but
6-
> there is currently no guarantee of stability or backwards-compatibility of
7-
> the command line interface, configuration file format or generated code.
5+
> The code it generates is correct and safe for production use, but there is
6+
> currently no guarantee of stability or backwards-compatibility of the command
7+
> line interface, configuration file format or generated code.
88
>
99
> 🚨
1010
@@ -14,33 +14,43 @@
1414
>
1515
> "SQL is actually pretty great"
1616
17-
sqlc generates **fully-type safe idiomatic Go code** from SQL. Here's how it works:
17+
sqlc generates **fully-type safe idiomatic Go code** from SQL. Here's how it
18+
works:
1819

1920
1. You write SQL queries
20-
1. You run sqlc to generate Go code that presents type-safe interfaces to those queries
21+
1. You run sqlc to generate Go code that presents type-safe interfaces to those
22+
queries
2123
1. You write application code that calls the methods sqlc generated.
2224

23-
Seriously, it's that easy. You don't have to write any boilerplate SQL querying code ever again.
25+
Seriously, it's that easy. You don't have to write any boilerplate SQL querying
26+
code ever again.
2427

2528
## Preventing Errors
26-
But sqlc doesn't just make you more productive by generating boilerplate for you.
27-
sqlc **also prevents entire classes of common errors in SQL code**. Have you ever:
2829

29-
- Mixed up the order of the arguments when invoking the query so they didn't match up with the SQL text
30+
But sqlc doesn't just make you more productive by generating boilerplate for
31+
you. sqlc **also prevents entire classes of common errors in SQL code**. Have
32+
you ever:
33+
34+
- Mixed up the order of the arguments when invoking the query so they didn't
35+
match up with the SQL text
3036
- Updated the name of a column in one query both not another
3137
- Mistyped the name of a column in a query
32-
- Changed the number of arguments in a query but forgot to pass the additional values
38+
- Changed the number of arguments in a query but forgot to pass the additional
39+
values
3340
- Changed the type of a column but forgot to change the type in your code?
3441

3542
All of these errors are *impossible* with sqlc. Wait, what? How?
3643

37-
sqlc parses your all of your queries and the DDL (e.g. `CREATE TABLE`) statements during the code generation processes
38-
so that it knows the names and types of every column in your tables and every expression in your queries.
39-
If any of them do not match, sqlc *will fail to compile your queries*, preventing entire classes of runtime problems
40-
at compile time.
44+
sqlc parses your all of your queries and the DDL (e.g. `CREATE TABLE`)
45+
statements during the code generation processes so that it knows the names and
46+
types of every column in your tables and every expression in your queries. If
47+
any of them do not match, sqlc *will fail to compile your queries*, preventing
48+
entire classes of runtime problems at compile time.
4149

42-
Likewise, the methods that sqlc generates for you have a stricty arity and correct Go type definitions that match your columns. So if you
43-
change a query's arguments or a column's type but don't update your code, it will fail to compile.
50+
Likewise, the methods that sqlc generates for you have a strict arity and
51+
correct Go type definitions that match your columns. So if you change a query's
52+
arguments or a column's type but don't update your code, it will fail to
53+
compile.
4454

4555
## Getting Started
4656
Okay, enough hype, let's see it in action.
@@ -105,7 +115,8 @@ if err != nil {
105115
fmt.Println(reflect.DeepEqual(insertedAuthor, fetchedAuthor))
106116
```
107117

108-
To make that possible, sqlc generates readable, **idiomatic** Go code that you otherwise would have had to write yourself. Take a look:
118+
To make that possible, sqlc generates readable, **idiomatic** Go code that you
119+
otherwise would have had to write yourself. Take a look:
109120

110121
```go
111122
package db
@@ -297,6 +308,66 @@ Each package document has the following keys:
297308
- `schema`:
298309
- Directory of SQL migrations or path to single SQL file
299310

311+
### Type Overrides
312+
313+
The default mapping of PostgreSQL types to Go types only uses packages outside
314+
the standard library when it must.
315+
316+
For example, the `uuid` PostgreSQL type is mapped to `github.com/google/uuid`.
317+
If a different Go package for UUIDs is required, specify the package in the
318+
`overrides` array. In this case, I'm going to use the `github.com/gofrs/uuid`
319+
instead.
320+
321+
```
322+
{
323+
"version": "1",
324+
"packages": [...],
325+
"overrides": [
326+
{
327+
"go_type": "uuid.UUID",
328+
"package": "github.com/gofrs/uuid",
329+
"postgres_type": "uuid"
330+
}
331+
]
332+
}
333+
```
334+
335+
Each override document has the following keys:
336+
- `postgres_type`:
337+
- The PostgreSQL type to override. Find the full list of supported types in [gen.go](https://github.com/kyleconroy/sqlc/blob/master/internal/dinosql/gen.go#L438).
338+
- `go_type`:
339+
- The Go type, with package name, to use in the generated code.
340+
- `package`:
341+
- The full import path for the package.
342+
- `null`:
343+
- If true, use this type when a column in nullable. Defaults to `false`.
344+
345+
### Renaming Struct Fields
346+
347+
Struct field names are generated from column names using a simple algorithm:
348+
split the column name on underscores and capitalize the first letter of each
349+
part.
350+
351+
```
352+
account -> Account
353+
spotify_url -> SpotifyUrl
354+
app_id -> AppID
355+
```
356+
357+
If you're not happy with a field's generated name, use the `rename` dictionary
358+
to pick a new name. The keys are column names and the values are the struct
359+
field name to use.
360+
361+
```json
362+
{
363+
"version": "1",
364+
"packages": [...],
365+
"rename": {
366+
"spotify_url": "SpotifyURL"
367+
}
368+
}
369+
```
370+
300371
## Downloads
301372

302373
Each commit is deployed to the [`devel` channel on Equinox](https://dl.equinox.io/sqlc/sqlc/devel):

0 commit comments

Comments
 (0)