Show/Hide Toolbars

TMS XData Documentation

Navigation: Getting Started > Creating the Server Manually

Example 1: In-memory SQLite for testing/development

Scroll Prev Top Next More

The following example is a minimum console application that illustrates how to start an XData server at address "http://localhost:2001/tms/music" using an in-memory SQLite database. 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.

 

An extra note about this example: Since it is an in-memory database, the database will be empty every time the server starts. Thus, before the server starts, the method "UpdateDatabase(Connection)", which is declared in unit DatabaseUtils (also not listed here), will be called to create the required tables and data. Such a thing is a regular TMS Aurelius procedure and is out of the scope of this example as well. The purpose of this example is to show how to start an XData server.

 

program SQLiteConsoleServer;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils,
  Aurelius.Drivers.Interfaces,
  Aurelius.Drivers.SQLite,
  Aurelius.Sql.SQLite,
  Aurelius.Schema.SQLite,
  Sparkle.HttpSys.Server,
  XData.Aurelius.ConnectionPool,
  XData.Server.Module,
  AureliusEntities,
  DatabaseUtils;
 
procedure StartServer;
var
  Server: THttpSysServer;
  Connection: IDBConnection;
begin
  Server := THttpSysServer.Create;
  try
    Connection := TSQLiteNativeConnectionAdapter.Create(':memory:');
    UpdateDatabase(Connection);
    Server.AddModule(TXDataServerModule.Create('http://localhost:2001/tms/music',
      TDBConnectionPool.Create(1,
        function: IDBConnection
        begin
          Result := Connection;
        end)));
    Server.Start;
    WriteLn('Server started. Press ENTER to stop.');
    ReadLn;
  finally
    Server.Free;
  end;
end;
 
begin
  StartServer;
end.