Skip to content

Conversation

aasoni
Copy link
Contributor

@aasoni aasoni commented Oct 21, 2025

Description of Changes

Update typescript code generation to generate accessors on non unique indexes. These will use the filter function as is the convention in C# and module api.

Related to #1387 but doesn't close it.

API and ABI breaking changes

Expected complexity level and risk

2

Testing

Generated the following table:

#[spacetimedb::table(
    name = test_index,
    index(name = multi_column_test, btree(columns = [identity, col_1, col_2, col_3, col_4])), 
    public,
)]
#[derive(Clone)]
pub struct TestIndex {
    #[primary_key]
    #[auto_inc]
    pub id: u64,

    pub identity: Identity,
    pub col_1: u64,
    pub col_2: i32,
    pub col_3: f32,
    pub col_4: String,
}

Into typescript which resulted in the following code for index lookups:

  /**
   * Access to the `id` index on the table `test_index`,
   * with `ctx.db.testIndex.id.find(...)`.
   */
  id = {
    // Find the subscribed row matching the lookup value if present in the client cache.
    find: (col_vals: bigint): TestIndex | undefined => {
      for (let row of this.tableCache.iter()) {
        if (__deepEqual(row.id, col_vals)) {
          return row;
        }
      }
    },
  };
  /**
   * Access to the `multiColumnTest` index on the table `test_index`,
   * with `ctx.db.testIndex.multiColumnTest.filter(...)`.
   */
  multiColumnTest = {
    // Find the subscribed row matching the lookup value if present in the client cache.
    filter: (col_vals: [__Identity,bigint,number,number,string]): TestIndex[] => {
      const result: TestIndex[] = [];
      for (let row of this.tableCache.iter()) {
        if (__deepEqual([row.identity,row.col1,row.col2,row.col3,row.col4], col_vals)) {
          result.push(row);
        }
      }
      return result;
    },
  };

I then manually inserted values into the test_index table and checked that calling the new filter and find functions returned the expected values.

@aasoni aasoni requested review from cloutiertyler and jsdt and removed request for cloutiertyler October 22, 2025 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant