-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
71 lines (64 loc) · 1.74 KB
/
db.ts
File metadata and controls
71 lines (64 loc) · 1.74 KB
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
import { createClient, type Client } from "@libsql/client";
let client: Client | null = null;
function getClient(): Client {
if (!client) {
if (!process.env.TURSO_DATABASE_URL) {
throw new Error("TURSO_DATABASE_URL is not set");
}
client = createClient({
url: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
});
}
return client;
}
interface Release {
version: string;
summary: string;
score: number;
relevance: string;
analyzed_at: string;
releaseLink: string;
relevantPRs: string[];
}
export async function getAnalyzedReleases(): Promise<Release[]> {
try {
const dbClient = getClient();
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => reject(new Error("Database query timeout")), 5000);
});
const result = await Promise.race([
dbClient.execute("SELECT * FROM releases ORDER BY analyzed_at DESC"),
timeoutPromise,
]);
return result.rows.map((row) => ({
...row,
relevantPRs: JSON.parse(row.relevantPRs as string),
})) as unknown[] as Release[];
} catch (error) {
console.error("Error fetching releases:", error);
return [];
}
}
export async function insertAnalyzedRelease(
version: string,
summary: string,
score: number,
relevance: string,
releaseLink: string,
relevantPRs: string[],
): Promise<void> {
const dbClient = getClient();
await dbClient.execute({
sql: "INSERT INTO releases (version, summary, score, relevance, releaseLink, relevantPRs, analyzed_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
args: [
version,
summary,
score,
relevance,
releaseLink,
JSON.stringify(relevantPRs),
new Date().toISOString(),
],
});
}