I am trying to create a business hours application using DynamoDB
.
I saw lots of examples and schema designs for different databases but just can't find the right table design for DynamoDB
.
Here are my requirements :
Those are the operations I need to allow:
What I've tried:
Defined a table where business id is the partition key
( HASH
) and special dates / day of week is the sort key
( RANGE
).
The problem with this approach is that I cannot query by multiple business hours unless I use the scan
api which is not recommended due to expensive operations.
Please advice what kind of table design I should use for this application.
You probably need to first construct your overarching logic outside of DynamoDB, do decide if a business is working or not, and only use quarries in Dynamo for a subset of that logic.
Lets say though we use DynamoDB for querying in regards to normal working hours, and not include logic like holidays and special cases, you can use that to filter after you access Dynamo. You can't construct one query in Dynamo to answer all your questions that is more like what you can do in SQL.
So lets say we have a Table/Subset of values which relate to the normal working day. So you have something like this:
Partition Key (PK): business
, Range Key (RK): dayOfWeek
, and attributes, opens
& closes
.
We can then create 2 GSIs:
dayOfWeek
RK opens
dayOfWeek
RK closes
Now we can do two queries if a store is open between 3-4pm on Monday:
And collect only the values which appear in both queries.
Obviously though, having a PK of day, is probably not a great idea, as you will only have 7 partitions. So what do you do? Well you probably have more criteria in your query than simply day, for example, the type of store, the city the store is located it, etc. That would mean then you would have a PK of something like: city-category-dayOfWeek
.
Similarly on the sorting side, you might want higher rated stores to be the first option, so you might have something like: {rating}-{open}
& {rating}-{closes}
.
You will just have to get creative, firstly layout all the queries you have before you design your tables. I really like this video on table design, it's terrific.
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.