Skip to content

Commit

Permalink
data/db files migrated to sqlite syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
damiensedgwick committed Aug 23, 2024
1 parent db66fb4 commit 7d664d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
15 changes: 6 additions & 9 deletions packages/frontpage/lib/data/db/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,13 @@ export const getCommentsForPost = cache(async (postId: number) => {
.as("vote");

const commentRank = sql`
coalesce(${votes.voteCount}, 1) / (
-- Age
CAST(COALESCE(${votes.voteCount}, 1) AS REAL) / (
(
EXTRACT(
EPOCH
FROM
(CURRENT_TIMESTAMP - ${schema.Comment.createdAt})
) / 3600
) + 2
) ^ 1.8
(JULIANDAY('now') - JULIANDAY(${schema.Comment.createdAt})) * 24 + 2
) * (
(JULIANDAY('now') - JULIANDAY(${schema.Comment.createdAt})) * 24 + 2
) * SQRT((JULIANDAY('now') - JULIANDAY(${schema.Comment.createdAt})) * 24 + 2)
)
`
.mapWith(Number)
.as("rank");
Expand Down
32 changes: 19 additions & 13 deletions packages/frontpage/lib/data/db/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "server-only";
import { cache } from "react";

import { cache } from "react";
import { db } from "@/lib/db";
import { eq, sql, count, desc, and } from "drizzle-orm";
import * as schema from "@/lib/schema";
Expand Down Expand Up @@ -43,16 +43,13 @@ export const getFrontpagePosts = cache(async () => {
// This ranking is very naive. I believe it'll need to consider every row in the table even if you limit the results.
// We should closely monitor this and consider alternatives if it gets slow over time
const rank = sql`
coalesce(${votesSubQuery.voteCount}, 1) / (
-- Age
CAST(COALESCE(${votesSubQuery.voteCount}, 1) AS REAL) / (
(
EXTRACT(
EPOCH
FROM
(CURRENT_TIMESTAMP - ${schema.Post.createdAt})
) / 3600
) + 2
) ^ 1.8
(JULIANDAY('now') - JULIANDAY(${schema.Post.createdAt})) * 24 + 2
) * (
(JULIANDAY('now') - JULIANDAY(${schema.Post.createdAt})) * 24 + 2
) * SQRT((JULIANDAY('now') - JULIANDAY(${schema.Post.createdAt})) * 24 + 2)
)
`.as("rank");

const userHasVoted = await buildUserHasVotedQuery();
Expand Down Expand Up @@ -183,7 +180,7 @@ type CreatePostInput = {
authorDid: DID;
rkey: string;
cid: string;
offset: bigint;
offset: number;
};

export async function unauthed_createPost({
Expand All @@ -200,7 +197,16 @@ export async function unauthed_createPost({
authorDid,
title: post.title,
url: post.url,
createdAt: new Date(post.createdAt),
createdAt: new Date(post.createdAt).toISOString(),
});

await tx.insert(schema.Post).values({
rkey,
cid,
authorDid,
title: post.title,
url: post.url,
createdAt: new Date(post.createdAt).toISOString(),
});

await tx.insert(schema.ConsumedOffset).values({ offset });
Expand Down Expand Up @@ -248,7 +254,7 @@ export async function unauthed_createPost({

type DeletePostInput = {
rkey: string;
offset: bigint;
offset: number;
};

export async function unauthed_deletePost({ rkey, offset }: DeletePostInput) {
Expand Down

0 comments on commit 7d664d6

Please sign in to comment.