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