#1 2015-02-21 11:47:39

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Start with mORMot Framework in Lazarus

Hi,

First apologies because of these too newbie questions,But I'm newbie with mORMot and web applications but familiar with Lazarus.
So questions:
1-I want to make a simple JSON API for my server and searching for a good library and I stuck in deciding with library is good for this situation;mORMot,Brook,Fast ... . And I know mORMot from 2 years ago ( but with a different name) and I never test it for a real job but it is very interesting and seems powerful but complicated to me but I like to use it.So is it mORMot good for a simple JSON API or I should choose another framework?
2-For this goal (simple JSON API) is there any example or a point to begin so I not be confused like now?

Thanks

Offline

#2 2015-02-21 12:22:31

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

Re: Start with mORMot Framework in Lazarus

Welcome a board!

Creating a method-based service in mORMot is pretty easy.
See sample "06 - Remote JSON REST Service" and http://synopse.info/files/html/Synopse% … ml#TITL_49

If you want a much more powerful approach, based on interfaces, see http://synopse.info/files/html/Synopse% … ml#TITL_63

Offline

#3 2015-02-21 12:53:49

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Thank you ab.
I'm going to work with them and ask future question after I will stuck wink

Offline

#4 2015-02-22 21:21:26

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

So I'm stuck wink
First I should thank you for this BIG documention.
Second I should thank you for "25.4.4. Linux installation tips" section,It helps me a lot.
Now problem:
I get Lazarus and FPC with fpcup and make new simple project like this :

program project1;
{$I Synopse.inc}
{$APPTYPE CONSOLE}
uses {$ifdef Linux} cthreads, cwstring, {$endif} mORMotSelfTests;
begin
  SQLite3ConsoleTests;
end.                 

but in SynZip unit there is an error:

SynZip.pas(582,87) Error: Incompatible types: got "QWord" expected "LongWord"

Why?

Last edited by aradeonas (2015-02-22 21:21:44)

Offline

#5 2015-02-23 06:21:10

AOG
Member
Registered: 2014-02-24
Posts: 490

Re: Start with mORMot Framework in Lazarus

Just checked latest mORMot with latest Laz/FPC.
And your program-source.
I cannot reproduce your problem.

Questions: what's your setup (OS, bits, FPC version, ...) ?

Offline

#6 2015-02-23 08:39:15

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

OS:Xubunto 14.10
Lazarus : 1.5 2015-02-22
FPC : 3.1.1
I download all three 2 days ago and :
mORMot Nightly build 2015-02-20_160052_6eeb81a028

And I don't change anything in the sources,just make new project and copy documentation sample code and compile and error.

Offline

#7 2015-02-23 08:45:45

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

Re: Start with mORMot Framework in Lazarus

Ensure you got latest mORMot nightly zip from this morning.

Did you get the 32 bit version of FPC + Lazarus?
We tested (and also supported) only Linux 32 bit yet.
Perhaps you are creating Linux 64 bit executables, which are not yet supported.

Your exact issue may be fixed by http://synopse.info/fossil/info/44a3cb912e
But try to compile for Linux 32 bit.

Offline

#8 2015-02-23 08:54:29

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Yes my Xubuntu is 64 and I get it with fpcup.
Honestly until this time I didn't use fpcup and I don't know How to compile Laz and FPC 32 in a 64 linux.
How can I remove this one and compile a 32bit version?

Last edited by aradeonas (2015-02-23 08:56:14)

Offline

#9 2015-02-23 08:58:35

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

Re: Start with mORMot Framework in Lazarus

You can keep the 64 bit FPC and Lazarus I suspect.
Just cross-compile to Linux32 platform.

Offline

#10 2015-02-23 09:05:26

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

I don't know how to do it really,In the past time I try but just with CodeTyphone I code do this.Can you help me?
And I get last version of mORMot and error change to  SynCrypto :

SynCrypto.pas(1541,6) Error: Identifier not found "cfSSE41"
  {$ifdef CPU64}
  if cfSSE41 in CpuFeatures then begin
    Exclude(CpuFeatures,cfSSE41);
    result := result and SingleTest('abc', D1) and
       SingleTest('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', D2);
    Include(CpuFeatures,cfSSE41);
  end
  {$endif}    

Last edited by aradeonas (2015-02-23 09:05:37)

Offline

#11 2015-02-23 13:29:50

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

Re: Start with mORMot Framework in Lazarus

