You can use HTTP PATCH to update selected fields of an object exposed through an MDriven ViewModel REST endpoint; this page is for developers calling or designing that endpoint.
RestPatch is not an OCL operator
Despite this page's placement among OCL operator pages, RestPatch is not a standard OCL or Executable Action Language (EAL) command. It describes the REST convention of sending an HTTP PATCH request to change part of an existing resource.
For example, a client can change a profile's phone number without resending its name and email address. The fields omitted from the PATCH request remain unchanged.
For the general REST PATCH concept and an HTTP request example, see Documentation:Rest Patch.
How partial updates work
A PATCH request identifies an existing object and sends only the field values that should change.
| Request content | Result |
|---|---|
phoneNumber = "555-1234"
|
Updates the phone number only. |
| Name, email, and other fields are not included | Those fields are left unchanged. |
This differs from a complete-representation update. Use RestPut when you need to send a complete data structure to a remote URL.
Expose a ViewModel for REST access
MDriven exposes REST access through a ViewModel. Configure the ViewModel that represents the data you intend to expose.
- Open the ViewModel in MDriven Designer.
- Set the ViewModel's class to the domain class whose instances the endpoint will work with. For example, an
ArticleViewModelcan be rooted in anArticles1domain class. - Add the attributes that clients must be able to read or update. Use direct expressions for fields intended for straightforward updates. For example, use
self.titlefor a title field. - Open Tagged values in the ViewModel Editor.
- Add the
Eco.RestAllowedtagged value and set it toTrue. - Run the application and call the ViewModel's REST URL.
The RestAllowed tagged value is required to allow REST access to a ViewModel. Without it, REST operations are not available for that ViewModel. For the wider REST-service setup and calls made from within a ViewModel, see Documentation:Rest Services In MDriven.
Choose direct field expressions
A ViewModel field can be based on a calculated expression, but the REST response represents the ViewModel field value rather than necessarily exposing the raw stored attribute.
For example:
title : self.title
authorText : 'By ' + self.authorIf a client updates title, a direct expression such as self.title provides a clear mapping to the underlying attribute. A calculated field such as authorText returns its calculated display value, By .... Design the ViewModel fields deliberately so that the values clients send and receive have the meaning you expect.
Endpoint and object identification
A REST-enabled ViewModel is available under the REST path for its ViewModel name. A PATCH call targets a specific existing object by its MDriven object ID.
http://<host>/Rest/<ViewModelName>/Patch?id=<objectId>
For example:
http://localhost:8182/Rest/ArticleViewModel/Patch?id=3!396
The object ID 3!396 consists of the class ID and instance ID. The current endpoint form may also place the object ID in the path:
http://localhost:8182/Rest/ArticleViewModel/Patch/3!396
Send the fields to change
Send the fields to update as key-value pairs in the request body. Include multiple pairs when you need to update multiple fields in one request.
For example, to change an article title, send a title value of Updated Title. To change both title and content, send both title and content. Do not include fields that must remain unchanged.
Response
A successful PATCH request returns 200 OK and the updated ViewModel object as JSON.
The response reflects the value defined by the ViewModel expression. For example, when a field uses a calculated expression, the response contains the calculated value rather than an unformatted stored value. This is expected: the ViewModel controls the representation returned to the client.
Calling other REST services from MDriven
When your requirement is to call an external REST service from a ViewModel action, use the REST-related EAL commands on selfVM, not a RestPatch operator. For example, RestGet performs a GET request, while RestPut sends a complete representation to a remote URL. See Documentation:Rest Services In MDriven for the available REST service operations and their ViewModel context.
