New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'mapReduce' support to Model #75
Comments
Hi RagibHasin, thanks for pointing that out - I think it'd make a great addition if we can flesh out a decent API for the approach. Specifically, Before we get started implementing anything, let's think about how we'd like to work with Personally, I'd love to be able to completely ignore the fact that there's an intermediary collection in most cases and simply run something like the following: core.Orders.mapReduce(function() {
emit(new Date(this.date.valueOf() % 86000000), this.total)
}, function(key, values) {
return Array.sum(values)
}, { completed: true }).forEach(grossIncomeByDay => {
console.log("%s - $%d", grossIncomeByDay._id, grossIncomeByDay.value);
}); Of course, that approach doesn't map well to situations where you wish to memoize the results of your Maybe something like this instead? interface MapReducedDocument {
_id: Date;
value: number;
}
@Iridium.Collection("map_reduced")
@Iridium.MapReduce<Order>(function() {
emit(new Date(this.date.valueOf() % 86000000), this.total)
}, function(key, values) {
return Array.sum(values)
})
class MapReduced extends Iridium.Instance<MapReducedDocument, MapReduced> {
_id: Date;
value: number;
}
core.Orders.mapReduce(MapReduced, { completed: true });
core.MapReduced.find().forEach(grossIncomeByDay => {
console.log("%s - $%d", grossIncomeByDay._id, grossIncomeByDay.value);
}); What are your thoughts there? The latter one seems like the better solution for implementation purposes, but it obviously requires quite a bit more effort on the developer's part. The flip side of that is that they can interact with that collection using all the standard Iridium collection functionality and treat documents in it as normal Iridium instances if they wish. |
The latter obviously seems a better option. I have made some progress in inline version of mapReduce but could not figure out how collection could be returned. Thanks. I am on it. Hopefully will make the PR soon. BTW, I've found this project just today. So I may need help. At this moment... |
So Iridium doesn't really move collections around, instead we've got a class which maps to a collection and provides type-safe methods through which to access it (as well as being aware of the wrappers we use and knowing how to do validation, hooks etc). That wrapper is the My recommendation, however, is to avoid returning a raw MongoDB Collection object because it immediately breaks the chain of type-safe return values that Iridium strives so hard to maintain. While it will likely work, it's not really a great developer experience. As for Let me know if there's anything you'd like a hand with, there's plenty of examples for how to build decorators in the |
Should I commit to |
|
I'm thinking if we make Like interface MapReducedDocument {
_id: Date
value: number
}
@Iridium.Collection("map_reduced")
class MapReduced extends Iridium.Instance<MapReducedDocument, MapReduced> {
_id: Date
value: number
}
core.Orders.mapReduce<MapReduced>(
function () {
emit(new Date(this.date.valueOf() % 86000000), this.total)
},
function (key, values) {
return Array.sum(values)
}, { query: { completed: true } }).
then(reducedModel => {
reducedModel.find().forEach(grossIncomeByDay => {
console.log("%s - $%d", grossIncomeByDay._id, grossIncomeByDay.value)
})
}) Besides with this manner inline mapReduce will be possible also by omitting the generic parameter. I think it will feel more natural. |
The tricky part is that the generic parameter isn't accessible at runtime, so you wouldn't be able to pass an actual instance of The next part is that a model like As a final point, we try to avoid dynamically generating models in Iridium because (while it is possible to do so) it hides type information during development time and makes it harder to reason about how the code works. I'd be somewhat worried that by returning a model from |
I've just pushed out v7.2.0 with your PR, thanks for the contribution and please let me know if there's anything that should be improved in the docs supporting it. |
At this moment nothing. Thanks for being so informative and helpful. I am feeling grateful to be able contribute to community for the first time. Collaborating with you was just awesome. And finally, thanks for this v7.2.0 release. |
mapReduce
is a often used function ofcollection
and other ODMs support it already.It will be great to see it in this project. I'm ready to work out a PR for this (how to instantiate a Model form a MongoDB native collection?)
The text was updated successfully, but these errors were encountered: