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.03k stars 67 forks source link

Numeric sort key being ignored when building query params on a query operation. #308

Closed jmag651 closed 1 year ago

jmag651 commented 1 year ago

Describe the bug I am currently trying to migrate an existing project to electrodb. We have existing datasets with attributes as indexes. Whenever the sort key is numeric, the parameters that are sent to dynamo ignore the SK. If you switch the attribute type of the sort key to a string, it builds the dynamo expression correctly.

src/entity.js, the if statement on line 2725 appears to delete the Sk if the keyExpressions.ExpressionAttributeValues[":sk1"] is not of type string.

Is this intended?

ElectroDB Version Specify the version of ElectroDB you are using 2.10.1

ElectroDB Playground Link https://electrodb.fun/?#code/JYWwDg9gTgLgBAbzgUQHY2DAnnAvnAMyghDgHIBTAGwoGMZiATAIzIG4AoWiVAZ3hgBDZjTgBecmCYBXerwC0MCv3YcOFAB6RYcbn3hSIjWTF7i4qCgHcU6TFgAUHOImcu4II9QBcr9+4o7bF8yQ2M5MgAaN38ANwooXmAeEIBGKJj3XgTY4FoKEP5BKDJM3Gj-QRgGYGZpJV5fBEyXMJMAfW5GAr9-d2wwHrJ+KGBUAHNSvrwKvrb6duBGJpb+rEGQ1GkQZgSpvtwy2Zcx7o1lFemoOmhl3umwAGtL6fcCYGo70JkFrooM17ubjgCBJJS+ADa3yMHT+ZAAusdXkpwFQqkMACQIeYwTpeXAAwG6QRJCYhaRgQYlVYuco0uC8Z73V7vT4hHGLRiEwHAyBgnpQjlLBFI6YosBo8HkLFCxgE+mHaaK9zKukuJBCEQUPAcACUnC4PH4cDAxUEIDMEmarR+uL+IQAIgBZADChNlvlSACYAMwcXAGnG8AB0AEdpAksMHrtwoIwHKaoObeLrg4nkw59UA

Entity/Service Definitions

const products = new Entity(
  {
    model: {
      entity: 'products',
      version: '1',
      service: 'star'
    },
    attributes: {
      product_code: {
        type: 'string'
      },
      product_id: {
        type: 'number'
      }
    },
    indexes: {
      record: {
        pk: {
          field: 'product_code',
          composite: ['product_code'],
          template: '${product_code}',
          casing: 'upper'
        },
        sk: {
          field: 'product_id',
          composite: ['product_id'],
          template: '${product_id}'
        }
      }
    }
  },
  { client, table }
);

Expected behavior While using existing datasets with attribute as index, the expression sent to dynamo is ignoring string sort keys on an query operation. Expected it to send the sort key expression if specified.

Numeric Sort Key

await products.query.record({ product_code: 'DMC', product_id: 123 }).go();

{
    "KeyConditionExpression": "#pk = :pk",
    "TableName": "products-test",
    "ExpressionAttributeNames": {
        "#pk": "product_code"
    },
    "ExpressionAttributeValues": {
        ":pk": "DMC"
    }
}

String Sort Key

await products.query.record({ product_code: 'DMC', product_id: '123' }).go();
{
    "KeyConditionExpression": "#pk = :pk and #sk1 = :sk1",
    "TableName": "products-test",
    "ExpressionAttributeNames": {
        "#pk": "product_code",
        "#sk1": "product_id"
    },
    "ExpressionAttributeValues": {
        ":pk": "DMC",
        ":sk1": "123"
    }
}

Errors

If applicable, paste the errors you received

Additional context We are using existing tables with attributes as indexes.

tywalch commented 1 year ago

Try out version 2.10.2 for the fix, though you'll be able to see the impact in your original playground link as well 👍