Collection before.insert hook, AutoForm Hooks or autoValue - what's best way for auto generated fields

I have collection with a few auto generated fields (generating them on insert/creation) that could be edited later.

I’m using AutoForm with SimpleSchema and Collection2 for From generation.

Where and when it’s better place to generate data for such fields and put into database?

  1. Write needed code using autoValue directly in schema.
  2. Use AutoForm Hooks.
  3. Do it using matb33:collection-hooks before/after.insert hooks.

What’s are pluses and minuses in each case?

I’d say autoValue since it already provides you a context where you define everything about your schema and makes sure everything happens at once or throws an error, eliminating the possibility to get incomplete or perhaps orphaned tasks that you may get with form or collection hooks.

After all, neither meteor, nor any of those tools provide you with a valid transaction context so it is best to keep a unit of work as confined as possible.

Hopefully someone else will find this useful. I’m having the same issues atm mainly because I want to find a pattern for having server validated defaults. What I mean by that is:

  1. autoValue is purely client side, so even if you did put this in the schema, you’d have to overwrite it on the server on insert. (this is wrong, see next post)
  2. AutoForm hooks have the same drawback because it’s a client side hook.
  3. collection-hooks should work on both but there are considerations to be taken in for that:
    https://forums.meteor.com/t/meteor-collection-hooks-confused

With autoValue, we had a strange bug where on every update of a post, our dateCreated field was being updated and realised it was because we had a autoValue setting that to moment().format() and intended it only for a insert. That’s probably our fault but it’s alot clearer setting it in a meteor method Post.create rather than straight in the schema for that case.

autoValue is not purely client side. Collection2 and simple-schema work wherever you define them and if you define your schema at the server and if you are initiating the insert/update from server code, than you also have this.isFromTrustedCode available to you (collection2 feature).

So if I shared a schema on the server/client and have a autoValue set and using autoforms. Does that mean:

  1. Client side doc sets autoValue, sees the doc immediately (Latency Compensation)
  2. Form submission will have the client side value (I’d have to make sure server doesn’t validate this)
  3. Document insert/update will update the field with the server autoValue
  4. Pub/sub updates minimongo with server values

It sounds to me like if I wanted to do some simple crud, I’ll use autoValues, collection hooks and automate frontend with autoforms, but if I need a bit more complex logic, I’ll be better off with a method, no autoValues, no hooks, and probably just a subset of the server schema for autoforms.

2 Likes

Yep, you’ve summed it up pretty well and your conclusion is quite right to my taste.