sensedeep/dynamodb-onetable

[Usage Question] Possible to use an array value to populate a sort/hash key

KenFigueiredo opened this issue · 2 comments

Hi there,
Dug through the documentation a bit and tried to get this to work but not sure if this would work or not.
I'm attempting to model this hierarchy structure:

Group A
 - Group B
   - Group C 
     - Group D
       - UserMembership Z

Example PK / SKs for these two models

{
  PK: GRP#<GroupA Id>
  SK: GRP#<GroupB Id>#GRP#<GroupC Id>#GRP#<GroupD Id>
}

{
  PK: GRP#<GroupA Id>
  SK: GRP#<GroupB Id>#GRP#<GroupC Id>#GRP#<GroupD Id>#USRM#<MemberShip Z ID>
}

Basic table model schema:

  models: {
      Group: {
          pk: { type: String, value: '${_type}#${rootId}' },
          sk: { type: String, value: '${_type}#${groupHierarchy}' },
          rootId: { type: String, generate: 'ulid', validate: Match.ulid, encode: [ 'pk', '#', 1 ] },
          groupHierarchy: { type: Array, items: { type: String, generate: 'ulid', validate: Match.ulid, encode: [ 'sk', '#', 1 ] } },
          name: { type: String, required: true, unique: true, validate: Match.name },
      },
      UserMembership: {
          pk: { type: String, value: '${_type}#${rootId}' },
          sk: { type: String, value: '${_type}#${groupHierarchy}#${id}' },
          id: { type: String, generate: 'ulid', validate: Match.ulid, encode: ['sk', '#', 2]}
          rootId: { type: String, generate: 'ulid', validate: Match.ulid, encode: [ 'pk', '#', 1 ] },
          groupHierarchy: { type: Array, items: { type: String, generate: 'ulid', validate: Match.ulid, encode: [ 'sk', '#', 1 ] } },
          name: { type: String, required: true, unique: true, validate: Match.name },
      },

When I try to run this through Dynamodb-OneTable I get the following error:

OneTableError: Empty hash key. Check hash key and any value template variable references.

Is there a way to use this library to model this type of schema where we have a variable number of nested Ids based on an array value? Thanks!

Sounds like a reasonable use case. Could be a bug that an Array can't be serialized in the SK.

Turn on logging with your API call via params {log: true}

and post the logging output.

Best to disable encoding at the start as it makes debugging a little more difficult.

If you create a test sample and submit as an issue, it will get priority to address.

Closing until you can submit a test case. Please feel free to re-open with a test case.