#1 2018-10-21 00:20:22

RaelB
Member
Registered: 2010-08-04
Posts: 57

Working with an offline client and central server

Hi,

I would like to understand how to deal with the following type of scenario using mormot:

Offline client can create new records. Also, server can create new records.
At some point client and server will be synced.

Ordinarily, one would use a GUID as ID field for each record. (i.e. client and server need to be creating globally unique ids)
Also, when sending a record from client to server, one includes the ID in the record (i.e. not created at server.)

TSQLRecord.ID is int64, which is pretty large, so perhaps this could be used an an equivalent to GUID. (i.e a GUID is 16 bytes but only uses a restricted range for each byte (0-F). int64 is 8 bytes but could use a larger range..). However, more of a "problem" is that TSQLRecord.ID is read only, so one cannot achieve above requirement (of sending Record.ID to server).

So is the way to deal with such a situation to basically ignore the TSQLRecord.ID, and supply our own GUID field?

Thank you
Rael

Offline

#2 2018-10-21 06:05:11

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Working with an offline client and central server

For mORMot's built-in support for your case, check TSQLModel.SetIDGenerator and SynCommons.TSynUniqueIdentifierGenerator.

or check https://github.com/suiyunonghen/DelphiSnowflake.

or check my little function if your data volume is low: https://synopse.info/forum/viewtopic.php?id=2522


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#3 2018-10-21 14:03:00

RaelB
Member
Registered: 2010-08-04
Posts: 57

Re: Working with an offline client and central server

Thanks for the info, that is interesting.

However, it does not answer the second issue: How do I supply a rec.ID to the server when syncing records from the client, since rec.ID is readonly?

Offline

#4 2018-10-22 02:44:10

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: Working with an offline client and central server

Use rec.IDValue instead of rec.ID


Esteban

Offline

#5 2018-10-22 15:26:45

RaelB
Member
Registered: 2010-08-04
Posts: 57

Re: Working with an offline client and central server

Hi,

That does not seem to work. I tried the following (using Project 02 with new database)

procedure TForm1.AddButtonClick(Sender: TObject);
var
  Rec: TSQLSampleRecord;
begin
  Rec := TSQLSampleRecord.Create;
  try
    // we use explicit StringToUTF8() for conversion below
    // a real application should use TLanguageFile.StringToUTF8() in mORMoti18n
    Rec.Name := StringToUTF8(NameEdit.Text);
    Rec.Question := StringToUTF8(QuestionMemo.Text);

    Rec.IDValue := 30;  // <----

    if Database.Add(Rec, true) = 0 then
      ShowMessage('Error adding the data')
    else
    begin
      NameEdit.Text := '';
      QuestionMemo.Text := '';
      NameEdit.SetFocus;

      lblID.Caption := 'ID: ' + IntToStr(Rec.ID);
    end;
  finally
    Rec.Free;
  end;
end;

The added Rec.ID is 1, not 30 as I had set it.

Offline

#6 2018-10-22 16:12:40

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

Re: Working with an offline client and central server

RTFM and the TSQLRest.Add parameters.
I am sure you will find out the missing boolean flag. big_smile

Offline

Board footer

Powered by FluxBB