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.
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.
- TEMSDataSetResource – User in a RAD Server package, to send the data.
- TRESTClient, TRESTRequest to make the call to the RAD Server (or another kind of RESTful server).
- 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.
- 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.
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