Basic Firebase CRUD Operations in Angular

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.
Date: 2020-02-12
Building a CRUD Application with Angular and Firebase: A Comprehensive Guide
This article details the process of creating a simple yet complete CRUD (Create, Read, Update, Delete) application using Angular as the front-end framework and Firebase as the back-end database. We'll walk through each step, explaining the underlying concepts and the purpose of each component. No prior knowledge of either Angular or Firebase is assumed; however, familiarity with basic web development principles will be beneficial.
The foundation of our application lies in the interaction between Angular, a powerful JavaScript framework for building dynamic web applications, and Firebase, a comprehensive backend-as-a-service platform that provides real-time database capabilities, authentication, and more. Angular handles the user interface and interactions, while Firebase manages the data storage and retrieval. This combination allows for rapid development and deployment of web applications.
The first step involves setting up the development environment. This begins with installing necessary tools. We'll use Visual Studio Code, a popular code editor, and Node.js, a JavaScript runtime environment, which is essential for working with npm (Node Package Manager). The npm is used to install Angular and the necessary Firebase packages. We then use the Angular CLI (Command Line Interface) to create a new Angular project using the command ng new angular-firebase-crud-example. This generates the basic structure of our Angular application.
Next, we integrate Firebase into our Angular project. This requires obtaining Firebase configuration information from the Firebase console. After creating a Firebase project and setting up a Firestore database (choosing the test mode for initial development), you obtain a unique configuration object containing API keys and other credentials. This configuration is added to an environment file, typically src/environments/environment.ts, making this information accessible to our application without hardcoding it directly into the code. This is crucial for security best practices.
With Firebase integrated, the next phase focuses on designing the data model. For simplicity, we use a Student model to represent the data. This model defines the structure of each student record in our database, specifying attributes like name, ID, and other relevant information. In Angular, this model would be represented by a class; the command ng generate model shared/student would automatically create the necessary files.
We then create a service to handle interactions with the Firebase database. This service acts as an intermediary, abstracting away the complexities of database operations. The command ng generate service shared/student creates this service, usually residing in a shared folder. This service includes functions for creating new student records, retrieving all student records, updating existing records, and deleting records. These functions use Firebase's API to communicate with the Firestore database.
The user interface (UI) is built using Angular components. We create several components to handle different aspects of the CRUD operations. The students component acts as a container, encompassing other components responsible for specific functionalities. A student component manages the creation and update of student records, featuring a form for user input. This component utilizes Angular's form handling capabilities, requiring the inclusion of FormsModule in the app.module.ts file. A separate student-list component displays a list of existing student records and handles the deletion functionality. Each of these components (student, student-list, students) is created using the Angular CLI with commands like ng generate component students/student.
The student.component.html (and similarly, student-list.component.html) files define the HTML structure of the components, rendering the forms and lists. The corresponding .ts (TypeScript) files contain the logic behind the component's functionality, including handling user input, communicating with the student service, and triggering actions in the Firebase database.
The implementation details of these components involve handling events, validating user input, and interacting with the database through the service layer. For example, the student component's TypeScript file will contain functions that, upon form submission, call the relevant functions in the student service to add or update student records in Firebase. Similarly, the student-list component uses the service to fetch student data and displays it. The delete operation, also performed through the service, removes records from the database.
Finally, the application needs to be configured to load data on startup and display the relevant components in the application's main view. The app.component.html file is modified to include the students component, making it the main entry point for the CRUD operations.
Once all these components are created and connected, compiling and running the application using ng serve starts a local development server, allowing testing of the CRUD operations in a web browser. This process involves building the application, deploying it to the local server, and opening a browser to the specified URL (typically http://localhost:4200).
This detailed explanation covers the core concepts of building a simple CRUD application using Angular and Firebase. The process highlights the separation of concerns – data access via a service, user interface via components, and data modeling – crucial for creating maintainable and scalable applications. The use of Angular's CLI streamlines the development process, and Firebase provides a straightforward solution for database management. By following these steps, you can create your own basic CRUD application, which forms a foundation for more complex projects.