Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single table db architecture with AWS Amplify

By default AWS Amplify transformers creating tables per each graphql type.

But according DynamoDB documentation it's best practice to

  • Keep tables few as possible
  • Keep often queried together entries within a same table

I have an impression Amplify way of doing things stays in contradiction with the statement above.

I am new to both NoSQL and Amplify Can someone suggest ways to address those issues?

like image 581
Armen Yeganyan Avatar asked Oct 09 '20 14:10

Armen Yeganyan


1 Answers

I think we're in a bit of a transition or gray area here. I'm very new to Amplify and have been investigating moving to a single-table design as there are sources (below) that indicate that it's always be there but you'd have to write everything in VTL templates. But in 2020 they released direct lambda resolver support: https://youtu.be/EOQqi6Yun7g?t=960 (clip)

However, it seems like you lose access to the @auth directive (and probably others because you're no longer going to use @model) along with a lot of the nice out-of-the-box functionality that's available with Amplify's multi-table approach.

At this point, being that I'm developing a new app, I'm going to stick with the default multi-table design to hasten the process of getting the app functional.

Trying to implement the single-table design seems to go against what the Amplify team recommends and requires more manual work. You'd have to manually create custom lambda functions (AppSync) and code queries to DynamoDb for each data access element and manage authorization through some other means which I'm not aware of at this time. Maybe someone can chime in here...


Single table vs multi table info

Using Amplify with single table: https://youtu.be/EOQqi6Yun7g

Single vs Multi Clip:

  • https://youtu.be/1WF_wped808?t=1251 (clip)
  • https://www.alexdebrie.com/posts/dynamodb-single-table/ (towards bottom)
  • https://youtu.be/EOQqi6Yun7g?t=1288 (clip)

Example single table design by Alex Debrie: https://gist.github.com/dabit3/96dc51e688b18a7d40fc534331758c56

More Discussion: https://stackoverflow.com/a/56438716/1956540

Basic Setup steps

I setup a single table by following the below instructions. Again, you don't use @models for this. Also, I think you have to include a type query {} in your schema for it to compile, but I could be wrong here.

So the basic steps are:

  • Create a single table (amplify add storage)
  • amplify push
  • Create your schema in the schema.graphql file.
  • Create supporting lambda function (amplify add function)
    • Note: if you look at the example here, I believe you can create an entry point to routes to all other methods: https://gist.github.com/dabit3/96dc51e688b18a7d40fc534331758c56#lambda
  • Add the DynamoDb query code in the function.
  • amplify push

Complete steps for Setting up a single Table:
https://catalog.us-east-1.prod.workshops.aws/workshops/53b10bf8-2271-4ab4-bfd2-39e878a90dc8/en-US/lab2/1-vtl (both "Connecting to an existing DynamoDB table" and "Direct Lambda Resolver" steps)


Not trying to be negative about Amplify, it is awesome, I love what they are doing with this product. I just think it's very new to everyone and I'm hoping this post is no longer valid next year and we continue to see great progress from the team.

like image 169
BatteryAcid Avatar answered Oct 20 '22 14:10

BatteryAcid