You are trying to compile for Linux x64.
This is not supported yet.

Change the destination platform to Linux x86 (i.e. Linux 32 bit).
No need to recompile your FPC or Lazarus: you should be able to cross-compile to Linux x86 from a x64 FPC/IDE.

Offline

#12 2015-02-23 14:07:06

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

I try to change project target but it says :

Compiler "/home/pp/development/fpc/bin/x86_64-linux/fpc.sh" does not support target i386-linux

In this answer author of fpcup said we cant compile 32bit in a 64bit Linux(it what I understand,maybe wrong).

http://forum.lazarus.freepascal.org/ind … #msg153422

I should say I install DPC with fpcup_linux_x64,Should I reinstall with 86 version?
I know my question is too newbie but I never play with this options and I'm always developing for windows.

Last edited by aradeonas (2015-02-23 14:49:16)

Offline

#13 2015-02-24 00:48:56

brian_ch
Member
Registered: 2015-02-24
Posts: 4

Re: Start with mORMot Framework in Lazarus

Hi,

Hope I'll not get banned posting a link in my first post here hehe, but maybe the script from http://www.getlazarus.org/  is a good try, it install lazarus and let it ready to cross compile for 32 bit linux, windows and mac os x. Guess it will be easier than setup things manually.

Offline

#14 2015-02-24 07:57:46

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

Re: Start with mORMot Framework in Lazarus

Thanks for the link.

Offline

#15 2015-03-21 23:16:04

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

After a while I came back to mORMot and I'm reading documentation.
In chapter 13 it talks about  stored procedure but I cant understand.
I made a project before with this structure:
A server with MySQL and stored procedures for signup,sign in,getting list of users,getting list of users notes,search by user and subject and many others that are not a simple SQL.For these jobs I made stored procedures and in them I check validation of user ID or anything else and it will resturn result list and for jobs like signup it give some user info and session info and if there is error it add error to a variable and return result false so in php API it give last error and all result and errors will convert to JSON with php.
In client I work with API just like many other services with JSON API.
Also beside of Lazarus PC client I have Android and iOS version and I cant use mORMot in them and they will work with JSON.

So I want to make a new project like that one and for this one I want to remove php and do all works with FPC and mORMot and feel proud wink
I think I cant remove stored procedures because I cant let client do SQL jobs.
So can any one help me what is the best structure for doing this with mORMot?

Offline

#16 2015-03-21 23:22:20

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Also I want to ask is there a way to choose stored procedure for insert ,delete or add like libraries for example UniDac?
So when we call delete class call DeleteUser with needed parameters procedure and not delete from usertable where Id=n ?

Offline

#17 2015-03-22 08:29:46

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

Re: Start with mORMot Framework in Lazarus

I guess "13.2.1. Why to avoid stored procedures" - see http://synopse.info/files/html/Synopse% … #TITLE_286 - may be disturbing...
It breaks a well established dogma of the last decades, in most companies.
But it is not my own opinion, and where n-Tier (and later on SOA) has been identified as a much better design.
Putting the logic in the DB is a wrong idea, especially for a new project, aiming to scale to Internet clients.

aradeonas wrote:

I think I cant remove stored procedures because I cant let client do SQL jobs.

Yes you can!
Or even more, you should remove stored procedures!
Just let clients access to the server via SOA interface-based services.

aradeonas wrote:

Also beside of Lazarus PC client I have Android and iOS version and I cant use mORMot in them and they will work with JSON.

Sorry, but you did not get what mORMot is aware of.
You can write Android and iOS clients with mORMot!
See http://synopse.info/files/html/Synopse% … ml#TITL_86

Ensure you read the framework overview, and the first chapters.
See http://synopse.info/files/html/Synopse% … tml#SOURCE

Offline

#18 2015-03-22 08:55:12

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Hi ab and Thank you,
Its not distributing but its mind shaking wink
I read from first to chapter 14 until now, About iOS and Android I now mORMot support them but my team use AndroidStudio and java for android programing for example.For this I need JSON API like other services API.Or there is other way that I dont know?

About access to the server,its tempting what you say but one of my biggest problem is Im not alone just with FPC,project have Java Android client,iOS client and a FPC client,also maybe we have a web client so this way that client do all things and error messages and other stuffs is confusing for me also what if I want to release API for others?
Like you I dont like writing stored procedure in MySQL for many reason like speed and debugging but its not easy to understand.

I should say project is simple but its priority is multi platforming but I want to done all server stuff with FPC not PHP or python.

Last edited by aradeonas (2015-03-22 08:56:50)

Offline

#19 2015-03-22 09:11:16

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

Re: Start with mORMot Framework in Lazarus

About Java clients, see http://synopse.info/forum/viewtopic.php … 379#p15379

There is some working Java code available at https://github.com/synopse/mORMot/tree/ … /ChinaPeng
Mutstache templates for Java are on the way!
Any help is welcome.

Offline

#20 2015-03-22 09:21:58

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

So in this way I should for example SignUp user do all checking in FPC or Delphi and JAVA?
Write all code again for any new language?For that we made stored procedure in DB so we have procedure that get parameters and do all stuff and return result so we  can use API from every where.
So ab please help me to understand what is the best way for such a thing? You now mORMot more that any one and I explained project for you and now I will explain more
Project is a simple and fast note synchronizer that we want to make it for free for our selves and other people specially developers.
Windows,Web,Android and iOS clients.they are in any languages just PC versions will be in FPC and maybe Website.
Also in the future we want to publish API for other developers to work with service.
How can I do it with mORMot in best way?

Offline

#21 2015-03-22 13:14:17

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

Re: Start with mORMot Framework in Lazarus

No, you write the logic once on the server side, using Delphi/FPC.
The DB is accessed only on the server side.
Then you publish it via SOA interface-based services.
And from the client side, you execute the SOA services, as a REST call with a few parameters.

I suspect you did not understand exactly what SOA means, and how interface-based services work.
See http://synopse.info/files/html/Synopse% … ml#TITL_17
and http://synopse.info/files/html/Synopse% … ml#TITL_63
and http://synopse.info/files/html/Synopse% … DE_TITL_86

Offline

#22 2015-03-22 15:53:10

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Thank you,I'm reading now.
It feels I'm in school again wink (feeling good)

Also I want to ask again is there a way to choose stored procedure for insert ,delete or add like libraries for example UniDac?
So when we call delete class call DeleteUser with needed parameters procedure and not delete from usertable where Id=n ?

Last edited by aradeonas (2015-03-22 16:05:21)

Offline

#23 2015-03-22 19:03:25

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

Re: Start with mORMot Framework in Lazarus

No we do not support procedure for insert ,delete or add.

From our point of view, there is no benefit among out Batch mode.

Offline

#24 2015-03-28 08:25:28

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Hi,
I read to from beginning to end of chapter 17.
I learned about mORmot and its great system and about SOA but still I cant find out how make my service,I tried my best but its hard for me,in PHP its easy but in mORmot I still cant understand it enough.
So I decided to make a test project until it be the best.

Test project is a simple note manager server with MySQL DB and should have an simple API,user can login,add,update,delete and logout,Just it.

I made what I learned (I learned more but its not useful for making project),Here it is and I know its so less.

unit NoteInterface;

interface
uses mORMot;

type
  TNote=record
  ID:Integer;
  Text:RawUTF8;
  Timestamp:TDateTime;
  end;

  INoteManagaer = interface(IInvokable)
   ['{539876CF-E719-4129-A359-2916A99F8FA3}']
    function LogIn(Username,Password:RawUTF8):Boolean;
    function LogOut:Boolean;
    function Add(Text:RawUTF8;Out Note:TNote):Boolean;
    function Delete(Note:TNote):Boolean;
    function Update(var Note:TNote;Text:RawUTF8):Boolean;
  end;
const
  ROOT_NAME = 'root';
  PORT_NAME = '888';
  APPLICATION_NAME = 'NoteManager';

implementation

end.
program NoteTest;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  NoteInterface in 'NoteInterface.pas';

type
  TServiceCalculator = class(TInterfacedObject, INoteManagaer)
  public
       function LogIn(Username,Password:RawUTF8):Boolean;
    function LogOut:Boolean;
    function Add(Text:RawUTF8;Out Note:TNote):Boolean;
    function Delete(Note:TNote):Boolean;
    function Update(var Note:TNote;Text:RawUTF8):Boolean;
  end;

{ TServiceCalculator }

function TServiceCalculator.Add(Text: RawUTF8; out Note: TNote): Boolean;
begin
  //?
end;

function TServiceCalculator.Delete(Note: TNote): Boolean;
begin
 //?
end;

function TServiceCalculator.LogIn(Username, Password: RawUTF8): Boolean;
begin
  //?
