|
1 |
| -from typing import Optional |
2 |
| - |
3 | 1 | from motor.motor_asyncio import AsyncIOMotorClient
|
4 | 2 |
|
5 | 3 | from config import settings
|
6 | 4 |
|
7 |
| -# Global variables for MongoDB client and database |
8 |
| -client = None |
9 |
| -db = None |
10 |
| - |
11 |
| -# Initialize the database connection |
12 |
| - |
13 |
| - |
14 |
| -async def init_db(): |
15 |
| - global client, db |
16 |
| - # Create a new MongoDB client |
17 |
| - client = AsyncIOMotorClient(settings.MONGODB_URI) |
18 |
| - # Select the database |
19 |
| - db = client[settings.DB_NAME] |
20 |
| - |
21 |
| -# Close the database connection |
22 |
| - |
23 |
| - |
24 |
| -async def close_db(): |
25 |
| - global client |
26 |
| - if client: |
27 |
| - # Close the MongoDB client connection |
28 |
| - client.close() |
29 |
| - |
30 |
| -# Get a specific collection from the database |
31 |
| - |
32 |
| - |
33 |
| -async def get_collection(name): |
34 |
| - return db[name] |
35 |
| - |
36 |
| -# Update a single document in the collection |
37 |
| - |
38 |
| - |
39 |
| -async def update_one(collection_name, filter, update, upsert=False): |
40 |
| - collection = await get_collection(collection_name) |
41 |
| - # Update a document in the collection with the specified filter and update |
42 |
| - return await collection.update_one(filter, update, upsert=upsert) |
43 |
| - |
44 |
| -# Find documents in the collection with optional sorting, skipping, and limiting |
45 |
| - |
46 |
| - |
47 |
| -async def find(collection_name, filter={}, sort=None, skip=0, limit=0): |
48 |
| - collection = await get_collection(collection_name) |
49 |
| - cursor = collection.find(filter) |
50 |
| - if sort: |
51 |
| - cursor = cursor.sort(sort) |
52 |
| - if skip: |
53 |
| - cursor = cursor.skip(skip) |
54 |
| - if limit: |
55 |
| - cursor = cursor.limit(limit) |
56 |
| - # Convert the cursor to a list with the specified limit |
57 |
| - return await cursor.to_list(length=limit) |
58 |
| - |
59 |
| -# Find a single document in the collection |
60 |
| - |
61 |
| - |
62 |
| -async def find_one(collection_name, filter): |
63 |
| - collection = await get_collection(collection_name) |
64 |
| - # Find one document that matches the filter |
65 |
| - return await collection.find_one(filter) |
66 |
| - |
67 |
| -# List all collection names in the database |
68 |
| - |
69 |
| - |
70 |
| -async def list_collection_names(): |
71 |
| - return await db.list_collection_names() |
72 |
| - |
73 |
| -# Count the number of posts matching the query and optional city |
74 |
| - |
75 |
| - |
76 |
| -async def count_posts(collection_name: str): |
77 |
| - collection = await get_collection(collection_name) |
78 |
| - return await collection.count_documents({'_id': {'$ne': 'last_update'}}) |
79 |
| - |
80 |
| -# Get the latest posts sorted by post date |
81 |
| - |
82 | 5 |
|
83 |
| -async def get_latest_posts(limit: int): |
84 |
| - collection = await get_collection('posts') |
85 |
| - # Find the latest posts sorted by post date in descending order |
86 |
| - cursor = collection.find().sort([('postDate', -1)]).limit(limit) |
87 |
| - # Convert the cursor to a list with the specified limit |
88 |
| - return await cursor.to_list(length=limit) |
| 6 | +class Database: |
| 7 | + client = None |
| 8 | + db = None |
| 9 | + |
| 10 | + @classmethod |
| 11 | + async def connect(cls): |
| 12 | + cls.client = AsyncIOMotorClient(settings.MONGODB_URI) |
| 13 | + cls.db = cls.client[settings.DB_NAME] |
| 14 | + |
| 15 | + @classmethod |
| 16 | + async def close(cls): |
| 17 | + if cls.client: |
| 18 | + cls.client.close() |
| 19 | + |
| 20 | + @classmethod |
| 21 | + async def get_collection(cls, name): |
| 22 | + return cls.db[name] |
| 23 | + |
| 24 | + @classmethod |
| 25 | + async def update_one(cls, collection_name, filter, update, upsert=False): |
| 26 | + collection = await cls.get_collection(collection_name) |
| 27 | + return await collection.update_one(filter, update, upsert=upsert) |
| 28 | + |
| 29 | + @classmethod |
| 30 | + async def find(cls, collection_name, filter={}, sort=None, skip=0, limit=0): |
| 31 | + collection = await cls.get_collection(collection_name) |
| 32 | + cursor = collection.find(filter) |
| 33 | + if sort: |
| 34 | + cursor = cursor.sort(sort) |
| 35 | + return await cursor.skip(skip).limit(limit).to_list(length=limit) |
| 36 | + |
| 37 | + @classmethod |
| 38 | + async def find_one(cls, collection_name, filter): |
| 39 | + collection = await cls.get_collection(collection_name) |
| 40 | + return await collection.find_one(filter) |
| 41 | + |
| 42 | + @classmethod |
| 43 | + async def list_collection_names(cls): |
| 44 | + return await cls.db.list_collection_names() |
| 45 | + |
| 46 | + @classmethod |
| 47 | + async def count_posts(cls, collection_name: str): |
| 48 | + collection = await cls.get_collection(collection_name) |
| 49 | + return await collection.count_documents({'_id': {'$ne': 'last_update'}}) |
0 commit comments