Firebase frees developers to focus on crafting fantastic user experiences. You don’t need to manage servers or write APIs. Firebase acts as your server, API, and datastore - written generically as to best suit most needs
Why Firebase?
It’s a Realtime Database
Real-time data is the way of the future. Nothing compares to it. Most databases require you to make HTTP calls to get and sync your data. Most databases give you data only when you ask for it. When you connect your app to Firebase, you’re not connecting through normal HTTP. You’re connecting through a WebSocket. WebSockets are much, much faster than HTTP. Firebase sends you new data as soon as it’s updated. When your client saves a change to the data, all connected clients receive the updated data almost instantly.
It’s File Storage
Firebase Storage provides a simple way to save binary files — most often images, but it could be anything
Firebase Storage has its own system of security rules to protect your GCloud bucket from the masses while granting detailed write privileges to your authenticated clients.
It’s Authentication
Firebase auth has a built-in email/password authentication system. It also supports OAuth2 for Google, Facebook, Twitter and GitHub. Firebase Auth integrates directly into its Database, so you can use it to control access to your data
It’s Hosting
Firebase includes an easy-to-use hosting service for all of your static files and to make your development particularly painless,
Getting Started
Log into https://firebase.google.com/ with your Google account and create a new project to get started.
Once the project is added, select a real-time database using the left side menu of the console as shown in the below image. We can start the database in test mode for now and later permissions can be modified appropriately.
Once we start the project, the browser will be navigated to the database view. It has subsections to view data and permission as given in the image.
From the firebase console, you can view, add, delete or update nodes
Add Firebase to your app
To add Firebase to your app, you'll need a Firebase project and a short snippet of initialization code that has a few details about your project.
1. From the project overview page in the Firebase console, click Add Firebase to your web app. If your project already has an app, select Add App from the project overview page.
2. Required packages for Firebase database can be installed using below commands. These commands should be executed from your source location.
yarn add firebase
Note: These instructions are for using the Firebase SDK as a client for end-user access (for example, in a Node.js desktop or IoT application). If you are instead interested in having admin access from a privileged environment (like a server), you should Add the following package
yarn add firebase-admin
you can follow the instructions for setting up the Admin Node.js SDK.
3. Then, initialize the Firebase SDK using the code snippet from above, which should look like this:
// Set the configuration for your app
// TODO: Replace with your project's config object
const firebase=require(‘firebase’)
var config = {
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com",
storageBucket: "bucket.appspot.com"
};
firebase.initializeApp(config);
CRUD operations with firebase
Firebase has provided a fully usable CRUD function. CRUD itself is a function to Create, Read, Update and Delete data on an application. As a reference to perform CRUD operations First, create a database reference to your user data
// Get a reference to the database service
const database = firebase.database();
//Points to root node of database
const root=database.ref() ;
You're ready to start using the Firebase Real-time Database!
. CREATE
Set
Write or replace data to a defined path, like messages/users/<username>
//Creates data at users node of the database
database.ref('users/').set({
username:'Mel',
email:'melgo@gmail.com'
Setting the value will overwrite the data at the specified location, including any child nodes.
If no unique key is specified, the firebase database creates its own unique key from the current timestamp to store data.
Push
usersRef.push().set({
username:'Daniel',
email:'daniel@gmail.com'
})
a push() function that generates a unique key for each new child. By using unique child keys, several clients can add children to the same location at the same time without worrying about write conflicts. The unique key is based on a timestamp, so list items will automatically be ordered chronologically. Because Firebase generates a unique key for each blog post, no write conflicts will occur if multiple users add a post at the same time.
READ
Value
The value event is used to read a static snapshot of the contents at a given database path, as they existed at the time of the read event. It is triggered once with the initial data and again every time the data changes.
usersRef.on("value", function(snapshot) {
console.log(snapshot.val());
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
Reading Data Once
In some cases, it may be useful for a callback to be called once and then immediately removed. We've created a helper function to make this easy:
usersRef.once("value", function(snapshot) {
console.log(snapshot.val());
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
UPDATE
Update some of the keys for a defined path without replacing all of the data
const usersRef=database.ref('users/')
const user1Ref = usersRef.child("user1");
user1Ref.update({
"username": "dylan"
},);
DELETE
usersRef.child("user1").remove();
Delete command can be used to delete all records of a node or delete just a single child node.
You can access the complete code from our Github page (https://github.com/Robocraze/CRUD-Operations-on-Firebase)
TUTORIAL: