mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
// Copyright (c) Facebook, Inc. and its affiliates. 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.
|
|
|
|
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package entv1
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql"
|
|
|
|
"github.com/facebookincubator/ent/entc/integration/migrate/entv1/user"
|
|
)
|
|
|
|
// dsn for the database. In order to run the tests locally, run the following command:
|
|
//
|
|
// ENTV1_INTEGRATION_ENDPOINT="root:pass@tcp(localhost:3306)/test?parseTime=True" go test -v
|
|
//
|
|
var dsn string
|
|
|
|
func ExampleCar() {
|
|
if dsn == "" {
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
drv, err := sql.Open("mysql", dsn)
|
|
if err != nil {
|
|
log.Fatalf("failed creating database client: %v", err)
|
|
}
|
|
defer drv.Close()
|
|
client := NewClient(Driver(drv))
|
|
// creating vertices for the car's edges.
|
|
|
|
// create car vertex with its edges.
|
|
c := client.Car.
|
|
Create().
|
|
SaveX(ctx)
|
|
log.Println("car created:", c)
|
|
|
|
// query edges.
|
|
|
|
// Output:
|
|
}
|
|
func ExampleUser() {
|
|
if dsn == "" {
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
drv, err := sql.Open("mysql", dsn)
|
|
if err != nil {
|
|
log.Fatalf("failed creating database client: %v", err)
|
|
}
|
|
defer drv.Close()
|
|
client := NewClient(Driver(drv))
|
|
// creating vertices for the user's edges.
|
|
u1 := client.User.
|
|
Create().
|
|
SetAge(1).
|
|
SetName("string").
|
|
SetNickname("string").
|
|
SetAddress("string").
|
|
SetRenamed("string").
|
|
SetBlob(nil).
|
|
SetState(user.StateLoggedIn).
|
|
SaveX(ctx)
|
|
log.Println("user created:", u1)
|
|
u2 := client.User.
|
|
Create().
|
|
SetAge(1).
|
|
SetName("string").
|
|
SetNickname("string").
|
|
SetAddress("string").
|
|
SetRenamed("string").
|
|
SetBlob(nil).
|
|
SetState(user.StateLoggedIn).
|
|
SaveX(ctx)
|
|
log.Println("user created:", u2)
|
|
c3 := client.Car.
|
|
Create().
|
|
SaveX(ctx)
|
|
log.Println("car created:", c3)
|
|
|
|
// create user vertex with its edges.
|
|
u := client.User.
|
|
Create().
|
|
SetAge(1).
|
|
SetName("string").
|
|
SetNickname("string").
|
|
SetAddress("string").
|
|
SetRenamed("string").
|
|
SetBlob(nil).
|
|
SetState(user.StateLoggedIn).
|
|
AddChildren(u1).
|
|
SetSpouse(u2).
|
|
SetCar(c3).
|
|
SaveX(ctx)
|
|
log.Println("user created:", u)
|
|
|
|
// query edges.
|
|
|
|
u1, err = u.QueryChildren().First(ctx)
|
|
if err != nil {
|
|
log.Fatalf("failed querying children: %v", err)
|
|
}
|
|
log.Println("children found:", u1)
|
|
|
|
u2, err = u.QuerySpouse().First(ctx)
|
|
if err != nil {
|
|
log.Fatalf("failed querying spouse: %v", err)
|
|
}
|
|
log.Println("spouse found:", u2)
|
|
|
|
c3, err = u.QueryCar().First(ctx)
|
|
if err != nil {
|
|
log.Fatalf("failed querying car: %v", err)
|
|
}
|
|
log.Println("car found:", c3)
|
|
|
|
// Output:
|
|
}
|