createTarget
Create a data sink.
createTarget(config: TargetConfig): Target
Parameters:
write: Write function async ({ctx, read}) => void (required)
fork: Fork handler async (previousBlocks: BlockCursor[]) => BlockCursor | null (optional)
Example:
const target = createTarget({
write: async ({ logger, read }) => {
for await (const { data, ctx } of read()) {
const span = ctx.profiler.start('save');
await database.save(data);
span.end();
}
},
});
You can avoid implementing rollback handlers by configuring your source to use only finalized blocks.
Context & Utilities
Logger
The logger interface is Pino-compatible. Use ctx.logger in write handlers.
Profiler
Track performance metrics for pipeline operations.
interface Profiler {
start(name: string): Profiler;
measure<T>(name: string, fn: (span: Profiler) => Promise<T>): Promise<T>;
addLabels(labels: string | string[]): Profiler;
end(): Profiler;
elapsed: number;
children: Profiler[];
}
BlockCursor
Cursor format for resuming indexing from a specific block.
interface BlockCursor {
number: number;
hash?: string;
timestamp?: number;
}
Example:
const target = createTarget({
write: async ({ read }) => {
// Resume from block 20000000
for await (const { data } of read({ number: 20000000 })) {
await processData(data);
}
}
})