Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB ID prefix and REST API

I'm currently struggling with something that should be trivial, and maybe it is, but I can't find a decent solution.

My problem originates with RavenDB default ID structure. Let's say we use the HiLo algorithm to create a person, we'll get our first document:

people/A-1

And the related collection "People" has now one member.

My problem is really simple: designing a normal RESTful method to get a single person:

GET people/:id

And calling it with the sample document ID, my call will become:

GET people/people/A-1

Which will obviously not work, since the slash character is the route separator character, but with RavenDB it's also part of the entity ID.

I've tried using a different separator (changing the conventions on my client code), so to get an ID like people#A-1 but this is not recognized by RavenDB Studio: if I try to "link" two documents together by their ID, only the standard format gets recognized. Also I couldn't find a way to set the convention on the database at server level, but only at client level.

This solution also feels like a dirty workaround, and I'm looking for the proper way of doing things.

I know I can use arbitrary IDs, like email addresses without any prefix, but this isn't feasible for all kinds of entities I'm going to store, natural key isn't an option in every scenario.

So I'm asking to anyone who is more experienced than me in using RavenDB. What's the proper way to address this problem?

like image 840
Matteo Mosca Avatar asked Apr 16 '18 13:04

Matteo Mosca


2 Answers

You could pass only the 1-A to the controller that recieve the request because PeopleController implies that you are going to work with People and then you can add the prefix to the id using RavenDB functions:

var collectionName = documentStore.Conventions.FindCollectionName(typeof(People));
var idPrefix = documentStore.Conventions.TransformTypeCollectionNameToDocumentIdPrefix(collectionName);

var id = idPrefix +"/"+ "1-A";

Otherwise you could change the id generation convention of that type

like image 136
Embri Avatar answered Nov 18 '22 17:11

Embri


I have not used ravendb outside of toy projects, but when I read ayendes book, I thought this would be a problem. I see three possible ways to circumvent this:

  1. assign an guid as id, this way you just have no / in your ids. This can be done on document creation
  2. Replace the "/" with something less bad on each controller method, which is annoying and error prone.
  3. Url-escape the id part when generating URLS.

I would use #1 I think.

like image 2
Christian Sauer Avatar answered Nov 18 '22 17:11

Christian Sauer