Package: dynamobase
DynamoBaseA lightweight JSON model that simplifies DynamoDB's obscure query operations. It retains all original parameters and supports DynamoDB versions and future upgrades.What can be done:Query condition is JSON formatNo need to consider complex expressions no matter query or updateColumn names can be any reserved keywordsReturn result correctly returns int, float, number typesNew function get_first can get the frist item by GSI/LSIWhat can't be done:Create, modify, and drop DynamoDB tablesInstallpip install dynamobaseUseBasic tryfrom DynamoBase import DynamoBase

DynamoBase.table_region = "ap-southeast-2"
DynamoBase.table_name = "users"
DynamoBase.session = ... # if you need specific settings like aws profile, credentials etc.

# get a single item
user = DynamoBase.get_item(query={"first_name": "Jackson"})
print(user)

# get a list of items, IndexName is optional
# query relate to KeyConditions, filter relate to FilterExpression
user = DynamoBase.get_items(query={"first_name": "Jackson"}, IndexName='ix_name')
print(user)

# get the first item, IndexName is optional
user = DynamoBase.get_first(query={"first_name": "Jackson"}, IndexName='ix_name')
print(user)

# insert an item
DynamoBase.put_item(Item={"first_name": "Jackson"})

# update an item, Item is part or all of item
DynamoBase.update_item(query={"first_name": "Jackson"}, Item={'field': 12345})

# delete an item
DynamoBase.delete_item(query={"first_name": "Jackson"})RecommendationCreate the corresponding model for each table.from DynamoBase import DynamoBase

class User(DynamoBase):
    table_region = "ap-southeast-2"
    table_name = "users"Query databaseuser = User.get_first(query={"first_name": "Jackson"})
print(user)(Optional) You can also create a base-model to configure common properties, such as region and credentialsfrom DynamoBase import DynamoBase

class BaseModel(DynamoBase):
    table_region = "ap-southeast-2"from BaseModel import BaseModel

class User(BaseModel):
    table_name = "users"(Optional) Extend your classes to meet your business needsfrom DynamoBase import DynamoBase

class User(DynamoBase):
    table_region = "ap-southeast-2"
    table_name = "users"

    @classmethod
    def get_item(cls, **kwargs):
        ...

    @classmethod
    def other_method(cls, **kwargs):
        ...More exampleres = Article.get_item(query={"_id": "hH2ZZTgQqzgK888zrkG8ui"})
print(1, res)
res = Article.get_items(
    IndexName="ix_category_id",
    query={"category_id": 1, "created_at": {">": 1}},
    ProjectionExpression="title",
    ScanIndexForward=True,
)
print(2, res)
res = Article.get_items(
    IndexName="ix_test",
    query={"_id": "ewTx3gannQUinM7ECjpQad", "url": {"begins_with": "1"}},
    ProjectionExpression="title",
    ScanIndexForward=True,
)
print(3, res)
res = Article.get_first(
    IndexName="ix_category_id",
    query={"category_id": 1, "created_at": {"between": [1670367685098, 1670367663116]}},
    ProjectionExpression="title",
    ScanIndexForward=False,
)
print(4, res)
res = Article.put_item(Item={"_id": "123", "data": {"a": 123}})
print(5, res)
res = Article.update_item(query={"_id": "123"}, Item={"data": {"a": 456}})
print(6, res)
res = Article.delete_item(query={"_id": "123"})
print(7, res)res = Article.get_items(
    IndexName="ix_status",
    query={"status": 1},
    ProjectionExpression="title, #url",
    ExpressionAttributeNames={"#url": "url"},
    ScanIndexForward=True,
)APIsAll "GET" operations support: = | <= | < | >= | > | begins_with | betweenget_itemparametersNameTypeExampledescriptionquerydict{"id": 123}query must be primary key (and sort key)filterdict{"id": 123}any fields and any valuesreturn: Dict or Noneget_itemsparameters: The parameters supported byget_itemsandget_firstare as follows:NameTypedescriptionquerydictquery can be primary key or GSI columnsIndexNameStringrequired if query is GSI or LSISelectStringAttributesToGetListLimitintConsistentReadStringKeyConditionsdictQueryFilterdictConditionalOperatorStringScanIndexForwardbooleanExclusiveStartKeydictReturnConsumedCapacityStringProjectionExpressionStringFilterExpressionboto3.dynamodb.conditions.Attrreturn: List or Noneget_firstparameters: same as get_itemsreturn: Dict or Noneput_itemparametersNameTypeExampleItemdict{"id": 123}update_itemparametersNameTypeExampledescriptionquerydict{"id": 123}query must be primary key (and sort key)Itemdict{"field": "some value"}--delete_itemparametersNameTypeExampledescriptionquerydict{"id": 123}query must be primary key (and sort key)DynamoDB docsDynamoDB Query Parameters