Skip to content

Instantly share code, notes, and snippets.

@HonbraDev
Last active March 31, 2021 15:52
Show Gist options
  • Save HonbraDev/692a24e7a4251ce3ec3b6a2aa1791537 to your computer and use it in GitHub Desktop.
Save HonbraDev/692a24e7a4251ce3ec3b6a2aa1791537 to your computer and use it in GitHub Desktop.
Underwater
import db from "../libs/database";
import { DBUserProfile } from "./types";
const getUser = (id: string): Promise<DBUserProfile> =>
new Promise((res, rej) =>
db
.ref("users")
.orderByChild("id")
.equalTo(id)
.once("value", (snap) => {
const value = snap.val();
if (value) {
res(value[Object.keys(value)[0]]);
} else rej();
})
);
export default getUser;
export type PopularRooms = {
rooms: {
voiceServerId: string;
peoplePreviewList: {
numFollowers: number;
id: string;
displayName: string;
}[];
numPeopleInside: number;
name: string;
isPrivate: boolean;
inserted_at: string;
id: string;
description: string;
creatorId: string;
}[];
nextCursor: null;
initial: boolean;
};
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