TypeORM instead of direct Sqlite

This commit is contained in:
Matthew Raymer
2023-08-15 21:05:46 +08:00
parent a6de7aaf18
commit 6a30c69649
6 changed files with 2535 additions and 550 deletions

17
src/Subscription.ts Normal file
View 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;
}

View File

@@ -1,58 +1,29 @@
import { Database } from 'sqlite3';
import { DataSource, Repository } from "typeorm";
import {Subscription} from './Subscription.js'
export class DBService {
private db: Database;
private dataSource: DataSource;
private subscriptionRepository: Repository<Subscription>;
constructor() {
this.db = new Database('subscriptions.db', (err) => {
if (err) {
console.error("Failed to connect to the database:", err);
return;
}
console.log("Connected to SQLite database");
this.createTable();
this.dataSource = new DataSource({
type: "sqlite",
database: "subscriptions",
entities: [Subscription],
});
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() {
const createSubscriptionsTableSQL = `
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);
});
});
async getSubscriptions(): Promise<Subscription[]> {
return this.subscriptionRepository.find();
}
}

View File

@@ -1,7 +1,11 @@
import { Subscription, SubscriptionService } from './subscriptionService.js';
import express, { Express, Request, Response } from 'express';
import path from 'path';
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 {
private app: Express;
@@ -29,7 +33,7 @@ class Server {
}
private startWorker(): void {
const workerPath = path.join(__dirname, 'worker.js');
const workerPath = join(__dirname,'./worker.js');
this.worker = new Worker(workerPath, { workerData: 'world' });