Matthew Raymer
1 year ago
6 changed files with 112 additions and 48 deletions
@ -1,51 +1,71 @@ |
|||
|
|||
import { DataSource, Repository } from "typeorm"; |
|||
import {Subscription} from './Subscription.js' |
|||
import { Subscription } from './Subscription.js' |
|||
import { VapidKeys } from './VapidKeys.js'; |
|||
|
|||
export class DBService { |
|||
class DBService { |
|||
private static instance: DBService; |
|||
private dataSource: DataSource; |
|||
private vapidSource: DataSource; |
|||
private subscriptionRepository: Repository<Subscription>; |
|||
private vapidRepository: Repository<VapidKeys>; |
|||
public isReady = false; |
|||
|
|||
constructor() { |
|||
|
|||
private constructor() { |
|||
console.log("DBService constructor"); |
|||
this.dataSource = new DataSource({ |
|||
type: "sqlite", |
|||
database: "subscriptions", |
|||
entities: [Subscription], |
|||
database: "push_server", |
|||
entities: [VapidKeys, Subscription] |
|||
}); |
|||
this.subscriptionRepository = this.dataSource.getRepository(Subscription) |
|||
this.dataSource.initialize().then(()=>{ |
|||
console.log("Initialized"); |
|||
this.subscriptionRepository = this.dataSource.getRepository(Subscription); |
|||
this.vapidRepository = this.dataSource.getRepository(VapidKeys); |
|||
this.isReady = true; |
|||
}).catch((err)=>{console.error(err);}); |
|||
} |
|||
|
|||
this.vapidSource = new DataSource({ |
|||
type: "sqlite", |
|||
database: "vapidKeys", |
|||
entities: [VapidKeys], |
|||
}); |
|||
|
|||
this.vapidRepository = this.vapidSource.getRepository(VapidKeys); |
|||
|
|||
public static getInstance(): DBService { |
|||
if (!DBService.instance) { |
|||
DBService.instance = new DBService(); |
|||
} |
|||
return DBService.instance; |
|||
} |
|||
|
|||
|
|||
|
|||
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); |
|||
return await this.dataSource.manager.save(subscription); |
|||
} |
|||
|
|||
|
|||
async getSubscriptions(): Promise<Subscription[]> { |
|||
return this.subscriptionRepository.find(); |
|||
return await this.subscriptionRepository.find(); |
|||
} |
|||
|
|||
|
|||
async getVapidKeys(): Promise<VapidKeys[]> { |
|||
return this.vapidRepository.find(); |
|||
let result = [ new VapidKeys() ]; |
|||
if ( this.vapidRepository && this.isReady) { |
|||
result = await this.vapidRepository.find(); |
|||
} else { |
|||
console.log("vapidRepository is null or db is not ready"); |
|||
} |
|||
return result |
|||
} |
|||
|
|||
|
|||
async saveVapidKeys(publicKey: string, privateKey: string) { |
|||
const vapidkeys = new VapidKeys(); |
|||
vapidkeys.privateKey = privateKey; |
|||
vapidkeys.publicKey = publicKey; |
|||
return this.vapidRepository.save(vapidkeys); |
|||
return await this.dataSource.manager.save(vapidkeys); |
|||
} |
|||
} |
|||
|
|||
export default DBService; |
Loading…
Reference in new issue