Show/Hide Toolbars

TMS XData Documentation

Navigation: Getting Started > Creating the Server Manually

Example 3: MS SQL Server with FireDAC (using TDataModule)

Scroll Prev Top Next More

The following example illustrates how to start an XData server at address "http://localhost:2001/tms/music" using FireDac to connect to any database (in the example, a MS SQL Server database). Compared to Example 2, this example uses a TFDConnection dropped onto a TDataModule to configure the database connection at design-time. It uses an unit named "AureliusEntities" which is not shown here for simplification. Such a unit should just contain the Aurelius mapped classes that will be exposed by the server. The connection pool is configured to hold up to 15 connections to the database.

 

Thus, consider you have a data module with an existing configured database connection using FireDac:

 

 

example3_datamodule

 

You can use such a configuration and create one instance of that data module when required by the server. You can use the TFDConnection component from it.

 

program FireDacConsoleServer;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils,
  Aurelius.Drivers.Base,
  Aurelius.Drivers.Interfaces,
  Aurelius.Drivers.FireDac,
  Aurelius.Sql.MSSQL,
  Aurelius.Schema.MSSQL,
  Sparkle.HttpSys.Server,
  XData.Aurelius.ConnectionPool,
  XData.Server.Module,
  AureliusEntities in '..\common\AureliusEntities.pas',
  DBDataModuleUnit in 'DBDataModuleUnit.pas' {DBDataModule: TDataModule};
 
procedure StartServer;
var
  Server: THttpSysServer;
begin
  Server := THttpSysServer.Create;
  try
    Server.AddModule(TXDataServerModule.Create('http://localhost:2001/tms/music',
      TDBConnectionPool.Create(15, TDBConnectionFactory.Create(
        function: IDBConnection
        var
          DBDataModule: TDBDataModule;
        begin
          // Example using FireDac and Data Module
          DBDataModule := TDBDataModule.Create(nil);
          Result := TFireDacConnectionAdapter.Create(DBDataModule.FDConnection1, DBDataModule);
        end
    ))));
    Server.Start;
    WriteLn('Server started. Press ENTER to stop.');
    ReadLn;
  finally
    Server.Free;
  end;
end;
 
begin
  StartServer;
end.