So I'm using Motor in my application and I'm looking to create helper functions for interacting with the database, I initially looked into using MongoTurbine but I'm biased against using an ORM, although making routine functions easier to call would be nice.
Let's take an upsert
for example:
I could write an upsert using the following:
await collection.update_one({'key': value}, {'set': {'key': value}}, upsert = True)
But it would be easier to use:
await collection.upsert({'key': value}, {'set': {'key': value}})
Or even a set
method:
await collection.set({'key': value}, {'key': value})
I have quite a few methods like these and some more involved but if anyone could point me in the right direction that'd be awesome!
The trigger that made me ask this question was I saw there was a document_class
argument in the AsyncIOMotorDatabase which allows you to specify the return type of documents. But no real easy way to specify a custom collections class.
I know all of this can be done using the standard conventions, I'm only trying to make things easier as sometimes there can be some long filter, update, projection dicts and I'm just trying to implement my own convention.
edit
Basically to put more context into this I'm looking to make custom functions to reduce the amount of dicts I need to write over and over. Take updating a user's point's for example; I'd like to be able to write a sort of "wrapper" function around the update_one function to improve readability and usability across multiple modules.
Ex:
async def update_set(key, value, **kwargs):
self.update_one({key: value}, {'$set': kwargs})
await db.users.update_set('username', 'johndoe', foo = 1, bar = 'test')
#vs
await db.users.update_one({'username': 'johndoe'}, {'$set': {'foo': 1, 'bar': 'batz'}})