jet/wiki/Generator.md

102 lines
3.1 KiB
Markdown
Raw Normal View History

2019-06-27 14:31:57 +02:00
Before we can write SQL queries in Go we have to generate necessary Go files.
To generate files we need running database instance containing already defined database schema.
This files can be generated in two ways:
2019-06-27 19:55:21 +02:00
#### 1) Generating from command line
2019-06-27 14:31:57 +02:00
2019-07-08 10:48:03 +02:00
Install jet to GOPATH bin folder. This will allow generating jet files from the command line.
2019-06-27 14:31:57 +02:00
```sh
2019-07-08 10:48:03 +02:00
go install github.com/go-jet/jet/cmd/jet
2019-06-27 14:31:57 +02:00
```
Make sure GOPATH bin folder is added to the PATH environment variable.
2019-07-08 10:48:03 +02:00
Test jet can be found in the PATH.
2019-06-27 14:31:57 +02:00
```sh
2019-07-08 10:48:03 +02:00
jet -h
Usage of jet:
2019-06-27 14:31:57 +02:00
-host string
Database host path (Example: localhost)
-port string
Database port
-user string
Database user
-password string
The users password
-dbname string
name of the database
-schema string
Database schema name. (default "public")
-path string
Destination dir for generated files.
-sslmode string
Whether or not to use SSL(optional) (default "disable")
-params string
Additional connection string parameters(optional)
```
Now to generate sample database:
```sh
2019-07-08 10:48:03 +02:00
jet -host=localhost -port=5432 -user=jet -password=jet -dbname=jetdb -schema dvds -path ./gen
2019-06-27 14:31:57 +02:00
```
```sh
Connecting to postgres database: host=localhost port=5432 user=jet password=jet dbname=jetdb sslmode=disable
Retrieving schema information...
FOUND 15 table(s), 1 enum(s)
Cleaning up destination directory...
Generating table sql builder files...
Generating table model files...
Generating enum sql builder files...
Generating enum model files...
Done
```
2019-06-27 19:55:21 +02:00
#### 2) Generating from code
2019-06-27 14:31:57 +02:00
```
2019-07-08 10:48:03 +02:00
import "github.com/go-jet/jet/generator/postgres"
2019-06-27 14:31:57 +02:00
...
2019-07-08 10:48:03 +02:00
err = postgres.Generate("./gen", postgres.DBConnection{
2019-06-27 14:31:57 +02:00
Host: "localhost",
Port: "5432",
User: "jet",
Password: "jet",
DBName: "jetdb",
SchemaName: "dvds",
SslMode: "disable",
})
```
In both ways, generator will:
- connect to postgres database and retrieve information about the tables and enums of `dvds` schema
- delete everything in destination folder `./gen`,
- generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`.
Generated files folder structure will look like this:
2019-06-27 19:55:21 +02:00
```
2019-06-27 14:31:57 +02:00
|-- gen # destination folder
| `-- jetdb # database name
| `-- dvds # schema name
| |-- enum # sql builder folder for enums
| | |-- mpaa_rating.go
| |-- table # sql builder folder for tables
| |-- actor.go
| |-- address.go
| |-- film.go
...
| |-- model # Plain Old Data for every enum and table
| | |-- actor.go
| | |-- address.go
| | |-- film.go
...
```
Table and enums from database schema are used as a template to generate two types of Go files:
2019-07-04 19:10:31 +02:00
* SQL Builder files - used to write type safe SQL statements in Go (`enum` and `table` package)
* Model files - used to store result from database queries (`model` package)