ponder-sh / ponder

A backend framework for crypto apps

Home Page:https://ponder.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom database indices

0xOlias opened this issue · comments

To improve the usefulness of direct SQL, we should make it possible to add custom database indices to indexed tables (including multi-column indices).

API

We could add a new .indexed() option to the column builder object, similar to .references():

import { createSchema } from "@ponder/core";

export default createSchema((p) => ({
  LiquidationEvent: p.createTable({
    id: p.string(),
    liquidator: p.hex().indexed(),
    account: p.hex(),
    amount: p.bigint(),
  }),
}));

Unfortunately, that API doesn't make it easy to support multi-column indices. Alternatively, we could introduce a 2nd argument to p.createTable that contains custom indices and (eventually) constraints. This is similar to drizzle's API.

import { createSchema } from "@ponder/core";

export default createSchema((p) => ({
  LiquidationEvent: p.createTable({
    id: p.string(),
    liquidator: p.hex(),
    account: p.hex(),
    amount: p.bigint()
  }, {
    liquidatorIndex: p.index("liquidator"),
    compositeIndex: p.index("liquidator", "account"),
  }),
}));

definitely vote for the 2nd option!

Shipped in #852