hexlabsio/dynamo-ts

Single Design Table question

obazoud opened this issue · 4 comments

Hello,

Thank for writing dynamo-ts.

A question about query in your example with Repository and Workflow (Workflow with a join to Repository).

Let's say that we have these datas:

Repository Table
Partition | Sort | ...
#ACCOUNT$acc1 | #REPO$repo1 | ... other fields
#ACCOUNT$acc2 | #REPO$repo2 | ... other fields

Workflow Table
Partition | Sort | ...
#ACCOUNT$acc1 | #WORKFLOW#REPO$repo1#WORKFLOW$wf1 | ... other fields
#ACCOUNT$acc1 | #WORKFLOW#REPO$repo1#WORKFLOW$wf2 | ... other fields
#ACCOUNT$acc1 | #WORKFLOW#REPO$repo1#WORKFLOW$wf3 | ... other fields
#ACCOUNT$acc2 | #WORKFLOW#REPO$repo2#WORKFLOW$wf4| ... other fields
#ACCOUNT$acc2 | #WORKFLOW#REPO$repo2#WORKFLOW$wf5 | ... other fields

In this modified example:

  • the account acc1 owns a repository repo1 with 3 workflows wf1, wf2, wf3.
  • the account acc1 owns a repository repo2 with 2 workflows wf4, wf5

With the API, we can get with all the repository with all the workflows as mentioned in the example.

For my use case, I would like also to get all the workflows for a given user and a given repo.

Does dynamo-ts be able to generate query like this ?

QueryInput: {
      "TableName": "singleTableDesignDefinition",
      "KeyConditionExpression": "#11 = :22  AND begins_with(#33, :44)",
      "ExpressionAttributeNames": {
        "#11": "partition",
        "#33": "sort"
      },
      "ExpressionAttributeValues": {
        ":22": "#ACCOUNT$acc1",
        ":44": "#WORKFLOW#REPO$repo1#WORKFLOW"
      }
    }

Thank you!

Hey @obazoud,

Thanks for the interest!

To achieve this, you can use the second argument which lets you dig down through all the layers.
Here is an example:

singleTableClient.workflow.query({account: 'acc1'}, ({repo}) => repo('repo1'));

This outputs the following:

QueryInput: {
      "TableName": "gitboard-repo-status-acc1",
      "KeyConditionExpression": "#7013ba9ba6d6ac58f8eba9ad2ed791c5 = :9462751 AND begins_with(#cadc8c8db42409733582cb3e2298ef87,:6096170)",
      "ExpressionAttributeNames": {
        "#7013ba9ba6d6ac58f8eba9ad2ed791c5": "partition",
        "#cadc8c8db42409733582cb3e2298ef87": "sort"
      },
      "ExpressionAttributeValues": {
        ":6096170": "#WORKFLOW#REPO$repo1",
        ":9462751": "#ACCOUNT$acc1"
      }
    }

Hope this helps,
Please reopen if this does not resolve your issue.

Another question about the SDT with dynamon-ts.

I would like to create a Global Secondary Index on this table.
I don't find a way to do this.
How can I do this?

The best way to do this would be to set up a separate client that specifies the partition and sort key that can be used for the index, i plan to add support for this from the single table client soon

Is there a way to backfill my object with the value of partition and sort computed by dynamo-ts after a put or a batchPut?