Firestore Import avatar
Firestore Import

Pricing

Pay per usage

Go to Store
Firestore Import

Firestore Import

Developed by

Apify

Maintained by Apify

Seamlessly import data from Apify datasets into Firebase Firestore database. This integration allows full control over document IDs, conflict resolution (overwrite, merge, skip), and data transformation using a custom JavaScript function.

5.0 (4)

Pricing

Pay per usage

3

Monthly users

1

Runs succeeded

67%

Last modified

22 days ago

The Firestore Import is Apify integration Actor that import data into Firebase Firestore (NoSQL cloud database build on Google Cloud infrastructure) from Apify dataset. It allows you to configure various options, such as the target collection, handling conflicts in data, and transforming the dataset item before importing it into Firestore.

Features

The Firestore Import Actor takes a dataset, applies transformations, and imports the data into a Firestore database. This Actor is highly customizable, you can control how the data are imported such as:

  • Selecting Firestore database and collection.
  • Automatically generating document IDs or using a field from the dataset for the document ID.
  • Handling document conflicts by either overwriting, merging, or skipping documents with existing ID.
  • Transforming data before it gets imported using a customizable JavaScript function.
  • One dataset item can lead to multiple Firestore inserts/updates.
  • Each document can have its own configuration, such as a custom collection or document ID.

Input

The actor requires several input fields to work correctly. Below is a detailed description of each input field:

Field NameTypeDescription
serviceAccountKeystring (secret, required)Service account key in JSON format.
You can get it from Firebase Console -> Project Settings -> Service accounts -> Generate new private key.

Paste the whole JSON string here, don't worry this is secret input which store the value in encrypted form.
datasetIdstring (required)ID of the Apify dataset to import data from.
collectionstring (required)Firestore collection to import data to. If it doesn't exist, it will be created.

Note: you can customize the collection for each record by using the transformFunction input. This can be useful when you want to import data to sub-collections.
databaseNamestring (optional)Name of the Firestore database.
If not provided, the default database ("(default)") will be used.
idFieldstring (optional)Field in the dataset item that will be used as a Firestore document ID. It must be string or number.
If not provided, all documents will be created with a random ID generated by Firestore (it means that value of documentConflictResolution is ignored in that case).

This is useful when you want to update existing documents in Firestore.

Note: you can customize the ID for each document independently using the transformFunction input field.
documentConflictResolutionenum: overwrite, merge, skip (required)How to handle conflicts when importing data to Firestore:
- overwrite: replace existing Firestore documents with the same ID.
- merge: merge data from the dataset items with existing Firestore documents.
- skip: documents with existing IDs will be skipped.

⚠️ Please note that the skip resolution has really bad performance on large scale and can't use batch writes (it makes request to Firestore for each document separately).
transformFunctionstring (javascript, optional)Javascript function that transforms each item from the dataset before importing it to Firestore.

The function must return an object (or array of objects) with the data key that contains the transformed record and other optional fields. See examples below.
batchSizenumber (optional)Number of items to import in a single batch. Lower values are safer but slower, see Firestore limits (10 MiB batch write). Please note that skip conflict resolution does not use batch writes and will always import one item at a time.
Defaults to 500.

Transformation Function

The option transformFunction input field allows you to transform each dataset item before importing it to Firestore. The field accepts a JavaScript function that takes one dataset item as a parameter and returns an object (or array of objects) with the following keys:

  • data (required): transformed document that will be imported to Firestore.
  • id (optional): custom document ID. If not provided, the idField input field will be used to resolve document id or if not provided the document will be created with a random ID generated by Firestore.
  • collection (optional): custom collection name. If not provided, the collection input field will be used.
  • documentConflictResolution (optional): custom conflict resolution for the document. If not provided, the documentConflictResolution input field will be used.
1(item) => {
2    return {
3        data: item,                           // transformed document
4        id: item.id,                          // custom document ID
5        collection: "customCollection",       // custom collection name
6        documentConflictResolution: "merge",  // custom conflict resolution
7    };
8}

