Patch Requests in Raven 4 Some examples needed please.

164 views
Skip to first unread message

Ian Davies

unread,
Sep 28, 2017, 7:07:26 AM9/28/17
to RavenDB - 2nd generation document database
Hi Guys

Migrating our DB layer to Raven4 and having some problems around patching 

I have looked at the book and its not very clear how I would do these kind of patches

  patchRequests.Add(new PatchRequest
            {
                Type = PatchCommandType.Add,Set,UnSet,Insert etc...
                Name = propertyName;
                Value = ***
            });


I currently also have these as deferred patches and without the entity loaded for efficiency. 

In the book i found this example 

 session.Advanced.Patch(call, c => c.Comments, 
  	comments => comments.Add("This is important stuff!"));

I could find overloads for setting a value on a property without loading the entity and just the ID passed in but nothing for Add, Insert or Unset. Anything deferred seems to have to be a manually crafted JavaScript patch or am i missing something ? 

We have quite a lot of dictionaries on our entities so we use Unset quite a lot when removing values via a patch

Thanks in Advance





Oren Eini (Ayende Rahien)

unread,
Sep 28, 2017, 9:09:56 AM9/28/17
to ravendb
Yes, the idea is that you can either use `session.Advanced.Patch`  to do the common operations or use JS code to directly modify the document. 
You can do something like this:

"delete this['FIELD_TO_DELETE']'

Or even pass this as an argument.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian Davies

unread,
Sep 28, 2017, 9:58:49 AM9/28/17
to RavenDB - 2nd generation document database
Ho Oren,

So I can do things like this
Session.Advanced.Defer(new PatchRequest
  {
     Script = $"delete this.{fieldName}" // Unset equivalent // Or
     Script =  $"this.{fieldName} = {value}" // Set  equivalent // Or
     Script =  $"insert(this.{fieldName}, {value})" // Insert  equivalent // Or 
 Script =  $"add(this.{fieldName}, {value})" // Insert  equivalent // Or 

Ian Davies

unread,
Sep 28, 2017, 10:00:36 AM9/28/17
to RavenDB - 2nd generation document database
Where can I find a reference for the commands and commands available like 'delete' ?

Oren Eini (Ayende Rahien)

unread,
Sep 29, 2017, 9:59:14 AM9/29/17
to ravendb
Yes

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


Oren Eini (Ayende Rahien)

unread,
Sep 29, 2017, 9:59:33 AM9/29/17
to ravendb
This is just javascript code running on the document in question (which is the this instance)

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


Ian Davies

unread,
Oct 2, 2017, 5:50:35 AM10/2/17
to RavenDB - 2nd generation document database
@Oren,

Ok, Sorry, started to get the hang of this the 'delete' confused me, I didn't know this was a built in JavaScript function / keyword.

But I'm still having some problems.

It's not clear what types of values can go into the values dictionary and will Raven automatically enclose strings , and convert dates to ISO etc ? . I have looked at your tests and these just seem to be any .net types. But i'm putting complex types into here and they are not getting set correctly in Raven I just seem to get a load of arrays (See last modified in the attachment) this is a complex type with a userid, username and date time stamp.

   
  var script = $"this.{propertyName} = args.data.item";
     patchRequests.Add(
            new PatchRequest
            {
                Script = script,
                Values =  {
                            { "data", new {item = value} } // Note : value could be complex type
                        } 
            });



Also Ideally I really need my complex types serialized using my custom contract resolver. I have certain kinds of properties that I don't want in the DB.
This was easy when it was a jObject that I could pass through on the patch request, now it seems I will have to serialize using my resolver and deserialise to something else ? dynamic ? this does not seem the way to go.

Or I just build the script and serialise my objects manually and I will have to deal with enclosures manually based on the types manually also.

It's doable of course , but what i'm getting at is that this is now a harder development experience in my opinion.

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Grisha Kotler

unread,
Oct 2, 2017, 6:56:24 AM10/2/17
to rav...@googlegroups.com

Hibernating Rhinos Ltd  cid:image001.png@01CF95E2.8ED1B7D0

Grisha Kotler l RavenDB Core Team Developer Mobile: +972-54-586-8647

RavenDB paving the way to "Data Made Simplehttp://ravendb.net/


To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Oct 2, 2017, 8:45:15 AM10/2/17
to ravendb
Did you attach anything here, I'm not seeing it.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages