anchor.diff:      fun message -> processor.PostAndAsyncReply(fun replyChannel -> message, replyChannel)
   
   let fetchChatHistoryById (id) =
  -    use db = db ()
       db.Table<ChatHistory>().Where(fun x -> x.EntityId = id).FirstOrDefault()
   
   let updateChatHistoryReadById (id) =
  -    use db = db ()
       let history = fetchChatHistoryById id
       match history with
       | null -> ()
       | history ->
           history.Read <- true
  -        db.Update history |> ignore
  +        write (fun db -> db.Update history |> ignore)
   
   let fetchChatHistoryUnreadCount () =
  -    use db = db ()
       db.Table<ChatHistory>().Where(fun s -> s.Read = false).Count()
   
   let fetchChatHistory () = 
  -    use db = db ()
       db.Table<ChatHistory>().OrderByDescending(fun s -> s.LastStamp).Take(100).ToList() 
   
   let fetchChatEventHistoryByEntity entityId =
  -    use db = db ()
       db.Table<ChatHistoryEvent>().Where(fun e -> e.EntityId = entityId).OrderByDescending(fun s -> s.LastStamp).Take(100).ToList() 
   
   let removeChatHistoryById (id) =
  -    use db = db ()
       let history = fetchChatHistoryById id
  -    db.Delete(history) |> ignore
  +    write (fun db -> db.Delete(history) |> ignore)
  
positive.code:     #endif
  
  let clearErrors () =
      use db = db ()
      let errors = db.Table<Error>().ToList()
      for error in errors.Take(100) do
          db.Delete(error) |> ignore
  
  let createChatHistoryEvent (entity:Entity) historyType eventJson =
      use db = db ()
      db.Insert
          (ChatHistoryEvent(
              EntityId = entity.Id,
              Type = historyType,
              EventJson = eventJson
          )) 
      |> ignore
  
      let count = db.Table<ChatHistoryEvent>().Count()
      if count > 500 then
          let toClear = db.Table<ChatHistoryEvent>().OrderBy(fun s -> s.LastStamp).Take(100).ToList()
  
          //TODO batch this
          for event in toClear do
              db.Delete(event) |> ignore
  
  let createChatHistory = 
      let processor = MailboxProcessor<(Entity * ChatHistoryType * (string * bool) option) * AsyncReplyChannel<unit>>.Start(fun inbox ->
          let rec loop () = async {
              let! (entity, historyType, last), channel = inbox.Receive()
              use db = db ()
              let existing = db.Table<ChatHistory>().Where(fun x -> x.EntityId = entity.Id).FirstOrDefault()
  
              match existing with
              | null -> 
                  db.Insert
                      (ChatHistory(
                          EntityId = entity.Id,
                          Label = entity.Label,
  
                          Slug = entity.Slug,
                          Image = entity.Image,
                          Last = "",
                          Read = true,
                          Type = historyType
                      )) 
                  |> ignore
  
              | existing -> 
                  match last with
  
negative.code:                     existing.Read <- read
                  | _ -> ()
  
                  write (fun db -> db.Update existing |> ignore)
              
              channel.Reply(())
  
              return! loop ()
  