-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
Each documents of collection can attach some default values while insert or update #15
Comments
I'm not sure what your issue is. However, I have some suggestions for you:
|
new Date() to save a time object, and i always use this with yarp timeago to display timeago feature Date.now() to sort if it is safe to use userId instead Meteor.userId() inside hook i'll change and if you don't insert a username how do u know who created it ? most time user can't change their login account name. |
@crapthings, first, for the various dates and username stuff, I recommend instead that you only store the bare minimum (one datestamp, the creatorId) and then rely on a Second, on having some way to have collections always have some default properties added, this would be the job of an intermediary layer, something like a Collection Manager. Currently, you are probably approaching collection creation the same way the Meteor examples do, i.e. creating a global variable: The end result would allow for something like this: // Create collection
var projects = CollectionManager.create("projects");
// Retrieve collection (if already created like above)
var projects = CollectionManager.get("projects");
projects.insert(...);
projects.findOne(...);
// Opt-in to defaults system:
CollectionManager.includeDefaults("projects"); What Let me know if you have any questions |
I should mention that to achieve a transform on Meteor.users, you'll need to do something like this: var transform = function (doc) { return new YourUserConstructorHere(doc); };
var find = Meteor.users.find;
var findOne = Meteor.users.findOne;
Meteor.users.find = function (selector, options) {
selector = selector || {};
options = options || {};
return find.call(this, selector, _.extend({transform: transform}, options));
};
Meteor.users.findOne = function (selector, options) {
selector = selector || {};
options = options || {};
return findOne.call(this, selector, _.extend({transform: transform}, options));
}; IMPORTANT: Collection hooks' EDIT: Fixed zero-length arguments issue: #22 (comment) |
rely on a transform to augment your document with extra properties. Is this only work with client side ?
when use transform inside public with find, and it doesn't work. i've tried this
|
Transform works anywhere. Although I'm not fluent in CoffeeScript, it looks like when you extend project, you may be overwriting some of its fields with those from users (like _id). Perhaps don't extend, instead add a property like getUser which would be a function that performs the findOne. This would maintain reactivity in case the user changes some of his properties Also consider applying the transform at the collection level instead. |
transform doesn't work this way.
|
Almost. Now that I have a keyboard handy, I'll give you an example instead of little hints! Try this: Items = new Meteor.Collection("items", {
transform: function (doc) {
...
return doc;
}
});
System = new Meteor.Collection("system", {
transform: function (doc) {
return _.extend(doc, {
getItem: function () {
return Items.findOne({_id: doc.itemId});
}
});
}
}); The key takeaway is that a transform defined at collection-level affects all find, findOne, allow and deny for this collection. You can override this transform by specifying it as an option in find/findOne/allow/deny of course, but when defined at the collection level, it's effectively the base transform. HTH EDIT: I'm not totally sure if the reactivity chain will be maintained with the getItem function. I'd have to test it out -- but I get this feeling it won't make a difference. So if you prefer, you can replace getItem with: |
After tried, collection transform doesn't work this way.
but this can work with client.
|
This sounds like an issue in your code. Transform absolutely works in both client and server. Try in a new project and test it out. Using _transform is not necessary |
@matb33 Do you have any sample CollectionManager code to share? I think this is exactly what I'm looking for and would be interested to see how you implemented. |
@scottsimock unfortunately I wrote that only as an example, but it shouldn't be too hard to write. That being said, there are a ton more options around these days. There may be a smart package that does this by now |
@matb33 Thanks for the quick reply. Believe I'm overthinking the CollectionManager. You're just suggesting its code that calls new Mongo.Collection plus other collection setup code like collection-hooks. I was thinking it was a superclass above Mongo.Collection. Thanks again! |
i always add createdAt, createdBy, timestamp and such keys on each 'collection.before.insert' like
but some collections do not need attaching
Could we add some feature like we can allow some collections do attach default values ?
maybe something like
The text was updated successfully, but these errors were encountered: