Apibara blog

News and updates about Apibara

Entity mode for the PostgreSQL sink

This week, we introduce entity mode to the PostgreSQL sink. This feature enables indexers to insert and update stateful entities.

For example, let's consider an onchain game with the following events:

  • GameStarted(game_id): emitted when a game is started.
  • GameEnded(game_id, score): emitted when the game finishes.

The following indexer inserts a new "STARTED" game when the first event is emitted, and updates the game status when GameEnded is emitted.

export default function transform({ header, events }) {
    const { timestamp } = header;
    return events.flatMap(({ event }) => {
        if (isGameStarted(event)) {
            const { game_id } = decodeEvent(event);
            return {
                insert: {
                    game_id,
                    status: "STARTED",
                    created_at: timestamp,
                },
            };
        } else if (isGameEnded(event)) {
            const { game_id, score } = decodeEvent(event);
            return {
                entity: {
                    game_id,
                },
                update: {
                    score,
                    status: "ENDED",
                    updated_at: timestamp,
                },
            };
        } else {
            return [];
        }
    });
}

This first release should be considered a preview and we're still collecting feedback on entity mode. You can open an issue on GitHub to tell us what you think about it.

Multiple MongoDB indexers per collection

It's now possible to have multiple indexers write data to the same MongoDB collection! This enables multiple indexers to run in parallel, increasing indexing speed.

You can specify additional conditions that will be used when invalidating data with the invalidate option. Use this to restrict which documents are affected by an indexer.

export const config = {
  // ...
  sinkType: "mongo",
  sinkOptions: {
    database: "example",
    collectionName: "transfers",
    // Make sure to also add these properties to the documents
    // returned by the transform function.
    invalidate: {
      network: "starknet-goerli",
      symbol: "ETH",
    },
  },
};

We are now adding support for one indexer inserting data into multiple MongoDB collections.

Reduced memory usage across the board

We switched the allocator used by all the Apibara binaries to jemalloc. From our experience in production, we have seen DNA instances dropping from 20Gb of memory used down to 500Mb 🤯!

More flexible environment variables

Previously, users could specify which environment variables were available to the indexer's script by using the --allow-env flag and pointing it to a .env file.

We updated all sinks to allow indexers to inherit and access the current environment. Users can use the --allow-env-from-env flag to specify which variables the indexer has access to. This feature is especially useful to users running indexers in production, since they can use their platform (e.g. Docker or Kubernetes) to set environment variables.

Apibara

Apibara is the fastest platform to build production-grade indexers that connect onchain data to web2 services.

© 2024 GNC Labs Limited. All rights reserved.