"update" events should always reflect the changes from previous events even if neighboring events are removed

I created a document:

Create(Collection("Property"), {data: {year: 1, rooms: 1}})
{
  ref: Ref(Collection("Property"), "277906182656492045"),
  ts: 1601290838810000,
  data: {
    year: 1,
    rooms: 1
  }
}

Updated it twice:

Update(Ref(Collection("Property"), "277906182656492045"), {data: {year: 100, rooms: 100}})
Update(Ref(Collection("Property"), "277906182656492045"), {data: {rooms: 50}})

Checked the events:

Paginate(Events(Ref(Collection("Property"), "277906182656492045")))

{
  data: [
    {
      ts: 1601290838810000,
      action: "create",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        year: 1,
        rooms: 1
      }
    },
    {
      ts: 1601290876580000,
      action: "update",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        year: 100,
        rooms: 100
      }
    },
    {
      ts: 1601290895130000,
      action: "update",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        rooms: 50
      }
    }
  ]
}

You can see that using the events, you could reason how the document reached its current state. Each “update” is a diff of the state of the document before it.

Now if you remove an event from the middle:

Remove(Ref(Collection("Property"), "277906182656492045"), 1601290876580000, 'update')

and then run the following queries:

Paginate(Events(Ref(Collection("Property"), "277906182656492045")))
{
  data: [
    {
      ts: 1601290838810000,
      action: "create",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        year: 1,
        rooms: 1
      }
    },
    {
      ts: 1601290895130000,
      action: "update",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        rooms: 50
      }
    }
  ]
}

Get(Ref(Collection("Property"), "277906182656492045"))
{
  ref: Ref(Collection("Property"), "277906182656492045"),
  ts: 1601290895130000,
  data: {
    year: 100,
    rooms: 50
  }
}

The fact that the year is currently 100 makes no sense based on the events in the document. I think that the event:

{
      ts: 1601290895130000,
      action: "update",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        rooms: 50
      }
    }

should automatically update itself to be a diff to the previous event and look like this:

{
      ts: 1601290895130000,
      action: "update",
      document: Ref(Collection("Property"), "277906182656492045"),
      data: {
        year: 100,
        rooms: 50
      }
    }

That way the events and the current document remain consistent.

Great suggestion, imo I would also expect it to work as you would expect.
I added a ticket that refers to this post and hope to give you some more feedback soon.

1 Like