I have a DynamoDB table that contains invoice items in this format:
PK: invoice#ID_12345
SK: department#electronics
client: Client A
PK: invoice#ID_45678
SK: department#furniture
client: Client B
When the user creates a new invoice the "client" field is free text, but of course most clients are repeat customers and would already have an invoice somewhere. To avoid inconsistencies when entering the client name, I would like a dropdown of existing client names the user can select from, or click "create new" and free text a new one.
Is there an approach to keep a running list of unique client names that the frontend can access to population the dropdown? I've been looking at DynamoDB Streams, and not sure if this is a good candidate for that?
There's a few options that you can utilise to achieve your use-case. I'll list them in order of my preference but feel free to use whichever works best for you.
When a user creates a new client, create a metadata item that identifies that column.
Use a static partition key for your index, such as the value 1
and the client name as the sort key.
GSI_PK | client |
---|---|
1 | Client A |
1 | Client B |
Now your index will only contain unique clients, as it's only created when a user creates a new one. Using the static partition key allows you to order the clients lexicographically which will benefit your drop down.
You could use DynamoDB streams to listen to new user creations and update a second table which holds your unique customer names. Unlike the index method above, which has the possibility to contain duplicates, having a second table will ensure that there is only one of each client.
As there is no consistency issue, streams and Lambda can let you offload the logic asynchronously.
To combine both options, you could simply have a second table where you add a client when it's created, it'll allow you to reject the write should the client already exist (avoid duplicates). And when creating a new invoice, if you so need, you could us transactions and a condition that the client already exists on the client table before adding the new invoice.