TypeORM instead of direct Sqlite
This commit is contained in:
2981
package-lock.json
generated
2981
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -11,11 +11,11 @@
|
|||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/jest": "~29.5",
|
"@types/jest": "~29.5",
|
||||||
"@types/jsonwebtoken": "^9.0.2",
|
"@types/jsonwebtoken": "^9.0.2",
|
||||||
"@types/node": "~18",
|
"@types/node": "~20",
|
||||||
"@types/sqlite3": "^3.1.8",
|
"@types/sqlite3": "^3.1.8",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.3.0",
|
"@typescript-eslint/eslint-plugin": "^6.4.0",
|
||||||
"@typescript-eslint/parser": "^6.3.0",
|
"@typescript-eslint/parser": "^6.4.0",
|
||||||
"eslint": "~8.46",
|
"eslint": "~8.47",
|
||||||
"eslint-config-prettier": "~9.0",
|
"eslint-config-prettier": "~9.0",
|
||||||
"eslint-plugin-jest": "~27.2",
|
"eslint-plugin-jest": "~27.2",
|
||||||
"jest": "~29.6",
|
"jest": "~29.6",
|
||||||
@@ -46,8 +46,8 @@
|
|||||||
"http_ece": "^1.1.0",
|
"http_ece": "^1.1.0",
|
||||||
"jsonwebtoken": "^9.0.1",
|
"jsonwebtoken": "^9.0.1",
|
||||||
"node-fetch": "^3.3.2",
|
"node-fetch": "^3.3.2",
|
||||||
|
"npm-check-updates": "16.11.1",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"sqlite": "^5.0.1",
|
|
||||||
"sqlite3": "^5.1.6",
|
"sqlite3": "^5.1.6",
|
||||||
"tslib": "~2.6",
|
"tslib": "~2.6",
|
||||||
"typeorm": "^0.3.17"
|
"typeorm": "^0.3.17"
|
||||||
|
|||||||
17
src/Subscription.ts
Normal file
17
src/Subscription.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
// Subscription.ts
|
||||||
|
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class Subscription {
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
endpoint: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
keys_p256dh: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
keys_auth: string;
|
||||||
|
}
|
||||||
69
src/db.ts
69
src/db.ts
@@ -1,58 +1,29 @@
|
|||||||
import { Database } from 'sqlite3';
|
|
||||||
|
import { DataSource, Repository } from "typeorm";
|
||||||
|
import {Subscription} from './Subscription.js'
|
||||||
|
|
||||||
export class DBService {
|
export class DBService {
|
||||||
private db: Database;
|
private dataSource: DataSource;
|
||||||
|
private subscriptionRepository: Repository<Subscription>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.db = new Database('subscriptions.db', (err) => {
|
this.dataSource = new DataSource({
|
||||||
if (err) {
|
type: "sqlite",
|
||||||
console.error("Failed to connect to the database:", err);
|
database: "subscriptions",
|
||||||
return;
|
entities: [Subscription],
|
||||||
}
|
|
||||||
console.log("Connected to SQLite database");
|
|
||||||
this.createTable();
|
|
||||||
});
|
});
|
||||||
|
this.dataSource.getRepository(Subscription)
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveSubscription(endpoint: string, keys_p256dh: string, keys_auth: string) {
|
||||||
|
const subscription = new Subscription();
|
||||||
|
subscription.endpoint = endpoint;
|
||||||
|
subscription.keys_auth = keys_auth;
|
||||||
|
subscription.keys_p256dh = keys_p256dh;
|
||||||
|
return this.subscriptionRepository.save(subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
private createTable() {
|
async getSubscriptions(): Promise<Subscription[]> {
|
||||||
const createSubscriptionsTableSQL = `
|
return this.subscriptionRepository.find();
|
||||||
CREATE TABLE IF NOT EXISTS subscriptions (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
endpoint TEXT NOT NULL,
|
|
||||||
keys_p256dh TEXT NOT NULL,
|
|
||||||
keys_auth TEXT NOT NULL
|
|
||||||
);
|
|
||||||
`;
|
|
||||||
|
|
||||||
this.db.run(createSubscriptionsTableSQL, (err) => {
|
|
||||||
if (err) throw err;
|
|
||||||
console.log("Subscriptions table ready");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
saveSubscription(endpoint: string, keys_p256dh: string, keys_auth: string) {
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
|
||||||
const insertSQL = `INSERT INTO subscriptions (endpoint, keys_p256dh, keys_auth) VALUES (?, ?, ?)`;
|
|
||||||
this.db.run(insertSQL, [endpoint, keys_p256dh, keys_auth], function(err) {
|
|
||||||
if (err) {
|
|
||||||
reject(new Error(`Failed to insert subscription: ${err.message}`));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
getSubscriptions(): Promise<Array<any>> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const selectSQL = `SELECT * FROM subscriptions`;
|
|
||||||
this.db.all(selectSQL, [], (err, rows) => {
|
|
||||||
if (err) {
|
|
||||||
reject(new Error(`Failed to retrieve subscriptions: ${err.message}`));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(rows);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { Subscription, SubscriptionService } from './subscriptionService.js';
|
import { Subscription, SubscriptionService } from './subscriptionService.js';
|
||||||
import express, { Express, Request, Response } from 'express';
|
import express, { Express, Request, Response } from 'express';
|
||||||
import path from 'path';
|
|
||||||
import { Worker } from 'worker_threads';
|
import { Worker } from 'worker_threads';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
import { dirname, join } from 'path';
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
private app: Express;
|
private app: Express;
|
||||||
@@ -29,7 +33,7 @@ class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private startWorker(): void {
|
private startWorker(): void {
|
||||||
const workerPath = path.join(__dirname, 'worker.js');
|
const workerPath = join(__dirname,'./worker.js');
|
||||||
|
|
||||||
this.worker = new Worker(workerPath, { workerData: 'world' });
|
this.worker = new Worker(workerPath, { workerData: 'world' });
|
||||||
|
|
||||||
|
|||||||
0
subscriptions.db
Normal file
0
subscriptions.db
Normal file
Reference in New Issue
Block a user