This week we released a new package to consume Apibara streams from Node.js. Developers can now write Apibara indexers using Typescript or Javascript.
The @apibara/protocol
package contains a client to connect to any Apibara stream. Once connected, the client receives historical blocks until it reaches the chain's tip. When streaming live data, the client is informed of chain reorganizations to invalidate the application's data and keep the off-chain state consistent with the on-chain state.
We provide Typescript definitions for StarkNet data in the @apibara/starknet
package. Use these type definitions to parse Apibara messages into Typescript objects.
import { NodeClient, credentials } from '@apibara/protocol'import { StreamMessagesResponse__Output } from '@apibara/protocol/dist/proto/apibara/node/v1alpha1/StreamMessagesResponse'import { Block } from '@apibara/starknet'async function main() {const node = new NodeClient('goerli.starknet.stream.apibara.com:443', credentials.createSsl())const messages = node.streamMessages({})return new Promise((resolve, reject) => {messages.on('end', resolve)messages.on('error', reject)messages.on('data', (data: StreamMessagesResponse__Output) => {const value = data.data?.data?.valueif (value) {const block = Block.decode(value)console.log(`${block.blockNumber} ${block.transactions.length}`)}})})}main().then(() => process.exit(0)).catch(console.error)
You can find more information and examples in our documentation or on GitHub.
Prisma ORM integration
The next step is integrating the Typescript SDK with Prisma ORM. Prisma is the best Typescript ORM: it generates a type-safe database client from your application model's definitions. Prisma supports multiple relational databases like PostgreSQL, MySQL, and SQLite, and NoSQL databases like MongoDB. The integration enables chain-aware storage for all Prisma models, enabling developers to easily roll back their application state after a chain reorganization.