#1 2020-12-06 03:33:40

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

mORMot2 examples

Hi!

I have a task to create a JSON REST API for my n-tier application. In my search for a suitable library, mORMot kept turning up as good candidate for the problem.

I tried installing mORMot 1.18 library (i created a runtime package to have all the units compiled, put the dcus in a folder and added it to library path. this way a build wont try to recompile the library - i do it for all the libraries i use, it saves a lot of time). However it seems Delphi 7 is having a hard time with huge library files syncommons.pas and mormot.pas. Every other time i invoke code completion the IDE hangs. Unfortunately this makes it impossible for me to use the library in any serious way.

I discovered mORMot2 is nearly ready and the libraries have been modularized and organized into more independent units. So i installed it in similar way and it seems Delphi 7 indeed has no issue with it.

Unfortunately there are no examples yet for version 2 and the examples from mORMot 1.18 i'm interested in (06), are not compiling with version 2 (i think i fixed the uses units correctly but some functions like UrlDecodeNeedParameters just no longer exist in v2, it would be extremely difficult for me to find out how it's functionality has been replaced).

I've been reading the documentation and trying some things for four days now. The learning curve is steep but i like what i see so far. I've decided to invest the time but maybe i should invest the time in mORMot2 since it is the future anyway?.

For now, I'm not interested in ORM functionality. All i need is a http(s) server with rest methods + json parameters parsing. Something similar to what dXmlRpc library offers but for JSON. API authentication solution would be a bonus.

I would appreciate suggestions for which classes/examples is should focus on and if it is a good idea to start with v2 (in that case, which v2 classes i should focus on and what are the counterparts in v1 so i can read the docs).

Thanks and regards,
Lauri

Offline

#2 2020-12-06 12:21:44

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: mORMot2 examples

However it seems Delphi 7 is having a hard time with huge library files syncommons.pas and mormot.pas. Every other time i invoke code completion the IDE hangs.

I've been using mORMot on Delphi 7 with success, but I feel your pain.
Which I'm doing is:
- do not use code-completion: disable on IDE or increase in 2 or more seconds to call automatically.
- clicking on classes/functions works: you'll use it to see constructors, parameters, documentation, etc on the source.
- on SynCommons, etc, navigation doesn't work: forget Ctrl+Shift+up/down, continue Clicking on tokens.

Offline

#3 2020-12-06 13:15:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: mORMot2 examples

The trouble is that Delphi has troubles with even smaller mORMot2 units... and not only Delphi 7, but any modern Delphi without the Andy IdeFixPack.
And be aware: mORMot 2 is NOT stable, and should NOT be use on production. There are a lot of bugs still to be fixed, and some mission functions - as you noticed.
I hope/pray/sight it would be usable for Christmas. smile

Another trick for stability: compile all mORMot units into .dcu, and don't compile from the source during development.
Or create a package.
It would make compilation of your code faster, and the IDE will be more responsible.

Offline

#4 2020-12-06 21:14:10

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

ab wrote:

The trouble is that Delphi has troubles with even smaller mORMot2 units... and not only Delphi 7, but any modern Delphi without the Andy IdeFixPack.
And be aware: mORMot 2 is NOT stable, and should NOT be use on production. There are a lot of bugs still to be fixed, and some mission functions - as you noticed.
I hope/pray/sight it would be usable for Christmas. smile

I understand the risk of using alpha-grade libraries. Fortunately the API wont be exposed to the world and i can always update it when there are important fixes in MORMot. It is likely i wont finish the project this year anyway.

ab wrote:

Another trick for stability: compile all mORMot units into .dcu, and don't compile from the source during development. Or create a package. It would make compilation of your code faster, and the IDE will be more responsible.

This is exactly what i'm doing (i describe that in my lengthy first post). I'm also using the IdeFixPack. I did not notice IDE issues with mORMot 2, only the current stable version is too much for my poor old D7.

Unfortunately i still don't know where to focus my attention. My eyes are sore from all the reading already smile I would really appreciate a hint on which building blocks to use for a HTTP server with REST api *without* all the ORM/DB stuff.

Last edited by Lauri (2020-12-06 21:24:29)

Offline

#5 2020-12-06 21:22:58

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

By the way, i found a minor issue with mORMot 2 tests. The tests that were using HttpGet function failed with AV because WinHttpAPIInitialize was not called.  I added a call to unit initialization of mormot.lib.winhttp.pas and now all test pass.

Offline

#6 2020-12-10 20:24:05

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

After a week and a half of reading and trying things i'm starting to get a handle on this library. I like how the source is full of detailed comments. And more abstract documentation is online (SAD and blog).

I found another bug though. It's in the blog smile
Various older links from this forum to the blog are broken and some links inside the blog that refrence prior articles also. See the link collection at the bottom of this post for example:
https://blog.synopse.info/?post/2012/03 … d-services
The fix is real easy -- need to add ? before post/ in the URI

I also have a question i have not found an answer yet:
given such URL - host.domain/API
where API is the root. how can i define and publish an interface for function "version(out major, minor: integer)" so that it would be accessible at host.domain/API/version or host.domain/API.version? In other words, how can i publish some interfaces under the root?

Also, while mORMot 2 is working much better with D7, there is one annoying issue - i can not use the built-in search because file names with multiple dots are confusing it (nothing is ever found in directory search). I've been using notepad+ to search but this is annoying. Is there a workaround? I guess asking for file name parts to be separated using an underscore would be unreasonable? smile

