4strid / nekodb

Tiny ODM for MongoDB/NeDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support Model.update

4strid opened this issue · comments

I think this involves writing a MongoDB update query processor, so not an easy fix. But worth doing, certainly.

I've given this some thought, and we could implement it in layers of varying efficiency. $set operations are easy to type check; that wouldn't require pulling down the model before calling update. The mathematical update operators would only need to be pulled down if there were relevant validators. ($mul might result in a value too small or too large etc.) $push, etc. are easy to type check, but we would have to pull down the model if there was a notEmpty validator.

Basically, we can perform some update operations flying blind, but others would require a find() before we were sure the update would not result in invalid values

In the spirit of having the user be free from writing update queries by hand, update could also take a function that looks like

Model.update(instance => {
   instance.field = 'new value'
   instance.numField.$mul(42)
   instance.dateField.$currentDate()
})

where instance is a special instance on which you can perform whatever updates you like. I'd then write the query from the instance's _updates object.