I have two entities, Books and Authors with a strict one-to-many relationship (many-to-many relationship not required for my use case)
The access patterns I want to satisfy are:
- Get Author Info by Author Name
- Get Book Info By just ISBN
- Get all Books records by an Author using Author Name.
Do I need any GSI given the constraint that I can make only a single request to DB when adding a Book or an Author, and fulfill above three access patterns also with a single request?
If my Author Entity uses this key schema:
- Partition Key:
AUTHOR#XYZ - Sort Key:
AUTHOR#XYZ
and for Book Entity I use
- Partition Key:
BOOK#123 - Sort Key
BOOK#123
I can get author info by name and book info by ISBN easily. How do I get the 3rd access pattern, entire book data by author name?
Two approaches I thought of:
- Have a third entity in the table with PK
AUTHOR#XYZ,SK BOOK#123, and useBEGINS_WITH(SK, 'BOOK')but in this approach, when adding a book to DB, I will have to write two items, PKBOOK#, SKBOOK#for getting book by just ISBN and PKAUTHOR#, SKBOOK#for getting all books by author, and the book info will be duplicated in both items.
- Add an attribute
GSIAuthorNameto Book entity when adding a book, and create a GSI with PKGSIAuthorName(AUTHOR#XYZ) and SK being PK of Book entity (BOOK#123). But in this the issue is, in projections I will have to select ALL, since I want all book info attributes by author name, and need to fetch in single query to the GSI, so entire Book Entity will be duplicated in this GSI.
Is there an easier way to model this data?