简体   繁体   English

使用 supabase-js 计算每个帖子的总喜欢数

[英]count totalLikes per post with supabase-js

I've been using the supabase-js for my application and I stumbled upon a problem.我一直在为我的应用程序使用 supabase-js,但我偶然发现了一个问题。 I want to get a table like this我想要一张这样的桌子

Post邮政 TotalLikes总赞数
0001 0001 3 3个
0002 0002 12 12
0005 0005 0 0

The SQL-query for this would be:对此的 SQL 查询将是:

SELECT Post, COUNT(*) as TotalLikes from "Votes"
where positive_vote = true
GROUP BY Post

I found this stackoverflow: How to get "COUNT(*)" in Supabase我发现了这个 stackoverflow: How to get "COUNT(*)" in Supabase

however this does not help in this case.然而,这在这种情况下没有帮助。

Any help/suggestion is welcome欢迎任何帮助/建议

You can create a view with this query and then run it using a normal select in the supabase-js library.您可以使用此查询创建视图,然后使用 supabase-js 库中的普通 select 运行它。

Create the view with the following code使用以下代码创建视图

CREATE VIEW the_name_of_the_view AS
SELECT Post, COUNT(*) as TotalLikes from "Votes"
where positive_vote = true
GROUP BY Post

And then call it with然后用

await supabase.from('the_name_of_the_view').select('*')

Do note that you can't apply row level security (rls) on views in Postgres 14 and below.请注意,您不能在 Postgres 14 及以下版本的视图上应用行级安全性 (rls)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM