Files
flutter/lib/powersync/connector.dart
2026-04-24 10:35:13 +02:00

156 lines
5.2 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2020 - 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// This file performs setup of the PowerSync database
import 'dart:convert';
import 'package:logging/logging.dart';
import 'package:powersync/powersync.dart';
import 'package:wger/powersync/api_client.dart';
final logger = Logger('powersync-django');
/// Postgres Response codes that we cannot recover from by retrying.
final List<RegExp> fatalResponseCodes = [
// Class 22 — Data Exception
// Examples include data type mismatch.
RegExp(r'^22...$'),
// Class 23 — Integrity Constraint Violation.
// Examples include NOT NULL, FOREIGN KEY and UNIQUE violations.
RegExp(r'^23...$'),
// INSUFFICIENT PRIVILEGE - typically a row-level security violation
RegExp(r'^42501$'),
];
class DjangoConnector extends PowerSyncBackendConnector {
final String baseUrl;
final ApiClient apiClient;
DjangoConnector({required this.baseUrl}) : apiClient = ApiClient(baseUrl);
/// Get a token to authenticate against the PowerSync instance.
@override
Future<PowerSyncCredentials?> fetchCredentials() async {
// Somewhat contrived to illustrate usage, see auth docs here:
// https://docs.powersync.com/usage/installation/authentication-setup/custom
final session = await apiClient.getPowersyncToken();
final token = session['token'] as String;
final payload = _decodeJwtPayload(token);
return PowerSyncCredentials(
endpoint: session['powersync_url'],
token: token,
userId: payload?['sub']?.toString(),
expiresAt: _jwtExp(payload),
);
}
/// Decodes the JWT payload (middle segment). Returns null on malformed input.
static Map<String, dynamic>? _decodeJwtPayload(String jwt) {
try {
final parts = jwt.split('.');
if (parts.length != 3) {
return null;
}
return json.decode(utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))))
as Map<String, dynamic>;
} catch (e) {
logger.warning('Could not decode PowerSync JWT payload', e);
return null;
}
}
/// Extracts the `exp` claim (unix seconds) as a UTC DateTime.
static DateTime? _jwtExp(Map<String, dynamic>? payload) {
final exp = payload?['exp'];
if (exp is! num) {
return null;
}
return DateTime.fromMillisecondsSinceEpoch(exp.toInt() * 1000, isUtc: true);
}
/// Transform a record before sending it to the backend.
///
/// Note that PowerSync hands us [op.opData] as native SQLite primitives only
/// (`null`, `int`, `double`, `String`, `Uint8List`).
///
/// * inject the row [id] (PowerSync stores it separately from the
/// payload),
/// * strip the `_id` suffix from foreign-key column names so the
/// Django serializers see `category` / `routine` / etc.
Map<String, dynamic> _genericTransform(Map<String, dynamic>? src, String id) {
final out = <String, dynamic>{'id': id};
if (src == null) {
return out;
}
src.forEach((k, v) {
if (k == 'id') {
return;
}
final key = k.endsWith('_id') ? k.substring(0, k.length - 3) : k;
out[key] = v;
});
return out;
}
// Upload pending changes to Postgres via Django backend
// this is generic. on the django side we inspect the request and do model-specific operations
// would it make sense to do api calls here specific to the relevant model? (e.g. put to a todo-specific endpoint)
@override
Future<void> uploadData(PowerSyncDatabase database) async {
final transaction = await database.getNextCrudTransaction();
if (transaction == null) {
return;
}
try {
for (final op in transaction.crud) {
final record = {
'table': op.table,
'data': _genericTransform(op.opData, op.id),
// 'data': {'id': op.id, ...?op.opData},
};
logger.finer('Uploading record $record to server with operation ${op.op}');
switch (op.op) {
case UpdateType.put:
await apiClient.upsert(record);
break;
case UpdateType.patch:
await apiClient.update(record);
break;
case UpdateType.delete:
await apiClient.delete(record);
break;
}
}
await transaction.complete();
} on Exception catch (e) {
logger.severe('Error uploading data', e);
// Error may be retryable - e.g. network error or temporary server error.
// Throwing an error here causes this call to be retried after a delay.
rethrow;
}
}
}