The system is divided into three main layers, each responsible for a specific aspect of the synchronization process:
- Client-Side Database Layer: Manages local storage using IndexedDB, facilitating offline data storage and operations.
- Synchronization Layer: Acts as the mediator between the client and server databases, managing data synchronization, conflict resolution, and change tracking.
- Server-Side Logic: Handles interactions with the MongoDB database, processes synchronization requests, and maintains a global state of data across all clients.
LoggerService
- Handles logging across the application.DatabaseService
- Manages database connections and operations.DataPreparationService
- Prepares and validates data before synchronization.DataSyncService
- Responsible for the synchronization logic, including conflict resolution.ConflictResolver
- Defines strategies for resolving data conflicts during synchronization.SyncManager
- Coordinates the entire synchronization process, utilizing all other services.IndexedDBUtil
- Manages local IndexedDB operations (if this is relevant to the overall architecture).
SyncManager
orchestrates the synchronization process, relying onDatabaseService
for database interactions,DataPreparationService
for preparing data, andDataSyncService
for handling the synchronization logic.DataSyncService
usesConflictResolver
to handle conflicts andLoggerService
to log activities.DataPreparationService
also utilizesLoggerService
to log its activities.DatabaseService
is used directly bySyncManager
for database operations.LoggerService
is used across various services for logging different events and errors.
Here's a textual layout of the architecture:
+-----------------+
| SyncManager |
+-----------------+
| |
+----------+ +-----------+
| |
+-------v--------+ +------v------+
| DatabaseService| | DataPrepService|
+----------------+ +--------------+
|
|
+----------------+ +------v------+
| IndexedDBUtil | | DataSyncService|
+----------------+ +--------------+
|
+-------v-------+
| ConflictResolver |
+-----------------+
+-----------------+
| LoggerService |
+-----------------+
+------------------------------------+
| <<service>> |
| LoggerService |
|------------------------------------|
| -logger: winston.Logger |
|------------------------------------|
| +log(message: string, level: string)|
+---------------^--------------------+
| uses
+------------------------------------+ +------------------------------------+
| <<service>> | | <<service>> |
| SyncManager | | DatabaseService |
|------------------------------------| |------------------------------------|
| -databaseService: DatabaseService |------->| +connect(): Promise<void> |
| -dataPreparationService: | | +disconnect(): Promise<void> |
| DataPreparationService | +------------------------------------+
| -dataSyncService: DataSyncService | | <<service>> |
| -loggerService: LoggerService | | DataPreparationService |
|------------------------------------| |------------------------------------|
| +performSync(): Promise<void> | | +prepareData(): { data: string } |
+-----------------^------------------+ +---------------^--------------------+
| injects uses |
+------------------------------------+ +------------------------------------+
| <<service>> | | <<service>> |
| DataSyncService | | DataSyncService |
|------------------------------------| |------------------------------------|
| -logger: LoggerService |<-------| -logger: LoggerService |
|------------------------------------| |------------------------------------|
| +sync(data: any): void | | +sync(data: any): void |
+------------------------------------+ +------------------------------------+
Step-by-Step Process:
-
Start Synchronization:
- The user or system triggers the synchronization process in the
SyncManager
.
- The user or system triggers the synchronization process in the
-
Connect to Database:
- The
SyncManager
sends a message toDatabaseService
to establish a database connection. DatabaseService
acknowledges the connection.
- The
-
Fetch Data:
SyncManager
requests raw data fromDatabaseService
.DatabaseService
retrieves and returns the data.
-
Prepare Data:
SyncManager
sends the raw data toDataPreparationService
for processing.DataPreparationService
processes the data and returns the prepared data.
-
Synchronize Data:
SyncManager
sends the prepared data toDataSyncService
.DataSyncService
initiates the synchronization.- If conflicts are detected,
DataSyncService
requests resolution fromConflictResolver
. ConflictResolver
resolves the conflicts and returns the resolved data.DataSyncService
completes the synchronization by sending the resolved data to the server or database.
-
Complete Synchronization:
SyncManager
logs the completion and possibly returns a status to the user or system.
+------------------+ +------------------+ +---------------------+ +----------------------+ +------------------+
| User/System | | SyncManager | | DatabaseService | | DataPreparationService| | DataSyncService |
+------------------+ +------------------+ +---------------------+ +----------------------+ +------------------+
| | | | |
|---------------------->| | | |
| Start Sync | | | |
|---------------------->| | | |
| Connect Database |------------------------>| | |
| | Connect | | |
| |<------------------------| | |
| | | | |
| | Fetch xData |-----------------------------| |
| | | Return Data | |
| |<------------------------| | |
| | | | |
| | Prepare Data |-----------------------------| |
| | | Return Prepared Data | |
| |<------------------------------------------------------| |
| | | | Synchronize Data |
| |------------------------------------------------------>| |
| | | | Resolve Conflicts |
| | | |---------------------------->
| | | | Return Resolved Data |
| | | |<----------------------------|
| | | | |
| | Complete Sync |-----------------------------| |
| |<------------------------------------------------------| |
+------------------+ +------------------+ +---------------------+ +----------------------+ +------------------+
+--------------+ +--------------+ +-----------------------+ +----------------+ +---------------+
| SyncManager | | DatabaseSvc | | DataPreparationSvc | | DataSyncSvc | | LoggerService |
+------+-------+ +------+-------+ +----------+-----------+ +--------+-------+ +-------+-------+
| | | | |
| [1] connect() | | | |
|-------------------->| | | |
| | | | |
| | [2] <Promise resolved> | | |
|<--------------------| | | |
| | | | |
| [3] prepareData() | | | |
|----------------------------------------------->| | |
| | | [4] <Data preparation> | |
| | |----------------------------| |
| | | | |
| | |<---------------------------| |
| | | | |
| [5] sync(data) | | | |
|----------------------------------------------------------+ | |
| | | | [6] <Sync start> | |
| | | |----------------->| |
| | | | | [7] log(info) |
| | | | |--------------------->|
| | | | | |
| | | | | [8] <Log recorded> |
| | | | |<---------------------|
| | | | | |
| | | | [9] <Sync done> | |
| | |<-------|-----------------| |
| | | | |
| | | [10] Return sync result | |
|<----------------------------------------------------------+ | |
| | | | |
| [11] log(error) if any | | |
|---------------------+------------------------->| | |
| | | | |
| [12] <Error logged> | | | |
|<--------------------| | | |
| | | | |
+------+-------+ +------+-------+ +----------+-----------+ +--------+-------+ +-------+-------+
| SyncManager | | DatabaseSvc | | DataPreparationSvc | | DataSyncSvc | | LoggerService |
+--------------+ +--------------+ +-----------------------+ +----------------+ +---------------+