• Adds multiple documents to the specified collection in Firestore. This function supports batched writes, and if the number of items exceeds the maximum batch size (500), it will split the items into multiple batches and write them sequentially.

    Throws

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

    Example

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

    const db = firestore();
    const users: User[] = [
    { name: "John Doe", age: 30 },
    { name: "Jane Smith", age: 25 },
    // ... more users ...
    ];

    async function run() {
    try {
    const path = 'Users'
    const results = await adds<User>(db, path, users);
    console.log(`Added ${users.length} users in ${results.length} batches.`);
    } catch (error) {
    console.error(`Error adding documents: ${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 to which the documents will be added.

    • items: T[]

      An array of document data to be added.

    Returns Promise<WriteResult[][]>

    An array of WriteResult arrays corresponding to each batch.

Generated using TypeDoc