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 { |
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); |
|
||||
}); |
|
||||
}); |
|
||||
} |
} |
||||
} |
} |
||||
|
Loading…
Reference in new issue