Python DynamoDB interface, specialized in single-table design.
Project description
DynamoDB SingleTable
Python DynamoDB interface, specialized in single-table design. DynamoDB is high-performance serverless NoSQL, but difficult to disign tables.
Single-table design needs only single table, and few GSIs (Global Secondary Indexes). It makes effective and easy to manage your whole data models for single service.
Getting Started
Init Table
from ddb_single.table import Table
from ddb_single.query import Query
table = Table(
table_name="sample",
endpoint_url="http://localhost:8000",
)
table.init()
Data Models
Each model hava to set 3 keys
- primary_key ... Hash key for single item. default:
{__model_name__}_{uuid}
- seconday_key ... Range key for item. default:
{__model_name__}_item
- unique_key ... key to identify the item is the same. Mainly used to update item.
And you can set serch_key
to enable search via GSI
from ddb_single.model import BaseModel, DBField
from ddb_single.table import FieldType
class User(BaseModel):
__table__=table
__model_name__ = "user"
pk = DBField(primary_key=True)
sk = DBField(secondary_key=True)
name = DBField(unique_key=True)
email = DBField(search_key=True)
age = DBField(type=FieldType.NUMBER, search_key=True)
description=DBField()
Usage
need "Qurey" object for CRUD; query.model(foo).create or search or update or delete
query = Query(table)
Create Item
If the item with same value of unique_key
already exist, exist item is updated.
user = User(name="John", email="john@example.com", description="test")
query.model(user).create()
Then, multible items added.
pk | sk | data | name | description | |
---|---|---|---|---|---|
user_xxxx | user_item | John | john@example.com | test | |
user_xxxx | search_user_name | John | |||
user_xxxx | search_user_email | new-john@example.com |
In addition to main item (sk=user_item
), multiple item (sk=search_{__model_name__}_{field_name}
) added to table.
Those "search items" are used to search
The GSI DataSearchIndex
is used to get "search items" to extract target's pk.
Then, batch_get
items by pk.
sk = hash | data = range | pk |
---|---|---|
search_user_name | John | user_xxxx |
search_user_email | new-john@example.com | user_xxxx |
Search Items
user = query.model(Test).search(Test.name.eq("John"))
print(user)
# -> [{"pk":"user_xxxx", "sk":"user_item", "name":"John", "email":"john@example.com"}]
get_by_unique
is easy to get single item by unique_key
user = query.model(Test).get_by_unique("John")
print(user)
# -> {"pk":"user_xxxx", "sk":"user_item", "name":"John", "email":"john@example.com"}
Update Item
user = query.model(Test).search(Test.email.eq("john@example.com"))
new_user = Test(**user[0])
new_user.email = "new-john@example.com"
query.model(new_user).update()
Or use unique value to detect exist item.
new_user = Test(name="John", email="new-john@example.com")
query.model(new_user).update()
Then, tha value of "main item" and "seach item" changed
pk | sk | data | name | description | |
---|---|---|---|---|---|
user_xxxx | user_item | John | new-john@example.com | test | |
user_xxxx | search_user_name | John | |||
user_xxxx | search_user_email | new-john@example.com |
Delete Item
user = query.model(Test).search(Test.email.eq("new-john@example.com"))
query.model(user[0]).delete()
Or use unique value to detect exist item.
query.model(User).delete_by_unique("John")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ddb_single-0.1.1.tar.gz
.
File metadata
- Download URL: ddb_single-0.1.1.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5999980cb055d2aff86faef4cdbf92e40f14abad3052900260a7909a4ee44d7d |
|
MD5 | f3e3ff3ca0f87b2339485f641f4890ac |
|
BLAKE2b-256 | 505792afe14b188ca0428c42b7e2d499375ef260648f6c5f8664eb3d629aeb88 |
File details
Details for the file ddb_single-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: ddb_single-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2288545c592d1d22b0da6ac5b56d226988b149e6b5f89ab8e50b5ada688aebf2 |
|
MD5 | 4e3d81d487ac9469c0364832c69884c2 |
|
BLAKE2b-256 | ab6303dd13932eff47c5d9a5e95d3c88f8af73c968d71829828f44735483ebcf |