Offline

#7 2020-12-10 21:11:43

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: mORMot2 examples

The interfaces are indeed published under the root of the TSQLRestServer/TRestServer model.

If root is "API", then IMyService.Version is located at host.domain/API/version but also at host/domain/API.version - both are accepted.
Check the doc https://synopse.info/files/html/Synopse … #TITLE_454

Offline

#8 2020-12-10 21:29:25

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

I see i was a bit unclear in my question. Sorry about that. I'll try again based on the doc --
Using TSQLRestRoutingREST routing, having URI scheme "/Model/Interface.Method[/ClientDrivenID]", is there any way to publish one or more methods without the interface name? Like so: "/Model/Method[/ClientDrivenID]"?

Offline

#9 2020-12-10 21:52:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: mORMot2 examples

No, it is not directly feasible.

You can do that by making your own routing class.

Offline

#10 2020-12-10 22:05:02

tbo
Member
Registered: 2015-04-20
Posts: 335

Re: mORMot2 examples

Lauri wrote:

Also, while mORMot 2 is working much better with D7, there is one annoying issue - i can not use the built-in search because file names with multiple dots are confusing it (nothing is ever found in directory search).

Install the free IDE extension GExperts and use Grep Search.

With best regards
Thomas

Offline

#11 2020-12-11 09:00:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: mORMot2 examples

If the units are part of the .dpr uses clause, with their full path, they will be found in Delphi 7.

Note that Delphi 7 compatibility is limited, we don't make any specific traitment any more for this IDE.
We ensure it compiles and passes the tests, but no specific code optimization any more (e.g. manual inlining), or no explicit trial to circumvent some Delphi 7 IDE limitations.
Premium support is for FPC stable and latest Delphi.

Offline

#12 2020-12-11 14:03:29

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: mORMot2 examples

This instability in Delphi 7 occurs only when the unit is open?
Or the simple fact of including it in the uses already generates instability?


If you use cnpack there is an option to highlight the words that are in the cursor.
This makes the editor very slow on units with many lines.
There is an option in the cnpack settings to disable this in units with more than x lines. It can help ...

Furthermore, I fully agree to focus mORMo2 on FPC and newer versions of Delphi.

Offline

#13 2020-12-12 17:54:41

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

It would seem that implementations of TRestServerRoutingREST and TRestServerRoutingJSON_RPC have gotten mixed up in mORMot2 smile

Offline

#14 2020-12-12 18:20:02

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

Hmm.. i'll try disabling cnPack and see if i can open v1.18 mormot.pas.

Regarding focusing on newer versions. I can't say i like that thinking too much. I have a huge, currently stable and relatively bug free, project written in D7. Porting it to latest version of Delphi (for which i do own a current subscription) is a lot of work. We can't all be so productive as Arnaud smile And it will destabilize the project for years. I would rather add useful new features my customers are interested in (such as an API for a web based client). D7, with some tweaks, can do everything that is necessary and produce a nice modern looking application. The only issue is Unicode UI. But that is something i can live without. Just because something is old does not mean its become crap. Just my POV.

Offline

#15 2020-12-12 20:20:44

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

I found another minor bug in v2.
Interface methods return output parameters without commas like so:
{"n1":2"n2":3"text":"test text"}
instead of:
{
    "n1": 2,
    "n2": 3,
    "text": "test text"
}
I have set
ResultAsJSONObject := True;
ResultAsJSONObjectWithoutResult := True;

And another one:
Defining an interface with function returning TServiceCustomAnswer will cause AV when calling TInterfaceFactory.RegisterInterfaces.
in mormot.core.base, function TSynTempBuffer.InitZero, there is a call "Init(ZeroLen - 16)" but ZeroLen is 12 in this case. That results in a negative value and buf getting set to nil. Next FillCharFast will fail.

Ab, do you want bugreports like this or should i not bother reporting?
(if yes then see post #13 also)

And a suggestion -- why not make TRestServerURIContext.Error(E: Exception; const Format: RawUTF8; const Args: array of const; Status: integer); ....virtual. I want to override it so i can handle some "normal" exceptions from interface implementing functions and return a bit less information than by default is returned for exception.

Last edited by Lauri (2020-12-13 01:14:55)

Offline

#16 2020-12-14 04:47:10

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

What happened to TTextWriter.RegisterCustomJSONSerializer in v2?
Only refrences left are in source comments.

Offline

#17 2020-12-14 07:46:28

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 209

Re: mORMot2 examples

You really should reconsider about using mORMot 2, @ab clearly said it's in alpha state so the code is highly volatile, not sure what you expected. You gain no benefit by using it at this stage, whatever code you write now will have to be updated later just as if you were using stable mORMot branch.

Offline

#18 2020-12-14 13:10:43

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

Don't worry about that. It will be easy enough to switch back to 1.18. But for discovering things and browsing around the code, v2 is a lot better. Plus my IDE doesn't crash every second click.
I don't mind reporting the bugs if it helps get mORMot 2 released sooner.

Offline

#19 2020-12-14 23:44:53

Lauri
Member
From: Under a rock
Registered: 2020-12-05
Posts: 21

Re: mORMot2 examples

Lauri wrote:

What happened to TTextWriter.RegisterCustomJSONSerializer in v2?
Only refrences left are in source comments.

Looks like new solution is TRttiJson.RegisterCustomSerializer. Seems to work just fine. Thank you!

Offline

Board footer

Powered by FluxBB