Before insert interceptor method.
Before insert interceptor method. This method is called just before record insertion. The default implementation returns a successful DBIO wrapping the passed entity.
The returned DBIOAction
is combined with the final insert DBIOAction
and 'marked' to run on the same transaction.
Override this method if you need to add extract validation or modify the entity before insert.
See examples bellow:
// simple validation example override def beforeInsert(foo: Foo)(implicit exc: ExecutionContext): DBIO[Foo] = { if (foo.name.trim.isEmpty) { DBIO.failed(new RuntimeException("Name can't be empty!!!") } else { DBIO.successful(foo) } }
// simple audit example override def beforeInsert(foo: Foo)(implicit exc: ExecutionContext): DBIO[Foo] = { // ensure that created and lastUpdate fields are updated just before insert val audited = foo.copy(created = DateTime.now, lastUpdate = DateTime.now) DBIO.successful(audited) }
Before update interceptor method.
Before update interceptor method. This method is called just before record update. The default implementation returns a successful DBIO wrapping the passed entity.
The returned DBIOAction
is combined with the final update DBIOAction
and 'marked' to run on the same transaction.
Override this method if you need to add extract validation or modify the entity before update.
See examples bellow:
// simple validation example override def beforeUpdate(id: Int, foo: Foo)(implicit exc: ExecutionContext): DBIO[Foo] = { findById(id).flatMap { oldFoo => if (oldFoo.name != foo.name) { DBIO.failed(new RuntimeException("Can't modify name!!!") } else { DBIO.successful(foo) } } }
// simple audit example override def beforeUpdate(id: Int, foo: Foo)(implicit exc: ExecutionContext): DBIO[Foo] = { // ensure that lastUpdate fields are updated just before update val audited = foo.copy(lastUpdate = DateTime.now) DBIO.successful(audited) }