TRESTRequestDataSetAdapter Example

The TRESTRequestDataSetAdapter component (introduced in RAD Studio 11.0) helps create a REST Request, from a TDataSet to send to a REST Server.

The TRESTRequestDataSetAdapter is a companion for the TRESTResponseDataSetAdapter component that takes a JSON data packet from a REST request response and concerts it into a TDataSet.

The TRESTRequestDataSetAdapter works well with the TEMSDataSetResource to accept data from POST, PUT, DELETE operations from the client side to update server side data.

This video will take you though how this works, and also demo’s a really useful unit of code, REST.DataUpdater.pas, that automatically manages the data transport based on the type of data update you have made.

To download the full source code check out the TasksExample on Github
https://github.com/DelphiABall/RADServer/

Components Used in the Example

To create a low code approach for serving data from a REST Server to the client and back, you can use the following components to Server, read, work with and send back data to update the server.

  1. TEMSDataSetResource – User in a RAD Server package, to send the data.
  2. TRESTClient, TRESTRequest to make the call to the RAD Server (or another kind of RESTful server).
  3. The TRESTResponse reads the resulting data package from the REST call, which can automatically convert the JSON array into a local DataSet (recommend TFDMemTable).

    With the data collected, you can now work on the data using Delphi/C++Builder app using direct data bindings via VCL or FMX and live bindings.
  4. When ready, post the resulting data changes back by using a TRESTRequestDataSetAdapter connected to the local DataSet via the original TRESTClient.

Initially, this final step requires a little coding to trigger the POST, PUT, DELETE depending on the change made, however, when being used with RAD Server, this is very simple to achieve, especially when using a helper class I’ve created.

REST.DataUpdater

Using the CacheUpdates property of the TFDMemTable, it is possible to cache the Insert, Update and Delete events that have changed data in the client. By querying the cache, you can then

var Updater := TRestDataSetUpdater.Create(
  Self,        // Owner of the Updater 
  RESTClient1, // Link REST Client, auth, URL etc
  'data/tasks/',  // End point to call
  'TASK_ID',   // Key field in the dataset
  FDMemTable1);// Source of the data to send
try
  Updater.ApplyRemoteUpdates(True);
  Memo1.Lines.Text := Updater.ErrorLog.Text;
finally
  Updater.Free;
end

https://github.com/DelphiABall/RADServer/tree/main/Delphi

Leave a Reply

Your email address will not be published. Required fields are marked *