-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb-utils.ts
More file actions
45 lines (37 loc) · 1.46 KB
/
db-utils.ts
File metadata and controls
45 lines (37 loc) · 1.46 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
import { db } from './db';
import { projects, teamMembers, contactMessages, type NewContactMessage, type Project, type TeamMember } from './schema';
import { eq } from 'drizzle-orm';
// Projects utilities
export async function getAllProjects(): Promise<Project[]> {
return await db.select().from(projects);
}
export async function getFeaturedProjects(): Promise<Project[]> {
return await db.select().from(projects).where(eq(projects.featured, true));
}
export async function getProjectById(id: number): Promise<Project | undefined> {
const result = await db.select().from(projects).where(eq(projects.id, id));
return result[0];
}
// Team utilities
export async function getAllTeamMembers(): Promise<TeamMember[]> {
return await db.select().from(teamMembers);
}
export async function getTeamMemberById(id: number): Promise<TeamMember | undefined> {
const result = await db.select().from(teamMembers).where(eq(teamMembers.id, id));
return result[0];
}
// Contact messages utilities
export async function createContactMessage(message: NewContactMessage) {
const result = await db.insert(contactMessages).values(message).returning();
return result[0];
}
export async function getAllContactMessages() {
return await db.select().from(contactMessages);
}
// Helper function to handle database errors
export function handleDatabaseError(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
return 'An unknown database error occurred';
}