tywalch / electrodb

A DynamoDB library to ease the use of modeling complex hierarchical relationships and implementing a Single Table Design while keeping your query code readable.
MIT License
1.02k stars 64 forks source link

Reverse index (sk,pk) in addition to (pk,sk) leads to error message #416

Open mattes3 opened 3 months ago

mattes3 commented 3 months ago

Describe the bug I have a reverse GSI on my table, but I fail to make it usable with ElectroDB. Basically, my normal keys are called (pk, sk), but I have a reverse index using (sk, pk). ElectroDB fails with an error message (see below).

ElectroDB Version 2.14.2

ElectroDB Playground Link This one

Entity/Service Definitions Include your entity model (or a model that sufficiently recreates your issue) to help troubleshoot.

   attributes: {
      followerID: {
        type: "string",
        required: true
      },
      followingID: {
        type: "string",
        required: true
      },
    },
    indexes: {
      followers: {
        pk: {
          field: "pk",
          composite: ["followingID"]
        },
        sk: {
          field: "sk",
          composite: ["followerID"]
        }
      },
      followings: {
        index: 'GSIreverse',
        pk: {
          field: "sk",
          composite: ["followerID"]
        },
        sk: {
          field: "pk",
          composite: ["followingID"]
        }
      },
    }

Expected behavior The Entity constructor should run without error.

Errors

This is the error that I get (see the Playground example):

The Sort Key (sk) on Access Pattern 'followings' references the field 'sk' which is already referenced by the Access Pattern(s) 'followers' as a Partition Key. Fields mapped to Partition Keys cannot be also mapped to Sort Keys.

For more detail on this error reference https://electrodb.dev/en/reference/errors/#inconsistent-index-definition

Additional context

I can't believe the error message because having a reverse index is one of the usual tricks in DynamoDB applications. See https://aws.amazon.com/tutorials/design-a-database-for-a-mobile-app-with-dynamodb/module-4/ for an example.

What am I missing here?

rdzidziguri commented 1 month ago

Unfortunately, we had to stop using the library for this very same reason and the very opinionated approach it takes.