import { api, ConnectionTestResult, replication, system, TearDownOptions } from '@powersync/service-core'; import { MySQLRouteAPIAdapter } from '../api/MySQLRouteAPIAdapter.js'; import { checkSourceConfiguration } from '../common/check-source-configuration.js'; import { BinLogReplicator } from '../replication/BinLogReplicator.js'; import { MySQLConnectionManager } from '../replication/MySQLConnectionManager.js'; import { MySQLConnectionManagerFactory } from '../replication/MySQLConnectionManagerFactory.js'; import { MySQLErrorRateLimiter } from '../replication/MySQLErrorRateLimiter.js'; import * as types from '../types/types.js'; import { MySQLConnectionConfig } from '../types/types.js'; export class MySQLModule extends replication.ReplicationModule { constructor() { super({ name: 'MySQL', type: types.MYSQL_CONNECTION_TYPE, configSchema: types.MySQLConnectionConfig }); } protected createRouteAPIAdapter(): api.RouteAPI { return new MySQLRouteAPIAdapter(this.resolveConfig(this.decodedConfig!)); } protected createReplicator(context: system.ServiceContext): replication.AbstractReplicator { const normalisedConfig = this.resolveConfig(this.decodedConfig!); const connectionFactory = new MySQLConnectionManagerFactory(normalisedConfig); return new BinLogReplicator({ id: this.getDefaultId(normalisedConfig.database), configuration: context.configuration, syncConfigEngine: context.syncConfigEngine, storageEngine: context.storageEngine, metricsEngine: context.metricsEngine, connectionFactory: connectionFactory, rateLimiter: new MySQLErrorRateLimiter() }); } /** * Combines base config with normalized connection settings */ private resolveConfig(config: types.MySQLConnectionConfig): types.ResolvedConnectionConfig { return { ...config, ...types.normalizeConnectionConfig(config) }; } async teardown(options: TearDownOptions): Promise { // No specific teardown required for MySQL } async testConnection(config: MySQLConnectionConfig) { this.decodeConfig(config); const normalizedConfig = this.resolveConfig(this.decodedConfig!); return await MySQLModule.testConnection(normalizedConfig); } static async testConnection(normalizedConfig: types.ResolvedConnectionConfig): Promise { const connectionManager = new MySQLConnectionManager(normalizedConfig, {}); const connection = await connectionManager.getConnection(); try { const errors = await checkSourceConfiguration(connection); if (errors.length > 0) { throw new Error(errors.join('\n')); } } finally { await connectionManager.end(); } return { connectionDescription: normalizedConfig.hostname }; } }