-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtriggers.ts
200 lines (176 loc) · 5.25 KB
/
triggers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import * as schema from "@/lib/schema";
import { ResultSet } from "@libsql/client";
import {
ExtractTablesWithRelations,
and,
eq,
inArray,
isNull,
sql,
} from "drizzle-orm";
import { SQLiteTransaction } from "drizzle-orm/sqlite-core";
type Transaction = SQLiteTransaction<
"async",
ResultSet,
typeof schema,
ExtractTablesWithRelations<typeof schema>
>;
const add = (column: schema.NumberColumn, value: number) => {
return sql`${column} + ${value}`;
};
export const calculateRankSql = (
voteCountColumn: schema.NumberColumn,
createdAtColumn: schema.DateIsoColumn,
) => {
return sql<number>`
(CAST(COALESCE(${voteCountColumn} + 1, 1) AS REAL) / (pow((JULIANDAY('now') - JULIANDAY(${createdAtColumn})) * 24 + 2, 1.8)))`;
};
//Post flow:
// - on new post create new aggregate, upadte all post ranks
export const newPostAggregateTrigger = async (
postId: number,
tx: Transaction,
) => {
await tx.insert(schema.PostAggregates).values({
postId,
commentCount: 0,
voteCount: 0,
});
await updateAllPostRanks(tx);
};
//Vote Post flow:
// - on new vote update post aggregate with vote count +1 and recalculate rank for all posts
export const newPostVoteAggregateTrigger = async (
postId: number,
tx: Transaction,
) => {
await tx
.update(schema.PostAggregates)
.set({
voteCount: add(schema.PostAggregates.voteCount, 1),
})
.where(eq(schema.PostAggregates.postId, postId));
await updateAllPostRanks(tx);
};
// - on delete vote update post aggregate with vote count -1 and recalculate rank for all posts
export const deletePostVoteAggregateTrigger = async (
postId: number,
tx: Transaction,
) => {
await tx
.update(schema.PostAggregates)
.set({
voteCount: add(schema.PostAggregates.voteCount, -1),
})
.where(eq(schema.PostAggregates.postId, postId));
await updateAllPostRanks(tx);
};
//Comment flow:
// - on new comment create new comment aggregate, update post aggregates with comment count +1 and recalculate rank for all comments on that post at the same nested level
export const newCommentAggregateTrigger = async (
postId: number,
commentId: number,
tx: Transaction,
) => {
await tx.insert(schema.CommentAggregates).values({
commentId,
voteCount: 0,
});
await tx
.update(schema.PostAggregates)
.set({
commentCount: add(schema.PostAggregates.commentCount, 1),
})
.where(eq(schema.PostAggregates.postId, postId));
await updateSiblingCommentRanksOnPost(postId, commentId, tx);
};
// - on new vote update comment aggregate with vote count +1 and recalculate rank for all comments on post
export const newCommentVoteAggregateTrigger = async (
postId: number,
commentId: number,
tx: Transaction,
) => {
await tx
.update(schema.CommentAggregates)
.set({
voteCount: add(schema.CommentAggregates.voteCount, 1),
})
.where(eq(schema.CommentAggregates.commentId, commentId));
await updateSiblingCommentRanksOnPost(postId, commentId, tx);
};
// - on delete comment update post aggregates with comment count -1 and recalculate comment_aggregate rank for all comments on that post
export const deleteCommentAggregateTrigger = async (
postId: number,
commentId: number,
tx: Transaction,
) => {
await tx
.update(schema.PostAggregates)
.set({
commentCount: add(schema.PostAggregates.commentCount, -1),
})
.where(eq(schema.PostAggregates.postId, postId));
await updateSiblingCommentRanksOnPost(postId, commentId, tx);
};
// - on delete vote update comment aggregate with vote count -1 and recalculate rank for all comments on post
export const deleteCommentVoteAggregateTrigger = async (
postId: number,
commentId: number,
tx: Transaction,
) => {
await tx
.update(schema.CommentAggregates)
.set({
voteCount: add(schema.CommentAggregates.voteCount, -1),
})
.where(eq(schema.CommentAggregates.commentId, commentId));
await updateSiblingCommentRanksOnPost(postId, commentId, tx);
};
export const updateAllPostRanks = async (tx: Transaction) => {
await tx.update(schema.PostAggregates).set({
rank: calculateRankSql(
schema.PostAggregates.voteCount,
schema.PostAggregates.createdAt,
),
});
};
export const updateSiblingCommentRanksOnPost = async (
postId: number,
commentId: number,
tx: SQLiteTransaction<
"async",
ResultSet,
typeof schema,
ExtractTablesWithRelations<typeof schema>
>,
) => {
const [comment] = await tx
.select({ parentCommentId: schema.Comment.parentCommentId })
.from(schema.Comment)
.where(
and(eq(schema.Comment.id, commentId), eq(schema.Comment.postId, postId)),
);
if (!comment) {
throw new Error("Comment not found");
}
const commentIds = tx
.select({ commentId: schema.Comment.id })
.from(schema.Comment)
.where(
and(
comment.parentCommentId === null
? isNull(schema.Comment.parentCommentId)
: eq(schema.Comment.parentCommentId, comment.parentCommentId),
eq(schema.Comment.postId, postId),
),
);
await tx
.update(schema.CommentAggregates)
.set({
rank: calculateRankSql(
schema.CommentAggregates.voteCount,
schema.CommentAggregates.createdAt,
),
})
.where(inArray(schema.CommentAggregates.commentId, commentIds));
};