Skip to content

Commit c37ec53

Browse files
committed
perf(notifications): use videoId column instead of JSON_EXTRACT in queries
1 parent b4eb2d6 commit c37ec53

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

apps/web/lib/Notification.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export async function createNotification(
148148
recipientId,
149149
type,
150150
data,
151+
videoId: notification.videoId,
151152
});
152153

153154
revalidatePath("/dashboard");
@@ -181,7 +182,7 @@ export async function createNotification(
181182
and(
182183
eq(notifications.type, "view"),
183184
eq(notifications.recipientId, videoResult.ownerId),
184-
sql`JSON_EXTRACT(${notifications.data}, '$.videoId') = ${notification.videoId}`,
185+
eq(notifications.videoId, notification.videoId),
185186
sql`JSON_EXTRACT(${notifications.data}, '$.authorId') = ${notification.authorId}`,
186187
),
187188
)
@@ -223,6 +224,7 @@ export async function createNotification(
223224
recipientId: videoResult.ownerId,
224225
type,
225226
data,
227+
videoId: notification.videoId,
226228
});
227229

228230
revalidatePath("/dashboard");
@@ -285,7 +287,7 @@ export async function createAnonymousViewNotification({
285287
eq(notifications.type, "anon_view"),
286288
eq(notifications.recipientId, videoWithOwner.ownerId),
287289
gte(notifications.createdAt, rateWindowStart),
288-
sql`JSON_EXTRACT(${notifications.data}, '$.videoId') = ${videoId}`,
290+
eq(notifications.videoId, videoId),
289291
),
290292
)
291293
.limit(1);
@@ -299,6 +301,7 @@ export async function createAnonymousViewNotification({
299301
recipientId: videoWithOwner.ownerId,
300302
type: "anon_view",
301303
data: { videoId, anonName, location },
304+
videoId,
302305
dedupKey,
303306
});
304307
revalidatePath("/dashboard");
@@ -316,16 +319,9 @@ export async function sendFirstViewEmail(
316319
try {
317320
const database = db();
318321

319-
const [video] = await database
320-
.select({ firstViewEmailSentAt: videos.firstViewEmailSentAt })
321-
.from(videos)
322-
.where(eq(videos.id, Video.VideoId.make(params.videoId)))
323-
.limit(1);
324-
325-
if (!video || video.firstViewEmailSentAt) return;
326-
327-
const [videoWithOwner] = await database
322+
const videoWithOwner = await database
328323
.select({
324+
firstViewEmailSentAt: videos.firstViewEmailSentAt,
329325
videoName: videos.name,
330326
ownerId: videos.ownerId,
331327
ownerEmail: users.email,
@@ -334,7 +330,12 @@ export async function sendFirstViewEmail(
334330
.from(videos)
335331
.innerJoin(users, eq(users.id, videos.ownerId))
336332
.where(eq(videos.id, Video.VideoId.make(params.videoId)))
337-
.limit(1);
333+
.limit(1)
334+
.then((rows) => {
335+
const row = rows[0];
336+
if (!row || row.firstViewEmailSentAt) return null;
337+
return row;
338+
});
338339

339340
if (!videoWithOwner?.ownerEmail) return;
340341

0 commit comments

Comments
 (0)