This repository was archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Ent hello world #81
Open
hedwigz
wants to merge
10
commits into
pingcap:master
Choose a base branch
from
hedwigz:ent-hello-world
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ent hello world #81
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f9fa592
ent hello world example
hedwigz 0ff0c9e
use latest ent version
hedwigz 6db29dc
format
hedwigz 2245076
Update en/for-ent.md
d14cd70
Update en/for-ent.md
747bf9c
latest tidb version
hedwigz f56bfde
Merge branch 'ent-hello-world' of github.com:hedwigz/docs-appdev into…
hedwigz 7a6661d
add external links
hedwigz a985edf
fenced code blocks
hedwigz bb5f058
fix markdownlint
hedwigz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
--- | ||
title: App Development for Ent | ||
summary: Learn how to build a simple Golang application based on TiDB and Ent. | ||
--- | ||
|
||
# App Development for Ent | ||
|
||
This tutorial shows you how to build a simple Golang application based on TiDB and Ent. The sample application to build here is a simple Todo app where you can add, query, and update Todos. | ||
|
||
## Step 1. Start a TiDB cluster | ||
|
||
Start a pseudo TiDB cluster on your local storage: | ||
|
||
{{< copyable "" >}} | ||
|
||
```bash | ||
docker run -p 127.0.0.1:$LOCAL_PORT:4000 pingcap/tidb:v6.0.0 | ||
``` | ||
|
||
The above command starts a temporary and single-node cluster with mock TiKV. The cluster listens on the port `$LOCAL_PORT`. After the cluster is stopped, any changes already made to the database are not persisted. | ||
|
||
> **Note:** | ||
> | ||
> To deploy a "real" TiDB cluster for production, see the following guides: | ||
> | ||
> + [Deploy TiDB using TiUP for On-Premises](https://docs.pingcap.com/tidb/v5.4/production-deployment-using-tiup) | ||
> + [Deploy TiDB on Kubernetes](https://docs.pingcap.com/tidb-in-kubernetes/stable) | ||
> | ||
> You can also [use TiDB Cloud](https://pingcap.com/products/tidbcloud/), a fully-managed Database-as-a-Service (DBaaS), which offers free trial. | ||
|
||
## Step 2. Create a database | ||
|
||
1. In the SQL shell, create the `entgo` database that your application will use: | ||
|
||
{{< copyable "" >}} | ||
|
||
```sql | ||
CREATE DATABASE entgo; | ||
``` | ||
|
||
2. Create a SQL user for your application: | ||
|
||
{{< copyable "" >}} | ||
|
||
```sql | ||
CREATE USER <username> IDENTIFIED BY <password>; | ||
``` | ||
|
||
Take note of the username and password. You will use them in your application code when initializing the project. | ||
|
||
3. Grant necessary permissions to the SQL user you have just created: | ||
|
||
{{< copyable "" >}} | ||
|
||
```sql | ||
GRANT ALL ON entgo.* TO <username>; | ||
``` | ||
|
||
## Step 3. Get and run the application code | ||
|
||
### Prerequisites | ||
|
||
Make sure you have [Go](https://golang.org/doc/install) installed (1.17 or above). | ||
Create a folder for this demo, and initialize using `go mod`: | ||
|
||
{{< copyable "" >}} | ||
|
||
```bash | ||
mkdir todo | ||
cd todo | ||
go mod init todo | ||
``` | ||
|
||
### Initialize project | ||
|
||
First, we need to install Ent and initialize the project structure. run: | ||
|
||
{{< copyable "" >}} | ||
|
||
```bash | ||
go run -mod=mod entgo.io/ent/cmd/ent init Todo | ||
``` | ||
|
||
After running the above, your project directory should look like this: | ||
|
||
```bash | ||
. | ||
├── ent | ||
│ ├── generate.go | ||
│ └── schema | ||
│ └── todo.go | ||
├── go.mod | ||
└── go.sum | ||
``` | ||
|
||
Open `schema/todo.go` and add some fields to the `Todo` entity: | ||
|
||
{{< copyable "" >}} | ||
|
||
```go | ||
func (Todo) Fields() []ent.Field { | ||
return []ent.Field{ | ||
field.String("title"), | ||
field.String("content"), | ||
field.Bool("done"). | ||
Default(false), | ||
field.Time("created_at"), | ||
} | ||
} | ||
``` | ||
|
||
Finally, run: | ||
|
||
{{< copyable "" >}} | ||
|
||
```bash | ||
go generate ./ent | ||
``` | ||
|
||
### Run the example application | ||
|
||
Create a file named `main.go` in your project's root folder and copy to it the the following code: | ||
|
||
{{< copyable "" >}} | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
"todo/ent" | ||
"todo/ent/todo" | ||
|
||
"entgo.io/ent/dialect/sql/schema" | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
func main() { | ||
// Connect to TiDB. | ||
client, err := ent.Open("mysql", "root@tcp(127.0.0.1:4000)/entgo?parseTime=true") | ||
if err != nil { | ||
log.Fatal("error opening ent client", err) | ||
} | ||
defer client.Close() | ||
// Migrate the local schema with the DB. | ||
if err := client.Schema.Create( | ||
context.Background(), | ||
// TiDB support is possible with Atlas engine only. | ||
schema.WithAtlas(true), | ||
); err != nil { | ||
log.Fatal("error migrating DB", err) | ||
} | ||
|
||
// Create some todos | ||
client.Todo.Create(). | ||
SetTitle("buy groceries"). | ||
SetContent("tomato, lettuce and cucumber"). | ||
SetCreatedAt(time.Now()). | ||
SaveX(context.Background()) | ||
client.Todo.Create(). | ||
SetTitle("See my doctor"). | ||
SetContent("periodic blood test"). | ||
SetCreatedAt(time.Now()). | ||
SaveX(context.Background()) | ||
|
||
// query todo with 'where' filter | ||
todos := client.Todo.Query(). | ||
Where(todo.ContentContains("tomato")). | ||
AllX(context.Background()) | ||
fmt.Printf("The following todos contain 'tomato' in their content: %v\n", todos) | ||
|
||
// after buying the groceries, set the todo as done. | ||
client.Todo.UpdateOne(todos[0]).SetDone(true).ExecX(context.Background()) | ||
|
||
// delete todos that are done. | ||
deletedCount := client.Todo.Delete().Where(todo.Done(true)).ExecX(context.Background()) | ||
fmt.Printf("deleted %d todos\n", deletedCount) | ||
} | ||
``` | ||
|
||
### Run the code | ||
|
||
Run the `main.go` code: | ||
|
||
{{< copyable "" >}} | ||
|
||
```bash | ||
go run main.go | ||
``` | ||
|
||
The expected output is as follows: | ||
|
||
``` | ||
The following todos contain 'tomato' in their content: | ||
[Todo(id=1, title=buy groceries, content=tomato, lettuce and cucumber, done=false, created_at=Mon May 2 14:32:20 2022)] | ||
deleted 1 todos | ||
``` | ||
|
||
## What's next? | ||
|
||
Learn more about how to use [Ent Framework](https://entgo.io/). | ||
|
||
You might also be interested in the following: | ||
|
||
* Automatically generate feature-rich, performant, and clean [GraphQL](https://entgo.io/docs/graphql/), [REST](https://entgo.io/blog/2021/07/29/generate-a-fully-working-go-crud-http-api-with-ent/), and [gRPC](https://entgo.io/docs/grpc-intro/) servers using Ent | ||
* [Versioned Migrations with Ent](https://entgo.io/docs/versioned-migrations/) | ||
* [Extending Ent with the Extension API](https://entgo.io/blog/2021/09/02/ent-extension-api) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.