end;

function TServiceCalculator.LogOut: Boolean;
begin
  //?
end;

function TServiceCalculator.Update(var Note: TNote;
  Text: RawUTF8): Boolean;
begin
 //?
end;

var
  aModel: TSQLModel;
  aServer: TSQLRestServer;
  aHTTPServer: TSQLHttpServer;
begin
  aModel := TSQLModel.Create([],ROOT_NAME);
  try
    aServer := TSQLRestServerFullMemory.Create(aModel,'test.json',false,true);//?
    try
      aServer.ServiceRegister(TServiceCalculator,[TypeInfo(INoteManagaer)],sicShared);
      aHTTPServer := TSQLHttpServer.Create(PORT_NAME,[aServer],'+',useHttpApiRegisteringURI);
      try
        writeln(#10'Press [Enter] to close the server.'#10);
        readln;
      finally
        aHTTPServer.Free;
      end;
    finally
      aServer.Free;
    end;
  finally
    aModel.Free;
  end;
end.

Now I dont know these things:
1-Is it my interface structure even right?
2-How can I make server for using it from a Delphi apps and a web browser?).
3-Should I right exact SQL code in functions like "insert into notetable values(:Text)" with TSQLDBConnectionProperties?
4-Im sure my server setting is wrong but in a lot of class and their options Im lost.

PS,Yes I read demos like CrossPlatform Clients but no I didnt get them right and yes maybe Im slow but Im triying for two week to learn and I need help.I always learn from examples and mORmot's examples are not a begin to end example and make it more complicated for me and about main demo its too complicated for a starter like me.

Offline

#25 2015-03-28 09:25:59

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

Re: Start with mORMot Framework in Lazarus

1. Take care of parameter "direction". Use explicitly "const", "var" and "out" corresponding to the direction you need.
You should put "const" for IN parameters, "out" for OUT parameters, and "var" for IN+OUT parameters.
I do not understand what you want to do. Having the "Text" value in both as a parameter and as "TNote" is of little interest IMHO.
You defined the service as "sicShared", but it may benefit of "sicClientDriven" so that the instance live time on the server would be synchronized with the instance live time on the client.

2. For web app, see the sample MVC, a the corresponding http://synopse.info/files/html/Synopse% … E_TITL_108

3. Rather use the ORM than manual SQL.

4. Your settings sounds right, but it depends on what to do.

Offline

#26 2015-03-28 10:11:31

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

1-Ok I get it.
Its a simple note manager,User write text and add it and I want to get back TNote that contain note info like time stamp,Id and others.
3-How?I read ORM parts but every way I think I need to write a sql code for getting for example note info or adding it into database.
4-As I said I want to make a test project and in this way learn how work with mORMot and the subject is a simple note manager.

ab is it OK Im asking these newbie question here? I dont want to spam here but as you saw I stuck wink

Last edited by aradeonas (2015-03-28 10:52:55)

Offline

#27 2015-03-28 13:04:55

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

I should say my imagination is like this in PHP mode:
-We have some MySQL procedures like login,add,search and ... that do what they should.
-We make some php procedures like before that call Mysql procedures and control error and return result as JSON and I could go to api.example.com/notemanager/notes?ID=1&key=piqmsbzctyonvmascus (key is a client key for auth) and get note with ID=1 as JSON.
-We work with JSON in Delphi,FPC or Android app and done.

In mORMot I understand that:
-I should make an interface for server that have all the functions and give results back as out variant so can take care of memory.
-I should implement interface as a class and use it in client.
-It sound easy but there is many many items that talked about in doc that I dont know how and when to use them,Also how can I get JSON from a browser for just looking at it or use it in for example in Android(as I dont know enough to use or make a mORMot implementation in java so use it old school way,pars and use).

If I can make it work I will make this project clean and it can help me others to have a walkthrough for mORMot.

Offline

#28 2015-03-28 16:12:40

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

Re: Start with mORMot Framework in Lazarus

Did you read the web MVC part of the doc?
(My previous link)
There is full ORM data access,with no SQL written.

Offline

#29 2015-03-28 16:52:23

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

I went for reading it when you said and its abot to finish.Then I will update test project and post here.

Offline

#30 2015-03-29 08:27:24

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

OK.I read the chapter and check demos 14,16 and 30 again.But I understand that I should use interfaces in server and client like calculator example but in MVC demo just right class,why is that?
I changes structure like this but is it right?

unit NoteInterface;

interface
uses
  SysUtils,
  SynCommons,
  SynCrypto,
  mORMot;

type
 TSQLRecordTimeStamped = class(TSQLRecord)
  private
    fCreatedAt: TCreateTime;
    fModifiedAt: TModTime;
  published
    property CreatedAt: TCreateTime read fCreatedAt write fCreatedAt;
    property ModifiedAt: TModTime read fModifiedAt write fModifiedAt;
  end;

  TSQLSomeone = class(TSQLRecordTimeStamped)
  private
    fFirstName: RawUTF8;
    fFamilyName: RawUTF8;
    fEmail: RawUTF8;
    fHashedPassword: RawUTF8;
    fLogonName: RawUTF8;
  public
    procedure SetPlainPassword(const PlainPassword: RawUTF8);
    function CheckPlainPassword(const PlainPassword: RawUTF8): boolean;
    function Name: RawUTF8; 
  published
    property LogonName: RawUTF8 index 30 read fLogonName write fLogonName stored AS_UNIQUE;
    property FirstName: RawUTF8 index 50 read fFirstName write fFirstName;
    property FamilyName: RawUTF8 index 50 read fFamilyName write fFamilyName;
    property Email: RawUTF8 index 40 read fEmail write fEmail;
    property HashedPassword: RawUTF8 index 64 read fHashedPassword write fHashedPassword;
  end;

  TSQLUser = class(TSQLSomeone)
  private
    public
          class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8;
      Options: TSQLInitializeTableOptions); override;
  end;

    TSQLContent = class(TSQLRecordTimeStamped)
  private
    fContent: RawUTF8;
    fTitle: RawUTF8;
    fUser: TSQLUser;

  published
    property Title: RawUTF8 index 120 read fTitle write fTitle;
    property Content: RawUTF8 read fContent write fContent;
    property User: TSQLUser read fUser write fUser;
  end;


  TSQLNote = class(TSQLContent)
  private
  public
    class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8;
      Options: TSQLInitializeTableOptions); override;
  end;

  INoteManager = interface(IInvokable)
   ['{539876CF-E719-4129-A359-2916A99F8FA3}']
    function LogIn(const Username:RawUTF8; const Password:RawUTF8):Boolean;
    function LogOut:Boolean;
    function Add(const Text:RawUTF8;Out Note:TSQLNote):Boolean;
    function Delete(const Note:TSQLNote):Boolean;
    function Update(var Note:TSQLNote;Text:RawUTF8):Boolean;
  end;



const
  ROOT_NAME = 'root';
  PORT_NAME = '888';
  APPLICATION_NAME = 'NoteManager';


    function CreateModel: TSQLModel;
implementation

const
  SALT = 'JustANoteManager';

function CreateModel: TSQLModel;
begin
  result := TSQLModel.Create([TSQLNote,TSQLUser],'note');
end;

function TSQLSomeone.CheckPlainPassword(const PlainPassword: RawUTF8): boolean;
begin
  result := fHashedPassword=SHA256(SALT+LogonName+PlainPassword);
end;

function TSQLSomeone.Name: RawUTF8;
begin
  result := FirstName+' '+FamilyName;
end;

procedure TSQLSomeone.SetPlainPassword(const PlainPassword: RawUTF8);
begin
  fHashedPassword := SHA256(SALT+LogonName+PlainPassword);
end;

{ TSQLUser }

class procedure TSQLUser.InitializeTable(Server: TSQLRestServer;
  const FieldName: RawUTF8; Options: TSQLInitializeTableOptions);
begin
  inherited;

end;

{ TSQLNote }

class procedure TSQLNote.InitializeTable(Server: TSQLRestServer;
  const FieldName: RawUTF8; Options: TSQLInitializeTableOptions);
begin
  inherited;

end;

end.

Offline

#31 2015-03-30 17:32:34

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

Dear ab Im waiting for your answer what ever it is.

Offline

#32 2015-03-30 18:05:44

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

Re: Start with mORMot Framework in Lazarus

It sounds better.

Now you need to implement a class (e.g. a TInterfacedObject) which to implement INoteManager methods.

Offline

#33 2015-03-30 19:34:15

aradeonas
Member
Registered: 2015-02-21
Posts: 19

Re: Start with mORMot Framework in Lazarus

And in the methods work with DB like MVC demo?

Offline

Board footer

Powered by FluxBB