-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path+server.ts
More file actions
28 lines (26 loc) · 896 Bytes
/
+server.ts
File metadata and controls
28 lines (26 loc) · 896 Bytes
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
import type { ServerLoad } from '@sveltejs/kit';
import { and, eq, gte } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/d1';
import { visitsTable } from '~/db/schema';
export const GET: ServerLoad = async ({ url, platform }) => {
const duration = Number.parseInt(url.searchParams.get('duration') ?? '');
if (!duration)
return new Response(
`{"error": "failed to parse ${url.searchParams.get('duration')} to number"}`
);
const threshold = new Date(new Date().getTime() - duration);
const kind = url.searchParams.get('kind') ?? 'all';
if (!platform) return new Response('platform not found');
const db = drizzle(platform.env.DB);
const resp = await db
.select()
.from(visitsTable)
.where(
and(
eq(visitsTable.kind, kind !== 'all' ? kind : visitsTable.kind),
gte(visitsTable.at, threshold)
)
)
.all();
return new Response(JSON.stringify(resp));
};