Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
remvn committed Sep 25, 2024
1 parent 7f5b4a3 commit 058d1a8
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ tags:
## What is pgx and sqlc?

- [pgx](https://github.com/jackc/pgx): a robust toolkit and PostgreSQL driver
for Golang. This module provide plenty of great APIs for handling complex
for Golang. This module also provides some useful tool for handling complex
queries easier and less error-prone.
- [sqlc](https://github.com/sqlc-dev/sqlc): a code generator tool that turns
your SQL querie in `.sql` files into Go code with type-safe for both query
your SQL queries in `.sql` files into Go code with type-safe for both query
params and query result. Check out an example here: [sqlc
playground](https://play.sqlc.dev/). sqlc also <mark>supports pgx out of the
box</mark>, which makes this a great combination for your database's need.
Expand All @@ -31,22 +31,22 @@ an easy choice to implement and use, plus you don't have to write SQL. Sounds
too good to be true... but here's the catch:

Based on my experience, almost all `ORM` I have ever used only perform well in
easy scenarios (eg `CRUD operation`). The more complex your query get, the
easy scenarios (eg `CRUD operation`). The more complex your query gets, the
harder to implement properly in these `ORM` sometime it's even harder than
writing raw SQL query manually, to the point you have to pull out the big
gun... yes, you grab the database driver under the wrapper and start
raw-dogging query, map the types manually and questioning yourself why you chose
to use an ORM in the first place. {{< emoji "beat_shot" >}}

Another foot gun of `ORM` is that you generally can't control the `SQL query`
they produces, they may do dumb things and write terrible queries. Here's a funny
they produce, they may do dumb things and write terrible queries. Here's a funny
story about `Prisma ORM` in javascript world: [video](https://youtu.be/jqhHXe746Ns)

A balance spot between `error-prone, untyped raw query` and `ORM` is `query
builder`. They provide some sort of type safety and the flexibility to build
and optimize complex query.

### The purpose of sqlc
### Usage of sqlc

`sqlc` is not exactly a query builder but a code-generation tool that reads
your queries and schema in `.sql` files and turns it into type-safe code for both
Expand All @@ -59,7 +59,7 @@ SELECT * FROM authors
WHERE id = $1 LIMIT 1;
```

In to this type-safe Go code:
In to this type-safe Go code, ready to use:
```go
const createAuthor = `-- name: CreateAuthor :one
INSERT INTO authors (
Expand All @@ -83,13 +83,14 @@ func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Aut
}
```

So I should provide a similar experience of using `query builder`: You can write
things that's close to SQL query and have the types mapped automatically
So it should provide a similar experience to using a `query builder`: You can
write things that are close to SQL queries and have the types mapped
automatically

### The purpose of pgx
### Usage of pgx

There's even more complex query that neither `query builder` nor `sqlc` can
handle it. This is where you can use `pgx` to handle these specific complex
handle. This is where you can use `pgx` to handle these specific complex
query.

`pgx` also comes with plenty of useful tools which made it easier to
Expand Down Expand Up @@ -237,7 +238,7 @@ type Database struct {

func NewDatabase(connStr string) *Database {
// this only create pgxpool struct, you may need to ping the database to
// initialize a connection and check availability
// grab a connection and check availability
pool, err := pgxpool.New(context.Background(), connStr)
if err != nil {
log.Fatal(err)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
25 changes: 25 additions & 0 deletions content/posts/some-great-golang-book-im-reading/index.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Some great Golang book that I'm reading
date: "2024-09-26T04:06:09+07:00"
draft: false
showComments: true
description: "Some great Golang book that I'm reading"
tags:
- golang
- random
- book
---

[Let's Go by Alex Edwards](https://lets-go.alexedwards.net/)
[<img class="mt-1" src="lets-go.jpg" width="300px"/>](https://lets-go.alexedwards.net/)

[Let's Go Further by Alex Edwards](https://lets-go-further.alexedwards.net/)
[<img class="mt-1" src="lets-go-further.jpg" width="300px"/>](https://lets-go-further.alexedwards.net/)

[100 Go Mistakes and How to Avoid Them by teivah](https://100go.co/book/)
[<img class="mt-1" src="100-mistakes.jpg" width="300px"/>](https://100go.co/book/)

[Concurrency in Go by Cox-Buday](https://www.oreilly.com/library/view/concurrency-in-go/9781491941294/)
[<img class="mt-1" src="concurrency.jpg" width="300px"/>](https://www.oreilly.com/library/view/concurrency-in-go/9781491941294/)


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 058d1a8

Please sign in to comment.