#1 2022-04-06 11:06:10

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

Make simple things even easier

I don't know if it's useful, but couldn't a handful of functions in a helper unit that look like the following be useful? There are always places in the source code where you want to solve something quickly and easily. And there are always small tools to write from time to time, just to do simple tasks. Just a thought.

With following functions:

function CreateRestServerDB(const pmcTables: array of TOrmClass; const pmcDBFileName: TFileName; pmDeleteDBFile: Boolean = True): TRestServerDB;
begin
  if pmDeleteDBFile then
    DeleteFile(pmcDBFileName);

  Result := TRestServerDB.CreateWithOwnModel(pmcTables, pmcDBFileName, {HandleUserAuthentication=} False);
  Result.DB.Synchronous := smOff;
  Result.DB.LockingMode := lmExclusive;
  Result.Server.CreateMissingTables;
end;

Or with the following simple classes:

type
  TSimpleRestServerDBHelper = class(TObject)
  private
    FRestServerDB: TRestServerDB;
    function GetRestServer: IRestOrm;
  protected
    procedure CreateRestServerDB(const pmcTables: array of TOrmClass; const pmcDBFileName: TFileName; pmDeleteDBFile: Boolean); virtual;
    procedure Main; virtual; abstract;
    property RestServer: IRestOrm
      read GetRestServer;
  public
    destructor Destroy; override;
    class procedure Run(const pmcTables: array of TOrmClass; const pmcDBFileName: TFileName; pmDeleteDBFile: Boolean = True);
  end;

And an implementation like:

destructor TSimpleRestServerDBHelper.Destroy;
begin
  FRestServerDB.Free;
  inherited Destroy;
end;

function TSimpleRestServerDBHelper.GetRestServer: IRestOrm;
begin
  if FRestServerDB <> Nil then
    Result := FRestServerDB.Server
  else
    Result := Nil;
end;

procedure TSimpleRestServerDBHelper.CreateRestServerDB(const pmcTables: array of TOrmClass; const pmcDBFileName: TFileName; pmDeleteDBFile: Boolean);
begin
  if pmDeleteDBFile then
    DeleteFile(pmcDBFileName);

  FRestServerDB := TRestServerDB.CreateWithOwnModel(pmcTables, pmcDBFileName, {HandleUserAuthentication=} False);
  FRestServerDB.DB.Synchronous := smOff;
  FRestServerDB.DB.LockingMode := lmExclusive;
  FRestServerDB.Server.CreateMissingTables;
end;

class procedure TSimpleRestServerDBHelper.Run(const pmcTables: array of TOrmClass; const pmcDBFileName: TFileName; pmDeleteDBFile: Boolean = True);
begin
  with Self.Create do
  try
    CreateRestServerDB(pmcTables, pmcDBFileName, pmDeleteDBFile);
    try
      Main;
    except
      on E: Exception do
        WriteLn(E.ClassName, ': ', E.Message);
    end;

    WriteLn('Press [Enter] to quit');
    ConsoleWaitForEnterKey;
  finally
    Free;
  end;
end;

Which can be used as follows:

type
  TMySimpleRestServerDB = class(TSimpleRestServerDBHelper)
  protected
    procedure Main; override;
  end;
  
procedure TMySimpleRestServerDB.Main;
var
  i: Integer;
  item: TOrmTest;
begin
  item := TOrmTest.Create;
  try
    for i := 1 to 10 do
      RestServer.Add(item, True);
  finally
    item.Free;
  end;

  RestServer.Delete(TOrmTest, 'RowID>?', [2]);
end;

begin
  TMySimpleRestServerDB.Run([TOrmTest], SQLITE_MEMORY_DATABASE_NAME);
end.

I have written for myself some small functions that are helpful, whether an official unit makes sense, I do not know. If not, maybe this post can show some beginners the first steps with mORMot.

With best regards
Thomas

Offline

#2 2022-04-07 07:37:34

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

Re: Make simple things even easier

Please follow the forum rules, and don't post such huge piece of code directly in the forum.

I guess your ideas are very good.
Simplification is indeed needed for the simplest tasks: stand-alone DB access, public API server running as a daemon, and so on...
The idea of having a ready-to-be-consume set of classes, without the need to instantiate each lower level mORMot class (TDaemon/TOrmModel/TRestDBServer...) make perfectly sense.
If a user knows what he needs, we may offer some ready-to-be-instantiated class for it.

Could you post what you have on gist page(s), so that we can look at it?
Then discuss here.

Offline

Board footer

Powered by FluxBB