Do you need a repository layer on top of sqlc?
Redowan Delowar
March 16, 2026
Today in [r/golang], user Leading-West-4881 asked:
> Is a repository layer over sqlc over-engineering or necessary for scale? I'm building a
> notification engine in Go using sqlc for the DB layer. Do you just inject db.Queries
> into your services, or do you find the abstraction of a repository layer worth the extra
> code?
I [attempted to answer it] there and the gist is correct. But I wrote it in a hurry so the
example and the explanation could be better. Capturing it properly here.
---
Call it repository or whatever you want, the name doesn't matter. The point is that your
business logic should be oblivious to the persistence layer. Doesn't matter if it's [sqlc],
raw database/sql, or [gorm]. If your service functions call sqlc queries directly, your
core logic is coupled to your database code. That makes it harder to test in isolation and
harder to swap out later.
Put a small interface between your business code and your storage code. The business side
defines what it needs, the storage side satisfies it, and they live in separate packages.
Say you're building a service that manages books. Start with the domain type and the storage
interface:
The service depends only on that interface:
RegisterBook doesn't know about SQL, sqlc, or Postgres. It builds a Book, asks the store
to persist it, and gets an ID back.
The concrete implementation goes in a separate package. This is where sqlc-generated code
would live:
Wire it up at startup:
In tests, swap in a fake that satisfies the same interface:
Now the test reads exactly like production code, minus Postgres:
Same service code, no database needed. The test exercises RegisterBook without touching
SQL. If the storage layer changes tomorrow, the service and its tests stay the same.
A working example with transactions, tests, and an HTTP server is [on GitHub].
See also:
- [How do you handle transactions with the repository pattern?]
- [Repositories, transactions, and unit of work in Go]
[r/golang]:
https://www.reddit.com/r/golang/
[attempted to answer it]:
https://www.reddit.com/r/golang/comments/1rv65k9/comment/oasp30r/
[sqlc]:
https://github.com/sqlc-dev/sqlc
[gorm]:
https://github.com/go-gorm/gorm
[on GitHub]:
https://github.com/rednafi/examples/tree/main/repository-transactions
[How do you handle transactions with the repository pattern?]:
/shards/2026/03/transactions-with-repository-pattern/
[Repositories, transactions, and unit of work in Go]:
/go/repo-txn-uow/
Discussion in the ATmosphere