Skip to content

Commit

Permalink
feat: add entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Elton Minetto committed Sep 29, 2021
1 parent 3f486ba commit d891154
Show file tree
Hide file tree
Showing 53 changed files with 15,073 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/
data/
.idea/
75 changes: 75 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Ent

## Install

go get entgo.io/ent/cmd/ent

## Creating entities

go run entgo.io/ent/cmd/ent init Student Course Lesson Teacher

## Configure entities

- [x] Add fields

- [x] Add relationships

Execute:

go mod tidy
go generate ./ent

to generate the schema files

Let's create an main.go just to test the tabel generation.


Install PostgreSQL lib:

go get github.com/lib/pq

main.go content:

```go

package main

import (
"context"
"log"

"github.com/eminetto/go-with-frameworks/ent"
_ "github.com/lib/pq"
)

func main() {
client, err := ent.Open("postgres", "host=localhost port=5432 user=prest password=prest dbname=prest sslmode=disable")
if err != nil {
log.Fatalf("failed opening connection to db: %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)
}
}

```

Execute:

go run main.go

And check the tables created in the database


# UseCases

Create directory:

mkdir -p usecase/student

Create the interface in `usecase/student/interface.go`:



16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3"
services:
postgres:
image: postgres
volumes:
- "./data/postgres:/var/lib/postgresql/data"
environment:
- POSTGRES_USER=prest
- POSTGRES_DB=prest
- POSTGRES_PASSWORD=prest
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-U", "prest"]
interval: 30s
retries: 3
Loading

0 comments on commit d891154

Please sign in to comment.