RdsDataDao
A wrapper around the RDS Data API offering query escaping and automatic parameterization.
Warning
Not an AWS endorsed project - used at Stavvy for managing database queries for some services.
Installation
pip install rds-data-dao
Example usages.
-
Provide cmd and data arguments to the crud methods on the RdsDataDao object. It will automatically cast and convert the arguments to parameterized form.
-
Use
%s
in thecmd
format string for any parameterized argument (except arrays). -
Use
%s::type
to cast arguments, for example%s::json
, will cast the argument to json.
ex usage:
cmd = "select * from banks where id = %s" data = [bank_id] bank = dao.get_single_result(cmd, data) return bank
For arrays, make_list
from db_util
should be used as arrays are not natively parameterizable as of this writing in the RDS Data API.
cmd = "select bank_id from files where id in {}".format(make_list(ids)) # ids is list of integers here. banks = dao.get(cmd) return banks
Example Response
The RdsDataDao translates responses into dict format using returned column definitions in the data api:
{ "numberOfRecordsUpdated": 0, "columnMetadata": [ { "name": "id" }, { "name": "name" }], "records": [ [ { "longValue": 1 }, { "stringValue": "John" } ], [ { "longValue": 2 }, { "stringValue": "Joe" } ] ] }
maps to:
[ {"id": 1, "name": "John"}, {"id": 2, "name": "Joe"} ]
References
- https://medium.com/@bfortuner/python-unit-testing-with-pytest-and-mock-197499c4623c
- https://github.com/lyft/python-blessclient/blob/master/tests/blessclient/bless_lambda_test.py
Publishing (internal)
- python setup.py sdist
- twine upload dist/*