Skip to content

Commit 2feea51

Browse files
committed
sqlboiler and ent
1 parent 54e1454 commit 2feea51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+25117
-172
lines changed

Diff for: Makefile

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
.PHONY : init sqlboiler run all
2+
13
init:
24
go install github.com/kyleconroy/sqlc/cmd/sqlc@latest
35
go get -u gorm.io/gorm
46
go get -u gorm.io/driver/postgres
7+
go install github.com/volatiletech/sqlboiler/v4@latest
8+
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest
9+
go get -d entgo.io/ent/cmd/ent
510
go mod tidy
611

7-
sqlcgen:
8-
sqlc generate
12+
sqlboiler:
13+
sqlboiler --output db/sqlboiler/models psql
14+
15+
run:
16+
go mod tidy
17+
go vet ./...
18+
go fmt ./...
19+
go run main.go
20+
21+
all: init sqlboiler
22+
go mod tidy
23+
go vet ./...
24+
go fmt ./...
25+
go generate ./...
26+
go run main.go

Diff for: README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Introduction
2+
3+
Examples of using various popular database libraries and ORM in Go.
4+
5+
- [sqlx](https://jmoiron.github.io/sqlx/)
6+
- [sqlc](https://docs.sqlc.dev)
7+
- [Gorm](https://github.com/go-gorm/gorm)
8+
- [sqlboiler](https://github.com/volatiletech/sqlboiler)
9+
- [ent](https://entgo.io/docs/getting-started)
10+
11+
The aim is to demonstrate and compare usage for several operations
12+
13+
1. Simple CRUD operation
14+
2. 1-1 queries
15+
3. 1-to-Many queries
16+
4. Many-to-many queries
17+
5. Dynamic list filter from query parameter
18+
6. Transaction
19+
20+
The schema contains optional fields, for example middle name, and a field that must not be returned, for example, a password.
21+
22+
23+
# Usage
24+
25+
## Setup
26+
27+
Setup postgres database by either running from docker-compose or manually.
28+
29+
docker-compose up
30+
31+
This creates both `postgres` database (which this repo uses) and `ent` database which is used by ent ORM.
32+
33+
Default database credentials are defined in `config/config.go`. These can be overwritten by setting environment variables. For example:
34+
35+
export DB_NAME=test_db
36+
37+
## Run
38+
39+
Run with
40+
41+
go run main.go
42+
43+
44+
Run examples from `examples` folder.

Diff for: database/01-schema.sql

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
BEGIN;
2+
13
create table countries
24
(
35
id bigint generated always as identity
@@ -17,7 +19,7 @@ create table addresses
1719
state text,
1820
country_id bigint
1921
constraint addresses_countries_id_fk
20-
references countries
22+
references countries ON DELETE CASCADE
2123
);
2224

2325
create table users
@@ -35,12 +37,24 @@ create table user_addresses
3537
(
3638
user_id bigint
3739
constraint user_addresses_users_id_fk
38-
references users,
40+
references users ON DELETE CASCADE ,
3941
address_id bigint
4042
constraint user_addresses_addresses_id_fk
41-
references addresses
43+
references addresses ON DELETE CASCADE ,
44+
constraint user_addresses_pk
45+
primary key (user_id, address_id)
4246
);
4347

48+
CREATE VIEW country_address as
49+
select c.id, c.code, c.name,
50+
(
51+
select array_to_json(array_agg(row_to_json(addresslist.*))) as array_to_json
52+
from (
53+
select a.*
54+
from addresses a
55+
where c.id = a.country_id
56+
) addresslist) as addresses
57+
from countries AS c;
4458

4559
INSERT INTO countries (code, name)
4660
VALUES ('AU', 'Australia');
@@ -55,4 +69,6 @@ VALUES ('Petronas Twin Towers', '', 50088, 'Kuala Lumpur', 'Wilayah Persekutuan'
5569
INSERT INTO users (first_name, last_name, email, password)
5670
VALUES ('John', 'Doe', '[email protected]', '$argon2id$v=19$m=16,t=2,p=1$SHVrWmRXc2tqOW5TWmVrRw$QCPRZ0MmOB/AEEMVB1LudA'); -- password
5771
INSERT INTO users (first_name, last_name, email, password)
58-
VALUES ('Jane', 'Doe', '[email protected]', '$argon2id$v=19$m=16,t=2,p=1$UDB3RXNPd3ZEWHQ4ZTRNVg$LhHurQuz9Q9dDEG1VNzbFg'); -- password
72+
VALUES ('Jane', 'Doe', '[email protected]', '$argon2id$v=19$m=16,t=2,p=1$UDB3RXNPd3ZEWHQ4ZTRNVg$LhHurQuz9Q9dDEG1VNzbFg'); -- password
73+
74+
COMMIT;

Diff for: database/query.sql

+29-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,32 @@ FROM users
1515
ORDER BY last_name;
1616

1717
-- name: DeleteUser :exec
18-
DELETE FROM users
19-
WHERE id = $1;
18+
DELETE
19+
FROM users
20+
WHERE id = $1;
21+
22+
-- name: UpdateUser :exec
23+
UPDATE users
24+
SET first_name=$1,
25+
middle_name=$2,
26+
last_name=$3,
27+
email=$4
28+
WHERE id = $5;
29+
30+
-- name: CountriesWithAddress :many
31+
SELECT c.id AS country_id,
32+
c.name,
33+
c.code,
34+
a.id AS address_id,
35+
a.line_1,
36+
a.line_2,
37+
a.postcode,
38+
a.city,
39+
a.state
40+
FROM countries c
41+
LEFT JOIN addresses a on c.id = a.country_id
42+
ORDER BY c.id;
43+
44+
45+
-- name: CountriesWithAddressAggregate :many
46+
select row_to_json(row) from (select * from country_address) row;

Diff for: db/ent/database.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ent
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"entgo.io/ent/dialect"
11+
entsql "entgo.io/ent/dialect/sql"
12+
13+
"godb/config"
14+
"godb/db/ent/ent/gen"
15+
)
16+
17+
type database struct {
18+
db *gen.Client
19+
}
20+
21+
func New(cfg config.Database) *gen.Client {
22+
db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s", cfg.Host, cfg.Port, cfg.User, "ent", cfg.Password, cfg.SSLMode))
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
27+
drv := entsql.OpenDB(dialect.Postgres, db)
28+
29+
client := gen.NewClient(gen.Driver(drv))
30+
31+
f, err := os.Create("./db/ent/migrate.sql")
32+
if err != nil {
33+
log.Fatalf("create migrate file: %v", err)
34+
}
35+
defer f.Close()
36+
37+
if err := client.Schema.Create(context.Background()); err != nil {
38+
log.Fatalf("failed creating schema resources: %v", err)
39+
}
40+
41+
if err := client.Schema.WriteTo(context.Background(), f); err != nil {
42+
log.Fatalf("failed printing schema changes: %v", err)
43+
}
44+
45+
return client
46+
}
47+
48+
func NewRepo(db *gen.Client) *database {
49+
return &database{
50+
db: db,
51+
}
52+
}

0 commit comments

Comments
 (0)