mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/doc: transaction example and docs
Summary: {F205899335}
Reviewed By: dlvhdr
Differential Revision: D17149531
fbshipit-source-id: cb8595d41ede6f813370564ca688f33d0dfe6905
This commit is contained in:
committed by
Facebook Github Bot
parent
4323141fe2
commit
9b7ea021ef
@@ -10,6 +10,7 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/user"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/pet"
|
||||
|
||||
@@ -41,6 +42,20 @@ func main() {
|
||||
if err := Traverse2(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Generate a group of entities in a transaction.
|
||||
if err := GenTx(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Wrap an existing function ("Gen") in a transaction.
|
||||
if err := WrapGen(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// WithTx helper.
|
||||
if err := WithTx(ctx, client, func(tx *ent.Tx) error {
|
||||
return Gen(ctx, tx.Client())
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Gen(ctx context.Context, client *ent.Client) error {
|
||||
@@ -117,6 +132,7 @@ func Traverse(ctx context.Context, client *ent.Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse2 example from the doc.
|
||||
func Traverse2(ctx context.Context, client *ent.Client) error {
|
||||
pets, err := client.Pet.
|
||||
Query().
|
||||
@@ -136,3 +152,89 @@ func Traverse2(ctx context.Context, client *ent.Client) error {
|
||||
// [Pet(id=1, name=Pedro) Pet(id=2, name=Xabi)]
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenTx example from the doc.
|
||||
func GenTx(ctx context.Context, client *ent.Client) error {
|
||||
tx, err := client.Tx(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("starting a transaction: %v", err)
|
||||
}
|
||||
hub, err := tx.Group.
|
||||
Create().
|
||||
SetName("Github").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return rollback(tx, fmt.Errorf("failed creating the group: %v", err))
|
||||
}
|
||||
// Create the admin of the group.
|
||||
dan, err := tx.User.
|
||||
Create().
|
||||
SetAge(29).
|
||||
SetName("Dan").
|
||||
AddManage(hub).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return rollback(tx, err)
|
||||
}
|
||||
// Create user "Ariel".
|
||||
a8m, err := tx.User.
|
||||
Create().
|
||||
SetAge(30).
|
||||
SetName("Ariel").
|
||||
AddGroups(hub).
|
||||
AddFriends(dan).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return rollback(tx, err)
|
||||
}
|
||||
fmt.Println(a8m)
|
||||
// Output:
|
||||
// User(id=1, age=30, name=Ariel)
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// WrapGen wraps the existing "Gen" function in a transaction.
|
||||
func WrapGen(ctx context.Context, client *ent.Client) error {
|
||||
tx, err := client.Tx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
txClient := tx.Client()
|
||||
// Use the same "Gen" as above, but give it the transactional client; no code changes to "Gen".
|
||||
if err := Gen(ctx, txClient); err != nil {
|
||||
return rollback(tx, err)
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// WithTx example from the doc.
|
||||
func WithTx(ctx context.Context, client *ent.Client, fn func(tx *ent.Tx) error) error {
|
||||
tx, err := client.Tx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if v := recover(); v != nil {
|
||||
tx.Rollback()
|
||||
panic(v)
|
||||
}
|
||||
}()
|
||||
if err := fn(tx); err != nil {
|
||||
if rerr := tx.Rollback(); rerr != nil {
|
||||
err = errors.Wrapf(err, "rolling back transaction: %v", rerr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return errors.Wrapf(err, "committing transaction: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
|
||||
func rollback(tx *ent.Tx, err error) error {
|
||||
if rerr := tx.Rollback(); rerr != nil {
|
||||
err = fmt.Errorf("%v: %v", err, rerr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user