0

At the core of my app is a table called Entities:

Entities:
-----------
id: number // PK
name: string
position_x: number
position_y: number

These entities have interactions with each other, called Events:

Events:
-----------
id: number // PK
name: string
date: Date
from: number // Entities FK
to: number // Entities FK

With events, we can manage all kinds of events between two entities. By selecting a timestamp / event, we can "time-travel" through each event, and see how changes affect the entities.

I just learned we want to support changes to entity attributes in the timeline as well. (travelling back should still get you the original attributes). So now I want to create a new Event-like table, which would look something like this:

SingleEntityEvents:
-----------
id: number // PK
name: string
date: Date
to: number // Entities FK
type: enum // CHANGE_NAME | CHANGE_POSITION
payload: json

Now my app would get the entities and render the original name if applicable, but render an updated name if there is a CHANGE_NAME event happening before the currently selected date. However, now I am leaning towards getting rid of the Entities table entirely, and make everything (including Entity creation) an event. But then how do I properly refer to these entities in the original (from -> to) Events? Is this even possible?

In other words, I want to be able to model this stream of events, but I am not sure how to:

  • Create entity through event:
name: 'Create Entity'
date: 01/01/2022
entityID: 123 // Its not to reference but to create an entity with this ID?
type: CREATE_ENTITY
payload: {
  "name": "New Entity",
  "position_x": 1,
  "position_y": 0
}

If I select any time after this first event, the app should render an entity called "New Entity", positioned on coordinate (1, 0).

  • Update name of entity through event
name: 'Update Entity Name'
date: 01/03/2022
entityID: 123 // Now, since this is an update event, this is an actual reference to the previously created entity
type: UPDATE_ENTITY_NAME
payload: "Foo Bar"

If I select any time after this second event, the app should now render the same entity but it's now called "Foo Bar". Its position is unchanged so its still on (1, 0).

My question is, are there best practices to model such event streams? And how do I go about creating this entity ID?

Sventies
  • 2,314
  • 1
  • 28
  • 44
  • What do you mean, "refer to these entities in the original (from -> to) Events"? Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. Where/how are you stuck following what information modelling & DB design method? PS Putting words in scare quotes does not clarify the idiosyncratic meaning that you don't make clear by actually saying what you mean. PS A FK constraint says subrow values appear elsewhere once. When that's so & not implied by other declarations, declare; otherwise don't. What is your problem? – philipxy May 04 '22 at 06:09
  • @philipxy thanks for your comment. I tried to add text to explain I'm trying to model an event stream, but not sure how to refer to a previously created entity (what ID should I use?) – Sventies May 04 '22 at 07:53
  • You didn't do the things I asked. You added "in other words", but you didn't give other words, you gave an example. Of what? Also: Adding to something unclear doesn't make it clear. Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. [ask] [Help] PS Some relavant terms may be "versions" of things & "historical" & "temporal" data/DBs. – philipxy May 04 '22 at 08:04
  • [Why is asking a question on "best practice" a bad thing?](https://meta.stackexchange.com/q/142353/266284) [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) – philipxy May 04 '22 at 08:05
  • [How can you represent inheritance in a database?](https://stackoverflow.com/q/3579079/3404097) [How do you effectively model inheritance in a database?](https://stackoverflow.com/q/190296/3404097) (etc etc) – philipxy May 04 '22 at 08:46

0 Answers0