5 Minutes: Firebase Rest Service
An idea comes to your mind and you have just five minutes. Get ready for speed up ⏫
Your idea works web, mobile or internet of things devices but always need backend service and consume, use database. When you used firebase rest service solutions, you’ll have a cloud backends app.
Let’s write a service.
Realtime Database Rest
Realtime database package maybe one of the oldest solutions firebase. This package has socket architecture and NoSQL design.
You’ll use the firebase database first time, follow these steps:
- Open this firebase console
- Create a new project and give a temporary name for now.
- Go to the database under Develop.
- Create realtime database then done.
Package use normally usage an official SDK. You have one more way to use on your database.
Are you ready for the magic? When added “.json” extension, you’ll get user model.
Don’t forget; this article created for test mode. (Choose rule tab on firebase console).
{
“rules”: {
“.read”: “auth == null”,
“.write”: “auth == null”
}
}
POST — User
This subject added data more important on the database. We need the most data, should use the data everywhere.
Lets’ add new data to the database. Open any API test platform. (postman- postwoman-cocoarestclient etc.). You get service endpoint on firebase console.
You can add your service path to the endpoint. You’ll add user data in the request then select the HTTP request type Post. Finally everything okay. Your data is now in the database.
Result:
If your request succeeds, firebase return key your data. You can use this key in order to get your data. (This key is the child-value architecture parent.)
Open the monitoring board firebase; you’ll see your data.
It’s great 🤗 It’s time to see your data.
GET — User
Let’s remember; what return the post request result?
- Yes, I hear you 🤗. Firebase service returns the unique key to the data.
Time to use this key. Whether by chrome or by postman or curl, add the path on-base URL end and select your request type GET. You’ll see the user data. So this key like them a relational database unique key.
Result:
GET — User List
We get and post scenario completed until now. Turn to get your user list. You can just call parent-child on the base URL end and actually request GET, you’ll see all user data.
Result:
We have a user list for now. We get ready the use on the client.
There is an important point. You are used to servicing return list-objects like JSON placeholder albums, but firebase doesn’t return list object. Firebase returns map List object, so you first map converts to list, and you should assign key value to new column value(like id).
Example:
if (responseBody is List)
{
return responseBody.map((e) => ChatModel.fromJson(e)).toList();
}
PUT — USER
Your software needs an update to any data. This time usually put the data instead of the old. You need old data key (unique id) and put the data this key.
Let’s update the name property. We send request the base URL end to user child with a key. Now, will add new data on the request body. Almost ready, send to PUT a request on URL.
Yo ye it’s done 🔥
PUT — User List
If you don't want to use map list, you could add on the data list but don't forget that every put request replaces the old data
We will prepare albums JSON list.
This list put the database. Firebase base URL add to end your service path.
Like a baseurl/routepath.json
Look at firebase dashboard and you will see on list data.
There is an important point. We want to add data; I use the post method. We insert data for the put method, retrieve the list object, and we directly usually use service. We don’t need any new map process.
DELETE — USER
Finally, we came to the end. I think it’s not used very often, more use post method. We want to delete any data or all child, will use DELETE.
We just add base URL end to the child(path) or key.
Result:
We talk authentication, query, filter and pros&cons next article.
Almost done 🏁
You could use own firebase URL because I’ll change the rule. You cannot put, post, delete. You can only see my data. If you want more details firebase rest database, look at this page.
See you in the new beauties 🥰