Create three tables demonstrating different key schemas.
(client)
| 95 | # --------------------------------------------------------------------------- |
| 96 | |
| 97 | def create_tables(client) -> None: |
| 98 | """Create three tables demonstrating different key schemas.""" |
| 99 | section("Step 1: Create Tables") |
| 100 | |
| 101 | # Table 1: Simple PK (user profiles) |
| 102 | print(f"Creating {USERS_TABLE} (simple HASH key)...") |
| 103 | client.create_table( |
| 104 | TableName=USERS_TABLE, |
| 105 | AttributeDefinitions=[ |
| 106 | {"AttributeName": "userId", "AttributeType": "S"}, |
| 107 | ], |
| 108 | KeySchema=[ |
| 109 | {"AttributeName": "userId", "KeyType": "HASH"}, |
| 110 | ], |
| 111 | BillingMode="PAY_PER_REQUEST", |
| 112 | ) |
| 113 | |
| 114 | # Table 2: PK + SK (orders by customer) |
| 115 | print(f"Creating {ORDERS_TABLE} (HASH + RANGE key)...") |
| 116 | client.create_table( |
| 117 | TableName=ORDERS_TABLE, |
| 118 | AttributeDefinitions=[ |
| 119 | {"AttributeName": "customerId", "AttributeType": "S"}, |
| 120 | {"AttributeName": "orderId", "AttributeType": "S"}, |
| 121 | {"AttributeName": "orderDate", "AttributeType": "S"}, |
| 122 | ], |
| 123 | KeySchema=[ |
| 124 | {"AttributeName": "customerId", "KeyType": "HASH"}, |
| 125 | {"AttributeName": "orderId", "KeyType": "RANGE"}, |
| 126 | ], |
| 127 | GlobalSecondaryIndexes=[ |
| 128 | { |
| 129 | "IndexName": "OrderDateIndex", |
| 130 | "KeySchema": [ |
| 131 | {"AttributeName": "customerId", "KeyType": "HASH"}, |
| 132 | {"AttributeName": "orderDate", "KeyType": "RANGE"}, |
| 133 | ], |
| 134 | "Projection": {"ProjectionType": "ALL"}, |
| 135 | }, |
| 136 | ], |
| 137 | BillingMode="PAY_PER_REQUEST", |
| 138 | ) |
| 139 | |
| 140 | # Table 3: Multi-part GSI keys (tournament pattern from AWS docs) |
| 141 | # https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.DesignPattern.MultiAttributeKeys.html |
| 142 | print(f"Creating {TOURNAMENT_TABLE} (multi-part GSI keys)...") |
| 143 | client.create_table( |
| 144 | TableName=TOURNAMENT_TABLE, |
| 145 | AttributeDefinitions=[ |
| 146 | {"AttributeName": "matchId", "AttributeType": "S"}, |
| 147 | {"AttributeName": "tournamentId", "AttributeType": "S"}, |
| 148 | {"AttributeName": "region", "AttributeType": "S"}, |
| 149 | {"AttributeName": "round", "AttributeType": "N"}, |
| 150 | {"AttributeName": "bracket", "AttributeType": "S"}, |
| 151 | {"AttributeName": "player1Id", "AttributeType": "S"}, |
| 152 | {"AttributeName": "matchDate", "AttributeType": "S"}, |
| 153 | ], |
| 154 | KeySchema=[ |
no test coverage detected