Skip to content

Commit

Permalink
feat: ttl index in mongodb collections (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjibansg committed Feb 22, 2024
1 parent 5bc9b7c commit ded981b
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/components/shareable.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import datetime
import os

from bson.objectid import ObjectId
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel
from pymongo import ASCENDING

DEFAULT_MONGO_URL = "mongodb://localhost:27017"

Expand All @@ -27,6 +29,10 @@ def __init__(self):
def initialize(self):
database = self.client["plans"]
collection = database["links"]

# TTL index in the createdAt field to expire record after a week
collection.create_index([("createdAt", ASCENDING)], expireAfterSeconds=604800)

return collection

async def get_record(self, collection, id):
Expand All @@ -37,6 +43,7 @@ async def add_record(self, collection, data):
data = {
"json_string": data.json_string,
"validator_overrides": data.validator_overrides,
"createdAt": datetime.utcnow()
}
result = await collection.insert_one(data)
return str(result.inserted_id)
Expand Down

0 comments on commit ded981b

Please sign in to comment.