Added layer of abstraction for database connection

This commit is contained in:
Kevin FEDYNA
2025-01-21 23:49:52 +01:00
parent b5fedbb425
commit 5e75c688c8
3 changed files with 41 additions and 49 deletions
+20
View File
@@ -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();
},
};
}
+6 -6
View File
@@ -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
);