Browse Source

TypeORM instead of direct Sqlite

pull/1/head
Matthew Raymer 1 year ago
parent
commit
6a30c69649
  1. 3025
      package-lock.json
  2. 10
      package.json
  3. 17
      src/Subscription.ts
  4. 65
      src/db.ts
  5. 8
      src/main.ts
  6. 0
      subscriptions.db

3025
package-lock.json

File diff suppressed because it is too large

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

@ -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;
}

65
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();
});
}
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");
}); });
this.dataSource.getRepository(Subscription)
} }
saveSubscription(endpoint: string, keys_p256dh: string, keys_auth: string) { async saveSubscription(endpoint: string, keys_p256dh: string, keys_auth: string) {
return new Promise<void>((resolve, reject) => { const subscription = new Subscription();
const insertSQL = `INSERT INTO subscriptions (endpoint, keys_p256dh, keys_auth) VALUES (?, ?, ?)`; subscription.endpoint = endpoint;
this.db.run(insertSQL, [endpoint, keys_p256dh, keys_auth], function(err) { subscription.keys_auth = keys_auth;
if (err) { subscription.keys_p256dh = keys_p256dh;
reject(new Error(`Failed to insert subscription: ${err.message}`)); return this.subscriptionRepository.save(subscription);
return;
}
resolve();
});
});
} }
getSubscriptions(): Promise<Array<any>> { async getSubscriptions(): Promise<Subscription[]> {
return new Promise((resolve, reject) => { return this.subscriptionRepository.find();
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);
});
});
} }
} }

8
src/main.ts

@ -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

Loading…
Cancel
Save