Package: pydataapi-postgres
py-data-api - Data API Client for Pythonpy-data-api is a user-friendly client which supports SQLAlchemy models.
Also, the package includes DB API 2.0 Client and SQLAlchemy Dialects.FeaturesA user-friendly client which supports SQLAlchemy modelsSQLAlchemy Dialects (experimental)DB API 2.0 compatible clientPEP 249What's AWS Aurora Serverless's Data API?https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.htmlThis project is an experimental phase.Warning: Some interface will be changed.How to installpydataapi requires Python 3.6.1 or later$pipinstallpydataapiExamplefromtypingimportListfromsqlalchemyimportColumn,Integer,Stringfromsqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemy.ormimportQueryfromsqlalchemy.sqlimportInsertfrompydataapipostgresimportDataAPI,transaction,Result,RecordclassPets(declarative_base()):__tablename__='pets'id=Column(Integer,primary_key=True,autoincrement=True)name=Column(String(255,collation='utf8_unicode_ci'),default=None)database:str='test'resource_arn:str='arn:aws:rds:us-east-1:123456789012:cluster:serverless-test-1'secret_arn:str='arn:aws:secretsmanager:us-east-1:123456789012:secret:serverless-test1'defexample_with_statement():# DataAPI supports with statement for handling transactionwithDataAPI(database=database,resource_arn=resource_arn,secret_arn=secret_arn)asdata_api:# start transactioninsert:Insert=Insert(Pets,{'name':'dog'})# INSERT INTO pets (name) VALUES ('dog')# `execute` accepts SQL statement as str or SQL Alchemy SQL objectsresult:Result=data_api.execute(insert)print(result.number_of_records_updated)# 1query=Query(Pets).filter(Pets.id==1)result:Result=data_api.execute(query)# or data_api.execute('select id, name from pets')# SELECT pets.id, pets.name FROM pets WHERE pets.id = 1# `Result` like a Result object in SQL Alchemyprint(result.scalar())# 1print(result.one())# [Record<id=1, name='dog'>]# `Result` is Sequence[Record]records:List[Record]=list(result)print(records)# [Record<id=1, name='dog'>]# Record is Sequence and Iteratorrecord=records[0]print(record[0])# 1print(record[1])# dogforcolumninrecord:print(column)# 1 ...# show record as dict()print(record.dict())# {'id': 1, 'name': 'dog'}# batch insertinsert:Insert=Insert(Pets)data_api.batch_execute(insert,[{'id':2,'name':'cat'},{'id':3,'name':'snake'},{'id':4,'name':'rabbit'},])result=data_api.execute('select * from pets')print(list(result))# [Record<id=1, name='dog'>, Record<id=2, name='cat'>, Record<id=3, name='snake'>, Record<id=4, name='rabbit'>]# result is a sequence objectforrecordinresult:print(record)# Record<id=1, name='dog'> ...# commitdefexample_decorator():pet_names:List[str]=['dog','cat','snake']add_pets(pet_names)@transaction(database=database,resource_arn=resource_arn,secret_arn=secret_arn)defadd_pets(data_api:DataAPI,pet_names:List[str])->None:# start transactionforpet_nameinpet_names:data_api.execute(Insert(Pets,{'name':pet_name}))# some logic ...# commitdefexample_simple_execute():data_api=DataAPI(resource_arn,secret_arn,database=database)result:Result=data_api.execute('show tables')print(result.scalar())# Petsdefexample_rollback():withDataAPI(resource_arn,secret_arn)asdata_api:data_api.execute(Insert(Pets,{'name':'dog'}))# you can rollback by ExceptionraiseExceptiondefexample_rollback_with_custom_exception():classOriginalError(Exception):passwithDataAPI(resource_arn,secret_arn,rollback_exception=OriginalError)asdata_api:data_api.execute(Insert(Pets,{'name':'dog'}))# some logic ...# rollback when happen `rollback_exception`raiseOriginalError# rollback# raise Exception <- DataAPI don't rollbackdefexample_driver_for_sqlalchemy():fromsqlalchemy.engineimportcreate_engineengine=create_engine('mysql+pydataapi://',connect_args={'resource_arn':'arn:aws:rds:us-east-1:123456789012:cluster:dummy','secret_arn':'arn:aws:secretsmanager:us-east-1:123456789012:secret:dummy','database':'test'})result:ResultProxy=engine.execute("select * from pets")print(result.fetchall())