tywalch/electrodb

Ignore Duplicate Index Composite Attributes Error

Closed this issue · 1 comments

Describe the bug
It's not actually a bug. I am trying to implement the schema from the Dynamodb-adapter from Next Auth js.

https://authjs.dev/reference/adapter/dynamodb#getting-started
hGZtWDq

ElectroDB Version
2.7.1

Entity/Service Definitions
Here is my model trying to implement the scheme from the Dynamodb-adapter from Next Auth js.
The problem is that the pk and the sk are using the same composite attribute, which is not allowed in ElectroDB.

const Users = new Entity(
  {
    model: {
      entity: "user",
      service: "",
      version: "1",
    },
    attributes: {
      id: {
        type: "string",
        default: () => uuidv4(),
      },
      name: {
        type: "string",
      },
      email: {
        type: "string",
      },
      emailVerified: {
        type: "boolean",
        nullify: true,
      },
      image: {
        type: "string",
      },
      type: {
        type: "string",
      },
      provider: {
        type: "string",
      },
      providerAccountId: {
        type: "string",
      },
    },

    indexes: {
      byAccount: {
        pk: {
          field: "pk",
          composite: ["id"],
          template: "USER#${id}",
          casing: "none",
        },
        sk: {
          field: "sk",
          composite: ["id"],
          template: "USER#${id}",
          casing: "none",
        },
      },
      emailIndex: {
        index: "gsi1",
        pk: { field: "GSI1PK", composite: [] },
        sk: { field: "GSI1SK", composite: [] },
      },
    },
  },
  { table, client }
);

Expected behavior
A configuration option to bypass this error just like the example below.

// when querying the table
.go({ ignoreOwnership: true })

Errors

ElectroError: The Access Pattern 'byAccount' contains duplicate references the composite attribute(s): "id". Composite attributes may only be used once within an index. If this leaves the Sort Key (sk) without any composite attributes simply set this to be an empty array. - For more detail on this error reference: https://electrodb.dev/en/reference/errors/#duplicate-index-composite-attributes

An exception was added to this validation to allow duplicate attributes if the sort key only has one composite attribute assigned. This should address your issue here, you can find the update in 2.8.2!