Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nosql.py #41

Closed
wants to merge 1 commit into from
Closed

Update nosql.py #41

wants to merge 1 commit into from

Conversation

m7mdhka
Copy link

@m7mdhka m7mdhka commented Jan 28, 2025

Currently, there's a redundant UUID to string conversion happening in our NoSQLBaseDocument class. The to_mongo method performs UUID to string conversion even though this conversion is already handled by the overridden model_dump method.

Current flow:

  1. model_dump converts all UUID values to strings
  2. to_mongo calls model_dump
  3. to_mongo then unnecessarily converts UUID values to strings again

Changes:

  • Remove the redundant UUID conversion loop in to_mongo

Before:

def to_mongo(self: T, **kwargs) -> dict:
    parsed = self.model_dump(...)
    if "_id" not in parsed and "id" in parsed:
        parsed["_id"] = str(parsed.pop("id"))
    for key, value in parsed.items():  # Redundant conversion
        if isinstance(value, uuid.UUID):
            parsed[key] = str(value)
    return parsed

After:

def to_mongo(self: T, **kwargs) -> dict:
    parsed = self.model_dump(...)
    if "_id" not in parsed and "id" in parsed:
        parsed["_id"] = str(parsed.pop("id"))
    return parsed

@m7mdhka m7mdhka closed this by deleting the head repository Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant