Проверка на существование БД/коллекции

This commit is contained in:
Lulzette 2021-09-27 02:19:19 +03:00
parent 85a860eb1a
commit 755e7c5ce4

View File

@ -5,6 +5,7 @@ import pymongo
# TODO: auth to /admin # TODO: auth to /admin
# TODO: timestamps to posts # TODO: timestamps to posts
# TODO: author to posts and multiple users # TODO: author to posts and multiple users
# TODO: add bottle's params to config
class Config: class Config:
@ -23,9 +24,13 @@ class Config:
self.readConfig() self.readConfig()
mongoclient = pymongo.MongoClient(self.host, self.port) mongoclient = pymongo.MongoClient(self.host, self.port)
if self.dbname not in mongoclient.list_database_names():
print('DB not found, creating')
database = mongoclient[self.dbname] database = mongoclient[self.dbname]
# TODO: Create table if not exists # TODO: Create table if not exists
if 'posts' not in database.list_collection_names():
print('Table not fount, creating')
posts = database['posts'] posts = database['posts']
self.posts = posts self.posts = posts