npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

solidity-events-to-typeorm

v0.0.9

Published

Generate TypeORM entities from Solidity event abi

Readme

solidity-events-to-typeorm

A tool that generates TypeORM entities from Ethereum Solidity event ABIs. This library helps you build blockchain indexers by automatically creating the database entities needed to store event data.

Features

  • Automatically generate TypeORM entities from Solidity contract ABIs
  • Support for complex data structures including nested objects and arrays
  • TypeORM migrations generation for easy database setup
  • UML diagram generation for visualizing entity relationships
  • Handles all Solidity data types with appropriate TypeORM column types

Installation

npm install solidity-events-to-typeorm

Usage

  1. Create a configuration file for your project:
import { Config } from 'solidity-events-to-typeorm';
import path from 'path';
import MyContractArtifact from './path/to/MyContract.json';

const outputPath = path.resolve(__dirname, './output/');

export const config: Config = {
  output: {
    path: outputPath,
    entities: path.resolve(outputPath, 'entities/'),
    abis: path.resolve(outputPath, 'abis/'),
  },
  contracts: {
    MyContract: {
      abi: MyContractArtifact.abi,
    },
  },
};
  1. Run the generation script:
import { generate } from 'solidity-events-to-typeorm';
import { config } from './config';

generate(config).catch((err) => {
  console.error('Fatal error during generation:', err);
  process.exit(1);
});

Configuration Options

The configuration object supports the following options:

interface Config {
  // Required: Contract ABIs to process
  contracts: {
    [contractName: string]: {
      abi: ABI; // Solidity ABI
      filterEvents?: (abi: ABI) => ABI; // Optional function to filter out unwanted events
    };
  };
  
  // Required: Output paths
  output: {
    path: string; // Base output directory
    entities: string; // Path for generated entities
    abis: string; // Path for processed ABIs
  };
  
  // Optional: Documentation generation
  docs?: {
    path: string; // Path for generated documentation
    plantUmlServer?: string; // Optional PlantUML server URL
  };
  
  // Optional: Migration generation
  migrations?: {
    path: string; // Path for generated migrations
    migrationName: string; // Name of the migration class
    schemaName: string; // Database schema name or env variable name
    schemaVariable?: boolean; // When true, schemaName is treated as an env variable name
  };
  
  // Optional: Enable logging during generation
  enableLogging?: boolean;
}

Generated Output

The tool generates the following files:

Entities

For each event in the Solidity ABI, the tool generates:

  1. A main event entity class that extends BlockchainEventEntity
  2. Related entity classes for nested structures or arrays
  3. An index.ts file that exports all generated entities

All entities include TypeORM decorators and appropriate column types for Solidity data types.

Base Entity

All generated event entities inherit from the BlockchainEventEntity base class, which includes common blockchain event fields:

  • uniqueEventId: A unique identifier for the event
  • eventOriginAddress: The contract address that emitted the event
  • blockHash, blockNumber, blockTimestamp: Information about the block
  • transactionHash, txIndex, logIndex: Transaction details
  • topics, logData: Raw event data

Migrations

If the migrations option is configured, the tool generates TypeORM migration files that:

  1. Create tables for all entities
  2. Define foreign key relationships

Migrations Configuration

The migrations configuration supports dynamic schema names through environment variables:

migrations?: {
  path: string;           // Path for generated migrations
  migrationName: string;  // Name of the migration class
  schemaName: string;     // Database schema name or env variable name
  schemaVariable?: boolean; // When true, schemaName is treated as an env variable name
};

Using Environment Variables for Schema Names

You can use environment variables for schema names by setting schemaVariable: true in your configuration:

migrations: {
  path: path.resolve(outputPath, 'migrations/'),
  migrationName: 'MyMigrations',
  schemaName: 'SQL_SCHEMA', // Name of the environment variable
  schemaVariable: true,     // Treat schemaName as an env variable
}

When schemaVariable is set to true:

  • The migration generator treats schemaName as the name of an environment variable
  • Generated migrations will use ${process.env.SQL_SCHEMA} instead of a hardcoded schema name
  • This allows you to deploy the same migrations to different environments with different schema names

If schemaVariable is omitted or set to false (default), schemaName is used as a literal schema name in the generated migrations.

Documentation

If the docs option is configured, the tool generates UML diagrams to visualize the entity relationships. It is recommended you run your own plant uml server if running this for a lot of entities as i've noticed the official server url doesnt return responses if you do a lot of requests at once (which the package will do). You can do that through the docker-compose file.

Examples

Simple Example (Counter)

import CounterArtifact from './counter.json';
import { generate, Config } from 'solidity-events-to-typeorm';
import path from 'path';

const outputPath = path.resolve(__dirname, './output/');

export const config: Config = {
  output: {
    path: outputPath,
    entities: path.resolve(outputPath, 'entities/'),
    abis: path.resolve(outputPath, 'abis/'),
  },
  contracts: {
    Counter: {
      abi: CounterArtifact.abi,
    },
  },
};

generate(config);

Advanced Example (Complex Structures)

The tool can handle complex Solidity event structures including:

  • Dynamic arrays (uint64[])
  • Fixed-size arrays (uint64[3])
  • Nested structs
  • Arrays of structs
  • Structs containing arrays

For examples of handling complex structures, see the test suite example in the examples/test-suite directory which uses the contract from solidity-event-test-suite to illustrate complex nesting and array types.

License

MIT