It is time to write down some changes I plan for v0.6. The code is
already written and it's a matter of time to be merged. All features
are not listed and I'd like to share only features that changes the
way documents are stored into the database.
When I wrote MongoKit for the first time, pymongo was not advanced as
it is today. Now it is time to implement and use the features pymongo
is bringing. For instance, MongoKit will using `as_class` feature
which help to wrap the data into a document.
Another feature MongoKit will use is the `SONManipulator` class. It
help to make some change into the document when it is saved or
retrieved. Three features will use the SONManipulator class :
Autorefs, CustomTypes and i18n. This bring some major advantages :
* It is much more maintainable. As the conversion is managed via
pymongo, there are less code lines in MongoKit and the code is
clearer. So : less bug, faster, more maintainable and still KISS :-)
* Autorefs, CustomTypes and i18n could be used even if you choose to
use directly pymongo. This mean that you will be able to have faster
code with all good features you like.
* You won't have to enable autorefs anymore : MongoKit will detect on
the fly if it need to convert Document to DBRef automatically.
Well, this is great but there is a draw back : some changes must be
made into the data.
As pymongo is blind on your data (pymongo doesn't know that you are
dealing with a User or a MyDoc document), we have to tell him what
type of data he is dealing with. Here are the breaking changes for
v0.6:
== Autorefs ==
=== migration ===
In order to tell pymongo in what type of Document he must wrap the
data, we have to add the "obj" attribute to the DBRefs.
All DBRefs shoud now embed the value `obj` which describes the name of
the Document class to wrap with.
DBRef(collection="test", database="test", id="test", obj="MyDoc")
=== Breaking change : saving an embed reference ===
If you modify a reference into a document, you should call `save` on it:
embed = con.test.mongokit.Embed()
embed['foo'] = 3
embed.save()
doc = con.test.mongokit.MyDoc()
doc['embed'] = embed
doc.save()
doc['embed']['foo'] = 4
doc['embed'].save()
Otherwise, the embed document won't be update
== Custom Types ==
Same way for the custom types, we have to store the type of
CustomTypes the data is stored.
Let's say that we made a CustomDate which store raw string and convert
it in python datetime. The document will be stored like this:
{'_id': 'mydoc', 'date':{'_custom': 'CustomDate', 'v': '03-08-2010'}}
There is no API changes for CustomTypes
== i18n ==
Idem for i18n, before 0.6, i18n value was store in the database like this :
{'foo':{'fr':'Salut', 'en': 'Hello'}} # in python
{'foo':[{'lang':fr', 'name':'Salut'}, {'lang':en',
'name':Hello'}]} # in mongodb
with 0.6 values into the database look like this::
{'foo':{'_i18n':[{'lang':fr', 'name':'Salut'}, {'lang':'en',
'name':'Hello'}]}} # in mongodb
There is no API change for i18n.
I know how it is hard to migrate data but I think it worth it.
MongoKit will be much cleaner, faster and stable. Plus, if you wish to
leave mongokit and come back to pymongo for good, you don't lose your
custom types and i18n.
Of course, if you have any suggestions, I'd be glad to listen to them.
This will make MongoKit better.
I wish you a good week-end,
N.