Skip to main content
Version: v8.0.0

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<{ response: 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<{ response: Tea, payload: TeaSchema }>()({
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<{ response: Tea, payload: TeaSchema }>()({
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<{ response: Tea, payload: TeaSchema }>()({
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<{ response: Tea }>()({
endpoint: ":teaId",
method: "remove",
})
.setParams({ teaId: 1 });

In case of remove - returned data equals null