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 (
|
create table students (
|
||||||
userId text primary key,
|
userId text primary key,
|
||||||
firstName text,
|
firstName text,
|
||||||
@@ -6,9 +12,3 @@ create table students (
|
|||||||
promo integer,
|
promo integer,
|
||||||
foreign key(promo) references promo(id)
|
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 { Handlers } from "$fresh/server.ts";
|
||||||
import { Database } from "@db/sqlite";
|
import connect from "$root/databases/connect.ts";
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
async GET() {
|
async GET() {
|
||||||
try {
|
try {
|
||||||
const db = new Database("databases/data/mobility.db");
|
using connection = connect("students");
|
||||||
|
|
||||||
db.prepare(
|
const promotions = connection.database.prepare(
|
||||||
`
|
"select id from promotions",
|
||||||
CREATE TABLE IF NOT EXISTS promotions (
|
).all();
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
name TEXT UNIQUE NOT NULL
|
|
||||||
);
|
|
||||||
`
|
|
||||||
).run();
|
|
||||||
|
|
||||||
db.prepare(
|
const students = connection.database
|
||||||
`
|
|
||||||
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
|
|
||||||
.prepare(
|
.prepare(
|
||||||
`
|
`select userId, firstName, lastName, mail, promo from students`,
|
||||||
SELECT students.id, firstName, lastName, email, promotionId, promotions.name AS promotionName
|
|
||||||
FROM students
|
|
||||||
JOIN promotions ON students.promotionId = promotions.id
|
|
||||||
`
|
|
||||||
)
|
)
|
||||||
.all();
|
.all();
|
||||||
|
|
||||||
db.close();
|
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ promotions, students }),
|
JSON.stringify({ promotions, students }),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching data:", error);
|
console.error("Error fetching data:", error);
|
||||||
@@ -68,21 +42,21 @@ export const handler: Handlers = {
|
|||||||
throw new Error("Invalid request body");
|
throw new Error("Invalid request body");
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = new Database("databases/data/mobility.db");
|
using connection = connect("students");
|
||||||
|
|
||||||
db.prepare(
|
connection.database.prepare(
|
||||||
"INSERT OR IGNORE INTO promotions (name) VALUES (?)"
|
"INSERT OR IGNORE INTO promotions (name) VALUES (?)",
|
||||||
).run(promoName);
|
).run(promoName);
|
||||||
|
|
||||||
const promoIdRow = db
|
const promoIdRow: {id: string} = connection.database
|
||||||
.prepare("SELECT id FROM promotions WHERE name = ?")
|
.prepare("SELECT id FROM promotions WHERE name = ?")
|
||||||
.get(promoName);
|
.get(promoName)!;
|
||||||
const promoId = promoIdRow.id;
|
const promoId = promoIdRow.id;
|
||||||
|
|
||||||
console.log(`Promotion ID for "${promoName}":`, promoId);
|
console.log(`Promotion ID for "${promoName}":`, promoId);
|
||||||
|
|
||||||
const insertQuery = db.prepare(
|
const insertQuery = connection.database.prepare(
|
||||||
"INSERT INTO students (firstName, lastName, email, promotionId) VALUES (?, ?, ?, ?)"
|
"INSERT INTO students (firstName, lastName, email, promotionId) VALUES (?, ?, ?, ?)",
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const student of data) {
|
for (const student of data) {
|
||||||
@@ -90,8 +64,6 @@ export const handler: Handlers = {
|
|||||||
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoId);
|
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoId);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close();
|
|
||||||
|
|
||||||
console.log("All data inserted successfully");
|
console.log("All data inserted successfully");
|
||||||
return new Response("Data inserted successfully", { status: 201 });
|
return new Response("Data inserted successfully", { status: 201 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user