Skip to content

Composite index query response types should mark composite key attributes as required #559

@anatolzak

Description

@anatolzak

Describe the bug

When querying a composite index (type: "composite"), attributes used as pk/sk composites that are defined as optional in the entity schema still appear as optional in the query response type. This is incorrect — DynamoDB composite indexes are sparse, meaning an item only appears in the index if all its key attributes are present. Every item returned from the index is guaranteed to have those attributes.

Current behavior

// email is optional in the schema
attributes: {
  email: { type: "string" }, // no required: true
}
// composite index with pk: { composite: ["email"] }

// query response type:
{ email?: string } // optional — incorrect

ElectroDB Version
Specify the version of ElectroDB you are using
3.7.1

ElectroDB Playground Link
https://electrodb.fun/?#code/PQKgBAsg9gJgpgGzARwK5wE4Es4GcA0YuccYGeqCALgUQBYCG5YA7llXWAGbZwB2MXGBDAAUKKwBbAA5QMVMAG8wAUT5V2AT0IBlTADcsAY1IBfbhiiSwAIkRwjVSzABGNgNzijUPrgVUGFwRSAF5bTShUDAB9AKC4aL4GSTgPcVAwABUGXABrITUNKk1hMW9fBX4ikrC+OBZVdS0AClEwJTb2sElYRAAuDq6uqq0BmwC83Bt8TqH9TFwsHzGARmnZruIMQxMxidyGaWkbDdMZoYYqJywXVCo8AcUN9qwYR7Bi6TgBgHI-bD4AHMfmAzs8wHREAgoO9Pt8wH9rkCQWChu0WHIEG8lB9NF9fv8sMjQadzl0ifAAB4PQZo6TYSRMTSPcHtaS5FlotFcHBYsbs9ZcobeGRQRb3AYAbRsrxsAF1WaCyUNUWiRbJxXAAJICOCUzlCil6saAxYrdkAWlNWBWeQtRspgqFcN+6rF7DgP2VdI5tKFYDdmqlNkhCGh8sVqpV3tMnVVyjiwRJAEpPKJyn4cTBLgxQWAwgwWAx2GARsUAHRoTCacuBj06qnNZSh6G-DEYLEo5PlwFQZrJ0TAYD+gB6ONeA0JQPcEKhUAA-JOkYCZ+2sYuiMvQZKFUPS5Svo44DBx9ipyvZ2GYZuARe1zAN+ftwqgA

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

{
  model: {
    entity: "tasks",
    version: "1",
    service: "taskapp"
  },
  attributes: {
    id: { type: 'string' },
    hello: { type: 'string' },
    world: { type: 'string' }
  },
  indexes: {
    primary: {
      pk: {
        field: "pk",
        composite: ["id"]
      },
    },
    compositeIndex: {
      index: "gsi1pk-gsi1sk-index",
      type: 'composite',
      pk: {
        composite: ["hello"]
      },
    },
  }
}

Expected behavior

// query response type:
{ email: string } // required — every item in the index has this attribute

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