Skip to content

Commit

Permalink
fix(db): move from sql templates to aggregate helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksasiriski committed Dec 26, 2024
1 parent 78da299 commit 65068ea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/lib/server/db/employee.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { or, eq, sql, and } from 'drizzle-orm';
import { or, eq, and, max } from 'drizzle-orm';
import { employee, employeeEntry, employeeExit } from './schema/employee';
import { StateInside, StateOutside, type State } from '$lib/types/state';
import { fuzzySearchFilters } from './fuzzysearch';
Expand Down Expand Up @@ -39,7 +39,7 @@ export async function getEmployees(
const maxEntrySubquery = db
.select({
employeeId: employeeEntry.employeeId,
maxEntryTimestamp: sql<Date>`MAX(${employeeEntry.timestamp})`.as('maxEntryTimestamp')
maxEntryTimestamp: max(employeeEntry.timestamp).as('max_entry_timestamp')
})
.from(employeeEntry)
.groupBy(employeeEntry.employeeId)
Expand All @@ -54,7 +54,7 @@ export async function getEmployees(
department: employee.department,
entryTimestamp: maxEntrySubquery.maxEntryTimestamp,
entryBuilding: employeeEntry.building,
exitTimestamp: sql<Date | null>`MAX(${employeeExit.timestamp})`.as('exitTimestamp')
exitTimestamp: max(employeeExit.timestamp)
})
.from(employee)
.leftJoin(maxEntrySubquery, eq(maxEntrySubquery.employeeId, employee.id))
Expand Down Expand Up @@ -88,6 +88,7 @@ export async function getEmployees(
employee.email,
employee.fname,
employee.lname,
employee.department,
maxEntrySubquery.maxEntryTimestamp,
employeeEntry.building
)
Expand Down Expand Up @@ -211,8 +212,8 @@ export async function toggleEmployeeState(
const [{ entryTimestamp, exitTimestamp }] = await tx
.select({
id: employee.id,
entryTimestamp: sql<Date>`MAX(${employeeEntry.timestamp})`.as('entryTimestamp'),
exitTimestamp: sql<Date | null>`MAX(${employeeExit.timestamp})`.as('exitTimestamp')
entryTimestamp: max(employeeEntry.timestamp),
exitTimestamp: max(employeeExit.timestamp)
})
.from(employee)
.leftJoin(employeeEntry, eq(employee.id, employeeEntry.employeeId))
Expand Down
11 changes: 6 additions & 5 deletions src/lib/server/db/student.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { or, eq, sql, and } from 'drizzle-orm';
import { or, eq, and, max } from 'drizzle-orm';
import { student, studentEntry, studentExit } from './schema/student';
import { StateInside, StateOutside, type State } from '$lib/types/state';
import { fuzzySearchFilters } from './fuzzysearch';
Expand Down Expand Up @@ -39,7 +39,7 @@ export async function getStudents(
const maxEntrySubquery = db
.select({
studentId: studentEntry.studentId,
maxEntryTimestamp: sql<Date>`MAX(${studentEntry.timestamp})`.as('maxEntryTimestamp')
maxEntryTimestamp: max(studentEntry.timestamp).as('max_entry_timestamp')
})
.from(studentEntry)
.groupBy(studentEntry.studentId)
Expand All @@ -54,7 +54,7 @@ export async function getStudents(
department: student.department,
entryTimestamp: maxEntrySubquery.maxEntryTimestamp,
entryBuilding: studentEntry.building,
exitTimestamp: sql<Date | null>`MAX(${studentExit.timestamp})`.as('exitTimestamp')
exitTimestamp: max(studentExit.timestamp)
})
.from(student)
.leftJoin(maxEntrySubquery, eq(maxEntrySubquery.studentId, student.id))
Expand Down Expand Up @@ -88,6 +88,7 @@ export async function getStudents(
student.index,
student.fname,
student.lname,
student.department,
maxEntrySubquery.maxEntryTimestamp,
studentEntry.building
)
Expand Down Expand Up @@ -211,8 +212,8 @@ export async function toggleStudentState(
const [{ entryTimestamp, exitTimestamp }] = await tx
.select({
id: student.id,
entryTimestamp: sql<Date>`MAX(${studentEntry.timestamp})`.as('entryTimestamp'),
exitTimestamp: sql<Date | null>`MAX(${studentExit.timestamp})`.as('exitTimestamp')
entryTimestamp: max(studentEntry.timestamp),
exitTimestamp: max(studentExit.timestamp)
})
.from(student)
.leftJoin(studentEntry, eq(student.id, studentEntry.studentId))
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/isInside.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function isInside(entryTimestamp: Date, exitTimestamp: Date | null): boolean {
export function isInside(entryTimestamp: Date | null, exitTimestamp: Date | null): boolean {
// Assert entryTimestamp is valid
if (entryTimestamp === null || entryTimestamp === undefined) {
throw new Error('Entry timestamp is not valid');
Expand Down

0 comments on commit 65068ea

Please sign in to comment.