Examples

  1. Simple transformation function:

    The function below increments the value of the oldField by 1 and removes the unused field from the dataset item.

    1(item) => {
    2    item.newField = item.oldField + 1;
    3    delete item.unused;
    4    return { data: item };
    5}
  2. Nested objects:

    The function below transforms the dataset item into a Firestore document with nested objects. It updates the subdocument.field field and overwrites the whole author sub-document.

    1(item) => {
    2    return {
    3        data: {
    4            title: item.title,
    5            "subdocument.field": item.name,  // update single field of subdocument
    6            author: item.author              // overwrite whole subdocument
    7        },
    8    };
    9}
  3. Field value functions:

    The function below demonstrates how to use Firestore FieldValue functions. It adds new IDs to the existing ids array, removes values from the values array, increments the count field, and deletes the old field.

    1(item) => {
    2    return {
    3        data: {
    4            ids: FieldValue.arrayUnion(item.ids),         // add new ids to existing ids array
    5            values: FieldValue.arrayRemove(item.values),  // remove new values from existing array
    6            count: FieldValue.increment(item.count),      // increment existing count field by provided value
    7            old: FieldValue.delete(),                     // removes field
    8        },
    9    };
    10}
  4. Data types:

    The function below demonstrates how to create Firestore data types such as Timestamp, Vector, GeoPoint, and DocumentReference.

    1(item) => {
    2    return {
    3        data: {
    4            updatedAt: Timestamp.fromDate(Date.parse(item.date)),          // create Timestamp data type
    5            vector: FieldValue.VectorValue(item.values),                   // create vector data type
    6            position: GeoPoint(item.lat, item.lon),                        // create geopoint data type
    7            reference: DocumentReference("collection", "referenceDocId"),  // create reference type
    8        },
    9    };
    10}
  5. Subcollection:

    The function below demonstrates how to import data to sub-collections. It returns an array where the first item is the main document and other items are documents for sub-collection.

    1(item) => {
    2    const subDocuments = item.items.map((subItem) => ({
    3        id: subItem.id,
    4        collection: `records/${item.customId}/items`,
    5        documentConflictResolution: "skip",
    6        data: {
    7            weight: subItem.weight,
    8            length: subItem.length,
    9            name: subItem.name,
    10        },
    11    }));
    12
    13    return [
    14        {
    15            id: item.customId,
    16            collection: "records",
    17            documentConflictResolution: "merge",
    18            data: {
    19                title: item.title,
    20                description: item.description,
    21            },
    22        },
    23        ...subDocuments,
    24    ];
    25}

Output

The Actor outputs statistics about the import to Key-Value store key Statistics with the following structure:

  • imported: total number of processed Firestore documents (either created, updated or skipped).
  • skipped: number of skipped Firestore documents.
  • overwritten: number of overwritten Firestore documents.
  • merged: number of merged Firestore documents.
  • created: number of created Firestore documents (counts written document if documentConflictResolution is skip).
  • failed: number of failed writes to Firestore documents.
  • itemsProcessed: total number of processed dataset items (including failed items).
  • itemsFailed: number of failed dataset items.
  • executionTimeMs: time in milliseconds it took to import the data.
  • startTime: timestamp when the import started.
  • endTime: timestamp when the import ended.
1{
2  "imported": 59278,
3  "skipped": 0,
4  "overwritten": 0,
5  "merged": 59278,
6  "created": 0,
7  "failed": 0,
8  "itemsProcessed": 1136,
9  "itemsFailed": 0,
10  "executionTimeMs": 19725,
11  "startTime": "2025-02-26T17:56:22.652Z",
12  "endTime": "2025-02-26T17:56:42.377Z"
13}

Pricing

Pricing model

Pay per usage

This Actor is paid per platform usage. The Actor is free to use, and you only pay for the Apify platform usage.