2021-04-10 09:23:54 +00:00
|
|
|
import { Singleton, Inject } from "@extollo/di"
|
|
|
|
import { Unit, Logging, Config } from "@extollo/lib"
|
2021-04-10 02:26:57 +00:00
|
|
|
import * as firebase from "firebase-admin"
|
|
|
|
|
2021-04-10 07:01:57 +00:00
|
|
|
export type RTDBRef = 'peers' | 'transaction' | 'block'
|
2021-04-10 05:47:05 +00:00
|
|
|
|
2021-04-10 02:26:57 +00:00
|
|
|
/**
|
|
|
|
* FirebaseUnit Unit
|
|
|
|
* ---------------------------------------
|
|
|
|
* Fetch credentials from config and setup the firebase-admin connection.
|
|
|
|
*/
|
|
|
|
@Singleton()
|
|
|
|
export class FirebaseUnit extends Unit {
|
2021-04-10 09:23:54 +00:00
|
|
|
protected _firebase = firebase
|
2021-04-10 02:26:57 +00:00
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly config!: Config
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return this._firebase
|
|
|
|
}
|
|
|
|
|
2021-04-10 05:47:05 +00:00
|
|
|
ref(name: RTDBRef): firebase.database.Reference {
|
|
|
|
return this._firebase.database().ref(
|
|
|
|
String(this.config.get(`app.firebase.rtdb.refs.${name}`))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-10 02:26:57 +00:00
|
|
|
/** Called on app start. */
|
|
|
|
public async up() {
|
|
|
|
this.logging.info('Initializing Firebase application credentials...')
|
|
|
|
this._firebase.initializeApp({
|
|
|
|
credential: firebase.credential.cert(this.config.get('app.firebase.credentials')),
|
2021-04-10 05:47:05 +00:00
|
|
|
databaseURL: this.config.get('app.firebase.rtdb.default'),
|
2021-04-10 02:26:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called on app shutdown. */
|
|
|
|
public async down() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|