From d9ee679c8f43ec965f612aaf6fdec06c4a14e069 Mon Sep 17 00:00:00 2001 From: Chris Hua Date: Mon, 19 Jun 2023 13:54:35 -0400 Subject: [PATCH 1/2] chore: minor - doc update --- docs/pages/docs/walkthrough/raw.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/pages/docs/walkthrough/raw.md b/docs/pages/docs/walkthrough/raw.md index 8c1bf264..59e1e3a4 100644 --- a/docs/pages/docs/walkthrough/raw.md +++ b/docs/pages/docs/walkthrough/raw.md @@ -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) ``` +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. + #### Operations Use `ExecuteRaw` for operations such as `INSERT`, `UPDATE` or `DELETE`. It will always return a `Result{Count: int}`, which contains the affected rows. @@ -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"` } err := client.Prisma.QueryRaw(`SELECT post_id, count(*) as comments FROM "Comment" GROUP BY post_id`).Exec(ctx, &res) ``` From d5268738f60595a7529159290fb8a1fb8bd65adf Mon Sep 17 00:00:00 2001 From: Chris Hua Date: Mon, 19 Jun 2023 13:57:05 -0400 Subject: [PATCH 2/2] fix: link --- docs/pages/docs/getting-started/advanced.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pages/docs/getting-started/advanced.md b/docs/pages/docs/getting-started/advanced.md index 9f114dac..2124809e 100644 --- a/docs/pages/docs/getting-started/advanced.md +++ b/docs/pages/docs/getting-started/advanced.md @@ -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).