|
|
@ -1,29 +1,30 @@ |
|
|
|
|
|
|
|
import { DataSource, Repository } from "typeorm"; |
|
|
|
import { DataSource } from "typeorm"; |
|
|
|
import { Subscription } from './Subscription.js' |
|
|
|
import { VapidKeys } from './VapidKeys.js'; |
|
|
|
import { fileURLToPath } from 'url'; |
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url); |
|
|
|
|
|
|
|
class DBService { |
|
|
|
private static instance: DBService; |
|
|
|
private dataSource: DataSource; |
|
|
|
private subscriptionRepository: Repository<Subscription>; |
|
|
|
private vapidRepository: Repository<VapidKeys>; |
|
|
|
public isReady = false; |
|
|
|
|
|
|
|
|
|
|
|
private constructor() { |
|
|
|
console.log("DBService constructor"); |
|
|
|
this.dataSource = new DataSource({ |
|
|
|
type: "sqlite", |
|
|
|
database: "push_server", |
|
|
|
entities: [VapidKeys, Subscription] |
|
|
|
entities: [VapidKeys, Subscription], |
|
|
|
synchronize: true |
|
|
|
}); |
|
|
|
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);}); |
|
|
|
}).catch((err)=>{ |
|
|
|
console.error(err); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -45,27 +46,45 @@ class DBService { |
|
|
|
|
|
|
|
|
|
|
|
async getSubscriptions(): Promise<Subscription[]> { |
|
|
|
return await this.subscriptionRepository.find(); |
|
|
|
let result = [ new Subscription ]; |
|
|
|
if (this.isReady) { |
|
|
|
result = await this.dataSource.manager.find(Subscription); |
|
|
|
|
|
|
|
} else { |
|
|
|
console.log(__filename, "Database not ready.") |
|
|
|
|
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getVapidKeys(): Promise<VapidKeys[]> { |
|
|
|
console.log(__filename, "getVapidKeys", this.isReady); |
|
|
|
let result = [ new VapidKeys() ]; |
|
|
|
if ( this.vapidRepository && this.isReady) { |
|
|
|
result = await this.vapidRepository.find(); |
|
|
|
if ( this.isReady ) { |
|
|
|
result = await this.dataSource.manager.find(VapidKeys); |
|
|
|
console.log(__filename, "results of find: ", result); |
|
|
|
} else { |
|
|
|
console.log("vapidRepository is null or db is not ready"); |
|
|
|
console.log(__filename, "Database is not ready"); |
|
|
|
|
|
|
|
} |
|
|
|
return result |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async saveVapidKeys(publicKey: string, privateKey: string) { |
|
|
|
const vapidkeys = new VapidKeys(); |
|
|
|
vapidkeys.privateKey = privateKey; |
|
|
|
vapidkeys.publicKey = publicKey; |
|
|
|
return await this.dataSource.manager.save(vapidkeys); |
|
|
|
async saveVapidKeys(publicKey: string, privateKey: string): Promise<VapidKeys> { |
|
|
|
let result = new VapidKeys(); |
|
|
|
result.privateKey = privateKey; |
|
|
|
result.publicKey = publicKey; |
|
|
|
if ( this.isReady ) { |
|
|
|
result = await this.dataSource.manager.save(result); |
|
|
|
|
|
|
|
} else { |
|
|
|
console.log(__filename, "Database is not ready."); |
|
|
|
|
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export default DBService; |
|
|
|
export default DBService; |
|
|
|