• Updates or inserts a document in the specified Firestore collection with the given data.

    Throws

    Throws an exception with an error message if an error occurs.

    Example

    import { firestore } from 'firebase-admin';
    import { upsert } from '@skeet-framework/firestore';

    const db = firestore();
    const upsertData: firestore.UpdateData<User> = {
    age: 31
    };

    async function run() {
    try {
    const path = 'Users';
    const docId = '123456'; // Assuming this ID exists in the Users collection.
    const success = await upsert<User>(db, path, docId, upsertData);
    if (success) {
    console.log(`Document with ID ${docId} upserted successfully.`);
    }
    } catch (error) {
    console.error(`Error upserting document: ${error}`);
    }
    }

    run();

    Type Parameters

    • T extends DocumentData

    Parameters

    • db: Firestore

      The instance of the Firestore database to use.

    • collectionPath: string

      The path of the collection containing the document to be updated or inserted.

    • docId: string

      The ID of the document to be updated or inserted.

    • params: UpdateData<WithFieldValue<T>>

      The data to update or insert the document with.

    Returns Promise<boolean>

    A boolean indicating the success of the upsert operation.

Generated using TypeDoc