Learning CRUD operations in Angular + Firebase

Let’s say we have a collection in our Firebase Database called survey and we want to perform the CRUD operations to manipulate this collection.

survey collection on Firebase Database

But, before we go there, first let’s look at the type of data the collection holds.

export interface ISurvey {
    id?: string;
    name: string;
    desc: string;
    isConfigured: boolean;
    passScore: number;
}

Above is the survey interface in Angular which we are going to use as the type or document of surveys for our collection.

Create
saveSurvey(survey: ISurvey) {
    return this.firestore.collection(COLLECTION_SURVEY).add(survey);
}

We are adding a new survey to the collection.

Read
getSurveys() {
    return this.firestore.collection(COLLECTION_SURVEY).snapshotChanges();
}

Get the documents in the collection. The function actually returns an Observable, so to get the actual payload we need to subscribe to the function and parse the data to the needed type, see below.

surveys: ISurvey[];

getSurveys().subscribe(
    (data) => {
        this.surveys = data.map((e) => {
            const s: ISurvey = e.payload.doc.data() as ISurvey;
            s.id = e.payload.doc.id;
            return s;
        });
        this.loading = false;
        this._cd.markForCheck();
    },
    (error) => {
        this.errorText = error;
        this.loading = false;
        this._cd.markForCheck();
    }
);
Update
updateSurvey(survey: ISurvey) {
    const id = survey.id;
    delete survey.id;
    return this.firestore.doc(`${COLLECTION_SURVEY}/` + id).update(survey);
 }
Delete
deleteSurvey(surveyId) {
    return this.firestore.doc(`${COLLECTION_SURVEY}/` + surveyId).delete();
}

All the above functions except getSurveys returns a promise, so you would use then() and catch() blocks to process the response. For example, say after deleting a survey we need to refresh the data. The we can do this on the then() block of the promise returned by the deleteSurvey method, see below for sample code.

this.deleteSurvey(survey.id)
    .then(() => {
      this.loading = false;
      this.getSurveys();
    })
    .catch((error) => {
      this.loading = false;
      this.errorText = error;
    }
);
Versions
  • @angular/cli@~12.2.6
  • @angular/fire@^7.1.1

**Soon I will share the complete code on Github.

Published by Kumar Gandhi K

Hi! I’m Kumar and I live in Bangalore (IN) with my family. By profession I’m a Web Developer experienced in a vast variety of frameworks and technologies, like, HTML, CSS, JavaScript, Angular, Bootstrap… Using these i have built or sometimes maintained mid market and enterprise level applications. I worked for few software companies over the years and on few domains like finance, field-service and storage. I also did consulting job at one point. I am loyal and hard (or for better word smart) working individual. In my free time I read books, in fact I buy a lot of books hoping that some day I might find all the time in the world to read them and I also enjoy watching TV.

Leave a comment