34 lines
776 B
Python
34 lines
776 B
Python
from functools import cached_property
|
|
|
|
import pymongo
|
|
|
|
import settings
|
|
|
|
|
|
class Mongo:
|
|
def __init__(self):
|
|
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
|
|
self.client = pymongo.MongoClient(url)
|
|
self.database = self.client.get_database("b-jokes")
|
|
self.jokes_collection.create_index([
|
|
("id", 1)
|
|
])
|
|
self.events_collection.create_index([
|
|
("date", 1),
|
|
("event", 1)
|
|
])
|
|
|
|
def __getitem__(self, item):
|
|
return self.database.get_collection(item)
|
|
|
|
@cached_property
|
|
def jokes_collection(self):
|
|
return self["jokes"]
|
|
|
|
@cached_property
|
|
def events_collection(self):
|
|
return self['events']
|
|
|
|
|
|
mongo = Mongo()
|