anchor.diff:      #endif
   
   let clearErrors () =
  -    use db = db ()
       let errors = db.Table<Error>().ToList()
  -    for error in errors.Take(100) do
  -        db.Delete(error) |> ignore
  +    write (fun db -> 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
  +    write (fun 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
  +        write (fun db -> 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,
  +                write (fun db ->
  +                    db.Insert
  +                        (ChatHistory(
  +                            EntityId = entity.Id,
  +                            Label = entity.Label,
   
  -                        Slug = entity.Slug,
  -                        Image = entity.Image,
  -                        Last = "",
  -                        Read = true,
  -                        Type = historyType
  -                    )) 
  -                |> ignore
  +                            Slug = entity.Slug,
  +                            Image = entity.Image,
  +                            Last = "",
  +                            Read = true,
  +                            Type = historyType
  +                        )) |> ignore
  +                )
   
               | existing -> 
                   match last with
  
positive.code: init ()
  
  let deleteAll<'T when 'T : (new : unit -> 'T)>() = 
      use db = db ()
      let toDelete = db.Table<'T>().ToList()
      for event in toDelete do
          db.Delete(event) |> ignore
      
  let fetchErrors () =
      use db = db ()
      db.Table<Error>().ToList() 
  
  let createError (error:Error) =
      use db = db ()
      db.Insert (error) |> ignore
  
      #if DEBUG
      let errors = fetchErrors()
  
negative.code:     [<MaxLength(Int32.MaxValue)>]
      member val EventJson = "" with get, set
  
  let db = new SQLiteConnection(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "db"))
  let write computation =  
      lock db (fun _ -> computation db) 
  
  let init () =
      db.CreateTable<ChatHistory> () |> ignore
      db.CreateTable<ChatHistoryEvent> () |> ignore
      db.CreateTable<Error> () |> ignore
  