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
956 stars 58 forks source link

Collection validation should check `field` property of composite attributes #394

Open raryanpur opened 3 weeks ago

raryanpur commented 3 weeks ago

Describe the bug When defining a collection, ElectroDB validates composite attributes without checking the field property. Since collections map to DynamoDB queries, and composite key attributes are mapped to DynamoDB names using the field property if it's specified, the collection validator should take this property into account.

ElectroDB Version 2.14.1

ElectroDB Playground Link N/A

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

{
    model: {
        entity: "restaurant",
        service: "app",
        version: "1"
    },
    attributes: {
        id: {
            type: "string"
            field: "restaurantId"
        },
        name: {
            type: "string"
        }
    },
    indexes: {
        record: {
            collection: "patrons"
            pk: {
                field: "pk",
                composite: ["id"],
            },
            sk: {
                field: "sk",
                composite: ["name"],
            },
        }
    }
}
{
    model: {
        entity: "customer",
        service: "app",
        version: "1"
    },
    attributes: {
        restaurantId: {
            type: "string"
        },
        name: {
            type: "string"
        }
    },
    indexes: {
        record: {
            collection: "patrons"
            pk: {
                field: "pk",
                composite: ["restaurantId"],
            },
            sk: {
                field: "sk",
                composite: ["name"],
            },
        }
    }
}

Expected behavior The collections schema validator should check the field property during schema validation and the above should succeed.

Errors

ref: {
      code: 1013,
      section: 'join',
      name: 'InvalidJoin',
      sym: Symbol(error-code)
},