Felix
Felix

Reputation: 5619

slick db.run return direct the object

At the moment I do this:

  def addProcessStepTemplateToProcessTemplate(step: ProcessStepTemplatesModel, processId: Int): Future[Option[ProcessStepTemplatesModel]] = {
    val action = (processStepTemplates returning processStepTemplates.map(_.id)) += ProcessStepTemplatesModel(None, step.title, step.createdat, step.updatedat, step.deadline, step.comment, step.stepType, step.deleted, Some(processId))
    db.run(action).flatMap(id => {
      db.run(processStepTemplates.filter(_.id === id).result.headOption)
    })
  }

I have two db.run to return the created record.

Is there a way to achieve this with only one db.run?

Upvotes: 0

Views: 396

Answers (1)

John Mullins
John Mullins

Reputation: 1101

You can use actions composition (with for-comprehension or with map/flatMap). For example:

def addProcessStepTemplateToProcessTemplate(step: ProcessStepTemplatesModel, processId: Int): Future[Option[ProcessStepTemplatesModel]] = {
    val action = (processStepTemplates returning ...)

    val composedAction = action.flatMap { id =>
      processStepTemplates.filter(_.id === id).result.headOption
    }

    db.run(composedAction)
  }

Upvotes: 2

Related Questions