Skip to content

Commit 778b738

Browse files
committed
docs: Add SQLC type naming conventions and usage guidelines
- Introduced documentation on SQLC type naming conventions, emphasizing the importance of using schema-prefixed types in code. - Provided examples of correct and incorrect usage of generated types in repository interfaces, service code, and tests. - Updated related documentation files to ensure consistency and clarity regarding the use of SQLC-generated types. Signed-off-by: Christian Melgarejo <cmelgarejo@users.noreply.github.com>
1 parent 675eacf commit 778b738

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

.cursor/rules/30-data-sqlc.mdc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,37 @@ alwaysApply: false
99
- SQL queries in `modules/<mod>/internal/db/query/*.sql`
1010
- generated SQLC store in `modules/<mod>/internal/db/store/`
1111

12+
## SQLC Type Naming Conventions
13+
14+
**IMPORTANT:** SQLC generates types with schema prefixes. Types are named using the pattern `{Schema}{TableName}`.
15+
16+
- Tables in the `auth` schema generate types like:
17+
- `auth.users` → `store.AuthUser` (NOT `store.User`)
18+
- `auth.magic_codes` → `store.AuthMagicCode` (NOT `store.MagicCode`)
19+
- `auth.sessions` → `store.AuthSession`
20+
- `auth.oauth_states` → `store.AuthOauthState`
21+
22+
- Always use the full prefixed type names in code:
23+
```go
24+
// ✅ Correct
25+
func GetUser(ctx context.Context, id string) (*store.AuthUser, error)
26+
func GetMagicCode(ctx context.Context, code string) (*store.AuthMagicCode, error)
27+
28+
// ❌ Wrong
29+
func GetUser(ctx context.Context, id string) (*store.User, error)
30+
func GetMagicCode(ctx context.Context, code string) (*store.MagicCode, error)
31+
```
32+
33+
- After running `make sqlc`, check `modules/<mod>/internal/db/store/models.go` to see the exact generated type names.
34+
1235
## Repository pattern
1336

1437
- Flow: `store (sqlc)` → `repository` → `service` → `proto`.
1538
- The repository:
1639
- does not generate IDs
1740
- does not contain business logic
1841
- wraps external errors with context (`fmt.Errorf("...: %w", err)`)
42+
- uses the correct SQLC-generated type names (with schema prefixes)
1943

2044
## Transactions
2145

docs/MODULITH_ARCHITECTURE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,29 @@ func (r *SQLRepository) ListModules(ctx context.Context) ([]*store.Module, error
15661566
}
15671567
```
15681568

1569+
**SQLC Type Naming Conventions:**
1570+
1571+
SQLC generates types with schema prefixes. Types follow the pattern `{Schema}{TableName}`:
1572+
1573+
- Tables in the `auth` schema generate:
1574+
- `auth.users` → `store.AuthUser` (NOT `store.User`)
1575+
- `auth.magic_codes` → `store.AuthMagicCode` (NOT `store.MagicCode`)
1576+
- `auth.sessions` → `store.AuthSession`
1577+
1578+
**Always use the full prefixed type names** in repository interfaces, service code, and tests:
1579+
1580+
```go
1581+
// ✅ Correct
1582+
func GetUser(ctx context.Context, id string) (*store.AuthUser, error)
1583+
func GetMagicCode(ctx context.Context, code string) (*store.AuthMagicCode, error)
1584+
1585+
// ❌ Wrong - will cause "undefined: store.User" compilation errors
1586+
func GetUser(ctx context.Context, id string) (*store.User, error)
1587+
func GetMagicCode(ctx context.Context, code string) (*store.MagicCode, error)
1588+
```
1589+
1590+
After running `make sqlc`, check `modules/<mod>/internal/db/store/models.go` to see the exact generated type names.
1591+
15691592
**Transaction Handling:**
15701593

15711594
The `WithTx` method includes proper panic and error handling:

docs/TESTING_GUIDE.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,39 @@ mockgen -source=modules/auth/internal/repository/repository.go \
9797

9898
See `modules/auth/internal/service/service_mock_test.go` for examples.
9999

100+
### SQLC Type Names in Tests
101+
102+
**Important:** When writing tests that use SQLC-generated types, always use the correct type names with schema prefixes:
103+
104+
```go
105+
// ✅ Correct - Use schema-prefixed types
106+
mockRepo.EXPECT().
107+
GetUserByEmail(gomock.Any(), email).
108+
Return(&store.AuthUser{
109+
ID: "user-123",
110+
Email: sql.NullString{String: email, Valid: true},
111+
}, nil)
112+
113+
mockRepo.EXPECT().
114+
GetValidMagicCodeByEmail(gomock.Any(), email, code).
115+
Return(&store.AuthMagicCode{
116+
Code: code,
117+
UserEmail: sql.NullString{String: email, Valid: true},
118+
}, nil)
119+
120+
// ❌ Wrong - Missing schema prefix
121+
mockRepo.EXPECT().
122+
GetUserByEmail(gomock.Any(), email).
123+
Return(&store.User{...}, nil) // This will cause "undefined: store.User" error
124+
```
125+
126+
SQLC generates types using the pattern `{Schema}{TableName}`:
127+
- `auth.users``store.AuthUser`
128+
- `auth.magic_codes``store.AuthMagicCode`
129+
- `auth.sessions``store.AuthSession`
130+
131+
Always check `modules/<mod>/internal/db/store/models.go` after running `make sqlc` to see the exact generated type names.
132+
100133
## Integration Testing
101134

102135
### Testing Module Initialization

0 commit comments

Comments
 (0)