You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+142Lines changed: 142 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,6 +56,148 @@ ID types in this package can be used with [database/sql](https://pkg.go.dev/data
56
56
57
57
When using the standard library SQL, IDs will be stored as their string representation and can be scanned and valued accordingly. When using pgx, both TEXT and UUID columns can be used directly. However, note that the type information is lost when using UUID columns, unless you take additional steps at the database layer. Be mindful of your identifier semantics, especially in complex JOIN queries.
58
58
59
+
If using `pgx` with PostgreSQL, you can generate UUIDv4 (for usage with `typeid.Random`) as the default value for your primary key:
60
+
61
+
```sql
62
+
CREATETABLEusers (
63
+
id UUID PRIMARY KEY DEFAULT GEN_RANDOM_UUID(),
64
+
...
65
+
);
66
+
```
67
+
68
+
For k-sortable TypeIDs (`typeid.Sortable`, using UUIDv7) you will have to generate the UUID in your application unless you are running [PostgreSQL 18](https://www.thenile.dev/blog/uuidv7) or new where both are supported:
69
+
70
+
```sql
71
+
-- With random TypeID
72
+
CREATETABLEusers (
73
+
id UUID PRIMARY KEY DEFAULT uuidv4(),
74
+
...
75
+
);
76
+
77
+
-- With sortable TypeID
78
+
CREATETABLEusers (
79
+
id UUID PRIMARY KEY DEFAULT uuidv7(),
80
+
...
81
+
);
82
+
```
83
+
84
+
## Using with sqlc
85
+
86
+
TypeIDs work seamlessly with [sqlc](https://sqlc.dev/) by using column overrides in your `sqlc.yaml` configuration:
TypeIDs can be used with [oapi-codegen](https://github.com/oapi-codegen/oapi-codegen) to generate type-safe API clients and servers. Use the `x-go-type` and `x-go-type-import` extensions in your OpenAPI specification:
TypeIDs implement `encoding.TextMarshaler` and `encoding.TextUnmarshaler`, so they work with JSON encoding/decoding in generated API code without any additional configuration.
0 commit comments