Skip to content

Commit

Permalink
refactor: Event CRUD to use eventCollections from MongoDB Atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
luthfiarifin committed Sep 26, 2024
1 parent a2e8944 commit 2ef18b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
21 changes: 17 additions & 4 deletions app/events/crud.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import os

from dotenv import load_dotenv

from bson import ObjectId
from langchain_core import documents
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain_huggingface import HuggingFaceEmbeddings
from pymongo import MongoClient

load_dotenv()

DB_NAME = os.getenv("MONGO_API_DB_NAME")
COLLECTION_NAME = "events"
client = MongoClient(os.getenv("MONGO_URI"))
db = client[DB_NAME]
eventCollections = db[COLLECTION_NAME]

vectorstore = MongoDBAtlasVectorSearch.from_connection_string(
os.environ["MONGO_URI"],
Expand Down Expand Up @@ -48,7 +59,9 @@ def update_event_vector(id: ObjectId, updatedFields):
Update the event vector in the MongoDB Atlas database.
"""

return vectorstore._collection.update_one(
{"_id": id},
{"$set": updatedFields},
)
event = eventCollections.find_one({"_id": id})
updated_event = {**event, **updatedFields}

vectorstore._collection.delete_one({"_id": id})

return insert_event_vector(updated_event)
18 changes: 2 additions & 16 deletions app/events/watch.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import os

from dotenv import load_dotenv

from urllib.parse import urljoin

from pymongo import MongoClient
from bson.json_util import dumps

from crud import insert_event_vector, update_event_vector

load_dotenv()
from crud import insert_event_vector, update_event_vector, eventCollections

DB_NAME = os.getenv("MONGO_API_DB_NAME")
COLLECTION_NAME = "events"
client = MongoClient(os.getenv("MONGO_URI"))
db = client[DB_NAME]
MONGODB_COLLECTION = db[COLLECTION_NAME]

change_stream = MONGODB_COLLECTION.watch()
change_stream = eventCollections.watch()
for change in change_stream:
if change["operationType"] == "insert":
insert_event_vector(change["fullDocument"])
Expand Down

0 comments on commit 2ef18b2

Please sign in to comment.