Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I INSERT one-to-many object with quill?

Tags:

mysql

scala

quill

I have a simple Parent object:

case class ParentCreate(name: String, age: Int, kids: Seq[String])

Now, in the database I have 2 tables to represent this, perent & kid, cause a parent can have many kids, and in the kid table I have a forein key to parentId.

the creation of the tables looks like this:

CREATE TABLE Parent (
    parentId int NOT NULL AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    age int NOT NULL,
    PRIMARY KEY (parentId)
);

CREATE TABLE Kid (
    kidId int NOT NULL AUTO_INCREMENT,
    name: varchar(255) NOT NULL,
    parentId int NOT NULL,
    PRIMARY KEY (kidId),
    FOREIGN KEY (parentId) REFERENCES Parent(parentId)
);

So now when I get a request of ParentCreate object, I need to have another layer of case classes that will represent the database structure, so I need to transform ParentCreate object to Parent and Kid objects:

Parent(name: String, age: Int)

Kid(name: String)

cause this is how the data is modeled in the database.

my issue is the I get a ParentCreate request that still dont have parentId, and to insert the kids I need the kids parentId...

How this is done the best way with quill?

appreciate any help :)

like image 784
John Deer Avatar asked Oct 11 '25 09:10

John Deer


1 Answers

This is the way I did it in my project:

case class KidId(value: Long) extends AnyVal
case class ParentId(value: Long) extends AnyVal

case class ParentRecord(id: ParentId
                  , name: String
                  , age: Int
                 )

case class Parent(id: ParentId
                  , name: String
                  , age: Int
                 )

object Parent {
  def createNew(name: String, age: Int, id: ParentId = ParentId(0L)): Parent = {
    Parent(id
      , name
      , age
    )
  }
}


case class KidRecord(
                      id: KidId
                      , parentId: ParentId
                      , name: String
                    )

case class Kid(
                id: KidId
                , parent: Parent
                , name: String
               )

object Kid {
  def createNew(name: String, parent: Parent): Kid = {
    Kid(KidId(0L)
      , parent
      , name
    )
  }
}

and then here is the quill code that would go in a different package

def createParent(parent: Parent): ParentId = {
  ctx.run(quote {
      query[ParentRecord].insert (
        _.name -> lift (parent.name)
        , _.age -> lift (parent.age)
      ).returning (_.id)
    }
  )
}

def createKid(kid: Kid): KidId = {
  ctx.run(quote {
      query[KidRecord].insert(
        _.name -> lift(kid.name)
        , _.parentId -> lift(kid.parent.id)
      ).returning(_.id)
    }
  )
}

and then used by this controller code

def createParentController(name: String, age: Integer, kids: Seq[String]) = {
  val parentToCreate = Parent.createNew(name, age)
  val parentId = createParent(parentToCreate)
  val createdParent = Parent.createNew(name, age, parentId)
  for (kid <- kids) {
    createKidController(kid, parentToCreate)
  }
}

def createKidController(name: String, parent: Parent) = {
  val kidToCreate = Kid.createNew(name, parent)
  val kidId = createKid(kidToCreate)
}

The primary thing is that you need to create the parent first and then pass the parent to whatever method you are using to create the kid. Caveat: I'm still fairly new to Scala and Quill.

like image 171
jmkoni Avatar answered Oct 14 '25 09:10

jmkoni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!