mekanika / skematic

Data model & rule engine for JS objects
9 stars 0 forks source link

Self referencing values #24

Closed cayuu closed 8 years ago

cayuu commented 8 years ago

It can be useful to reference another value on the object itself. This may or may not be technically feasible/practical, but sometimes you might want to do something like:

// Pseduo
var item = {
  created: Date,
  lastUpdate: "this.created"
}

Or even something like:

var obj = {
  numCooks: 5,
  crazyInKitchen: {generate: fn() { return this.numCooks > 4 }}
}
cayuu commented 8 years ago

Yep - and see #13 Add custom routes comment on how Sequelize does this

cayuu commented 8 years ago

There are two ordering issues for self-referencing generate considerations.

  1. format(model, data) creates a modified data^ based on an ordered sequence of changes: a. Defaults, b. Generators, c. Transforms.. etc. It is essential to document 📖 that this references in Generators will be acting on the post-defaults-set data.
  2. More tricky.. 💣 Generators referencing other generator fields. In short, this is a bad idea, first because it's a PITA :finnadie: to guarantee the order the generators are run (not impossible), but mostly because this simply becomes harder to reason about (and it's possible to create really nasty chains of reference). It might be worth throwing a warning or even an outright Error if referencing a generator field.
cayuu commented 8 years ago

The gun is loaded. Generator fns have unfettered access to fields via this 😵 🔫

cayuu commented 8 years ago

Custom transform functions now also have access to model fields via this 💥 🎉 👻