简体   繁体   中英

DynamoDB one to many to many relationship

While the question on how to model 1 to many relationships is well answered on stackoverflow, I couldn't find any information for hierarchical lookup where every intermediate level must be accessible.

Let's assume the following entities: Accounts Group Instances InstanceProviders .

One account has multiple groups. One account has configured multiple InstanceProvider accounts. One group has access to multiple instances, one instance is assigned to one group only. The group name can be chosen freely and is tied to the account. Hence it must be unique on the account level.
The external instance name is provided by the InstanceProvider, uniquely within Account-InstanceProvider-InstanceId .

Now I need to answer the following read patterns:

  • Read instance with id
  • Read instance with external id from provider
  • Read all instances in a group (which depends on an account)
  • Read all instances in an account
  • Read all instances from a provider in an account
  • Read all instances from a provider in a group (which depends on an account)
  • ...

Restrictions:

  • Group name unique within an account
  • One instance assigned to one group, not multiple
  • External ID unique within Account-Provider combination (avoid duplicates for the same external id)

The "Read all" part is where I am struggeling. These lookup would require an GSI each per level, since every sub-level is dependent on the level before it.

Like for one Instance

  • PK=ACCOUNT#123#INSTANCE#11b14ba1 SK=ACCOUNT#123#INSTANCE#11b14ba1
  • GSI1PK=ACCOUNT#123 GSI1SK=INSTANCE#11b14ba1
  • GSI2PK=ACCOUNT#123#PROVIDER#GoodCompany GSI2SK=GROUP#AdminGroup#INSTANCE#11b14ba1
  • GSI3PK=ACCOUNT#123#GROUP#AdminGroup GSI3SK=PROVIDER#GoodCompany#INSTANCE#11b14ba1

Here it's basically one GSI per attribute "chain". Is there a better way?

According to best practices for managing many-to-many relationships by AWS, I think you are doing great.

From my experience, I think you are doing the adjacency list design pattern which

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM