Matthew Raymer
1 year ago
6 changed files with 2557 additions and 572 deletions
File diff suppressed because it is too large
@ -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; |
|||
} |
@ -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(); |
|||
}); |
|||
} |
|||
|
|||
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 = new DataSource({ |
|||
type: "sqlite", |
|||
database: "subscriptions", |
|||
entities: [Subscription], |
|||
}); |
|||
this.dataSource.getRepository(Subscription) |
|||
} |
|||
|
|||
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(); |
|||
}); |
|||
}); |
|||
|
|||
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); |
|||
} |
|||
|
|||
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(); |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue