# 3. Meet mORMot 2 - Unit Structure
Enter the new territory
mORMot 2 represents a complete restructuring of the original framework. The monolithic units (SynCommons.pas, mORMot.pas) have been split into focused, layered units following SOLID principles.
This chapter introduces the new unit organization and helps you understand which units to include for your specific needs.
mORMot 2 organizes its ~130 units into 6 dependency layers:
┌─────────────────────────────────────────────────────────────────────┐
│ Layer 5: Application & Tools │
│ ┌─────┬─────┬────────┬──────┬───────┬─────┐ │
│ │ app │ ui │ script │ misc │ tools │ ddd │ │
│ └─────┴─────┴────────┴──────┴───────┴─────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────────┐
│ Layer 4: ORM / REST / SOA │
│ ┌─────┬──────┬─────┐ │
│ │ orm │ rest │ soa │ │
│ └─────┴──────┴─────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────────┐
│ Layer 3: Database Access │
│ ┌────┐ │
│ │ db │ │
│ └────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────────┐
│ Layer 2: Networking │
│ ┌─────┐ │
│ │ net │ │
│ └─────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────────┐
│ Layer 1: System Libraries & Cryptography │
│ ┌─────┬────────┐ │
│ │ lib │ crypt │ │
│ └─────┴────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────────┐
│ Layer 0: Foundation (RTL-Only) │
│ ┌──────┐ │
│ │ core │ (24 units - no mORMot dependencies) │
│ └──────┘ │
└─────────────────────────────────────────────────────────────────────┘
Key Principle: Lower layers never depend on higher ones. This ensures:
All units follow a consistent naming pattern:
mormot.<layer>.<feature>.pas
Examples:
mormot.core.json.pas - Core layer, JSON featuremormot.orm.core.pas - ORM layer, core functionalitymormot.db.sql.postgres.pas - Database layer, PostgreSQL SQL connectorUnits are located in folders matching their layer:
src/core/mormot.core..passrc/orm/mormot.orm..passrc/db/mormot.db..pas24 units providing RTL-level functionality with zero mORMot dependencies.
mormot.core.base (RTL types, ASM stubs, no dependencies)
└─► mormot.core.os (OS abstraction, threading)
└─► mormot.core.unicode (charset/encoding)
└─► mormot.core.text (text parsing, formatting)
└─► mormot.core.datetime (ISO-8601, TTimeLog)
└─► mormot.core.rtti (RTTI wrapper)
└─► mormot.core.buffers (compression, base64)
└─► mormot.core.data (TDynArray, serialization)
└─► mormot.core.json (JSON parsing)
└─► [higher units: variants, log, etc.]
| Unit | Purpose | Key Types |
|---|---|---|
mormot.core.base |
Foundation types, ASM | RawUtf8, PtrInt, TDynArray basics |
mormot.core.os |
OS abstraction | TSynLocker, GetTickCount64 |
mormot.core.unicode |
Charset conversion | Utf8ToWideString, WinAnsiToUtf8 |
mormot.core.text |
Text processing | FormatUtf8, CSV parsing |
mormot.core.rtti |
RTTI abstraction | TRttiCustom, PRttiInfo |
mormot.core.json |
JSON handling | TJsonWriter, GetJsonField |
mormot.core.data |
Data structures | TDynArray, TDynArrayHashed |
mormot.core.variants |
Dynamic documents | TDocVariant, IDocDict |
mormot.core.log |
Logging framework | TSynLog, ISynLog |
mormot.core.threads |
Threading utilities | TSynBackgroundThread |
mormot.core.test |
Testing framework | TSynTestCase |
mormot.core.mustache |
Template engine | TSynMustache |
mormot.core.interfaces |
Interface support | DI/IoC container |
uses
mormot.core.base, // Foundation types
mormot.core.os, // OS abstraction
mormot.core.text, // Text utilities
mormot.core.json; // JSON support
var
doc: TDocVariantData;
begin
doc.InitJson('{"name":"John","age":30}');
WriteLn(doc.U['name']); // Output: John
end;
14 units wrapping external libraries:
| Unit | External Library |
|---|---|
mormot.lib.z |
zlib/libdeflate compression |
mormot.lib.openssl11 |
OpenSSL 1.1/3.x |
mormot.lib.curl |
libcurl HTTP client |
mormot.lib.sspi |
Windows SSPI (Kerberos) |
mormot.lib.gssapi |
POSIX GSSAPI |
mormot.lib.quickjs |
QuickJS JavaScript engine |
mormot.lib.winhttp |
Windows WinHTTP |
mormot.lib.pkcs11 |
Hardware security modules |
10 units for cryptographic operations:
| Unit | Purpose |
|---|---|
mormot.crypt.core |
AES, SHA-2, SHA-3, HMAC, PBKDF2 |
mormot.crypt.secure |
High-level factories, password hashing |
mormot.crypt.ecc |
Elliptic Curve Cryptography |
mormot.crypt.ecc256r1 |
secp256r1 curve implementation |
mormot.crypt.rsa |
RSA encryption/signatures |
mormot.crypt.jwt |
JSON Web Tokens |
mormot.crypt.x509 |
X.509 certificates |
mormot.crypt.openssl |
OpenSSL wrapper |
uses
mormot.crypt.secure;
// High-level factories (recommended)
var
h: ICryptHash;
c: ICryptCipher;
begin
// Hashing
h := Hash('sha256');
digest := h.Full(@data[1], length(data));
// Encryption
c := Cipher('aes-256-ctr', @key[1], {encrypt:}true);
c.Process(plain, encrypted, '');
end;
18 units for network communication:
| Unit | Purpose |
|---|---|
mormot.net.sock |
Cross-platform socket abstraction |
mormot.net.http |
HTTP protocol state machine |
mormot.net.client |
HTTP clients (Socket, WinHTTP, curl) |
mormot.net.server |
HTTP servers (threaded, http.sys) |
mormot.net.async |
Event-driven async I/O |
| Unit | Purpose |
|---|---|
mormot.net.ws.core |
WebSocket protocol core |
mormot.net.ws.client |
WebSocket client |
mormot.net.ws.server |
WebSocket server |
mormot.net.ws.async |
Async WebSocket server |
| Unit | Purpose |
|---|---|
mormot.net.dns |
DNS resolution |
mormot.net.ldap |
LDAP client |
mormot.net.acme |
ACME/Let's Encrypt |
mormot.net.relay |
Firewall traversal |
mormot.net.openapi |
OpenAPI client generator |
27 units for SQL and NoSQL database access:
| Unit | Purpose |
|---|---|
mormot.db.core |
Shared database types |
mormot.db.sql |
Abstract SQL classes |
mormot.db.proxy |
Remote database proxy |
| Unit | Database |
|---|---|
mormot.db.raw.sqlite3 |
SQLite3 native API |
mormot.db.raw.postgres |
PostgreSQL API |
mormot.db.raw.oracle |
Oracle OCI |
mormot.db.raw.odbc |
ODBC API |
mormot.db.raw.oledb |
OLE DB API |
| Unit | Database |
|---|---|
mormot.db.sql.sqlite3 |
SQLite3 |
mormot.db.sql.postgres |
PostgreSQL |
mormot.db.sql.oracle |
Oracle |
mormot.db.sql.odbc |
Any ODBC source |
mormot.db.sql.oledb |
Any OLE DB source |
mormot.db.sql.zeos |
Via ZDBC (cross-database) |
| Unit | Database |
|---|---|
mormot.db.nosql.bson |
BSON encoding |
mormot.db.nosql.mongodb |
MongoDB client |
| Unit | Purpose |
|---|---|
mormot.db.rad.firedac |
FireDAC integration |
mormot.db.rad.unidac |
UniDAC integration |
mormot.db.rad.ui.orm |
ORM-aware TDataSet |
9 units for Object-Relational Mapping:
| Unit | Purpose |
|---|---|
mormot.orm.base |
Low-level ORM types |
mormot.orm.core |
TOrm, TOrmModel, IRestOrm |
mormot.orm.rest |
REST-based ORM base |
mormot.orm.client |
ORM client |
mormot.orm.server |
ORM server |
mormot.orm.storage |
Storage engine abstraction |
mormot.orm.sql |
SQL-based storage |
mormot.orm.sqlite3 |
SQLite3 ORM |
mormot.orm.mongodb |
MongoDB ODM |
8 units for RESTful services:
| Unit | Purpose |
|---|---|
mormot.rest.core |
TRest base class |
mormot.rest.client |
REST client |
mormot.rest.server |
REST server |
mormot.rest.http.client |
HTTP REST client |
mormot.rest.http.server |
HTTP REST server |
mormot.rest.mvc |
MVC/MVVM support |
mormot.rest.sqlite3 |
SQLite3 REST server |
mormot.rest.memserver |
In-memory REST server |
4 units for Service-Oriented Architecture:
| Unit | Purpose |
|---|---|
mormot.soa.core |
Interface-based SOA types |
mormot.soa.client |
Client service stubs |
mormot.soa.server |
Server service implementation |
mormot.soa.codegen |
Service code generation |
| Unit | Purpose |
|---|---|
mormot.app.console |
Console application support |
mormot.app.daemon |
Daemon/service support |
| Unit | Purpose |
|---|---|
mormot.ui.core |
VCL/LCL compatibility |
mormot.ui.controls |
Custom controls |
mormot.ui.grid.orm |
ORM-aware grids |
mormot.ui.report |
Reporting engine |
mormot.ui.pdf |
PDF generation |
| Unit | Purpose |
|---|---|
mormot.script.quickjs |
QuickJS JavaScript engine |
uses
mormot.core.base,
mormot.core.os,
mormot.orm.core,
mormot.orm.sqlite3,
mormot.rest.sqlite3,
mormot.rest.http.server;
type
TOrmPerson = class(TOrm)
private
fName: RawUtf8;
fAge: Integer;
published
property Name: RawUtf8 read fName write fName;
property Age: Integer read fAge write fAge;
end;
var
Model: TOrmModel;
Server: TRestServerDB;
HttpServer: TRestHttpServer;
begin
Model := TOrmModel.Create([TOrmPerson]);
Server := TRestServerDB.Create(Model, 'data.db3');
Server.Server.CreateMissingTables;
HttpServer := TRestHttpServer.Create('8080', [Server], '+', useHttpAsync);
try
WriteLn('Server running...');
ReadLn;
finally
HttpServer.Free;
Server.Free;
Model.Free;
end;
end.
uses
mormot.core.base,
mormot.core.interfaces,
mormot.soa.core,
mormot.soa.server,
mormot.rest.server;
type
ICalculator = interface(IInvokable)
['{...GUID...}']
function Add(A, B: Integer): Integer;
end;
TCalculator = class(TInjectableObjectRest, ICalculator)
public
function Add(A, B: Integer): Integer;
end;
function TCalculator.Add(A, B: Integer): Integer;
begin
Result := A + B;
end;
// Registration
Server.ServiceDefine(TCalculator, [ICalculator], sicShared);
uses
mormot.core.base,
mormot.db.core,
mormot.db.sql,
mormot.db.sql.postgres;
var
Props: TSqlDBPostgresConnectionProperties;
Conn: TSqlDBConnection;
Stmt: TSqlDBStatement;
begin
Props := TSqlDBPostgresConnectionProperties.Create(
'localhost:5432', 'mydb', 'user', 'pass');
Conn := Props.ThreadSafeConnection;
Stmt := Conn.NewStatementPrepared('SELECT * FROM users WHERE id=?', true);
try
Stmt.Bind(1, 42);
Stmt.ExecutePrepared;
while Stmt.Step do
WriteLn(Stmt.ColumnUtf8(0));
finally
Stmt.Free;
end;
end;
| mORMot 1 Unit | mORMot 2 Units |
|---|---|
SynCommons.pas |
mormot.core. (24 units) |
mORMot.pas |
mormot.orm. + mormot.rest. |
SynDB.pas |
mormot.db. |
SynCrypto.pas |
mormot.crypt. |
SynCrtSock.pas |
mormot.net. |
SynEcc.pas |
mormot.crypt.ecc |
| mORMot 1 | mORMot 2 |
|---|---|
TSQLRecord |
TOrm |
TSQLModel |
TOrmModel |
TSQLRest |
TRest |
TSQLRestServer |
TRestServer |
TSQLRestClient |
TRestClient |
TSQLRestServerDB |
TRestServerDB |
TSQLHttpServer |
TRestHttpServer |
RawUTF8 |
RawUtf8 |
By default, mORMot 2 provides type aliases for compatibility:
type
TSQLRecord = TOrm;
TSQLModel = TOrmModel;
// etc.
Define PUREMORMOT2 to disable these and use only new names:
{$DEFINE PUREMORMOT2}
Next Chapter: Core Units (mormot.core.)*
| Previous | Index | Next |
|---|---|---|
| Chapter 2: Architecture Principles | Index | Chapter 4: Core Units |