Skip to content

Feature request: Add Conditional Indexing Based on Attribute Presence #300

Closed
@misterjoshua

Description

@misterjoshua

Description
I'd like ElectroDB to have a conditional indexing feature to prevent unnecessary GSI writes & hot partitions for certain data models.

Use Case
I have a data model that supports up to five levels of hierarchical depth (lv1 through lv5). However, not all entities utilize all five levels. This leads to wasted GSI write capacity and creates hot partitions for GSIs.

Playground Link

Possible Solution
I envisioned extending the index definition to accept a condition field with a function that can evaluate whether or not to populate the index fields. The idea is that if the condition function is present and it returns false, the pk & sk for that GSI are not written.

In the following example, for instance, lv5pk and lv5sk would be left unset and the byLv5 index would not be populated unless the lv5 attribute is set:

byLv5: {
  index: 'byLv5',
  // This is the new function
  condition: (attrs) => attrs.lv5 !== '',
  pk: {
    field: 'lv5pk',
    composite: ['lv5'],
  },
  sk: {
    field: 'lv5sk',
    composite: ['lv1', 'lv2', 'lv3', 'lv4'],
  }
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions