---
pkg: '@nocobase/plugin-user-data-sync'
title: "扩展同步目标资源"
description: "NocoBase 用户数据同步插件扩展：自定义同步目标资源、SyncResource 接口、用户表与部门表映射。"
keywords: "扩展同步资源,SyncResource,同步目标,用户表映射,plugin-user-data-sync,NocoBase"
---

# 扩展同步目标资源

## 概述

NocoBase 默认支持将用户数据同步至**用户**和**部门**表，也支持按照需要扩展数据同步的目标资源，实现将数据写入其他表或其他自定义处理。

:::warning{title=实验性}
完整文档待补充
:::

## 目标资源处理接口

```ts
export abstract class UserDataResource {
  name: string;
  accepts: SyncAccept[];
  db: Database;
  logger: SystemLogger;

  constructor(db: Database, logger: SystemLogger) {
    this.db = db;
    this.logger = logger;
  }

  abstract update(
    record: OriginRecord,
    resourcePks: PrimaryKey[],
    matchKey?: string,
  ): Promise<RecordResourceChanged[]>;
  abstract create(
    record: OriginRecord,
    matchKey: string,
  ): Promise<RecordResourceChanged[]>;

  get syncRecordRepo() {
    return this.db.getRepository('userDataSyncRecords');
  }

  get syncRecordResourceRepo() {
    return this.db.getRepository('userDataSyncRecordsResources');
  }
}
```

## 注册目标资源

`registerResource(resource: UserDataResource, options?: ToposortOptions)`

```ts
import { Plugin } from '@nocobase/server';
import PluginUserDataSyncServer from '@nocobase/plugin-user-data-sync';

class CustomUserResourcePluginServer extends Plugin {
  async load() {
    const userDataSyncPlugin = this.app.pm.get(PluginUserDataSyncServer);
    if (userDataSyncPlugin && userDataSyncPlugin.enabled) {
      userDataSyncPlugin.resourceManager.registerResource(new CustomDataSyncResource(this.db, this.app.logger)
    }
  }
}
```
