comments and README.md
This commit is contained in:
18
app/db.py
18
app/db.py
@@ -1,10 +1,15 @@
|
||||
from flask import g
|
||||
from werkzeug.local import LocalProxy
|
||||
from pymongo import MongoClient
|
||||
from .config import settings
|
||||
|
||||
|
||||
def connect_db():
|
||||
"""
|
||||
Creates the connection to the MongoDB using the config file data.
|
||||
Thos function is used by flask application as well as the scraper script
|
||||
|
||||
:return: connection to the collection in the mongodb
|
||||
"""
|
||||
client = MongoClient(settings["mongodb_uri"])
|
||||
db = client[settings["mongodb_db"]]
|
||||
collection = db[settings["mongodb_collection"]]
|
||||
@@ -12,12 +17,17 @@ def connect_db():
|
||||
|
||||
|
||||
def disconnect_db(conn):
|
||||
"""
|
||||
Disconnects open connection
|
||||
:param conn: open db connection
|
||||
"""
|
||||
conn.database.client.close()
|
||||
|
||||
|
||||
def get_db():
|
||||
"""
|
||||
Configuration method to return db instance
|
||||
Get collection instance for flask application. If no instance stored in global variables, then create connection
|
||||
and store it in g
|
||||
"""
|
||||
collection = getattr(g, "_database", None)
|
||||
|
||||
@@ -25,7 +35,3 @@ def get_db():
|
||||
collection = g._database = connect_db()
|
||||
|
||||
return collection
|
||||
|
||||
|
||||
# Use LocalProxy to read the global db instance with just `db`
|
||||
db = LocalProxy(get_db)
|
||||
|
||||
Reference in New Issue
Block a user