banner



How To Add And Get Key Record To Firebase Realtime Database

Method to use auto-increment keys in Firebase Realtime database

Firebase is a well-known BaaS (Backend as a Service). Some may describe firebase equally a PaaS (Platform as a Service) likewise. This platform is developed past Google for creating mobile and spider web applications.

This provides developers with a variety of tools and services to assistance them develop quality apps easily.

There are then many services offered by firebase. Firebase Realtime Database is a cloud-hosted NoSQL database that enables information to be stored and synced between users in real-time. The information is synced across all clients in real-time and is even so available when an app goes offline.

In my stance, a major drawback in this existent-time DB is not having auto-increment keys. Nosotros tin can see Autoincrement keys in Firebase Firestore Database, just not in Firebase Realtime Database. Indeed, this NoSQL DB is mainly designed for unstructured and semi-structured data. But we demand to shop information with unique keys like a primary key in RDBMS there's no proper style to do it.

In this article, I'm going to depict how I overcome this issue.

This database provides a Balance API to perform our Grime operations as needed. Information technology'due south very easy to consume and organize.

Example:-

proper name: {

"outset": "Jack",

"last": "Sparrow"

}

If we have structured data as above nosotros tin can recollect our data using below API endpoint.

'https://[PROJECT_ID].firebaseio.com/users/jack/name.json'

So the output will exist equally below,

{ "outset": "Jack", "terminal": "Sparrow" }

We can perform Get,PUT,Mail,PATCH,DELETE methods.

Using this link you can view the official documentation — https://firebase.google.com/docs/reference/residue/database

My goal is to create something like this.

Implementation is straightforward.

Step 1 — First we have to create this customer index in our DB to store the terminal id. You lot tin can define this fundamental when you are creating your DB.

Footstep 2 — Retrieving last id using GET request to — https://[PROJECT_ID].firebaseio.com/customerindex.json

Step 3 — Now nosotros have our last ID so we can increment this value in our app.

Pace iv — This code is a instance of creating a PUT asking using axios in react.

*new_customer_id is formatted string with incremented id

*userValues is a JSON Cord.

axios.put(`https://[PROJECT_ID].firebaseio.com/customers/${new_customer_id}.json, userValues)

.then((response) => {

if (response.status === 200) {

console.log("OK"); }

})

.catch((fault) => {

console.log(error) });

Step 5 — And don't forget to update our last client id in DB.

axios.put(`https://[PROJECT_ID].firebaseio.com/customerindex/customerlastid.json`, lastID)

.then((response) => {

if (response.status === 200) {

console.log("OK");

}

})

.grab((error) => {

console.log(mistake);

});

This method is non very efficient since it's using 2 API requests just to Become and UPDATE the Key.

Hope this will help you lot to get a quick agreement of the Firebase Realtime Database. You can detect more details virtually this DB in the firebase official documentation.

How To Add And Get Key Record To Firebase Realtime Database,

Source: https://yamikaperera.medium.com/method-to-use-auto-increment-keys-in-firebase-realtime-database-131ab5508dc0

Posted by: mumfordcoser1975.blogspot.com

0 Response to "How To Add And Get Key Record To Firebase Realtime Database"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel