sync method

dynamic sync({
  1. required String fromHost,
})

Implementation

sync({required String fromHost}) async {
  await initializeDatabase();
  var remote = shareHosts[fromHost];
  if (remote == null) return;
  for (var table in tableMap.values) {
    if (!table.memberMap.containsKey('sync')) continue;
    await server.session().transaction((tr) async {
      var localSyncTime =
          (await tr.request('select max(sync) as sync from ${table.name}'))
              .first['sync'];
      try {
        while (true) {
          var paquete = await remote
              .request('/${table.alias}/pull', data: {'sync': localSyncTime});

          //  si no vinieron datos
          if (paquete['data'].length == 0) {
            break;
          }

          // si no hay mas dataos
          if (paquete['rest'] == 0) {
            break;
          }
          final session = await tr.session();

          //  haciendo un push de datos
          await request('/${table.alias}/push',
              data: [paquete['data']], session: session);

          //  actualizando el ultimo indicador sync
          localSyncTime = paquete['data'].last['sync'];

          break;
        }
      } catch (e) {
        console.error('Fallo la sincronizacion');
        console.error(e);
      }
    });
  }
}