Lift (Mongo)Record field auto-computed from another field

123 views Asked by At

I have a field that holds a datetime as well as a denormalization field to store that same datetime value as a unix timestamp (for reasons that are beyond my control at the moment).

I would like the timestamp field to always be in sync with the "main" datetime field and preferrably even be not writable from outside the methods of that record class (but still be readable and queryable).

I've looked at both LifecycleCallbacks (which should work according some older documentation but now is meant to be used with Field types not Record types) as well as for a special Field type (e.g. AutoField or ComputedField) to no avail.

Should I look into implementing a custom Field, or am I overlooking something obvious? I'm on version Lift 2.6-M2.

class Job private() extends MongoRecord[Job] with ObjectIdKey[Job] {
  ...
  object regDate extends JodaTimeField(this)
  object regDateUnixtime extends LongField(this)
  ...
}
1

There are 1 answers

6
Dave Whittaker On BEST ANSWER

Have you tried something like

private object regDateUnixtime extends LongField(this)

object regDate extends JodaTimeField(this) {

  override def setBox(in: Box[DateTime]) = {
    super.setBox(in) map { v => 
      regDateUnixtime(v.getMillis) 
      v
    }
  }

}