Object Mappers

Object Mappers are a core component of AirSync that manage and track the relationships between objects imported from external systems and their corresponding entities in DevRev. Think of Object Mappers as a comprehensive lookup table that maintains the synchronization state between external and DevRev objects.

Each sync mapper record contains external system identifiers, corresponding DevRev entity IDs, sync unit scope, operational status, and additional metadata. This bidirectional mapping system enables AirSync to efficiently track which external objects have been synchronized and locate their DevRev counterparts during ongoing operations. Object Mappers are available through the adapter object as adapter.mappers in your AirSync snap-in functions.

When to use Object Mappers

Object Mappers are essential in several AirSync scenarios:

During data extraction and relationship management - Check if sync mapper records already exist to avoid duplicates, find related DevRev entities for nested objects like comments and attachments, and link child objects to their parent entities.

1import { SyncMapperRecordTargetType } from "@devrev/ts-adaas";
2
3// Check if sync mapper record for attachment already exists
4const existingSyncMapperRecord = await adapter.mappers.getByExternalId({
5 sync_unit: adapter.event.payload.event_context.sync_unit,
6 external_id: attachment.id,
7 target_type: SyncMapperRecordTargetType.ARTIFACT,
8});
9
10// Find parent work item for comment
11const parentSyncMapperRecord = await adapter.mappers.getByExternalId({
12 sync_unit: adapter.event.payload.event_context.sync_unit,
13 external_id: externalComment.ticket_id,
14 target_type: SyncMapperRecordTargetType.WORK,
15});

During loading operations - Locate IDs of objects in external system for DevRev entities when pushing data back to external systems and maintain consistency between systems.

1// Find external ID to update external system
2const syncMapperRecord = await adapter.mappers.getByTargetId({
3 sync_unit: adapter.event.payload.event_context.sync_unit,
4 target: devrevWorkId,
5});

Object Mapper methods

Get by DevRev ID

Use getByTargetId when you have a DevRev entity ID and need to find the corresponding ID of an object in external system:

1const response = await adapter.mappers.getByTargetId({
2 sync_unit: adapter.event.payload.event_context.sync_unit,
3 target: devrevEntityId,
4});
5
6const externalIds = response.data.sync_mapper_record.external_ids;

Get by external ID

Use getByExternalId when you have an ID of an object in external system and need to find the corresponding DevRev entity:

1import { SyncMapperRecordTargetType } from "@devrev/ts-adaas";
2
3const response = await adapter.mappers.getByExternalId({
4 sync_unit: adapter.event.payload.event_context.sync_unit,
5 external_id: externalSystemId,
6 target_type: SyncMapperRecordTargetType.WORK,
7});
8
9const devrevTargets = response.data.sync_mapper_record.targets;

Create new sync mapper record

Use create to establish a new sync mapper record between external and DevRev entities. Create a mapper only when you are persisting a new link (for example, right after creating the external object in reverse sync), not for read-only extraction. This prevents duplicates in subsequent syncs by enabling lookups in both directions.

1import { SyncMapperRecordStatus } from "@devrev/ts-adaas";
2
3const response = await adapter.mappers.create({
4 sync_unit: adapter.event.payload.event_context.sync_unit,
5 external_ids: [externalSystemId],
6 targets: [devrevEntityId],
7 status: SyncMapperRecordStatus.OPERATIONAL,
8});

Update existing sync mapper record

Use update to modify an existing sync mapper record:

1import { SyncMapperRecordStatus } from "@devrev/ts-adaas";
2
3const response = await adapter.mappers.update({
4 id: syncMapperRecordId,
5 sync_unit: adapter.event.payload.event_context.sync_unit,
6 external_ids: {
7 add: [newExternalId],
8 },
9 targets: {
10 add: [newDevrevEntityId],
11 },
12 status: SyncMapperRecordStatus.OPERATIONAL,
13});