package main import ( "context" "fmt" "log" "os" "strings" "time" "code.osinet.fr/fgm/entdemo/ent" "code.osinet.fr/fgm/entdemo/ent/car" "code.osinet.fr/fgm/entdemo/ent/group" "code.osinet.fr/fgm/entdemo/ent/user" _ "github.com/mattn/go-sqlite3" ) func CreateUser(ctx context.Context, client *ent.Client) (*ent.User, error) { u, err := client.User. Create(). SetAge(30). SetName("a8m"). Save(ctx) if err != nil { return nil, fmt.Errorf("failed creating user: %w", err) } log.Println("user was created: ", u) return u, nil } func QueryUser(ctx context.Context, client *ent.Client) (*ent.User, error) { const name = "a8m" u, err := client.User. Query(). Where(user.NameEQ(name)). // "Only" fails if no user found or more than 1 user returned. Only(ctx) if err != nil { return nil, fmt.Errorf("failed querying user %s: %w", name, err) } log.Println("user returned: ", u) return u, nil } func CreateCars(ctx context.Context, client *ent.Client) (*ent.User, error) { var err error name := "tesla" tesla, err := client.Car. Create(). SetModel(name). SetRegisteredAt(time.Now()). Save(ctx) if err != nil { return nil, fmt.Errorf("failed creating %s car: %v", name, err) } log.Println("car was created: ", tesla) name = "ford" ford, err := client.Car. Create(). SetModel(name). SetRegisteredAt(time.Now()). Save(ctx) if err != nil { return nil, fmt.Errorf("failed creating %s car: %v", name, err) } log.Println("car was created: ", ford) u, err := QueryUser(ctx, client) if err != nil { return nil, fmt.Errorf("failed looking for user: %w", err) } u.Update(). AddCars(tesla, ford). Save(ctx) if err != nil { return nil, fmt.Errorf("failed adding cards to user: %w", err) } return u, nil } func QueryCars(ctx context.Context, user *ent.User) error { cars, err := user.QueryCars(). All(ctx) if err != nil { return fmt.Errorf("failed querying user %s cars: %w", user.Name, err) } log.Println("returned all cars: ", cars) ford, err := user.QueryCars(). Where(car.ModelEQ("ford")). Only(ctx) if err != nil { return fmt.Errorf("failed searching for ford cars: %w", err) } log.Println("filtered cars: ", ford) return nil } func QueryCarUsers(ctx context.Context, u *ent.User) error { cars, err := u.QueryCars().All(ctx) if err != nil { return fmt.Errorf("failed querying user %s cars: %w", u.Name, err) } log.Println("returned all cars: ", cars) // query the inverse edge for _, car := range cars { owner, err := car.QueryOwner().Only(ctx) if err != nil { return fmt.Errorf("failed querying cat %s owner: %v", car.Model, err) } log.Printf("card %s owner: %q\n", car.Model, owner.Name) } return nil } func Open() (*ent.Client, error) { const db = "ent.db" err := os.Remove(db) if err != nil { pathErr, ok := err.(*os.PathError) if !ok { log.Fatalf("os.Remove returned a non-PathError: %w", err) } log.Printf("Error removing database: %s", pathErr.Error()) } // Running client.Schema.Create will panic is DB exists already. client, err := ent.Open("sqlite3", strings.Join([]string{ "file:", db, "?mode=rwc&cache=shared&_fk=1", }, "", )) // client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") return client, err } func BasicDemo(ctx context.Context, client *ent.Client) { u, err := CreateUser(ctx, client) if err != nil { panic(err) } _, err = QueryUser(ctx, client) if err != nil { panic(err) } _, err = CreateCars(ctx, client) if err != nil { panic(err) } err = QueryCars(ctx, u) if err != nil { panic(err) } err = QueryCarUsers(ctx, u) if err != nil { panic(err) } } func CreateGraph(ctx context.Context, client *ent.Client) error { ariel, err := client.User. Create(). SetAge(30). SetName("ariel"). Save(ctx) if err != nil { return err } neta, err := client.User. Create(). SetAge(28). SetName("neta"). Save(ctx) if err != nil { return err } // then, create the cars, and attach them to the users in the creation. _, err = client.Car. Create(). SetModel("tesla"). SetRegisteredAt(time.Now()). // ignore the time in the graph. SetOwner(ariel). // attach this graph to Ariel. Save(ctx) if err != nil { return err } _, err = client.Car. Create(). SetModel("mazda"). SetRegisteredAt(time.Now()). // ignore the time in the graph. SetOwner(ariel). // attach this graph to Ariel. Save(ctx) if err != nil { return err } _, err = client.Car. Create(). SetModel("ford"). SetRegisteredAt(time.Now()). SetOwner(neta). Save(ctx) if err != nil { return err } // create the groups, and add their users in the creation. _, err = client.Group. Create(). SetName("GitLab"). AddUsers(neta, ariel). Save(ctx) if err != nil { return err } _, err = client.Group. Create(). SetName("GitHub"). AddUsers(ariel). Save(ctx) if err != nil { return err } log.Println("The graph was created successfully") return nil } func QueryGithub(ctx context.Context, client *ent.Client) error { const name = "GitHub" cars, err := client.Group. Query(). Where(group.Name(name)). // (Group(Name=GitHub),) QueryUsers(). // (User(Name=Ariel, Age=30),) QueryCars(). // (Car(Model=Tesla, RegisteredAt=