Add a last updated timestamp on all my mongo collections

139 views
Skip to first unread message

bg

unread,
Oct 31, 2012, 5:02:26 PM10/31/12
to lif...@googlegroups.com
Hi all,

I would like to create a "last updated" timstamp across all my collections.

My model classes look like

class User extends MongoRecord[User] with MongoId[User] {

....

object User extends User with MongoMetaRecord[User] {

....

Any suggestions, or examples on a good way to implement this? Looking for every collection to get a field which gets poptulated with the currect time whenever a update is performed.

Thanks

Richard Dallaway

unread,
Nov 1, 2012, 2:50:38 AM11/1/12
to lif...@googlegroups.com, lif...@googlegroups.com
This was originally written for Squeryl, but as the example indicates, it'll work for Mongo too:

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 

bg

unread,
Nov 1, 2012, 6:21:19 AM11/1/12
to lif...@googlegroups.com
Perfect - thank you

bg

unread,
Nov 1, 2012, 2:29:05 PM11/1/12
to lif...@googlegroups.com

I'm having some trouble with the 

def  consumerCallbacks = Seq( beforeUpdate[Consumer] call {_.onUpdate}) 

Do I need to do something along the lines of the below to my User class?

class User extends MongoRecord[User] with MongoId[User] with LifecycleCallbacks { 

that give a compiler error - so I guess its not quite that!

illegal inheritance;
[error]  self-type com.blendology.sojo_database.model.User does not conform to net.liftweb.record.LifecycleCallbacks's selftype net.liftweb.record.LifecycleCallbacks with net.liftweb.record.BaseField
[error] class User extends MongoRecord[User] with MongoId[User] with CreatedUpdated[User] with LifecycleCallbacks {


And then would that line (or something similar) go in the User class?


Thanks

Carlos Montemuiño

unread,
Nov 1, 2012, 3:08:07 PM11/1/12
to lif...@googlegroups.com
Hi, I think next post might help you: http://bit.ly/X3TTLe

bg

unread,
Nov 1, 2012, 4:32:22 PM11/1/12
to lif...@googlegroups.com

My understanding of that post is LifecycleCallbacks are not for Records 

If so - is there anything else I can hook onto in order to update the "last updated" timestamp on each transaction?

Richard Dallaway

unread,
Nov 2, 2012, 6:13:25 AM11/2/12
to lif...@googlegroups.com
Hello

The cookbook example is confusing: it is Squeryl specific (and possibly only for 2.5 too), even though it gives a record that is Mongo-based. I'll re-work it to clear up that confusion.

However, that doesn't help you. 

For Mongo, although the updated and created fields will be there when you add a record, I can't currently see a way to automatically trigger the update.

Richard

--

bg

unread,
Nov 2, 2012, 6:20:26 AM11/2/12
to lif...@googlegroups.com
there  is a package net.liftweb.record.LifecycleCallbacks 

So I guess LifecycleCallbacks does exist within the Record world, but is it the case that MongoRecord doesn't use them?


On Wednesday, October 31, 2012 9:02:26 PM UTC, bg wrote:

Tim Nelson

unread,
Nov 3, 2012, 8:08:24 AM11/3/12
to lif...@googlegroups.com
Hi bg,

MongoRecord does use the LifecycleCallbacks. However, currently they only work for save and delete. I will add the update callbacks to the update function.

Here's a field that should work for you with the save function:

object updatedAt extends DateTimeField(this) with LifecycleCallbacks {
  override def beforeSave() {
    super.beforeSave
    this.set(Calendar.getInstance)
  }
}

Tim

Tim Nelson

unread,
Nov 3, 2012, 8:26:19 AM11/3/12
to lif...@googlegroups.com
Hi again,

You probably wanted a trait, so here's one that works. I will probably be adding this to Record at some point.

trait Updated[T <: Updated[T]] extends Record[T] {
  self: T =>

  /**
   * The updatedAt field.  You can change the behavior of this
   * field:
   * <pre name="code" class="scala">
   * override lazy val updatedAt = new MyUpdatedAt(this) {
   *   override def dbColumnName = "i_eat_time_for_breakfast"
   * }
   * </pre>
   */
  lazy val updatedAt: MyUpdatedAt = new MyUpdatedAt(this)

  protected class MyUpdatedAt(obj: T) extends DateTimeField(obj.asInstanceOf[T]) with LifecycleCallbacks {
    override def beforeSave() {
      super.beforeSave
      this.set(Calendar.getInstance)
    }
  }
}

Tim

bg

unread,
Nov 8, 2012, 7:36:02 AM11/8/12
to lif...@googlegroups.com
Brilliant - thank you very much, working on the create and saves.
Reply all
Reply to author
Forward
0 new messages