Sync changes back

External record loading example

Wire create and update callbacks into the SDK loading worker using public template helpers.

Save this worker under code/src/functions/loading/workers/load-data.ts in the public template. Read loading inputs and callback results first.

Data worker

The following worker uses the template's connector-owned helper paths. Implement the helpers against your external API; the template methods are examples, not a working task-service integration.

import {
  ExternalSystemItem,
  ExternalSystemItemLoadingParams,
  ExternalSystemItemLoadingResponse,
  processLoadingTask,
} from '@devrev/airsync-sdk';
import { HttpClient } from '../../external-system/http-client';
import { denormalizeTodo } from '../../external-system/data-denormalization';
import type { LoaderState } from '../index';

async function createTodo({ item, event }: ExternalSystemItemLoadingParams<ExternalSystemItem>): Promise<ExternalSystemItemLoadingResponse> {
  const client = new HttpClient(event);
  return client.createTodo(denormalizeTodo(item));
}

async function updateTodo({ item, mappers, event }: ExternalSystemItemLoadingParams<ExternalSystemItem>): Promise<ExternalSystemItemLoadingResponse> {
  const lookup = await mappers.getByTargetId({
    sync_unit: event.payload.event_context.sync_unit,
    target: item.id.devrev,
  });
  const externalId = lookup?.sync_mapper_record.external_ids[0];
  if (!externalId) {
    return { status: 'error', error: { message: 'External task identity is missing.' } };
  }
  const client = new HttpClient(event);
  return client.updateTodo({ ...denormalizeTodo(item), id: externalId });
}

processLoadingTask<LoaderState>({
  task: async ({ adapter }) => adapter.loadItemTypes({
    itemTypesToLoad: [{ itemType: 'todos', create: createTodo, update: updateTodo }],
  }),
});

Helper contracts are shown in the public template's HTTP client and denormalization module. Replace their placeholder behavior with API calls, input validation and rate-limit handling.

Related: Data loading.

Next: Write related records in dependency order.

Last updated on

On this page