dynamodb-toolbox / dynamodb-toolbox

Lightweight and type-safe query builder for DynamoDB and TypeScript
https://www.dynamodbtoolbox.com/
MIT License
1.82k stars 172 forks source link

Making the SortKey a PartitionKey for a GSI results in error #199

Open lucas-subli opened 3 years ago

lucas-subli commented 3 years ago

I have a table with a PK called 'pk' and SK called 'sk' And a GSI where the PK is 'sk' (The same as the default SK) and the SK is 'gsisk'

Table configuration:

 // Define partition and sort keys
  partitionKey: 'pk',
  sortKey: 'sk',
  indexes: {
    SecondaryIndex: { partitionKey: 'sk', sortKey: 'gsisk' },
  },

Entity configuration:

  // Define attributes
  attributes: {
    cognitoSub: { partitionKey: true }, // flag as partitionKey
    id: {
      sortKey: true,
      partitionKey: 'SecondaryIndex',
      prefix: 'payment#',
    },
    provider: { sortKey: 'SecondaryIndex' },
    type: { type: 'string', required: true },
    status: { type: 'string', required: true },
    amount: { type: 'number', required: true },
    currency: { type: 'string', required: true },
    externalId: { type: 'string' },
    providerData: { type: 'map' },
    providerCallback: { type: 'map' },
    subTransactions: { type: 'map' },
  },

with this setup whenever I try to add an entity I get the following error:

ERROR TypeError: config.sortKey.includes is not a function at Table.addEntity (/var/task/node_modules/dynamodb-toolbox/dist/classes/Table.js:172:83)

If I remove the Secondary indexes declarations from the entity the error is gone and I can add as normal. So the following configuration for the entity WORKS:

// Define attributes
  attributes: {
    cognitoSub: { partitionKey: true }, // flag as partitionKey
    id: { sortKey: true, prefix: 'payment#' },
    provider: { map: 'gsisk', type: 'string', required: true },
    type: { type: 'string', required: true },
    status: { type: 'string', required: true },
    amount: { type: 'number', required: true },
    currency: { type: 'string', required: true },
    externalId: { type: 'string' },
    providerData: { type: 'map' },
    providerCallback: { type: 'map' },
    subTransactions: { type: 'map' },
  },

Expected behavior: The entity should be added as normal with the correct secondary index configuration (the global sk as the GSI pk).

dermicus-miclip commented 6 months ago

Seems like this is still an issue in the latest version 0.9.2. I can get it to work by just not setting the property partitionKey and omitting required. But typing wise it is not entirely correct of course.