CodeChef
CodeChef

Reputation: 906

How to store references to arbitrary entities in RavenDB?

I have the need to store arbitrary references to entities within my Raven Database. Sometimes the entity is an aggregate root (see Events below) and other times it is a value entity (see Sessions below). I'm currently planning to store the references as Lucene queries (or a Lucene-like syntax.) Has anyone done anything like this? Am I heading down a difficult path?

Some of my concerns are:

I've included example documents below to illustrate my scenario. Any thoughts, ideas, guidance, or examples would be very appreciated.

Events Collection
{
  Id: “30f6...54a7”,
  Title: “Annual Meeting”
  Sessions: [
    { 
      Code: “COM001”, 
      Title: “Opening Ceremony” 
    },
    { 
      Code: “TEC201”, 
      Title: “Intermediate Tech” 
    }
  ]
}

People Collection
{
  Id: "45a8...f209",
  Name: "Chad"
}

Arbitrary Relationships Collection
{
  Id: “b613...8ebb”,
  SubjectEntityQuery: "People.Id:45a8...f209",
  TargetEntityQuery: “Events.Id:30f6...54a7.Sessions,Code:COM001”,
  Action: "Attended Session",
  Story: "Chad attended the Opening Ceremony session"
}

Edit

I'd like to give more detail on the arbitrary relationships. We will have the ability to extend the system to respond to system events and record the interaction between two entities. We have many more entities than Events, Sessions, and People. The relationship may be a person sharing a link or a tweet about a hashtag. Effectively, the Arbitrary Relationships collection becomes a graph-like structure that allows us to see all ~interactions~ for a given entity.

Upvotes: 0

Views: 298

Answers (1)

synhershko
synhershko

Reputation: 4492

This is screaming relational design.

The easiest way to do this is make the Relationship an object and its Subject and Target fields an array of strings holding IDs of the actual documents it references. This way you can take advantage of Includes to load them along with the relationship document. Eitherway, I don't see how storing a Lucene query syntax helps here.

There may be a better way to model this, but it really depends on your business model and on the thing you are trying to achieve.

Also, you might want to get rid of GUID IDs, just use Raven's conventions.

Upvotes: 2

Related Questions