Created
March 28, 2021 12:32
-
-
Save HonbraDev/8f5d16b06a9acbb0a1adb7239a09a7dc to your computer and use it in GitHub Desktop.
Realtime DB in DodgyCoin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User } from "../typings/Database"; | |
import * as admin from "firebase-admin"; | |
const serviceAccount = require("../../firebaseadmin.json"); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: | |
"https://dodgycoin-default-rtdb.europe-west1.firebasedatabase.app", | |
}); | |
const db = admin.database(); | |
const getUser = (id: string) => | |
new Promise<User>((resolve) => | |
db.ref(`users/${id}`).once("value", (snap) => { | |
if (snap.exists()) { | |
resolve(snap.val() as User); | |
} else { | |
resolve({ monies: 0 }); | |
} | |
}) | |
); | |
const setMonies = (id: string, monies: number) => | |
new Promise((resolve, reject) => | |
db.ref(`users/${id}/monies`).set(monies).then(resolve) | |
); | |
export { getUser, setMonies }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment