-
Notifications
You must be signed in to change notification settings - Fork 15
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
save without update the entity #46
Comments
@Alnyz I've made sorta what you are talking about while maintaining backward compatibility. Are the changes in 7459ffc what you meant?
This part doesn't make sense (to me atleast), because def save(self, model: T, **kwargs) -> Union[InsertOneResult, UpdateResult]:
"""
Save entity to database. It will update the entity if it has id, otherwise it will insert it.
:param model: Model to save
:param kwargs: kwargs for pymongo insert_one or update_one
:return: Union[InsertOneResult, UpdateResult]
"""
document = self.to_document(model)
if model.id:
mongo_id = document.pop("_id")
return self.get_collection().update_one(
{"_id": mongo_id}, {"$set": document}, upsert=True, **kwargs # <-----
)
.... |
Yes, that's what I meant. In your suggested change, you used
The main point of this section is to raise an error if 'entity' already exists in the database, eliminating the need to use I believe this step offers better performance since there's no need to query first to check if def save(self, model: T, **kwargs) -> InsertOneResult:
"""
Save entity to database. It will update the entity if it has id, otherwise it will insert it.
:param model: Model to save
:param kwargs: kwargs for pymongo insert_one or update_one
:return: Union[InsertOneResult, UpdateResult]
:raise: pymongo.errors.DuplicateKeyError if id already exists in db
"""
document = self.to_document(model)
return self.get_collection().insert_one(document, **kwargs)
.... |
Hello, I recently discovered this project and found it interesting and useful for certain cases. However, upon examining the source code, I observed that the
save
method updates an existing entity with its _id/id. I suggest raising an error if the existingid
already exists and introducing a separate method, likeupdate
, for modifying existing entities.This approach eliminates the need for a
query
to check whether theid
already exists, preventing potential errors when developers intend to save the latest data.The text was updated successfully, but these errors were encountered: