Skip to main content

Go Database Layer with sqlc & Transactions

Build a type-safe database layer in Go using sqlc for compile-time SQL validation with transactions, connection pooling, and migrations.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt asks the AI to build a type-safe database layer for a Go [app_type] using sqlc with [database]. It organizes query files by domain ([domains]), generates typed queries with proper null handling via pgtype, and wraps everything in a transaction helper that auto-commits or rolls back and supports nested transactions through savepoints. Connection pooling uses [pool_lib], migrations are managed with [migration_tool], and a repository layer wraps the generated sqlc code so your data access stays testable.

The structure works because sqlc validates SQL at compile time, eliminating a whole class of runtime query bugs, and this prompt builds the surrounding scaffolding that sqlc alone does not provide. Naming [domains] keeps query files organized instead of dumped into one folder, and the transaction wrapper plus repository pattern give you clean boundaries. Batch operations for [batch_operations] using COPY or multi-row INSERT, slow-query detection, and error mapping (unique violation to ConflictError) turn raw generated code into a layer you can actually grow on. Connection pooling through [pool_lib] with min/max sizing and health checks is the piece that keeps the layer stable under load, since an exhausted or stale pool surfaces as intermittent failures that are hard to trace back to the database.

When to use it

  • You are building the persistence layer for a Go backend and want compile-time SQL checking instead of runtime surprises.
  • You need clean transaction handling with automatic rollback and savepoint-based nesting.
  • Your data access is sprawling and you want queries grouped by domain like [domains].
  • You import or bulk-update large datasets and need efficient batch operations.
  • You want database errors mapped to domain errors so callers do not parse driver strings.
  • You want integration tests that run against a real database via [test_strategy].

Example output

Expect an sqlc.yaml configuration, domain-grouped .sql query files, the generated Go code, a transaction wrapper type, repository structs over the generated queries, migration files for [migration_tool], and integration test scaffolding using [test_strategy]. Error-mapping helpers and pool configuration for [pool_lib] are included.

Pro tips

  • Set [database] precisely (e.g. PostgreSQL 16) so pgtype null handling and feature use match your server version.
  • List [domains] as the real bounded contexts in your app; this directly drives query-file organization and keeps generated code navigable.
  • Be explicit about [batch_operations] — "import 10K rows" pushes toward COPY, while "bulk status update" leans on multi-row UPDATE.
  • Choose [pool_lib] to match your driver; pgxpool pairs naturally with pgx and pgtype, and mixing pools and drivers causes subtle issues.
  • For [test_strategy], testcontainers with per-test transaction rollback keeps tests isolated and fast — ask for it explicitly if speed matters.
  • Verify the savepoint logic in the nested-transaction wrapper; nested rollback semantics are easy to get subtly wrong, so read that code carefully.

Frequently Asked Questions

Why use sqlc instead of an ORM in this prompt?
sqlc generates type-safe Go code from raw SQL and validates queries at compile time, catching errors before they hit production. This prompt adds the transaction wrapper, repository layer, and migrations that sqlc itself does not provide, giving you ORM-like ergonomics without query-builder magic.
Does it support nested transactions?
Yes. The transaction wrapper supports nested transactions using savepoints, with automatic commit and rollback. Read the generated savepoint logic carefully, since nested rollback semantics are subtle and worth verifying against your own test cases.
How does it keep the generated code testable?
It wraps the generated sqlc functions in a repository pattern, so your business logic depends on interfaces rather than concrete queries. Integration tests use your `[test_strategy]`, such as testcontainers with per-test transaction rollback, to run against a real database in isolation.
Can it handle bulk imports efficiently?
Yes. The prompt includes batch operations for your `[batch_operations]`, using COPY or multi-row INSERT depending on the workload. Naming whether you are importing rows or doing bulk updates helps it pick the right approach for each case.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Rust & Go Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support