Skip to main content
Version: 5.x.x

Working with Realtime database

Available methods

Our Realtime Database adapter provides all the methods the original offers. Due to its nature, we've solved the realtime listening a bit differently, and thus - this method is not allowed in standard adapter. For the onValue-like usage, please check how to listen to queries.

get

import {firebaseAdapter} from "@hyper-fetch/firebase";

const client = new Client({ url: "teas/" }).setAdapter(() => firebaseAdapter(realtimeDbWeb));
const req = client.createRequest<Tea[]>()({
endpoint: "",
method: "get",
});

const { data, status, extra, success, error } = await req.send();

extra:

  1. ref - reference to the endpoint
  2. snapshot - 'raw' snapshot from the result

set

const setReq = client
.createRequest<Tea, Tea>()({
endpoint: ":teaId",
method: "set",
})
.setParams({ teaId: 1 })
.setData(newData);

const { data, status, extra, success, error } = await req.send();

In case of set - data returned is the same data as passed for setting.

extra:

  1. ref - database reference to the path

You can also remove data via set by sending the {data: null} object:

const setReq = client.createRequest<Tea, {data: null}>()({
endpoint: ":teaId",
method: "set",
})
.setParams({ teaId: 1 })
.setData({data: null});

push

const pushReq = client
.createRequest<Tea, Tea>()({
endpoint: "",
method: "push",
options: {},
})
.setData(newData);

In case of push - data returned is the same data as passed for setting + the __key param that equals id of a newly created document.

extra:

  1. ref - database reference to the path
  2. key - key of the newly created resource

update

const updateReq = client
.createRequest<Tea, Tea>()({
endpoint: ":teaId",
method: "update",
})
.setData(newData);

In case of update - data returned is the same data as passed for setting.

extra:

  1. ref - database reference to the path

remove

const removeReq = client
.createRequest<Tea>()({
endpoint: ":teaId",
method: "remove",
})
.setParams({ teaId: 1 });

In case of remove - returned data equals null