refactor(mobility): switch to Drizzle ORM and remove raw SQLite usage
- replace Database with db instance - use schema imports for tables - use db.select, db.insert, onConflictDoUpdate - remove manual connection handling and console logs - improve type safety and maintainability refactor(students): migrate to Drizzle ORM and async queries Replace raw sqlite queries with Drizzle ORM. Remove the connect helper and use the shared db instance and schema definitions. Convert getItself, getAll and addStudents to async functions, use eq and lt helpers, and simplify promotion handling. This improves type safety, maintainability, and allows non‑blocking database access.
This commit is contained in:
@@ -1,55 +1,36 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { Database } from "@db/sqlite";
|
||||
import { db } from "$root/databases/db.ts";
|
||||
import { mobility, promotions, students } from "$root/databases/schema.ts";
|
||||
import { eq } from "npm:drizzle-orm";
|
||||
|
||||
export const handler: Handlers = {
|
||||
// deno-lint-ignore require-await
|
||||
async GET() {
|
||||
try {
|
||||
console.log("Connecting to mobility database...");
|
||||
const connection = new Database("databases/data/mobility.db", {
|
||||
create: false,
|
||||
});
|
||||
connection.run(
|
||||
"ATTACH DATABASE 'databases/data/students.db' AS students",
|
||||
);
|
||||
console.log("Connected to databases.");
|
||||
const studentRows = await db
|
||||
.select({
|
||||
id: students.userId,
|
||||
firstName: students.firstName,
|
||||
lastName: students.lastName,
|
||||
promotionId: students.promotionId,
|
||||
endyear: promotions.endyear,
|
||||
current: promotions.current,
|
||||
})
|
||||
.from(students)
|
||||
.leftJoin(promotions, eq(students.promotionId, promotions.id));
|
||||
|
||||
const students = connection.prepare(
|
||||
`SELECT
|
||||
students.userId AS id,
|
||||
students.firstName,
|
||||
students.lastName,
|
||||
students.promotionId AS promotionId,
|
||||
promotions.name AS promotionName
|
||||
FROM students.students
|
||||
LEFT JOIN students.promotions ON students.promotionId = promotions.id`,
|
||||
).all();
|
||||
const mobilityRows = await db.select().from(mobility);
|
||||
|
||||
const mobilities = connection.prepare(
|
||||
`SELECT
|
||||
mobility.id,
|
||||
mobility.studentId,
|
||||
mobility.startDate,
|
||||
mobility.endDate,
|
||||
mobility.weeksCount,
|
||||
mobility.destinationCountry,
|
||||
mobility.destinationName,
|
||||
mobility.mobilityStatus
|
||||
FROM mobility`,
|
||||
).all();
|
||||
|
||||
const promotions = connection.prepare(
|
||||
`SELECT id, name FROM students.promotions`,
|
||||
).all();
|
||||
|
||||
connection.close();
|
||||
const promotionRows = await db
|
||||
.select({ id: promotions.id, endyear: promotions.endyear, current: promotions.current })
|
||||
.from(promotions);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ mobilities, students, promotions }),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
JSON.stringify({
|
||||
mobilities: mobilityRows,
|
||||
students: studentRows,
|
||||
promotions: promotionRows,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error fetching mobility data:", error);
|
||||
@@ -58,8 +39,6 @@ export const handler: Handlers = {
|
||||
},
|
||||
|
||||
async POST(request) {
|
||||
console.log("API /mobility/api/insert_mobility POST called");
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { data } = body;
|
||||
@@ -67,32 +46,8 @@ export const handler: Handlers = {
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error("Invalid request body");
|
||||
}
|
||||
console.log("Connecting to mobility database...");
|
||||
const connection = new Database("databases/data/mobility.db", {
|
||||
create: false,
|
||||
});
|
||||
|
||||
console.log("Attaching students database...");
|
||||
connection.run(
|
||||
"ATTACH DATABASE 'databases/data/students.db' AS students",
|
||||
);
|
||||
console.log("Students database attached successfully.");
|
||||
|
||||
const insertQuery = connection.prepare(
|
||||
`INSERT INTO mobility (
|
||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
startDate = excluded.startDate,
|
||||
endDate = excluded.endDate,
|
||||
weeksCount = excluded.weeksCount,
|
||||
destinationCountry = excluded.destinationCountry,
|
||||
destinationName = excluded.destinationName,
|
||||
mobilityStatus = excluded.mobilityStatus`,
|
||||
);
|
||||
|
||||
for (const mobility of data) {
|
||||
for (const entry of data) {
|
||||
const {
|
||||
id,
|
||||
studentId,
|
||||
@@ -102,19 +57,16 @@ export const handler: Handlers = {
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus = "N/A",
|
||||
} = mobility;
|
||||
} = entry;
|
||||
|
||||
console.log("Processing mobility data:", mobility);
|
||||
const studentExists = await db
|
||||
.select({ userId: students.userId })
|
||||
.from(students)
|
||||
.where(eq(students.userId, studentId))
|
||||
.limit(1)
|
||||
.then((rows) => rows.length > 0);
|
||||
|
||||
const studentExists = connection
|
||||
.prepare(
|
||||
`SELECT COUNT(*) AS count FROM students.students WHERE userId = ?`,
|
||||
)
|
||||
.get(studentId);
|
||||
|
||||
console.log(`Student ${studentId} exists:`, studentExists.count > 0);
|
||||
|
||||
if (studentExists.count === 0) {
|
||||
if (!studentExists) {
|
||||
console.warn(`Skipping mobility for unknown studentId: ${studentId}`);
|
||||
continue;
|
||||
}
|
||||
@@ -123,43 +75,39 @@ export const handler: Handlers = {
|
||||
if (startDate && endDate) {
|
||||
const start = new Date(startDate);
|
||||
const end = new Date(endDate);
|
||||
if (start <= end) {
|
||||
calculatedWeeksCount = Math.ceil(
|
||||
calculatedWeeksCount = start <= end
|
||||
? Math.ceil(
|
||||
(end.getTime() - start.getTime()) / (7 * 24 * 60 * 60 * 1000),
|
||||
);
|
||||
} else {
|
||||
calculatedWeeksCount = null;
|
||||
}
|
||||
)
|
||||
: null;
|
||||
}
|
||||
|
||||
console.log("Executing SQL insert/update query for:", {
|
||||
id,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
calculatedWeeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus,
|
||||
});
|
||||
|
||||
insertQuery.run(
|
||||
id,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
calculatedWeeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus,
|
||||
);
|
||||
await db
|
||||
.insert(mobility)
|
||||
.values({
|
||||
id,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
weeksCount: calculatedWeeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: mobility.id,
|
||||
set: {
|
||||
startDate,
|
||||
endDate,
|
||||
weeksCount: calculatedWeeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
connection.close();
|
||||
console.log("Mobility data inserted/updated successfully.");
|
||||
return new Response("Data inserted/updated successfully", {
|
||||
status: 200,
|
||||
});
|
||||
return new Response("Data inserted/updated successfully", { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("Error inserting mobility data:", error);
|
||||
return new Response("Failed to insert/update data", { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user