Skip to content

Commit d173d51

Browse files
committed
Updates docs for vote ranking and RLS
1 parent d947e4a commit d173d51

5 files changed

Lines changed: 93 additions & 8 deletions

File tree

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ A basic HackerNews-like clone where posts can be submitted with url links and th
3434
- Create Comment
3535
- Delete Comment
3636
- Upvote/Downvote Post
37+
- View Profile (Account)
38+
- View Profile (Public)
39+
- Pagination (Posts, Comments)
3740

3841
## QuickStart
3942

@@ -104,7 +107,7 @@ dbmate dump
104107
- Post `url` is unique
105108
- Vote is unique per Profile, Post (ie, you cannot vote more than once -- up or down)
106109

107-
See: `./data/db/schema.sql`
110+
See: [`./data/db/schema.sql`](./data/db/schema.sql)
108111

109112
> Note: The schema includes the entire Supabase schema with auth, storage, functions, etc.
110113
@@ -116,10 +119,12 @@ Note: Assumes a known `profileId` currently.
116119

117120
## GraphQL Schema
118121

119-
See: `./graphql/schema/schema.graphql`
122+
See: [`./graphql/schema/schema.graphql`](./graphql/schema/schema.graphql)
120123

121124
## Example Query
122125

126+
See: [`./graphql/queries/`](./graphql/queries/)
127+
123128
Use: `https://mvrfvzcivgabojxddwtk.supabase.co/rest/v1/rpc/graphql`
124129

125130
Note: Needs headers
@@ -173,6 +178,10 @@ query {
173178
}
174179
```
175180

181+
# Row Level Security Matrix (RLS)
182+
183+
See: [Row Level Security Matrix (RLS)](./data/supabase/rls-policies.md)
184+
176185
## Read More
177186

178187
- [pg_graphql](https://supabase.github.io/pg_graphql)

data/supabase/06-update-post-vote-counts.sql

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BEGIN
44

55
WITH r AS (
66
SELECT
7-
"postId",
7+
coalesce("Vote"."postId", "Post".id) AS "postId",
88
count(1) "voteTotal",
99
count(1) FILTER (WHERE direction = 'UP') "upVoteTotal",
1010
count(1) FILTER (WHERE direction = 'DOWN') "downVoteTotal",
@@ -16,25 +16,30 @@ SELECT
1616
ELSE
1717
0
1818
END), 0) "voteDelta",
19-
abs(sum(
19+
sum(
2020
CASE WHEN direction = 'UP' THEN
2121
1
2222
WHEN direction = 'DOWN' THEN
2323
- 1
2424
ELSE
2525
0
26-
END) - 1 / (DATE_PART('hour', now() - max("createdAt")) + 2) ^ 1.8) AS "score",
27-
dense_rank() OVER (ORDER BY sum( CASE WHEN direction = 'UP' THEN
26+
END) - 1 / (DATE_PART('hour', now() - max("Vote"."createdAt")) + 2) ^ 1.8 AS "score",
27+
rank() OVER (ORDER BY coalesce(sum( CASE WHEN direction = 'UP' THEN
2828
1
2929
WHEN direction = 'DOWN' THEN
3030
- 1
3131
ELSE
3232
0
33-
END) - 1 / (DATE_PART('hour', now() - max("createdAt")) + 2) ^ 1.8 DESC) "voteRank"
33+
END) - 1 / (DATE_PART('hour', now() - max("Vote"."createdAt")) + 2) ^ 1.8, '-infinity')
34+
DESC,
35+
"Post"."createdAt" DESC,
36+
"Post".title ASC) "voteRank"
3437
FROM
3538
"Vote"
39+
RIGHT JOIN "Post" ON "Vote"."postId" = "Post".id
3640
GROUP BY
37-
"postId"
41+
"Post".id,
42+
"Vote"."postId"
3843
)
3944

4045
UPDATE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ALTER TABLE ONLY public."Comment"
2+
DROP CONSTRAINT "Comment_postId_fkey";
3+
4+
ALTER TABLE ONLY public."Comment"
5+
ADD CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES public."Post"(id) ON DELETE CASCADE;
6+
7+
ALTER TABLE ONLY public."Vote"
8+
DROP CONSTRAINT "DownVote_postId_fkey";
9+
10+
ALTER TABLE ONLY public."Vote"
11+
ADD CONSTRAINT "Vote_postId_fkey" FOREIGN KEY ("postId") REFERENCES public."Post"(id) ON DELETE CASCADE;
12+

data/supabase/rls-policies.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Row Level Security Matrix (RLS)
2+
3+
## Profile
4+
5+
- All users can `SELECT` all `PROFILE`s
6+
- Only authenticated users can CREATE `PROFILE`
7+
- Only `PROFILE`s where auth user is `id` can `UPDATE`
8+
- No `PROFILE` `DELETE`. This might be an Admin role eventually.
9+
10+
## Posts
11+
12+
- All users can `SELECT` all `POST`s
13+
- Only authenticated users can `CREATE POST`
14+
- Only `POST`s where auth user is `profileId` can `UPDATE`
15+
- Only `POST`s where auth user is `profileId` can `DELETE`
16+
17+
FYI: `DELETE POST` cascade to `COMMENT`s and V`OTE`s
18+
19+
## Comment
20+
21+
- All users can `SELECT` all `COMMENT`s
22+
- Only authenticated users can `CREATE COMMENT`
23+
- Only `COMMENT`s where auth user is `profileId` can `UPDATE`
24+
- Only `COMMENT`s where auth user is `profileId` can `DELETE`
25+
26+
## Vote
27+
28+
- All users can `SELECT` all `VOTE`s
29+
- Only authenticated users can `CREATE VOTE`
30+
- Only `VOTE`s where auth user is `profileId` can `UPDATE`
31+
- Only `VOTE`s where auth user is `profileId` can `DELETE`
32+
33+
Note: Does this mean I can see how people voted?
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
query {
2+
post: postCollection(filter: { id: { eq: 22 } }, first: 1) {
3+
edges {
4+
post: node {
5+
id
6+
title
7+
upVoteCount: voteCollection(
8+
filter: {
9+
profileId: { eq: "3e223118-04b2-4faa-8ed9-d3995fc50975" }
10+
direction: { eq: "UP" }
11+
}
12+
) {
13+
totalCount
14+
}
15+
downVoteCount: voteCollection(
16+
filter: {
17+
profileId: { eq: "3e223118-04b2-4faa-8ed9-d3995fc50975" }
18+
direction: { eq: "DOWN" }
19+
}
20+
) {
21+
totalCount
22+
}
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)