Added layer of abstraction for database connection
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { Database } from "@db/sqlite";
|
||||
|
||||
interface DatabaseConnection extends Disposable {
|
||||
database: Database;
|
||||
}
|
||||
|
||||
export default function connect(database: string): DatabaseConnection {
|
||||
const connection = new Database(`databases/data/${database}.db`, {
|
||||
create: false,
|
||||
});
|
||||
|
||||
connection.run("attach database 'databases/data/students.db' as students");
|
||||
|
||||
return {
|
||||
database: connection,
|
||||
[Symbol.dispose]: () => {
|
||||
connection.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,3 +1,9 @@
|
||||
create table promotions (
|
||||
id integer primary key autoincrement,
|
||||
endyear integer,
|
||||
current integer
|
||||
);
|
||||
|
||||
create table students (
|
||||
userId text primary key,
|
||||
firstName text,
|
||||
@@ -5,10 +11,4 @@ create table students (
|
||||
mail text,
|
||||
promo integer,
|
||||
foreign key(promo) references promo(id)
|
||||
);
|
||||
|
||||
create table promo (
|
||||
id integer,
|
||||
endyear integer,
|
||||
current integer
|
||||
);
|
||||
@@ -1,53 +1,27 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { Database } from "@db/sqlite";
|
||||
import connect from "$root/databases/connect.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET() {
|
||||
try {
|
||||
const db = new Database("databases/data/mobility.db");
|
||||
using connection = connect("students");
|
||||
|
||||
db.prepare(
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS promotions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL
|
||||
);
|
||||
`
|
||||
).run();
|
||||
const promotions = connection.database.prepare(
|
||||
"select id from promotions",
|
||||
).all();
|
||||
|
||||
db.prepare(
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS students (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
firstName TEXT NOT NULL,
|
||||
lastName TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
promotionId INTEGER NOT NULL,
|
||||
FOREIGN KEY (promotionId) REFERENCES promotions (id)
|
||||
);
|
||||
`
|
||||
).run();
|
||||
|
||||
const promotions = db.prepare("SELECT id, name FROM promotions").all();
|
||||
|
||||
const students = db
|
||||
const students = connection.database
|
||||
.prepare(
|
||||
`
|
||||
SELECT students.id, firstName, lastName, email, promotionId, promotions.name AS promotionName
|
||||
FROM students
|
||||
JOIN promotions ON students.promotionId = promotions.id
|
||||
`
|
||||
`select userId, firstName, lastName, mail, promo from students`,
|
||||
)
|
||||
.all();
|
||||
|
||||
db.close();
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ promotions, students }),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
@@ -68,21 +42,21 @@ export const handler: Handlers = {
|
||||
throw new Error("Invalid request body");
|
||||
}
|
||||
|
||||
const db = new Database("databases/data/mobility.db");
|
||||
using connection = connect("students");
|
||||
|
||||
db.prepare(
|
||||
"INSERT OR IGNORE INTO promotions (name) VALUES (?)"
|
||||
connection.database.prepare(
|
||||
"INSERT OR IGNORE INTO promotions (name) VALUES (?)",
|
||||
).run(promoName);
|
||||
|
||||
const promoIdRow = db
|
||||
const promoIdRow: {id: string} = connection.database
|
||||
.prepare("SELECT id FROM promotions WHERE name = ?")
|
||||
.get(promoName);
|
||||
.get(promoName)!;
|
||||
const promoId = promoIdRow.id;
|
||||
|
||||
console.log(`Promotion ID for "${promoName}":`, promoId);
|
||||
|
||||
const insertQuery = db.prepare(
|
||||
"INSERT INTO students (firstName, lastName, email, promotionId) VALUES (?, ?, ?, ?)"
|
||||
const insertQuery = connection.database.prepare(
|
||||
"INSERT INTO students (firstName, lastName, email, promotionId) VALUES (?, ?, ?, ?)",
|
||||
);
|
||||
|
||||
for (const student of data) {
|
||||
@@ -90,8 +64,6 @@ export const handler: Handlers = {
|
||||
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoId);
|
||||
}
|
||||
|
||||
db.close();
|
||||
|
||||
console.log("All data inserted successfully");
|
||||
return new Response("Data inserted successfully", { status: 201 });
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user