setTableReplicationTransform<TableDsl extends Table, D> method
void
setTableReplicationTransform<TableDsl extends Table, D>(
- TableInfo<
TableDsl, D> table, { - required Insertable<
D> transformInbound(- D row
- required Insertable<
D> transformOutbound(- D row
- Insertable<
D> toInsertable(- D
override
Puts transforms in place such that any data being replicated to or from this table is first handled appropriately while retaining type consistency.
Can be used to encrypt sensitive fields before they are replicated outside of their secure local source.
NOTE: usage is discouraged, but ensure transforms are set before replication is initiated using syncTable to avoid partially transformed tables.
Implementation
@override
void setTableReplicationTransform<TableDsl extends Table, D>(
TableInfo<TableDsl, D> table, {
required Insertable<D> Function(D row) transformInbound,
required Insertable<D> Function(D row) transformOutbound,
Insertable<D> Function(D)? toInsertable,
}) {
// forbid transforming relation keys to avoid breaking
// referential integrity
final tableRelations = getTableRelations(table)?.$relationsList ?? [];
final outgoingRelations =
tableRelations.where((r) => r.isOutgoingRelation());
final incomingRelations =
tableRelations.where((r) => r.isIncomingRelation());
// the column could be the FK column when it is an outgoing FK
// or it could be a PK column when it is an incoming FK
final fkCols = outgoingRelations.map((r) => r.fromField);
// Incoming relations don't have the `fromField` and `toField` filled in
// so we need to fetch the `toField` from the opposite relation
// which is effectively a column in this table to which the FK points
final pkCols =
incomingRelations.map((r) => r.getOppositeRelation(db).toField);
// Merge all columns that are part of a FK relation.
// Remove duplicate columns in case a column has both an outgoing FK and an incoming FK.
final immutableFields = <String>{...fkCols, ...pkCols}.toList();
final QualifiedTablename qualifiedTableName = _getQualifiedTableName(table);
// ignore: invalid_use_of_protected_member
_baseClient.replicationTransformManager.setTableTransform(
qualifiedTableName,
ReplicatedRowTransformer(
transformInbound: (DbRecord record) {
final dataClass = table.map(record) as D;
final insertable = transformTableRecord<TableDsl, D, DbRecord>(
table,
dataClass,
transformInbound,
immutableFields,
toInsertable: toInsertable,
);
return driftInsertableToValues(insertable);
},
transformOutbound: (DbRecord record) {
final dataClass = table.map(record) as D;
final insertable = transformTableRecord<TableDsl, D, DbRecord>(
table,
dataClass,
transformOutbound,
immutableFields,
toInsertable: toInsertable,
);
return driftInsertableToValues(insertable);
},
),
);
}