@nocobase/plugin-user-data-sync 1.4.0-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +159 -0
- package/README.md +1 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/Options.d.ts +12 -0
- package/dist/client/UserDataSyncSource.d.ts +10 -0
- package/dist/client/index.d.ts +21 -0
- package/dist/client/index.js +10 -0
- package/dist/client/locale/index.d.ts +10 -0
- package/dist/client/schemas/user-data-sync-sources.d.ts +155 -0
- package/dist/client/sourceType.d.ts +24 -0
- package/dist/externalVersion.js +24 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +60 -0
- package/dist/locale/en-US.json +21 -0
- package/dist/locale/zh-CN.json +24 -0
- package/dist/server/actions/user-data.d.ts +16 -0
- package/dist/server/actions/user-data.js +64 -0
- package/dist/server/collections/user-data-sync-records-resources.d.ts +10 -0
- package/dist/server/collections/user-data-sync-records-resources.js +54 -0
- package/dist/server/collections/user-data-sync-records.d.ts +10 -0
- package/dist/server/collections/user-data-sync-records.js +85 -0
- package/dist/server/collections/user-data-sync-sources.d.ts +10 -0
- package/dist/server/collections/user-data-sync-sources.js +109 -0
- package/dist/server/collections/user-data-sync-tasks.d.ts +10 -0
- package/dist/server/collections/user-data-sync-tasks.js +131 -0
- package/dist/server/error.d.ts +21 -0
- package/dist/server/error.js +58 -0
- package/dist/server/index.d.ts +12 -0
- package/dist/server/index.js +56 -0
- package/dist/server/models/sync-source.d.ts +15 -0
- package/dist/server/models/sync-source.js +38 -0
- package/dist/server/plugin.d.ts +27 -0
- package/dist/server/plugin.js +100 -0
- package/dist/server/sync-source-manager.d.ts +28 -0
- package/dist/server/sync-source-manager.js +71 -0
- package/dist/server/sync-source.d.ts +42 -0
- package/dist/server/sync-source.js +107 -0
- package/dist/server/user-data-resource-manager.d.ts +108 -0
- package/dist/server/user-data-resource-manager.js +214 -0
- package/dist/server/user-data-sync-service.d.ts +23 -0
- package/dist/server/user-data-sync-service.js +130 -0
- package/dist/swagger/index.d.ts +116 -0
- package/dist/swagger/index.js +138 -0
- package/package.json +18 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var sync_source_exports = {};
|
|
28
|
+
__export(sync_source_exports, {
|
|
29
|
+
SyncSourceModel: () => SyncSourceModel
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(sync_source_exports);
|
|
32
|
+
var import_database = require("@nocobase/database");
|
|
33
|
+
class SyncSourceModel extends import_database.Model {
|
|
34
|
+
}
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
SyncSourceModel
|
|
38
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Plugin } from '@nocobase/server';
|
|
10
|
+
import { UserDataResourceManager } from './user-data-resource-manager';
|
|
11
|
+
import { UserDataSyncService } from './user-data-sync-service';
|
|
12
|
+
import { SyncSourceManager } from './sync-source-manager';
|
|
13
|
+
import { Logger } from '@nocobase/logger';
|
|
14
|
+
export declare class PluginUserDataSyncServer extends Plugin {
|
|
15
|
+
sourceManager: SyncSourceManager;
|
|
16
|
+
resourceManager: UserDataResourceManager;
|
|
17
|
+
syncService: UserDataSyncService;
|
|
18
|
+
afterAdd(): Promise<void>;
|
|
19
|
+
beforeLoad(): Promise<void>;
|
|
20
|
+
getLogger(): Logger;
|
|
21
|
+
load(): Promise<void>;
|
|
22
|
+
install(): Promise<void>;
|
|
23
|
+
afterEnable(): Promise<void>;
|
|
24
|
+
afterDisable(): Promise<void>;
|
|
25
|
+
remove(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export default PluginUserDataSyncServer;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
+
mod
|
|
35
|
+
));
|
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
37
|
+
var plugin_exports = {};
|
|
38
|
+
__export(plugin_exports, {
|
|
39
|
+
PluginUserDataSyncServer: () => PluginUserDataSyncServer,
|
|
40
|
+
default: () => plugin_default
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(plugin_exports);
|
|
43
|
+
var import_server = require("@nocobase/server");
|
|
44
|
+
var import_user_data_resource_manager = require("./user-data-resource-manager");
|
|
45
|
+
var import_user_data_sync_service = require("./user-data-sync-service");
|
|
46
|
+
var import_user_data = __toESM(require("./actions/user-data"));
|
|
47
|
+
var import_sync_source_manager = require("./sync-source-manager");
|
|
48
|
+
var import_sync_source = require("./models/sync-source");
|
|
49
|
+
class PluginUserDataSyncServer extends import_server.Plugin {
|
|
50
|
+
sourceManager;
|
|
51
|
+
resourceManager;
|
|
52
|
+
syncService;
|
|
53
|
+
async afterAdd() {
|
|
54
|
+
}
|
|
55
|
+
async beforeLoad() {
|
|
56
|
+
this.app.db.registerModels({ SyncSourceModel: import_sync_source.SyncSourceModel });
|
|
57
|
+
this.sourceManager = new import_sync_source_manager.SyncSourceManager();
|
|
58
|
+
this.resourceManager = new import_user_data_resource_manager.UserDataResourceManager();
|
|
59
|
+
}
|
|
60
|
+
getLogger() {
|
|
61
|
+
const logger = this.createLogger({
|
|
62
|
+
dirname: "user-data-sync",
|
|
63
|
+
filename: "%DATE%.log",
|
|
64
|
+
format: "json"
|
|
65
|
+
});
|
|
66
|
+
return logger;
|
|
67
|
+
}
|
|
68
|
+
async load() {
|
|
69
|
+
const logger = this.getLogger();
|
|
70
|
+
this.resourceManager.db = this.app.db;
|
|
71
|
+
this.resourceManager.logger = this.app.logger;
|
|
72
|
+
this.syncService = new import_user_data_sync_service.UserDataSyncService(this.resourceManager, this.sourceManager, logger);
|
|
73
|
+
this.app.resourceManager.define({
|
|
74
|
+
name: "userData",
|
|
75
|
+
actions: {
|
|
76
|
+
listSyncTypes: import_user_data.default.listSyncTypes,
|
|
77
|
+
pull: import_user_data.default.pull,
|
|
78
|
+
push: import_user_data.default.push,
|
|
79
|
+
retry: import_user_data.default.retry
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
this.app.acl.registerSnippet({
|
|
83
|
+
name: `pm.${this.name}`,
|
|
84
|
+
actions: ["userData:*", "userDataSyncSources:*"]
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async install() {
|
|
88
|
+
}
|
|
89
|
+
async afterEnable() {
|
|
90
|
+
}
|
|
91
|
+
async afterDisable() {
|
|
92
|
+
}
|
|
93
|
+
async remove() {
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
var plugin_default = PluginUserDataSyncServer;
|
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
98
|
+
0 && (module.exports = {
|
|
99
|
+
PluginUserDataSyncServer
|
|
100
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Registry } from '@nocobase/utils';
|
|
10
|
+
import { SyncSource, SyncSourceExtend } from './sync-source';
|
|
11
|
+
import { Context } from '@nocobase/actions';
|
|
12
|
+
import { SyncSourceModel } from './models/sync-source';
|
|
13
|
+
type SyncSourceConfig = {
|
|
14
|
+
syncSource: SyncSourceExtend<SyncSource>;
|
|
15
|
+
title?: string;
|
|
16
|
+
};
|
|
17
|
+
export declare class SyncSourceManager {
|
|
18
|
+
protected syncSourceTypes: Registry<SyncSourceConfig>;
|
|
19
|
+
registerType(syncSourceType: string, syncSourceConfig: SyncSourceConfig): void;
|
|
20
|
+
listTypes(): {
|
|
21
|
+
name: string;
|
|
22
|
+
title: string;
|
|
23
|
+
}[];
|
|
24
|
+
getByName(name: string, ctx: Context): Promise<SyncSource>;
|
|
25
|
+
getById(id: number, ctx: Context): Promise<SyncSource>;
|
|
26
|
+
create(sourceInstance: SyncSourceModel, ctx: Context): SyncSource;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var sync_source_manager_exports = {};
|
|
28
|
+
__export(sync_source_manager_exports, {
|
|
29
|
+
SyncSourceManager: () => SyncSourceManager
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(sync_source_manager_exports);
|
|
32
|
+
var import_utils = require("@nocobase/utils");
|
|
33
|
+
class SyncSourceManager {
|
|
34
|
+
syncSourceTypes = new import_utils.Registry();
|
|
35
|
+
registerType(syncSourceType, syncSourceConfig) {
|
|
36
|
+
this.syncSourceTypes.register(syncSourceType, syncSourceConfig);
|
|
37
|
+
}
|
|
38
|
+
listTypes() {
|
|
39
|
+
return Array.from(this.syncSourceTypes.getEntities()).map(([syncSourceType, source]) => ({
|
|
40
|
+
name: syncSourceType,
|
|
41
|
+
title: source.title
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
async getByName(name, ctx) {
|
|
45
|
+
const repo = ctx.db.getRepository("userDataSyncSources");
|
|
46
|
+
const sourceInstance = await repo.findOne({ filter: { enabled: true, name } });
|
|
47
|
+
if (!sourceInstance) {
|
|
48
|
+
throw new Error(`SyncSource [${name}] is not found.`);
|
|
49
|
+
}
|
|
50
|
+
return this.create(sourceInstance, ctx);
|
|
51
|
+
}
|
|
52
|
+
async getById(id, ctx) {
|
|
53
|
+
const repo = ctx.db.getRepository("userDataSyncSources");
|
|
54
|
+
const sourceInstance = await repo.findOne({ filter: { enabled: true }, filterByTk: id });
|
|
55
|
+
if (!sourceInstance) {
|
|
56
|
+
throw new Error(`SyncSource [${id}] is not found.`);
|
|
57
|
+
}
|
|
58
|
+
return this.create(sourceInstance, ctx);
|
|
59
|
+
}
|
|
60
|
+
create(sourceInstance, ctx) {
|
|
61
|
+
const { syncSource } = this.syncSourceTypes.get(sourceInstance.sourceType) || {};
|
|
62
|
+
if (!syncSource) {
|
|
63
|
+
throw new Error(`SyncSourceType [${sourceInstance.sourceType}] is not found.`);
|
|
64
|
+
}
|
|
65
|
+
return new syncSource({ sourceInstance, options: sourceInstance.options, ctx });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
SyncSourceManager
|
|
71
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Context } from '@nocobase/actions';
|
|
10
|
+
import { SyncSourceModel } from './models/sync-source';
|
|
11
|
+
import { UserData } from './user-data-resource-manager';
|
|
12
|
+
export type SyncSourceConfig = {
|
|
13
|
+
sourceInstance: SyncSourceModel;
|
|
14
|
+
options: {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
};
|
|
17
|
+
ctx: Context;
|
|
18
|
+
};
|
|
19
|
+
interface ISyncSource {
|
|
20
|
+
pull(): Promise<UserData[]>;
|
|
21
|
+
}
|
|
22
|
+
export declare abstract class SyncSource implements ISyncSource {
|
|
23
|
+
instance: SyncSourceModel;
|
|
24
|
+
protected options: {
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
};
|
|
27
|
+
protected ctx: Context;
|
|
28
|
+
constructor(config: SyncSourceConfig);
|
|
29
|
+
abstract pull(): Promise<UserData[]>;
|
|
30
|
+
newTask(): Promise<any>;
|
|
31
|
+
beginTask(taskId: number): Promise<void>;
|
|
32
|
+
endTask(params: EndTaskParams): Promise<void>;
|
|
33
|
+
retryTask(taskId: number): Promise<any>;
|
|
34
|
+
}
|
|
35
|
+
export type SyncSourceExtend<T extends SyncSource> = new (config: SyncSourceConfig) => T;
|
|
36
|
+
type EndTaskParams = {
|
|
37
|
+
taskId: number;
|
|
38
|
+
success: boolean;
|
|
39
|
+
cost?: number;
|
|
40
|
+
message?: string;
|
|
41
|
+
};
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
+
mod
|
|
35
|
+
));
|
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
37
|
+
var sync_source_exports = {};
|
|
38
|
+
__export(sync_source_exports, {
|
|
39
|
+
SyncSource: () => SyncSource
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(sync_source_exports);
|
|
42
|
+
var import_dayjs = __toESM(require("dayjs"));
|
|
43
|
+
class SyncSource {
|
|
44
|
+
instance;
|
|
45
|
+
options;
|
|
46
|
+
ctx;
|
|
47
|
+
constructor(config) {
|
|
48
|
+
const { options, ctx, sourceInstance } = config;
|
|
49
|
+
this.instance = sourceInstance;
|
|
50
|
+
this.options = options;
|
|
51
|
+
this.ctx = ctx;
|
|
52
|
+
}
|
|
53
|
+
async newTask() {
|
|
54
|
+
const batch = generateUniqueNumber();
|
|
55
|
+
return await this.instance.createTask({ batch, status: "init" });
|
|
56
|
+
}
|
|
57
|
+
async beginTask(taskId) {
|
|
58
|
+
const tasks = await this.instance.getTasks({ where: { id: taskId } });
|
|
59
|
+
if (!tasks && !tasks.length) {
|
|
60
|
+
throw new Error(`Task [${taskId}] is not found.`);
|
|
61
|
+
}
|
|
62
|
+
const task = tasks[0];
|
|
63
|
+
if (task.status !== "init") {
|
|
64
|
+
throw new Error(`Task [${taskId}] is not init.`);
|
|
65
|
+
}
|
|
66
|
+
task.status = "processing";
|
|
67
|
+
await task.save();
|
|
68
|
+
}
|
|
69
|
+
async endTask(params) {
|
|
70
|
+
const { taskId, success, cost, message } = params;
|
|
71
|
+
const tasks = await this.instance.getTasks({ where: { id: taskId } });
|
|
72
|
+
if (!tasks && !tasks.length) {
|
|
73
|
+
throw new Error(`Task [${taskId}] is not found.`);
|
|
74
|
+
}
|
|
75
|
+
const task = tasks[0];
|
|
76
|
+
if (task.status !== "processing") {
|
|
77
|
+
throw new Error(`Task [${taskId}] is not processing.`);
|
|
78
|
+
}
|
|
79
|
+
task.status = success ? "success" : "failed";
|
|
80
|
+
task.cost = cost;
|
|
81
|
+
task.message = message;
|
|
82
|
+
await task.save();
|
|
83
|
+
}
|
|
84
|
+
async retryTask(taskId) {
|
|
85
|
+
const tasks = await this.instance.getTasks({ where: { id: taskId } });
|
|
86
|
+
if (!tasks && !tasks.length) {
|
|
87
|
+
throw new Error(`Task [${taskId}] is not found.`);
|
|
88
|
+
}
|
|
89
|
+
const task = tasks[0];
|
|
90
|
+
if (task.status !== "failed") {
|
|
91
|
+
throw new Error(`Task [${taskId}] is not failed.`);
|
|
92
|
+
}
|
|
93
|
+
task.status = "processing";
|
|
94
|
+
task.message = "";
|
|
95
|
+
await task.save();
|
|
96
|
+
return task;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function generateUniqueNumber() {
|
|
100
|
+
const formattedDate = (0, import_dayjs.default)().format("YYYYMMDDHHmmss");
|
|
101
|
+
const randomDigits = Math.floor(1e5 + Math.random() * 9e5);
|
|
102
|
+
return formattedDate + randomDigits;
|
|
103
|
+
}
|
|
104
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
105
|
+
0 && (module.exports = {
|
|
106
|
+
SyncSource
|
|
107
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Toposort, ToposortOptions } from '@nocobase/utils';
|
|
10
|
+
import Database, { Repository } from '@nocobase/database';
|
|
11
|
+
import { SystemLogger } from '@nocobase/logger';
|
|
12
|
+
export type FormatUser = {
|
|
13
|
+
uid: string;
|
|
14
|
+
username?: string;
|
|
15
|
+
email?: string;
|
|
16
|
+
nickname?: string;
|
|
17
|
+
phone?: string;
|
|
18
|
+
departments?: (string | FormatUserDepartment)[];
|
|
19
|
+
isDeleted?: boolean;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
};
|
|
22
|
+
export type FormatDepartment = {
|
|
23
|
+
uid: string;
|
|
24
|
+
title?: string;
|
|
25
|
+
parentUid?: string;
|
|
26
|
+
isDeleted?: boolean;
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
};
|
|
29
|
+
export type FormatUserDepartment = {
|
|
30
|
+
uid: string;
|
|
31
|
+
isOwner?: boolean;
|
|
32
|
+
isMain?: boolean;
|
|
33
|
+
};
|
|
34
|
+
export type UserDataRecord = FormatUser | FormatDepartment;
|
|
35
|
+
export type SyncDataType = 'user' | 'department';
|
|
36
|
+
export type SyncAccept = SyncDataType;
|
|
37
|
+
export type OriginRecord = {
|
|
38
|
+
id: number;
|
|
39
|
+
sourceName: string;
|
|
40
|
+
sourceUk: string;
|
|
41
|
+
dataType: SyncDataType;
|
|
42
|
+
metaData: UserDataRecord;
|
|
43
|
+
resources: {
|
|
44
|
+
resource: string;
|
|
45
|
+
resourcePk: string;
|
|
46
|
+
}[];
|
|
47
|
+
};
|
|
48
|
+
export type UserData = {
|
|
49
|
+
dataType: SyncDataType;
|
|
50
|
+
matchKey?: string;
|
|
51
|
+
records: UserDataRecord[];
|
|
52
|
+
sourceName: string;
|
|
53
|
+
};
|
|
54
|
+
export type PrimaryKey = number | string;
|
|
55
|
+
export type RecordResourceChanged = {
|
|
56
|
+
resourcesPk: PrimaryKey;
|
|
57
|
+
isDeleted: boolean;
|
|
58
|
+
};
|
|
59
|
+
export declare abstract class UserDataResource {
|
|
60
|
+
name: string;
|
|
61
|
+
accepts: SyncAccept[];
|
|
62
|
+
db: Database;
|
|
63
|
+
logger: SystemLogger;
|
|
64
|
+
constructor(db: Database, logger: SystemLogger);
|
|
65
|
+
abstract update(record: OriginRecord, resourcePks: PrimaryKey[], matchKey?: string): Promise<RecordResourceChanged[]>;
|
|
66
|
+
abstract create(record: OriginRecord, matchKey: string): Promise<RecordResourceChanged[]>;
|
|
67
|
+
get syncRecordRepo(): Repository<any, any>;
|
|
68
|
+
get syncRecordResourceRepo(): Repository<any, any>;
|
|
69
|
+
}
|
|
70
|
+
export type SyncResult = {
|
|
71
|
+
resource: string;
|
|
72
|
+
detail: {
|
|
73
|
+
count: {
|
|
74
|
+
all: number;
|
|
75
|
+
success: number;
|
|
76
|
+
failed: number;
|
|
77
|
+
};
|
|
78
|
+
failedRecords: {
|
|
79
|
+
record: UserDataRecord;
|
|
80
|
+
message: string;
|
|
81
|
+
}[];
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export declare class UserDataResourceManager {
|
|
85
|
+
resources: Toposort<UserDataResource>;
|
|
86
|
+
syncRecordRepo: Repository;
|
|
87
|
+
syncRecordResourceRepo: Repository;
|
|
88
|
+
logger: SystemLogger;
|
|
89
|
+
registerResource(resource: UserDataResource, options?: ToposortOptions): void;
|
|
90
|
+
set db(value: Database);
|
|
91
|
+
saveOriginRecords(data: UserData): Promise<void>;
|
|
92
|
+
findOriginRecords({ sourceName, dataType, sourceUks }: {
|
|
93
|
+
sourceName: any;
|
|
94
|
+
dataType: any;
|
|
95
|
+
sourceUks: any;
|
|
96
|
+
}): Promise<OriginRecord[]>;
|
|
97
|
+
addResourceToOriginRecord({ recordId, resource, resourcePk }: {
|
|
98
|
+
recordId: any;
|
|
99
|
+
resource: any;
|
|
100
|
+
resourcePk: any;
|
|
101
|
+
}): Promise<void>;
|
|
102
|
+
removeResourceFromOriginRecord({ recordId, resource, resourcePk }: {
|
|
103
|
+
recordId: any;
|
|
104
|
+
resource: any;
|
|
105
|
+
resourcePk: any;
|
|
106
|
+
}): Promise<void>;
|
|
107
|
+
updateOrCreate(data: UserData): Promise<SyncResult[]>;
|
|
108
|
+
}
|