astrapy icon indicating copy to clipboard operation
astrapy copied to clipboard

Request: include constructor functions on find operations

Open amorton opened this issue 1 year ago • 1 comments

I have this code:

    current_metadata: ChunkedArticleMetadataOnly = ChunkedArticleMetadataOnly.from_chunked_article(chunked_article)

    # get the existing chunks from the db
    resp = METADATA_COLLECTION.find_one(
        filter={"_id": current_metadata._id},
    )
    existing_metadata_dict = resp["data"]["document"]

    if not existing_metadata_dict:
        # everything is new !
        return ChunkedArticleDelta(
            article=chunked_article,
            new_chunks=chunked_article.chunks
        )

    # there are existing chunks we need to see if anything has changed
    existing_metadata: ChunkedArticleMetadataOnly = ChunkedArticleMetadataOnly(**existing_metadata_dict)

on the last line I am using the document returned to create a dataclass, it would be cool if I could pass a lambda to the find_one that would be called the the document and the result object returned.

So my code would become (assuming #149 is also accepted) ...

    current_metadata: ChunkedArticleMetadataOnly = ChunkedArticleMetadataOnly.from_chunked_article(chunked_article)

    # get the existing chunks from the db
    existing_metadata: ChunkedArticleMetadataOnly = METADATA_COLLECTION.find_one(
        filter={"_id": current_metadata._id},
        callback=lambda doc: ChunkedArticleMetadataOnly(**doc)
    )
    
    if not existing_metadata:
        # everything is new !
        return ChunkedArticleDelta(
            article=chunked_article,
            new_chunks=chunked_article.chunks
        )

    # there are existing chunks we need to see if anything has changed
    

this would be supported for any command that returns a document

amorton avatar Dec 20 '23 04:12 amorton

I suppose this would not align with the "clients as a thin layer on the Data API" philosophy that has gained traction. @amorton may I close as won't do ?

hemidactylus avatar Aug 28 '24 17:08 hemidactylus

@amorton Circling back on this topic to announce that the new (2.0) clients (all of them btw) offer a .map(f) method to precisely achieve what you describe here. Closing this one then.

hemidactylus avatar Nov 05 '24 09:11 hemidactylus