mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from bson import ObjectId
|
|
from pymongo import MongoClient
|
|
from config.db import MONGO_HOST, MONGO_PORT, MONGO_DB
|
|
|
|
|
|
class DbManager(object):
|
|
def __init__(self):
|
|
self.mongo = MongoClient(host=MONGO_HOST, port=MONGO_PORT)
|
|
self.db = self.mongo[MONGO_DB]
|
|
|
|
def save(self, col_name: str, item, **kwargs):
|
|
col = self.db[col_name]
|
|
col.save(item, **kwargs)
|
|
|
|
def remove(self, col_name: str, cond: dict, **kwargs):
|
|
col = self.db[col_name]
|
|
col.remove(cond, **kwargs)
|
|
|
|
def update(self, col_name: str, cond: dict, values: dict, **kwargs):
|
|
col = self.db[col_name]
|
|
col.update(cond, {'$set': values}, **kwargs)
|
|
|
|
def update_one(self, col_name: str, id: str, values: dict, **kwargs):
|
|
col = self.db[col_name]
|
|
col.find_one_and_update({'_id': ObjectId(id)}, {'$set': values})
|
|
|
|
def list(self, col_name: str, cond: dict, skip: int = 0, limit: int = 10, **kwargs):
|
|
col = self.db[col_name]
|
|
data = []
|
|
for item in col.find(cond).skip(skip).limit(limit):
|
|
data.append(item)
|
|
return data
|
|
|
|
def get(self, col_name: str, id: str):
|
|
col = self.db[col_name]
|
|
return col.find_one({'_id': ObjectId(id)})
|
|
|
|
def count(self, col_name: str, cond):
|
|
col = self.db[col_name]
|
|
return col.count(cond)
|
|
|
|
|
|
db_manager = DbManager()
|