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(walkthrough): minor clarifications #884

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/pages/docs/getting-started/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ log.Printf("post: %+v", post)

## API reference

To explore all query capabilities, check out the [API reference](walkthrough).
To explore all query capabilities, check out the [API reference](../walkthrough).
11 changes: 7 additions & 4 deletions docs/pages/docs/walkthrough/raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ err := client.Prisma.QueryRaw("SELECT * FROM `Post` WHERE id = ? AND title = ?",

#### Custom Query

The Prisma client doesn't support aggregations out of the box. But you can do that via a custom query:

```go
// note the usage of db.RawString, db.RawInt, etc.
var res []struct{
PostID db.RawString `json:"post_id"`
Comments db.RawInt `json:"comments"`
Comments db.RawInt `json:"n_comments"`
}
err := client.Prisma.QueryRaw("SELECT post_id, count(*) as comments FROM `Comment` GROUP BY post_id").Exec(ctx, &res)
err := client.Prisma.QueryRaw("SELECT post_id, count(*) as n_comments FROM `Comment` GROUP BY post_id").Exec(ctx, &res)
Copy link
Owner

Choose a reason for hiding this comment

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

maybe comments_count?

```

Note that the query uses `db.RawString` etc in the struct definition to maintain compatibility. Note also that the results are an array of structs, not a struct.
Copy link
Owner

Choose a reason for hiding this comment

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

not sure about this, would need a mention in the postgres part as well. Maybe I need to refactor this page with some mdx components to have a language switcher in the docs as it's a lot of duplicatoin here, but haven't gotten the chance


#### Operations

Use `ExecuteRaw` for operations such as `INSERT`, `UPDATE` or `DELETE`. It will always return a `Result{Count: int}`, which contains the affected rows.
Expand Down Expand Up @@ -109,7 +112,7 @@ To ensure compatibility with database and go types, you can use raw types.
// note the usage of db.RawString, db.RawInt, etc.
var res []struct{
ID db.RawString `json:"post_id"`
Published db.RawBoolean `json:"published"`
Comments db.RawInt `json:"comments"`
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Comments db.RawInt `json:"comments"`
Published db.RawBoolean `json:"published"`

}
err := client.Prisma.QueryRaw(`SELECT post_id, count(*) as comments FROM "Comment" GROUP BY post_id`).Exec(ctx, &res)
```
Expand Down
Loading