db

command
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

演示: 数据库接入 —— Wire 一行装配 sqlite(零外部服务), 覆盖 CRUD / 统一错误映射 / 分页 / 闭包式事务。

事务是本例重点: database.Tx 内任意 error(含 errcode 业务码)都会触发整体回滚, 业务码回滚后仍可正常作为响应错误渲染 —— 余额不足与程序性失败同一条路径。

运行:

cd example/db && go run .

访问(另开终端):

curl -X POST http://127.0.0.1:8082/api/v1/articles -H "Content-Type: application/json" -d '{"title":"hello"}'
curl http://127.0.0.1:8082/api/v1/articles/1
curl "http://127.0.0.1:8082/api/v1/articles?page=1&size=5"
curl http://127.0.0.1:8082/api/v1/accounts                  -> 查看两个账户余额
curl -X POST http://127.0.0.1:8082/api/v1/transfer -H "Content-Type: application/json" -d '{"from_id":1,"to_id":2,"amount":30}'
curl -X POST "http://127.0.0.1:8082/api/v1/transfer?fail=1" -H "Content-Type: application/json" -d '{"from_id":1,"to_id":2,"amount":30}'
  -> 故意中途失败, 事务整体回滚, 前后余额对比可见

说明: sqlite 数据落在 demo.db 文件, 删除该文件即可重置演示数据。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL