Define a Model
Only keys and indexes need to be defined. All other attributes are free-form (NoSQL philosophy).
Basic Model
from dkmio import DynamoDB, PK, SK
db = DynamoDB(region_name="us-east-1")
class Orders(db.Table):
__table_name__ = "orders"
pk = PK("user_id")
sk = SK("order_id")
Keys
| Key Type | Description |
|---|---|
PK | Partition Key (required) |
SK | Sort Key (optional) |
Indexes
Global Secondary Index (GSI)
from dkmio import Index
class Orders(db.Table):
__table_name__ = "orders"
pk = PK("user_id")
sk = SK("order_id")
# GSI with INCLUDE projection
by_status = Index(
"gsi-status-date",
pk="status",
sk="created_at",
projection=["total", "items_count"]
)
# GSI with ALL projection
by_date = Index("gsi-date", pk="user_id", sk="created_at", projection="ALL")
# GSI with KEYS_ONLY projection
by_region = Index("gsi-region", pk="region", projection="KEYS_ONLY")
Local Secondary Index (LSI)
from dkmio import LSI
class Orders(db.Table):
__table_name__ = "orders"
pk = PK("user_id")
sk = SK("order_id")
# LSI — inherits PK from the table automatically
by_amount = LSI("lsi-amount", sk="total")
# LSI with INCLUDE projection
by_priority = LSI("lsi-priority", sk="priority", projection=["status", "total"])
TTL
from dkmio import TTL
class Sessions(db.Table):
__table_name__ = "sessions"
pk = PK("session_id")
ttl = TTL("expires_at")
Projection Types
| Type | Description |
|---|---|
KEYS_ONLY | Only key attributes |
ALL | All table attributes |
INCLUDE | Key attributes + specified attributes |