Skip to content

Commit

Permalink
Began defining triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Apr 6, 2024
1 parent dcc5eb6 commit 2fae27b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions core/src/main/scala/lightdb/Collection.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lightdb

import cats.effect.IO
import fabric.rw.RW

abstract class Collection[D <: Document[D]](implicit val rw: RW[D]) {
Expand All @@ -10,5 +11,46 @@ abstract class Collection[D <: Document[D]](implicit val rw: RW[D]) {
protected def db: LightDB
protected def collectionName: String = defaultCollectionName

object triggers {
lazy val beforeSet: Triggers[BeforeSet[D]] = new Triggers
lazy val afterSet: Triggers[AfterSet[D]] = new Triggers
lazy val beforeDelete: Triggers[BeforeDelete[D]] = new Triggers
lazy val afterDelete: Triggers[AfterDelete[D]] = new Triggers
}
// TODO: Triggers
// TODO: set, modify, delete, get, apply
}

class Triggers[T] {
private var _list = List.empty[T]
def list: List[T] = _list

def add(triggers: T*): Unit = synchronized {
_list = _list ::: triggers.toList
}

def remove(triggers: T*): Unit = synchronized {
val set = triggers.toSet
_list = _list.filterNot(t => set.contains(t))
}

def +=(trigger: T): Unit = add(trigger)

def -=(trigger: T): Unit = remove(trigger)
}

trait BeforeSet[D <: Document[D]] {
def apply(document: D): IO[Option[D]]
}

trait AfterSet[D <: Document[D]] {
def apply(document: D): IO[Unit]
}

trait BeforeDelete[D <: Document[D]] {
def apply(id: Id[D]): IO[Option[Id[D]]]
}

trait AfterDelete[D <: Document[D]] {
def apply(id: Id[D]): IO[Unit]
}

0 comments on commit 2fae27b

Please sign in to comment.