Files
ent/examples/entcpkg/example_test.go
Giau. Tran Minh 2b1ad7e4b0 chore: bump x/tools to latest (#4329)
* chore: bump `x/tools` to latest

* chore: fixed lint on go1.24
2025-02-13 16:09:44 +07:00

49 lines
1.2 KiB
Go

// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"entgo.io/ent/examples/entcpkg/ent"
"entgo.io/ent/examples/entcpkg/ent/hook"
_ "github.com/mattn/go-sqlite3"
)
func Example_entcPkg() {
client, err := ent.Open(
"sqlite3",
"file:ent?mode=memory&cache=shared&_fk=1",
ent.Writer(os.Stdout),
ent.HTTPClient(http.DefaultClient),
)
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// An example for using the injected dependencies in the generated builders.
client.User.Use(func(next ent.Mutator) ent.Mutator {
return hook.UserFunc(func(ctx context.Context, m *ent.UserMutation) (ent.Value, error) {
_ = m.HTTPClient
_ = m.Writer
return next.Mutate(ctx, m)
})
})
ctx := context.Background()
// Run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
usr := client.User.Create().SaveX(ctx)
fmt.Println("boring user:", usr)
// Output: boring user: User(id=1, name=, age=0)
}