-
Notifications
You must be signed in to change notification settings - Fork 34
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
Make collection helpers work even if other transforms are present #59
base: master
Are you sure you want to change the base?
Conversation
Hey @ephemer it's intentional to not extend upon any existing transform function to avoid potentially very difficult to trace bugs. I'd suggest manually adding methods by supplying your own transform function when initialising a collection, e.g: Author = function(doc) { return _.extend(this, doc); };
Author.prototype.fullName = 'Charles Darwin';
Authors = new TAPi18n.Collection('authors', {
transform: function(doc) { return new Author(doc); }}); The tapi18n API really should be a mixin rather than a constructor IMO. |
Hey, I agree about the TAPi18n API Not sure what kind of bugs you could avoid by manually adding the transform compared to just doing it in The only thing I could think of is that you might get into some kind of weird reactivity loop of the helpers are accessing each other in a strange way. That's more of a meteor/tracker issue than one specific to the helpers implementation though. |
Well you wouldn't avoid bugs by doing that, but since collection-helpers simply won't work, you can instead do it manually :) One bug example would simply be overwriting a method, if you had a collection with an existing transform that added a method called It's one of those things where there's a small chance of it being a problem but I'm still leaning towards it being more correct to not allow it for the sake of this package and those who use it. By applying the transform on your own without the use of this package, you're then on your own when it comes to any potential conflicts. |
Yes, overriding one transform's methods with another's with the same name could be a problem. I think it's a shame to miss out on this helper+transform functionality entirely though. What if I update the PR to check the transformed document's prototype to see if the methods you want to add already exist and throw an error in that case? Specifically that would happen instead of a blind |
Yeah I've considered that, though I'm not entirely convinced that's the way to go either. To be honest it seems it's pretty rare to have a conflict like the one with tap-i18n-db. I think this is really the only one I can recall. If its API could apply its own transform after the collection has been instantiated (mixin) it would solve the problem. |
Books = new Mongo.Collection('books');
Books.helpers({
// ...
});
TAPi18n(Books); |
I've updated the pull request with checks to ensure helpers will never overwrite pre-existing transforms, with updated and expanded tests. I've also published |
I'm interested in being able to define my own transforms, as well. For instance, I have a collection that needs to store a Mongo query object, which will sometimes include keys beginning with '$'. This is not allowed by Mongo, so I've been using hooks to serialize ( I want this to occur invisibly, without having to manually invoke a helper on insert or on fetch. Unfortunately, I can't find a way to make this work with this package. Additionally, I'm getting errors from ephemer's collection-helpers package mentioned above:
Here's the transform I'd like to perform:
|
Hey @mtchllbrrn there's no easy way to do that with this package however, it's simple enough to achieve. Try the following (with this package): Test = new Mongo.Collection('test');
Test.helpers({
foo: 'bar'
});
Test._transform = doc => {
doc.query = JSON.parse(doc.query);
return new Test._helpers(doc);
}; |
Thanks for the tip, seems to work a charm 👌 |
@mtchllbrrn great! |
@dburles: I spoke too soon. It looks like the operation is occurring as expected, but I receive the following error in the server console:
I'm using exactly the same syntax as what you posted above. |
@mtchllbrrn Hmm try see if you can reproduce the bug in an isolated application, otherwise there's too many variables to consider |
I'd really like to see this solved. It plays a significant role when I try to fix the problem of identifying which subscription publicated a document. See meteor/meteor-feature-requests#183 |
We are using TAPi18nDB, which uses transforms, and we also want to continue using collection helpers.
Here's a way of having both. Cheers.