Hello community,
I look at the right way to handle mongoose schema migrations. I read lots of things, including
https://groups.google.com/forum/#!topic/mongoose-orm/6twzh-4d6q8 , and saw Aaron response. I'm really seduced by the lazy migration way of doing things, but unfortunately the middleware doesn't seem to provide enough hooks. Let's take an example. I got a schema like:
{
title: String,
updates: [Date],
schemaVersion: 1
}
and then we decide to add to the updates the id of the user. So my version 2 is:
{
title: String,
updates: [{date: Date, author: ObjectId}],
schemaVersion: 2
}
If I use the schema.pre('init') hook (which seems the first available hook in the lifecycle of the Document), the mongoose document has already been hydrated. So a document {title: 'Hello world', updates: [234234234], schemaVersion: 1} will be hydrated as: {title: 'Hello world, updates:[], schemaVersion: 1}.
I saw that in the pre('init') hook, the 2nd argument is the mongodb object. So I can cheat and lookup into it to correctly set the properties, according to the new schema. However, using this strategy, I should code migrations from any schema version to any other one, so if I have schema versions 1, 2 and 3, I should have migration from 1=>2, 1=>3, and 2=>3... That's complex.
I could have missed something, but for now it seems to me that the best way to handle those lazy migrations would be to have a hook before mongoose hydratation, that takes the mongodb document and returns the perhaps modfied document. Is this kind of thing stupid, or cool ?
Thanks,
Mickael