Package: coraline
Welcome to CoralineCoraline is a Python library that aims to use Pydantic models to work with AWS DynamoDB tables.Install$pipinstallcoralineCoraline needsboto3to work. If you don't have it installed, you can install it using:$pipinstallcoraline[boto]DocumentationTODOQuick Start:importuuidfromenumimportEnumfromcoralineimportCoralModel,KeyField,HashTypefrompydanticimportSecretStr,FieldclassUserType(Enum):USER="USER"ADMIN="ADMIN"classUsers(CoralModel):user_id:uuid.UUID=KeyField(default=lambda:uuid.uuid4(),hash_key=HashType.HASH,alias="userId")user_type:UserType=KeyField(...,hash_type=HashType.RANGE,alias="userType")name:strage:int=Field(...,gt=0)password:SecretStrUsers.get_or_create_table()new_user=Users(name="John Doe",user_type=UserType.USER,age=30,password="123456")new_user.save()This class will create a DynamoDB table namedUsers, withPAY_PER_REQUESTbilling mode and using default AWS
session, with the following fields:userIdas a String. It's the Table's Hash Key.userTypeas a String. It's the Table's Range Key.nameas a Stringemailas a Numberpasswordas a StringConfiguring the tableUse theCoralConfigclass to configure your table.importuuidfromenumimportEnumfromcoralineimportCoralModel,KeyField,HashType,CoralConfig,BillingMode,TableClassfrompydanticimportSecretStr,FieldclassUserType(Enum):USER="USER"ADMIN="ADMIN"defto_camel(string:str)->str:return''.join(word.capitalize()forwordinstring.split('_'))# CoralModel is a subclass of Pydantic's BaseModelclassUsers(CoralModel):# CoralConfig is a subclass of Pydantic's ConfigDictmodel_config=CoralConfig(table_name="MyUsers",billing_mode=BillingMode.PROVISIONED,read_capacity_units=5,write_capacity_units=5,alias_generator=to_camel,protect_from_exclusion=True,table_class=TableClass.STANDARD_INFREQUENT_ACCESS,extra_table_params={"Tags":[{"Key":"Project","Value":"MyProject"}]})# KeyField is a sub method of Pydantic's Fielduser_id:uuid.UUID=KeyField(default=lambda:uuid.uuid4(),hash_key=HashType.HASH)user_type:UserType=KeyField(...,hash_type=HashType.RANGE)name:strage:int=Field(...,gt=0)password:SecretStrFor Table Name, Billing Methods, you can use theBillingModeand Capacity Units constants. For any other parameter
accepted byboto3'screate_tableuse theextra_table_paramsparameter.Configuring AWS CredentialsTo configure boto3's client credentials, Coraline will:Check for Class specific configuration inmodel_configCheck for Coraline Environment Variables (
ex.CORALINE_AWS_REGION,CORALINE_AWS_ACCESS_KEY_ID,CORALINE_AWS_SECRET_ACCESS_KEY)Check for AWS Environment Variables (ex.AWS_REGION,AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)Env Example:# Add to .env file:
AWS_REGION="local"
CORALINE_ENDPOINT_URL="http://localhost:8000"Class Example:fromcoralineimportCoralModel,CoralConfigclassUsers(CoralModel):model_config=CoralConfig(aws_region="local",aws_endpoint_url="http://localhost:8000")Class Example using Boto3 Config instance:frombotocore.configimportConfigfromcoralineimportCoralModel,CoralConfigconfig=Config(region_name="local",endpoint_url="http://localhost:8000")classUsers(CoralModel):model_config=CoralConfig(aws_region="local",aws_config=config)Basic OperationsGet or Create TableUse to get Table info or create the table if it doesn't exist.table_description:dict=Users.get_or_create_table()Get Table InfoUse to get Table info. You can also add boto's clientdescribe_XXXmethods here, for any describe operation which does not have signature
or the only argument is the TableName:Allowable Descriptions Example:describe_continuous_backups,describe_time_to_live,descript_limits, etc...Not Allowable Descriptions Example:describe_backup,describe_global_table,describe_export, etc...table_info:dict=Users.get_table_info(include=["describe_time_to_live"])Check if Record existsUse to check if a record exists in the table. You need to pass on the parameters all hash and range keys defined in Model:user_exists:bool=Users.exists(user_id="12345678-1234-1234-1234-123456789012",user_type=UserType.USER)Get RecordUse to get a record from the table. You need to pass on the parameters all hash and range keys defined in Model:user:Users=Users.get(user_id="12345678-1234-1234-1234-123456789012",user_type=UserType.USER)Save RecordUse to save a record in the table:new_user=Users(name="John Doe",user_type=UserType.USER,age=30,password="123456")new_user.save()Using boto3's ClientYou can use boto3's client to perform any operation you need. Just use theget_clientmethod:new_user=Users(name="John Doe",user_type=UserType.USER,age=30,password="123456")new_user.save()new_user.get_client().create_backup(TableName=new_user.table_name(),# or Users.get_table_name()BackupName="MyBackup")Current Project StatusCurrent status: In ProgressWe strong advise to not use this lib in Production projects at this current stage.
Except bugs and breaking changes between each release.Future ImplementationsAdd option to "update" tables (create_or_update_tablemethod)Add native support for Global and Local Secondary IndexesAdd native support for Query operationsAdd native support for TransactWriteItems and TransactGetItemsAdd native support for BatchWriteItems and BatchGetItemsNot working?Don't panic. Get a towel and, please, open anissue.