Hi, In this blog post we will learn the basic operations of firebase real-time databases. We will do data inserting, fetching, updating and deleting. All the queries for that operations are given below for better understanding. Moreover, the complete source code of the application with all discussed operations is given below that will definitely help you.
Create DB App in Firebase
Open console of the firebase. If you already have a project, you can use that for the database. But if you don't have one, click "Add project". Give your project a unique name and click continue. Then disable google analytics and click "Create project". After some time, the project will be created. Click continue to open the project. Now, we will add a database app to the firebase project.
Click "Build" on the left menu and then click Realtime Database from the dropdown. Now click the "Create Database" button, select a database location and click next. Here, you have database security rules, we will change them later. Click "Enable" to create the database. Now, we have our database URL which we use in our web project for communication between the web and the database. Now click "Rules" tab and replace both false with true. It means we can now read/write in the database. We have done everything from firebase and now come to the coding part.
Firebase CDN for Databases
Add the firebase CDN to the project. These are the compact URLs that can use on websites. First CDN for integration with firebase working for all firebase apps. The other one is for database operations.
We have added firebase to the web application using CDN. Now, initialize firebase real-time databases using the URL generated before. This will connect the database to our web app. Provide database URL as the value of "databaseURL" property of the object and initialize it with initializeApp() function. Use database() constructor of the firebase object for further operations. So, store it in a variable to make it simple.
Let's try to send some data. As firebase database is a NoSQL database, data is saved in the JSON form and we can only send an object. First, we need to define a reference, where data will store. Here, we are using "messages" as the reference, and now all will store under that reference. We need to save data with unique IDs, so the data updating and deleting are done comfortably using unique IDs. Push() function will generate a server-side unique key for the new row, we enter. It works as same as the primary key in SQL. Now, add properties with their values in an object and pass them to set() function. Here we are using three properties, so it creates in the database as given below.
Use the same reference "messages", to fetch the same data that was sent before. On() function will return an object that has our fetched data. Use built-in val() function to extract data. Now, data will return with each row as given below.
Now, here we will separate each row to show in a table or to use anywhere. Here, "for in" statement loop is used to get the key of each row. Using that key in loop, we are getting values of each row that store in "value" variable. Now, use any property where ever you want to use it.
We need to pass an object with new data to update data in firebase databases. As given below, we update all three properties, but we can also update a single property. Updating single property will update the targeted data and other properties will remain same. Here, we are using the same reference with an extra unique id of row, where we want changes. Provide the object to the update() function which will update data.
If we provide a "Null" value to any property, it will be deleted if available.
To delete a row, provide a complete reference as in our case provide "messages" along with the row's unique id and use the remove function as given below. The row will be deleted.
To delete a single key, provide the reference as used before for deleting a row. Provide the row's unique id along with the key name as given below. A key will be deleted.
Firebase is one of the best cloud computing services offered by Google. It provides NoSQL and real-time hosting of databases, social authentication, real-time communication server, etc. Learning the real-time databases of firebase will definitely be fruitful for you. If you have any query, then comment below and get the source code of the full web application, discussed here.
Reference from firebase documentation