Cross-Platform mORMot Clients - Generating Code Wrappers
Current version of the main framework units target only Win32 and Win64 systems.
It allows to make easy self-hosting of mORMot servers for local
business applications in any corporation, or pay cheap hosting in the Cloud,
since mORMot CPU and RAM expectations are much lower than a regular
But in a Service-Oriented Architecture (SOA), you would probably need to create clients for platforms outside the Windows world, especially mobile devices.
A set of cross-platform client units is therefore available in the
CrossPlatform sub-folder of the source code repository. It allows
writing any client in modern object pascal language, for:
- Any version of Delphi, on any platform (Mac OSX, or any mobile supported devices);
- FreePascal Compiler 2.7.1;
- Smart Mobile Studio 2.1, to create AJAX or mobile applications (via PhoneGap, if needed).
This series of articles will introduce you to mORMot's Cross-Platform abilities:
- Units and platforms;
- Generating the client code wrappers;
- Delphi / FreePascal clients;
- Smart Mobile Studio clients.
Any feedback is welcome in our forum, as usual!
Generating client wrappers
Even if it feasible to write the client code by hand, your mORMot server is able to create the source code needed for client access, via a dedicated method-based service, and set of Mustache-based templates - see Mustache template engine.
The following templates are available in the
|Unit Name||Compiler Target|
||Delphi / FPC SynCrossPlatform* units|
||Delphi Win32/Win64 mORMot units|
||Smart Mobile Studio 2.1|
In the future, other wrappers may be added. And you can write your own,
which could be included within the framework source! Your input is warmly
welcome, especially if you want to write a template for Java or
C# client. The generated data context already contains the data
types corresponding to those compilers: e.g. a mORMot's
RawUTF8 field or parameter could be identified as
"typeJava":"String" in addition
Publishing the code generator
By default, and for security reasons, the code generation is not embedded to
your mORMot RESTful server. In fact, the
mORMotWrapper.pas unit will link both
SynMustache.pas units, and use Mustache templates to
generate code for a given
We will start from the interface-based service Sample code as
defined in the
SQLite3- Interface based services" folder.
After some minor modifications, we copied the server source code into
uses SysUtils, Classes, SynCommons, mORMot, mORMotHttpServer, mORMotWrappers, Project14Interface in '..\14 - Interface based services\Project14Interface.pas';
type TServiceCalculator = class(TInterfacedObject, ICalculator) public function Add(n1,n2: integer): integer; end;
function TServiceCalculator.Add(n1, n2: integer): integer; begin result := n1+n2; end;
var aModel: TSQLModel; aServer: TSQLRestServer; aHTTPServer: TSQLHttpServer; begin // create a Data Model aModel := TSQLModel.Create(,ROOT_NAME); try // initialize a TObjectList-based database engine aServer := TSQLRestServerFullMemory.Create(aModel,'test.json',false,true); try // add the http://localhost:888/root/wrapper code generation web page AddToServerWrapperMethod(aServer, ['..\..\..\CrossPlatform\templates','..\..\..\..\CrossPlatform\templates']); // register our ICalculator service on the server side aServer.ServiceRegister(TServiceCalculator,[TypeInfo(ICalculator)],sicShared); // launch the HTTP server aHTTPServer := TSQLHttpServer.Create(PORT_NAME,[aServer],'+',useHttpApiRegisteringURI); try aHTTPServer.AccessControlAllowOrigin := '*'; // for AJAX requests to work writeln(#10'Background server is running.'); writeln('You can test http://localhost:',PORT_NAME,'/wrapper'); writeln(#10'Press [Enter] to close the server.'#10); readln; finally aHTTPServer.Free; end; finally aServer.Free; end; finally aModel.Free; end; end.
As you can see, we just added a reference to the
unit, and a call to
AddToServerWrapperMethod() in order to publish
the available code generators.
Now, if you run the
Project14ServerHttpWrapper server, and
point your favorite browser to
you will see the following page:
mORMotClient.pas - - -
mORMotClient.pas - - -
mORMotClient.pas - - -
You can also retrieve the corresponding .
Each of the
*.mustache template available in the specified
folder is listed here. Links above will allow downloading a client source code
unit, or displaying it as text in the browser. The template can also be
displayed un-rendered, for reference. As true Mustache templates, the
source code files are generated from a data context, which can be
displayed, as JSON, from the "" link. It may help
you when debugging your own templates. Note that if you modify and save a
.mustache template file, just re-load the ""
browser page and your modification is taken in account immediately (you do not
need to restart the server).
Generated source code will follow the template name, and here will always be
mORMotClient.pas. Of course, you can change the unit
name for your end-user application. It could be even mandatory if the same
client would access to several mORMot servers at once, which could be
the case in a Service-Oriented Architecture (SOA) project.
Just ensure that you will never change the
generated content by hand. If necessary, you can create and customize your own
Mustache template, to be used for your exact purpose. By design, such
automated code generation will require to re-create the client unit each time
the server ORM or SOA structure is modified. In fact, as stated in the
mORMotClient.pas comment, any manual modification of this file may
be lost after regeneration. You have been warned!
If you feel that the current templates have some issues or need some
enhancements, you are very welcome to send us your change requests on our
forums. Once you are used at it, Mustache templates are fairly easy to
work with. Similarly, if you find out that some information is missing in the
generated data context, e.g. for a new platform or language, we would
be pleased to enhance the official
Visit our source code repository
There is nothing better than some real files.
You can take a look at the following files in our source code repository:
- The .mustache template files;
- The SynCrossPlatform*.pas units;
- A generated mORMotClient.pas unit for Delphi / FPC - generated from RegressionTestsServer.dpr;
- The same mORMotClient.pas unit for Smart Mobile Studio - also generated from RegressionTestsServer.dpr;
- Another simplier mORMotClient.pas unit for Smart Mobile Studio - generated from Project14ServerHttpWrapper.dpr.