Skip to content

Instantly share code, notes, and snippets.

@dmurawsky
Last active October 9, 2021 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmurawsky/7f1548a21bcea062888d716ff30099f6 to your computer and use it in GitHub Desktop.
Save dmurawsky/7f1548a21bcea062888d716ff30099f6 to your computer and use it in GitHub Desktop.
Example of Firebase v9 database functions vs Firebase v8
import firebase from "firebase/app";
import "firebase/database";
export const onceValue = <T>(path: string): Promise<Nullable<T>> =>
firebase
.database()
.ref(path)
.once("value")
.then((snap) => snap.val());
export const firebaseUpdate = (path: string, updateObj: Object) => firebase.database().ref(path).update(updateObj);
export const firebasePush = (path: string, object?: Object) => firebase.database().ref(path).push(object).key;
export const firebaseSet = (path: string, object: Object | string | number | boolean | null) =>
firebase.database().ref(path).set(object);
export const firebaseDelete = (path: string) => firebase.database().ref(path).remove();
import {
getDatabase,
ref,
get,
update,
set,
push,
remove,
} from "firebase/database";
const getRef = (path: string) => ref(getDatabase(), path);
export const onceValue = <T>(path: string): Promise<T | null> => get(getRef(path)).then((snap) => snap.val());
export const firebaseUpdate = (path: string, updateObj: Object) => update(getRef(path), updateObj);
export const firebasePush = (path: string, object?: Object) => push(getRef(path), object).key;
export const firebaseSet = (path: string, object: Object | string | number | boolean | null) =>
set(getRef(path), object);
export const firebaseDelete = (path: string) => remove(getRef(path));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment