Jet usage improvements.
This commit is contained in:
parent
63f2d04651
commit
5bff434eb0
2 changed files with 42 additions and 16 deletions
16
README.md
16
README.md
|
|
@ -63,8 +63,8 @@ Generating enum sql builder files...
|
|||
Generating enum model files...
|
||||
Done
|
||||
```
|
||||
As jet command output suggest, jet will:
|
||||
- connect to postgres database and retrieve information about the tables and enums of `dvds` schema
|
||||
As command output suggest, Jet will:
|
||||
- connect to postgres database and retrieve information about the _tables_ and _enums_ of `dvds` schema
|
||||
- delete everything in destination folder `./gen`,
|
||||
- and finally generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`.
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ Generated files folder structure will look like this:
|
|||
...
|
||||
|
||||
```
|
||||
You will be using types from `table` and `enum` to write type safe SQL in Go, and `model` types would be used to store results of the queries.
|
||||
Types from `table` and `enum` are used to write type safe SQL in Go, and `model` types are used to store results of the queries.
|
||||
#### Lets write some SQL queries in Go
|
||||
|
||||
First lets import jet and generated files from previous step
|
||||
|
|
@ -100,8 +100,8 @@ import (
|
|||
"github.com/go-jet/jet/examples/quick-start/gen/jetdb/dvds/model"
|
||||
)
|
||||
```
|
||||
Lets say we want to retrieve the list of all actors that acted in films longer than 180 minutes, film language is 'English'
|
||||
and film category is not 'Action'.
|
||||
Lets say we want to retrieve the list of all _actors_ that acted in _films_ longer than 180 minutes, _film language_ is 'English'
|
||||
and _film category_ is not 'Action'.
|
||||
```go
|
||||
stmt := SELECT(
|
||||
Actor.ActorID, Actor.FirstName, Actor.LastName, Actor.LastUpdate, // list of all actor columns (equivalent to Actor.AllColumns)
|
||||
|
|
@ -217,7 +217,9 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
|
|||
```
|
||||
</details>
|
||||
|
||||
Well formed sql is just a first half the job. Now lets execute sql statement and store result in desired structure.
|
||||
Well formed sql is just a first half the job.
|
||||
##### Execute query and store result
|
||||
|
||||
Let's say this is our desired structure:
|
||||
```go
|
||||
var dest []struct {
|
||||
|
|
@ -445,7 +447,7 @@ Writing code is much faster and code is more robust. Automatic scan to arbitrary
|
|||
boilerplate code needed to structure database query result.
|
||||
With Jet programmer have the power of SQL but also ease of use of NoSQL.
|
||||
##### Speed of execution
|
||||
Common web and database server usually is not on the same physical machine, and there is some latency between them.
|
||||
Common web and database server usually are not on the same physical machine, and there is some latency between them.
|
||||
Latency can vary from 5ms to 50+ms. In majority of cases query executed on database is simple query lasting no more than 1ms.
|
||||
In those cases web server handler execution time is directly proportional to latency between server and database.
|
||||
This is not such a big problem if handler calls database couple of times, but what if web server is using ORM to retrieve all data from database.
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@ import (
|
|||
"fmt"
|
||||
"github.com/go-jet/jet/generator/postgres"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
host string
|
||||
port string
|
||||
port int
|
||||
user string
|
||||
password string
|
||||
sslmode string
|
||||
|
|
@ -22,31 +23,54 @@ var (
|
|||
|
||||
func init() {
|
||||
flag.StringVar(&host, "host", "", "Database host path (Example: localhost)")
|
||||
flag.StringVar(&port, "port", "", "Database port")
|
||||
flag.IntVar(&port, "port", 0, "Database port")
|
||||
flag.StringVar(&user, "user", "", "Database user")
|
||||
flag.StringVar(&password, "password", "", "The user’s password")
|
||||
flag.StringVar(&sslmode, "sslmode", "disable", "Whether or not to use SSL(optional)")
|
||||
flag.StringVar(¶ms, "params", "", "Additional connection string parameters(optional)")
|
||||
|
||||
flag.StringVar(&dbName, "dbname", "", "name of the database")
|
||||
flag.StringVar(&schemaName, "schema", "public", "Database schema name.")
|
||||
|
||||
flag.StringVar(&destDir, "path", "", "Destination dir for generated files.")
|
||||
|
||||
flag.Parse()
|
||||
flag.StringVar(&destDir, "path", "", "Destination dir for files generated.")
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
if host == "" || port == "" || user == "" || dbName == "" || schemaName == "" {
|
||||
fmt.Println("jet: required flag missing")
|
||||
flag.Usage = func() {
|
||||
_, _ = fmt.Fprint(os.Stdout, `
|
||||
Usage of jet:
|
||||
-host string
|
||||
Database host path (Example: localhost)
|
||||
-port int
|
||||
Database port
|
||||
-user string
|
||||
Database user
|
||||
-password string
|
||||
The user’s password
|
||||
-dbname string
|
||||
name of the database
|
||||
-params string
|
||||
Additional connection string parameters(optional)
|
||||
-schema string
|
||||
Database schema name. (default "public")
|
||||
-sslmode string
|
||||
Whether or not to use SSL(optional) (default "disable")
|
||||
-path string
|
||||
Destination dir for files generated.
|
||||
`)
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if host == "" || port == 0 || user == "" || dbName == "" || schemaName == "" {
|
||||
fmt.Println("\njet: required flag missing")
|
||||
flag.Usage()
|
||||
os.Exit(-2)
|
||||
}
|
||||
|
||||
genData := postgres.DBConnection{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Port: strconv.Itoa(port),
|
||||
User: user,
|
||||
Password: password,
|
||||
SslMode: sslmode,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue