Skip to content

Commit

Permalink
[Docs] Update GORM Docs to reflect the latest changes in the Gorm orm…
Browse files Browse the repository at this point in the history
…-examples application (#21900)

* Upgrade the gorm docs to use latest go version and gorm v2

* Mentioned the use of smart drivers

* Harsh daryani896 patch 1 (#2)

* Update pgx driver version from v4 to v5 in docs.

* review comments and copied to preview

---------

Co-authored-by: Harsh Daryani <82017686+HarshDaryani896@users.noreply.github.com>
Co-authored-by: aishwarya24 <ashchakravarthy@gmail.com>
  • Loading branch information
3 people committed Apr 30, 2024
1 parent bd61a58 commit 95ec08a
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 147 deletions.
30 changes: 20 additions & 10 deletions docs/content/preview/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.

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/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 the [YugabyteDB smart driver for Go](https://github.com/yugabyte/pgx), which provides the connection load balancing feature.

Note: The application also works with the upstream Go driver (jackc/pgx). To use it, 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/preview/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
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.

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 the [YugabyteDB smart driver for Go](https://github.com/yugabyte/pgx), which provides the connection load balancing feature.

Note: The application also works with the upstream Go driver (jackc/pgx). To use it, 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

0 comments on commit 95ec08a

Please sign in to comment.