Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Update GORM Docs to reflect the latest changes in the Gorm orm-examples application #21900

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 14 additions & 23 deletions docs/content/preview/drivers-orms/orms/go/ysql-gorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,7 @@ The source for the above application can be found in the [repository](https://gi
This tutorial assumes that you have:

- YugabyteDB up and running. Download and install YugabyteDB by following the steps in [Quick start](../../../../quick-start/).
- Go 1.8, or later, is installed. The latest releases are available on the [Go Downloads page](https://golang.org/dl/).

### Go dependencies

To install the required Go dependencies, run the following commands.

```sh
go get github.com/jinzhu/gorm
go get github.com/jinzhu/gorm/dialects/postgres
go get github.com/google/uuid
go get github.com/gorilla/mux
go get github.com/lib/pq
go get github.com/lib/pq/hstore
```
- Go 1.21, or later, is installed. The latest releases are available on the [Go Downloads page](https://golang.org/dl/).

## Clone the "orm-examples" repository

Expand All @@ -51,28 +38,32 @@ Clone the Yugabyte [`orm-examples` repository](https://github.com/yugabyte/orm-e
```sh
$ git clone https://github.com/YugabyteDB-Samples/orm-examples.git
```
## Set up the application and install dependencies

Run the following `export` command to specify the `GOPATH` environment variable.
Initialize Go within the project:

```sh
export GOPATH=$GOPATH:$HOME/orm-examples/golang/gorm
cd golang/gorm
go mod init gorm-example
```

## Build and run the application

Change to the `gorm` directory.
Download the included packages and dependencies:

```sh
$ cd ./golang/gorm
go mod tidy
```

## Build and run the application

Create the `ysql_gorm` database in YugabyteDB by running the following `ysqlsh` command from the YugabyteDB home directory.

```sh
$ ./bin/ysqlsh -c "CREATE DATABASE ysql_gorm"
```

Build and start the REST API server by running the following shell script.
Build and start the REST API server by running the following shell script. The application uses `yugabytedb/pgx` driver, so load balance feature is enabled.

Note: To use the upstream driver replace `github.com/yugabyte/gorm-yugabytedb` with `gorm.io/driver/postgres` in the application code.
aishwarya24 marked this conversation as resolved.
Show resolved Hide resolved

```sh
$ ./build-and-run.sh
Expand Down Expand Up @@ -112,13 +103,13 @@ Create 2 orders.

```sh
$ curl \
--data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 } ] }' \
--data '{ "userId": 2, "products": [ { "productId": 1, "units": 2 } ] }' \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sfurti-yb - can you clarify ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddhodge it could be, we will have to test the individual applications to check if this fix is needed for any of them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wouldn't all the examples regardless of driver use the same REST syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, each ORM has a different way of parsing the message body, and some might not throw an error in this case. This needs to be verified with the individual ORM application to check how message bodies are being handled internally. Maybe create a ticket to verify this behaviour

-v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders
```

```sh
$ curl \
--data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
--data '{ "userId": 2, "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
-v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders
```

Expand Down
30 changes: 20 additions & 10 deletions docs/content/stable/drivers-orms/go/yb-pgx.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Import the YugabyteDB PGX driver package by adding the following import statemen

```go
import (
"github.com/yugabyte/pgx/v4"
"github.com/yugabyte/pgx/v5"
)
```

Expand All @@ -79,17 +79,15 @@ To install the package locally, run the following commands:
mkdir yb-pgx
cd yb-pgx
go mod init hello
go get github.com/yugabyte/pgx/v4
go get github.com/yugabyte/pgx/v4/pgxpool # Install pgxpool package if you write your application with pgxpool.Connect().
```

Optionally, you can choose to import the pgxpool package instead. Refer to [Use pgxpool API](../../../reference/drivers/go/yb-pgx-reference/#use-pgxpool-api) to learn more.

### Step 2: Set up the database connection

Go applications can connect to the YugabyteDB database using the `pgx.Connect()` and `pgxpool.Connect()` functions. The `pgx` package includes all the common functions or structs required for working with YugabyteDB.
Go applications can connect to the YugabyteDB database using the `pgx.Connect()` and `pgxpool.New()` functions. The `pgx` package includes all the common functions or structs required for working with YugabyteDB.
aishwarya24 marked this conversation as resolved.
Show resolved Hide resolved

Use the `pgx.Connect()` method or `pgxpool.Connect()` method to create a connection object for the YugabyteDB database. This can be used to perform DDLs and DMLs against the database.
Use the `pgx.Connect()` method or `pgxpool.New()` method to create a connection object for the YugabyteDB database. This can be used to perform DDLs and DMLs against the database.

The following table describes the connection parameters required to connect, including [smart driver parameters](../../smart-drivers/) for uniform and topology load balancing.

Expand Down Expand Up @@ -208,7 +206,7 @@ import (
"strconv"
"time"

"github.com/yugabyte/pgx/v4"
"github.com/yugabyte/pgx/v5"
)

const (
Expand Down Expand Up @@ -393,6 +391,12 @@ The **const** values are set to the defaults for a local installation of Yugabyt
- **dbname** - The name of the YugabyteDB database. The default name is **yugabyte**.
- **port** is set to 5433, which is the default port for the YSQL API.

Download the included packages and dependencies:

```go
go mod tidy
```

Run the project `QuickStartApp.go` using the following command:

```go
Expand Down Expand Up @@ -433,7 +437,7 @@ Closing 12 connections ...
Closing the application ...
```

### Step 4 : Write your application with pgxpool.Connect()
### Step 4 : Write your application with pgxpool.New()

Create a file called `QuickStart2.go` and add the following contents into it:

Expand All @@ -450,8 +454,8 @@ import (
"sync"
"time"

"github.com/yugabyte/pgx/v4"
"github.com/yugabyte/pgx/v4/pgxpool"
"github.com/yugabyte/pgx/v5"
"github.com/yugabyte/pgx/v5/pgxpool"
)

const (
Expand Down Expand Up @@ -496,7 +500,7 @@ func main() {
func initPool(url string) {
var err error
fmt.Printf("Initializing pool with url %s\n", url)
pool, err = pgxpool.Connect(context.Background(), url)
pool, err = pgxpool.New(context.Background(), url)
if err != nil {
log.Fatalf("Error initializing the pool: %s", err.Error())
}
Expand Down Expand Up @@ -629,6 +633,12 @@ The **const** values are set to the defaults for a local installation of Yugabyt

## Run the application

Download the included packages and dependencies:

```go
go mod tidy
```

Run the project `QuickStartApp2.go` using the following command:

```go
Expand Down
37 changes: 14 additions & 23 deletions docs/content/stable/drivers-orms/orms/go/ysql-gorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,7 @@ The source for the above application can be found in the [repository](https://gi
This tutorial assumes that you have:

- YugabyteDB up and running. Download and install YugabyteDB by following the steps in [Quick start](../../../../quick-start/).
- Go 1.8, or later, is installed. The latest releases are available on the [Go Downloads page](https://golang.org/dl/).

### Go dependencies

To install the required Go dependencies, run the following commands.

```sh
go get github.com/jinzhu/gorm
go get github.com/jinzhu/gorm/dialects/postgres
go get github.com/google/uuid
go get github.com/gorilla/mux
go get github.com/lib/pq
go get github.com/lib/pq/hstore
```
- Go 1.21, or later, is installed. The latest releases are available on the [Go Downloads page](https://golang.org/dl/).

## Clone the "orm-examples" repository

Expand All @@ -51,28 +38,32 @@ Clone the Yugabyte [`orm-examples` repository](https://github.com/yugabyte/orm-e
```sh
$ git clone https://github.com/YugabyteDB-Samples/orm-examples.git
```
## Set up the application and install dependencies

Run the following `export` command to specify the `GOPATH` environment variable.
Initialize Go within the project:

```sh
export GOPATH=$GOPATH:$HOME/orm-examples/golang/gorm
cd golang/gorm
go mod init gorm-example
```

## Build and run the application

Change to the `gorm` directory.
Download the included packages and dependencies:

```sh
$ cd ./golang/gorm
go mod tidy
```

## Build and run the application

Create the `ysql_gorm` database in YugabyteDB by running the following `ysqlsh` command from the YugabyteDB home directory.

```sh
$ ./bin/ysqlsh -c "CREATE DATABASE ysql_gorm"
```

Build and start the REST API server by running the following shell script.
Build and start the REST API server by running the following shell script. The application uses `yugabytedb/pgx` driver, so load balance feature is enabled.

Note: To use the upstream driver replace `github.com/yugabyte/gorm-yugabytedb` with `gorm.io/driver/postgres` in the application code.

```sh
$ ./build-and-run.sh
Expand Down Expand Up @@ -112,13 +103,13 @@ Create 2 orders.

```sh
$ curl \
--data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 } ] }' \
--data '{ "userId": 2, "products": [ { "productId": 1, "units": 2 } ] }' \
-v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders
```

```sh
$ curl \
--data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
--data '{ "userId": 2, "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
-v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders
```

Expand Down
12 changes: 6 additions & 6 deletions docs/content/stable/reference/drivers/go/yb-pgx-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ You can import the YugabyteDB PGX driver package by adding the following import

```go
import (
"github.com/yugabyte/pgx/v4"
"github.com/yugabyte/pgx/v5"
)
```

Expand Down Expand Up @@ -185,16 +185,16 @@ The YugabyteDB PGX driver also provides pool APIs via the `pgxpool` package. You

```go
import (
"github.com/yugabyte/pgx/tree/master/pgxpool"
"github.com/yugabyte/pgx/v5/pgxpool"
)
```

### Establish a connection

The primary way of establishing a connection is with `pgxpool.Connect()`.
The primary way of establishing a connection is with `pgxpool.New()`.

```go
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
pool, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
```

You can also provide configuration for the pool as follows:
Expand All @@ -208,7 +208,7 @@ config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
// do something with every new connection
}

pool, err := pgxpool.ConnectConfig(context.Background(), config)
pool, err := pgxpool.NewWithConfig(context.Background(), config)
```

You can either `Acquire` a connection from the pool and execute queries on it, or use the Query API to directly execute SQLs on the pool.
Expand All @@ -230,7 +230,7 @@ _, err = conn.Exec(context.Background(), createStmt)
rows, err := pool.Query(context.Background(), "SELECT name, age, language FROM employee WHERE id = 1")
```

For more details, see the [pgxpool package](https://pkg.go.dev/github.com/jackc/pgx/v4/pgxpool) documentation.
For more details, see the [pgxpool package](https://pkg.go.dev/github.com/jackc/pgx/v5/pgxpool) documentation.

## Configure SSL/TLS

Expand Down
37 changes: 14 additions & 23 deletions docs/content/v2.14/quick-start/build-apps/go/ysql-gorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,7 @@ YugabyteDB is up and running. If you are new to YugabyteDB, you can have Yugabyt

### Go

Go 1.8, or later, is installed. The latest releases are available on the [Go Downloads page](https://golang.org/dl/).

### Go dependencies

To install the required Go dependencies, run the following commands.

```sh
go get github.com/jinzhu/gorm
go get github.com/jinzhu/gorm/dialects/postgres
go get github.com/google/uuid
go get github.com/gorilla/mux
go get github.com/lib/pq
go get github.com/lib/pq/hstore
```
Go 1.21, or later, is installed. The latest releases are available on the [Go Downloads page](https://golang.org/dl/).

## Clone the "orm-examples" repository

Expand All @@ -92,28 +79,32 @@ Clone the Yugabyte [`orm-examples` repository](https://github.com/yugabyte/orm-e
```sh
$ git clone https://github.com/YugabyteDB-Samples/orm-examples.git
```
## Set up the application and install dependencies

Run the following `export` command to specify the `GOPATH` environment variable.
Initialize Go within the project:

```sh
export GOPATH=$GOPATH:$HOME/orm-examples/golang/gorm
cd golang/gorm
go mod init gorm-example
```

## Build and run the application

Change to the `gorm` directory.
Download the included packages and dependencies:

```sh
$ cd ./golang/gorm
go mod tidy
```

## Build and run the application

Create the `ysql_gorm` database in YugabyteDB by running the following `ysqlsh` command from the YugabyteDB home directory.

```sh
$ ./bin/ysqlsh -c "CREATE DATABASE ysql_gorm"
```

Build and start the REST API server by running the following shell script.
Build and start the REST API server by running the following shell script. The application uses `yugabytedb/pgx` driver, so load balance feature is enabled.

Note: To use the upstream driver replace `github.com/yugabyte/gorm-yugabytedb` with `gorm.io/driver/postgres` in the application code.

```sh
$ ./build-and-run.sh
Expand Down Expand Up @@ -153,13 +144,13 @@ Create 2 orders.

```sh
$ curl \
--data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 } ] }' \
--data '{ "userId": 2, "products": [ { "productId": 1, "units": 2 } ] }' \
-v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders
```

```sh
$ curl \
--data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
--data '{ "userId": 2, "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
-v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders
```

Expand Down