diff options
author | rj1 <rj1@riseup.net> | 2022-10-07 13:12:19 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-08 02:12:19 +0800 |
commit | 56aabf3e8dbbf44c73766ad915cc4808d594b48e (patch) | |
tree | d40892425274d91bfa7803838a481c3e34df9788 /docs/content/doc | |
parent | c08e42c47ef2a32b3b7ee422c73d6929c93b199e (diff) | |
download | gitea-56aabf3e8dbbf44c73766ad915cc4808d594b48e.tar.gz gitea-56aabf3e8dbbf44c73766ad915cc4808d594b48e.zip |
Fix some typos and update db transaction demo in backend guideline (#21322)
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'docs/content/doc')
-rw-r--r-- | docs/content/doc/developers/guidelines-backend.md | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/docs/content/doc/developers/guidelines-backend.md b/docs/content/doc/developers/guidelines-backend.md index b680d73c82..913898be83 100644 --- a/docs/content/doc/developers/guidelines-backend.md +++ b/docs/content/doc/developers/guidelines-backend.md @@ -67,22 +67,18 @@ Some actions should allow for rollback when database record insertion/update/del So services must be allowed to create a database transaction. Here is some example, ```go -// servcies/repository/repo.go -func CreateXXXX() error {\ - ctx, committer, err := db.TxContext() - if err != nil { - return err - } - defer committer.Close() - - // do something, if return err, it will rollback automatically when `committer.Close()` is invoked. - if err := issues.UpdateIssue(ctx, repoID); err != nil { - // ... - } - - // ...... - - return committer.Commit() +// services/repository/repository.go +func CreateXXXX() error { + return db.WithTx(func(ctx context.Context) error { + e := db.GetEngine(ctx) + // do something, if err is returned, it will rollback automatically + if err := issues.UpdateIssue(ctx, repoID); err != nil { + // ... + return err + } + // ... + return nil + }) } ``` @@ -94,14 +90,14 @@ If the function will be used in the transaction, just let `context.Context` as t func UpdateIssue(ctx context.Context, repoID int64) error { e := db.GetEngine(ctx) - // ...... + // ... } ``` ### Package Name For the top level package, use a plural as package name, i.e. `services`, `models`, for sub packages, use singular, -i.e. `servcies/user`, `models/repository`. +i.e. `services/user`, `models/repository`. ### Import Alias |