Ad

How To Update Whole Collection? Firebase Database Migration

I've made some updates in Article model and it's affects to all elements in the collection so I need to update whole collection.

I don't know how should make it in the right way. In ASP.NET it calls "Database Migration". When you can remove old fields and add new to all items in one click.

Here is my model.

Old model:

// article.ts
export class Article {
    id?: string;
    title: string;
    text: string;
    views: number;
    archived: boolean;
    published: boolean;
}

Updated model:

// article.ts
export class Article {
    id?: string;
    title: string;
    text: string;
    views: number;
    status: Status;
}

export enum Status {
    Archived = 'archived',
    Draft = 'draft',
    Published = 'published'
}

How should I update all items inside collections?

Ad

Answer

Neither database available with Firebase (Realtime Database and Cloud Firestore) have the ability to bulk update fields or perform migrations. You have to read each of the items, then write back the fields each item should contain.

Ad
source: stackoverflow.com
Ad