Closed bb78657 closed 6 years ago
Hi @bb78657! :wave:
connect
is just an helper that calls Thingy.connect
. You can use one or the other, the effect is the same: it creates a MongoClient
and puts it inside the root Thingy
class.
So basically, you just need to connect once, and from that moment the connection will be available in all your modules via Thingy
or any of its children. That means that you don't have to import Thingy
between your different modules, you can always import it from mongo_thingy
.
On how to use PyMongo directly, the proper way of doing it is to use Thingy.collection
, Thingy.database
or Thingy.client
.
If you often need to use the PyMongo database object directly inside Flask, I'd recommend you to put it in current_app
during your application bootstrapping:
from flask import Flask
from mongo_thingy import Thingy
Thingy.connect("mongodb://localhost/test")
app = Flask(__name__)
app.database = Thingy.database
That way, you could reuse it nicely in another module:
from flask import current_app
current_app.database.do_something()
Does that answer your questions?
On a side note, our goal with Mongo-Thingy is that you don't need to use PyMongo directly and have all the tools needed directly inside your Thingy child classes. Please open another issue if you feel something is missing to that purpose. :+1:
No news is good news, I'll close this for now. Feel free to reopen if needed!
OK great, much appreciated
I'm not clear on how to use connect (Thingy.connect) across multiple modules in a Flask app, and how to drop to using raw pymongo. In PyMongo I can create a module level variable (or use Flask-PyMongo to handle this)
client = MongoClient()
then just re-use that connection from other modules by importing it. Could I just create a single Thingy and connect instance for the app then import these:
connection_reference.py
`
`
module_2.py: `
where I want to use Thingy for Thingy defined objects
module_3.py
`
in 90% of my app I want to use pymongo directly
`