• Adds a new document to the specified collection in Firestore. If an ID is provided, the document will be set with that ID; otherwise, an ID will be automatically generated.

    Throws

    Throws an exception if any error occurs during the document addition or setting process.

    Example

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

    const db = firestore();
    const data: User = {
    name: "John Doe",
    age: 30
    };

    async function run() {
    try {
    const path = 'Users';
    // Example without providing an ID:
    const docRef1 = await add<User>(db, path, data);
    console.log(`Document added with ID: ${docRef1.id}`);

    // Example with providing an ID:
    const customID = 'custom_user_id';
    const docRef2 = await add<User>(db, path, data, customID);
    console.log(`Document set with ID: ${docRef2.id}`);
    } catch (error) {
    console.error(`Error processing document: ${error}`);
    }
    }

    run();

    Type Parameters

    • T extends DocumentData

    Parameters

    • db: Firestore

      The instance of the Firestore database to interact with.

    • collectionPath: string

      The path of the collection where the document will be added or set.

    • params: T

      The data of the document to be added or set.

    • Optional id: string

      Optional. If provided, the document will be set with this ID. If not, an ID will be automatically generated by Firestore.

    Returns Promise<DocumentReference<T>>

    A reference to the added or set document.

Generated using TypeDoc