Open softwareguy74 opened 3 years ago
Any word on this?
There is no out-of-the-box way to do this. Depending on the transformations you need you could query using the model interface and then iterate through and convert each model to a dictionary, or you could query using the table api and iterate through the raw results, transforming the dynamo json as necessary.
Related you can look here for an attempt to add to/from json methods to the model objects. https://github.com/pynamodb/PynamoDB/pull/857
I think that is highly inefficient. If you are able to do the conversion with .get() on a single item from DynamoDB to JSON internally, why can't you guys create an internal iterator that does the same on .query() or .scan()?
If you don't care about the model abstraction then using the low-level connection api is likely the best option: https://github.com/pynamodb/PynamoDB/blob/master/docs/low_level.rst
It should be easy to combine that with a ResultIterator where you can provide your own mapping function to transform the DynamoDB item into a dictionary: https://github.com/pynamodb/PynamoDB/blob/master/pynamodb/pagination.py#L153
As might be evident in the linked PR above, removing the dynamodb types is easy but converting into the correct python type (e.g. lists vs sets) requires type information from the model.
I just want the following returned when issuing a scan or query:
[
{"id":"1", "name":"product 1"},
{"id":"2","name":"product 2"}
]
Is this possible?
The DynamoDB boto API has a higher level table object to work with which returns standard json. Maybe we could use that behind the scenes for pynamodb?
I'm not too familiar with boto's higher-level API, though I am happy to review pull requests :) I don't have a good sense of the flexibility we have here without breaking backwards-compatibility.
Maybe add an additional optional parameter in the model definition to use table API instead? I believe it's mostly same as lower level except that it returns standard, non DynamoDB json instead.
On Mon, Nov 9, 2020, 7:51 AM jpinner-lyft notifications@github.com wrote:
I'm not too familiar with boto's higher-level API, though I am happy to review pull requests :) I don't have a good sense of the flexibility we have here without breaking backwards-compatibility.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pynamodb/PynamoDB/issues/882#issuecomment-724099146, or unsubscribe https://github.com/notifications/unsubscribe-auth/APCGJAVVPJYIHYBQBC2MBRDSPAFYPANCNFSM4TLG6LPA .
Generally the task of an ORM is to map raw data using a schema. If you don't need or care about a schema, perhaps using boto directly would in fact be what you want?
I'd like to return the results of a query as standard json objects rather than DynamoDB json. Is this possible? It so, how?