Reputation: 1494
I have an app that contains entities such as "catalogs", "collections", etc. I'd like to model relationships between entities using tagging. So for example I might have a sales catalog and a sales collection. I would know both entities are connected because they both have the same tag: "sales".
Here are the queries I need to do:
1) Get All Entities of a certain type i.e. catalogs
2) Get Entity By Id
3) Get all entities with a certain tag
4) Query a list of entities by a certain tag and retrieve the rest of the tags associated with that entity.
I'm wondering how to model this in Dynamo DB
I first thought I could do this:
PK = entityType_id (e.g. catalog_1)
SK = Tag (e.g. sales)
The problem is I can't get get all entities of a certain type. (1)
I thought maybe I could do this:
PK = entityType (e.g. catalog)
SK = id_tag (e.g. 1_sales)
I could accomplish: 1,2above using BeginsWith and EndsWith on the SK and 3 using a GSI where the type tag is PK but couldn't accomplish 4.
In the future I'd also like to possibly be able to query by tag type. I don't see how all of this is possible in one table like Amazon recommends or without using an RDBMS.
I'd really appreciate any input or direction I can get.
Thanks!
Upvotes: 1
Views: 1480
Reputation: 6634
you can use this as your schema
| pk | sk | GSI1 PK |
| uid1 | metadata | | tags:["sales","purchase", "borrow"] | entityType:["catalogs"]
| uid1 |entityType#catalogs | entityType#catalogs
| uid1 | tag#sales | tag#sales
| uid1 | tag#purchase | tag#purchase
| uid1 | tag#borrow | tag#borrow
| uid2 |entityType#collection | entityType#collection
| uid2 | tag#borrow | tag#borrow
| uid2 | metadata | | tags: ["borrow"] | entityType:["collection"]
where PK is SK of GSI1 (GSI1: global secondary index)
This will solve use cases like
Get all entity of sales
Select* where PK=entityType#sales in table GSI1
Get entity by id
Select * wehere PK=id and SK=metadata
Get all entities with a tag Sales
Select* where PK=tag#sales in table GSI1
Query a list of entities by a certain tag and retrieve the rest of the tags associated with that entity.
Use 3rd query and get all list of uids and then do a get all tags, or duplicate tags data as well in every row.
Upvotes: 2