Queries
Queries
By convention, all database queries are wrapped in a Queries layer that can be generated using the Copper CLI -
Querier
The auto-generated Queries
struct has a dependency on csql.Querier
. Querier allows you to run queries safely on your database and scan results into Go structs and slices with very little boilerplate.
The example below queries the rockets
table and scans the results into the rockets
slice -
Similarly, the example below scans the result into a single Go struct -
Finally, if you want to execute SQL statements (such as DELETE
), use the Querier.Exec
method -
Transaction Middleware
The csql.TxMiddleware
can be used to wrap every HTTP request in a database transaction. The transaction is automatically committed if the response code from the handler is success-like (i.e. 100-399) or rolled back for any other status code or even panics.
The middleware is added as a global middleware by default in pkg/app/handler.go
.
Last updated