Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GridFS support #50

Open
erny opened this issue Nov 15, 2020 · 3 comments
Open

GridFS support #50

erny opened this issue Nov 15, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@erny
Copy link
Contributor

erny commented Nov 15, 2020

Hello. Thanks for this great piece of software. The docs seems to me quite complete. My congrats.

I considering to use odmantic and I would like to store files in MongoDB. Could you provide an example? What do I have to take into account?

Thanks in advance.

@art049
Copy link
Owner

art049 commented Nov 16, 2020

Hello thanks for the feedback 🎉.
I didn't really thought yet about GridFS use with odmantic but maybe this issue is the opportunity for starting documenting this 😄 .

Basically, I think it should be quite easy to use GridFS externally.
First, you'll probably need to pass a motor client instance to the AIOEngine so you'll be able to reuse it later for accessing the files.
You could then add ObjectId field referencing your files in Odmantic models.
Finally you'll be able to query the files from the motor client directly.

I would really like to integrate GridFS Files to odmantic but I think it should come with Lazy loading and maybe streaming capabilities. This will probably be a good time to address this once lazy references will be handled 😉

@erny
Copy link
Contributor Author

erny commented Nov 17, 2020

Hi, thanks for your quick answer. Adding object Id(s) to reference files could be a good way to start.

Lazy loading is already provided by motor AFAIK and streaming shouldn't be very difficult. I was thinking about a thin layer for searching / getting files and accessing streams, something like (to be consistent with existing API):

...
from fastapi.responses import StreamingResponse
from odmantic import AIOEngine, File
engine = AIOEngine()

@app.get("/filestreams/", name: Optional[str]):
    file = await engine.find_one(File, File.name == "myfile.pdf")
    return StreamingResponse(await file.open_download_stream(), media_type=file. content_type)

so odmantic.File would really be a Model like class with some extras. It seems that the attributes are fixed anyway (name/filename, content_type, length, md5. upload_date, closed, chunksize, metadata) but taking into account AsyncIOMotorGridFSBucket , AsyncIOMotorGridIn, AsyncIOMotorGridOut there are a lot of methods (delete, rename, open_download_stream, open_upload_stream, download_to_stream, upload_from_stream, abort, close, set, write, writelines, read, readline, close).

Regards.

@art049 art049 added the enhancement New feature or request label Sep 1, 2022
@art049 art049 changed the title odmantic and gridfs GridFS support Sep 1, 2022
@tpougy
Copy link

tpougy commented Aug 29, 2023

Any news about this? I am really interested in using ODMantic with GridFS support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants