I want to create and use slug urls for use with a blog site built on top of Appsync and DynamoDB via AWS Amplify. The slug is a combination of the article title and a cuid; for example /article/an-interesting-post-on-bats-123abc would return the post with title "An Interesting Post on Bats." This would be relatively straight forward but for a few constraints:
- DynamoDB's GetItem action only works with the primary key.
- The primary key of an item cannot be changed. In order to "change" the primary key, you have to delete and recreate an item.
- The
Articleentity data is malleable over time in that a user can change the Article's title, text, etc. A change to the title may warrant a change to the slug. In fact, a user can save anArticlewithout having entered a title yet, so the slug could not be the basis for the primary key (as it is required to create an item).
It seems pretty clear that the slug itself is a poor choice for a primary key; however, the slug is the only input (as a path param) currently available upon which to create the query. Given these constraints, what is the most efficient way to query an Article if I only have the slug?
At this point, I'm early enough in the design phase that several resources can be modified: (1) the article urls, (2) the DynamoDB design (i.e. what the primary key and sort keys are); and (3) the Appsync schema.
Currently, the Article schema contains:
type Article @model {
id: ID! // the primary key
authorId: String
title: String
text: String
slug: String
}
Although it is possible to scan for the slug, this seems like an inefficient way to utilize Dynamo for a regular access pattern. One thought I had was to make authorId the primary key with slug as a sort key or local secondary index. This appears to be similar to how Medium and StackOverflow create post urls.