Refty / mongo-thingy

:leaves: Powerful schema-less ODM for MongoDB and Python (sync + async)
https://mongo-thingy.readthedocs.io
MIT License
68 stars 12 forks source link

Connect mongo-thingy to montyDB #48

Closed dawhn closed 1 year ago

dawhn commented 1 year ago

Hello, I've been trying to connect mongo-thingy with montydb. Using the example you gave, I tried

import mongo_thingy
import montydb

mongo_thingy.connect(client_cls=montydb.MontyClient)

This resulted in an error. I then tried

client = montydb.MontyClient(":memory:")
mongo_thingy.connect(client_cls=montydb.MontyClient)

It somehow fixed the error (the connect worked) but i now get AttributeError: Undefined database. on __get_database()

I was wondering if something had to be setup beforehand to use this library or if I was just doing something wrong here ?

ramnes commented 1 year ago

Hey there!

Yes, the first example should work, and I guess this is the error you're getting?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 connect(client_cls=MontyClient)

File ~/refty/mongo-thingy/mongo_thingy/__init__.py:287, in connect(*args, **kwargs)
    285 def connect(*args, **kwargs):
    286     if AsyncThingy._client_cls is not None:
--> 287         AsyncThingy.connect(*args, **kwargs)
    288     Thingy.connect(*args, **kwargs)

File ~/refty/mongo-thingy/mongo_thingy/__init__.py:134, in BaseThingy.connect(cls, client_cls, database_name, *args, **kwargs)
    131 if not client_cls:
    132     client_cls = cls._client_cls
--> 134 cls._client = client_cls(*args, **kwargs)
    135 try:
    136     cls._database = cls._client.get_database(database_name)

File ~/refty/mongo-thingy/.venv/lib/python3.9/site-packages/montydb/client.py:50, in MontyClient.__init__(self, repository, document_class, tz_aware, **kwargs)
     48 options["document_class"] = document_class
     49 options["tz_aware"] = tz_aware or False
---> 50 self.__options = ClientOptions(options, wconcern)
     51 super(MontyClient, self).__init__(self.__options.codec_options,
     52                                   self.__options.write_concern)

File ~/refty/mongo-thingy/.venv/lib/python3.9/site-packages/montydb/base.py:205, in ClientOptions.__init__(self, options, storage_wconcern)
    203 def __init__(self, options, storage_wconcern=None):
    204     self.__options = options
--> 205     self.__codec_options = bson.parse_codec_options(options)
    207     if storage_wconcern is not None:
    208         self.__write_concern = storage_wconcern

TypeError: 'NoneType' object is not callable

This is a weird one: it seems that it only happens when you already have a .monty.storage file, because it works just as expected when I remove it. So this may be an upstream issue but I'll investigate.

ramnes commented 1 year ago

In the meantime, if you're only interested in the memory backend of MontyDB, it appears to work as expected and this is how you want to use it:

connect(":memory:", client_cls=MontyClient)
dawhn commented 1 year ago

Thank you for the answer. ":memory:" should be working for now We are planning on using flatfile / sqlite how would you suggest using these with mongo-thingy ?

ramnes commented 1 year ago

SQLite also seems to work as expected:

from mongo_thingy import connect
from montydb import MontyClient, set_storage

set_storage("./data", storage="sqlite")
connect("./data", client_cls=MontyClient)
dawhn commented 1 year ago

Thank you for your quick reply, It is now working as we wanted using sqlite.

(see https://github.com/Flowtter/crispy/commit/1df46b0bead90e28bddc76ae5756d5d65f7e1f3f)

ramnes commented 1 year ago

That was indeed an upstream bug, fixed in https://github.com/davidlatwe/montydb/pull/75. Thank you @dawhn for raising the issue and @davidlatwe for the quick fix!