-
Notifications
You must be signed in to change notification settings - Fork 10
Description
the dbmodels package (generated by sqlc) is used pretty much everywhere in the codebase, which makes it hard to change things when the DB changes.
This exposes things like sql.NullString to clients, which they shouldn't have to deal with.
An example - while working on #144, I had to modify the DB models and it required that I change stuff throughout the entire codebase, CLI and everything despite it just being a change to the database.
After doing some research, something like this is standard:
HTTP Request (DTOs)
↓
Handler Layer (DTOs ↔ Domain Models)
↓
Service Layer (Domain Models)
↓
Repository Layer (Domain Models ↔ DB Models)
↓
Database (sqlc-generated DB Models)
DTOs are Data Transfer Objects. They're outward facing and only exist to move data around. This lets us control what fields are exposed in the API.
Domain models are for internal use and should replace the usage of db models in places like the CLI.
The repository layer is an abstraction that essentially translates db models into domain models.
Order of tasks:
- Add domain models in
internal/domainpackage for each db table. Only use standard Go types for these. - Add repository layer at
internal/api/repository. Thedbmodelspackage should be kept internal to this package. - Create API DTOs at
internal/dto/requestandinternal/dto/response. I'm suggesting NOT putting them within the API package due to Allow JSON input in CLI #152 . These should all have JSON tags. - Update service layer to work with domain models. Should call repositories instead of stuff from
dbmodels. - Update handlers to use DTOs for input/output.
- Update CLI to use domain models instead of
dbmodels. - Update docs and potentially unit test for translation layers.