### Create Client and Schema Migration (SQLite) Source: https://entgo.io/docs/getting-started/index Opens a connection to a SQLite database in memory and runs the auto-migration tool to create the schema. This example uses a temporary in-memory database. ```go package main import ( "context" "log" "entdemo/ent" _ "github.com/mattn/go-sqlite3" ) func main() { client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") if err != nil { log.Fatalf("failed opening connection to sqlite: %v", err) } defer client.Close() // Run the auto migration tool. if err := client.Schema.Create(context.Background()); err != nil { log.Fatalf("failed creating schema resources: %v", err) } } ``` -------------------------------- ### Install Atlas CLI using cURL Source: https://entgo.io/docs/getting-started/index This command installs the latest release of the Atlas CLI tool by downloading and executing the official installation script from a cURL command. ```bash curl -sSf https://atlasgo.sh | sh ``` -------------------------------- ### Install Atlas CLI using Homebrew Source: https://entgo.io/docs/getting-started/index This command installs the latest release of the Atlas CLI tool on macOS using the Homebrew package manager. ```bash brew install ariga/tap/atlas ``` -------------------------------- ### Pull and Run Atlas Docker Image Source: https://entgo.io/docs/getting-started/index This command pulls the latest official Atlas Docker image and then runs it to display the help information, confirming the installation. ```bash docker pull arigaio/atlas docker run --rm arigaio/atlas --help ``` -------------------------------- ### Apply Database Migrations with Atlas CLI Source: https://entgo.io/docs/getting-started/index These commands use the Atlas CLI to apply generated migration files to a target database. They require the migration directory and the production database URL. Examples are provided for connecting to MySQL, MariaDB, PostgreSQL, and SQLite databases. The command executes the SQL statements contained within the migration files. ```bash atlas migrate apply \ --dir "file://ent/migrate/migrations" \ --url "mysql://root:pass@localhost:3306/example" ``` ```bash atlas migrate apply \ --dir "file://ent/migrate/migrations" \ --url "maria://root:pass@localhost:3306/example" ``` ```bash atlas migrate apply \ --dir "file://ent/migrate/migrations" \ --url "postgres://postgres:pass@localhost:5432/database?search_path=public&sslmode=disable" ``` ```bash atlas migrate apply \ --dir "file://ent/migrate/migrations" \ --url "sqlite://file.db?_fk=1" ``` -------------------------------- ### Query Inverse Edge for Car Owner in Go Source: https://entgo.io/docs/getting-started/index This Go code demonstrates how to query the inverse 'owner' edge from a Car object. It first retrieves a user's cars and then iterates through them, querying the 'owner' of each car using `QueryOwner().Only(ctx)` to get the associated user. ```go func QueryCarUsers(ctx context.Context, a8m *ent.User) error { cars, err := a8m.QueryCars().All(ctx) if err != nil { return fmt.Errorf("failed querying user cars: %w", err) } // Query the inverse edge. for _, c := range cars { owner, err := c.QueryOwner().Only(ctx) if err != nil { return fmt.Errorf("failed querying car %q owner: %w", c.Model, err) } log.Printf("car %q owner: %q\n", c.Model, owner.Name) } return nil } ``` -------------------------------- ### Query Cars for Users in a Specific Group (Ent Go) Source: https://entgo.io/docs/getting-started/index This Go function queries for all cars belonging to users within the 'GitHub' group. It demonstrates multi-step traversal starting from the Group entity, filtering by name, querying associated users, and then their cars. ```go func QueryGithub(ctx context.Context, client *ent.Client) error { cars, err := client.Group. Query(). Where(group.Name("GitHub")). // (Group(Name=GitHub),) QueryUsers(). // (User(Name=Ariel, Age=30),) QueryCars(). // (Car(Model=Tesla, RegisteredAt=