minimongo icon indicating copy to clipboard operation
minimongo copied to clipboard

Support for automatic DBRef field dereferencing via wrapper types

Open superbobry opened this issue 14 years ago • 6 comments

Needs further details, in my opinion.

superbobry avatar Mar 14 '11 14:03 superbobry

What I was thinking here is:

When an object is read from the DB, we would like to go through all the fields (likely via an overridden setitem method on Model) and see if the type of the field being set is a pymongo DBRef type. If so, then construct a custom wrapper object that, when accessed (or via a special method) would automatically dereference and load the DBRef-valued field.

This would work very similarly to pymongo.son_manipulator.AutoReference(db) (see docs on http://api.mongodb.org/python/1.9%2B/api/pymongo/son_manipulator.html)

This task requires some thought about the different paradigm's of embedded documents, and since I haven't had a need for this yet, I haven't thought about it.

slacy avatar Mar 14 '11 20:03 slacy

Do we allow two different models work with the same collection? because if yes -- there's no way we can do dereferencing automatically.

class Foo(Model):
    class Meta:
        collection = "baz"


class Bar(Model):
    class Meta:
        collection = "baz"


# Now how do we dereference DBRef(_id="...", collection="baz")?

superbobry avatar Mar 18 '11 18:03 superbobry

I would like to allow 2 document types in the same collection, as I suspect that's what some people are doing today (without minimongo) and I want to make sure I support that use case.

I guess you could annotate (via some members on the Meta) what fields should get what document types, but that seems sloppy. I'm open to any other ideas. :) I'd hate to have to store our own DBRef type, but maybe a specially generated type field would be okay? I'm not sure, haven't thought about this enough yet. :)

slacy avatar Mar 24 '11 19:03 slacy

Actually, I'm thinking restricting the case, when two 2 document types are stored in a single collection -- seems like an easy and simple solution. At least for now.

superbobry avatar Mar 25 '11 13:03 superbobry

Yeah, restricting that case by default would be fine with me, although there should be something you can put in Meta to allow multiple documents in the same collection. Something like:

class Meta 
    multi_document_collection = True 

I'm not sure what the right name there is and am certainly open to suggestions or alternates. :)

slacy avatar Mar 25 '11 16:03 slacy

Hey, I just implemented and pushed something that could be used for exactly this case. It's a way to map fields from one type to another type, pretty much arbitrarily. The syntax isn't 100% complete, but the functionality is there. Check the unit tests and let me know what you think.

slacy avatar Mar 30 '11 22:03 slacy