plugin-block-cross-join 0.1.1 → 0.1.4
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/README.md +17 -0
- package/client-v2.d.ts +2 -0
- package/client-v2.js +1 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +4 -3
- package/dist/server/collections/cross-join.js +44 -0
- package/dist/server/plugin.js +14 -0
- package/package.json +44 -33
- package/src/client/CrossJoinBlockInitializer.tsx +34 -0
- package/src/client/CrossJoinBlockProvider.tsx +88 -0
- package/src/client/CrossJoinConfigurator.tsx +415 -0
- package/src/client/CrossJoinTable.tsx +106 -0
- package/src/client/createBlockUISchema.ts +33 -0
- package/src/client/index.tsx +50 -0
- package/src/client/schemaSettings.tsx +50 -0
- package/src/client-v2/index.tsx +1 -0
- package/src/client-v2/plugin.tsx +7 -0
- package/src/index.ts +11 -0
- package/src/locale/en-US.json +23 -0
- package/src/locale/zh-CN.json +23 -0
- package/src/server/actions/cross-join-query.ts +246 -0
- package/src/server/collections/cross-join.ts +23 -0
- package/src/server/index.ts +10 -0
- package/src/server/plugin.ts +36 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Cross Join": "跨源联合",
|
|
3
|
+
"Cross Join Block": "跨源联合区块",
|
|
4
|
+
"Configure cross join": "配置跨源联合",
|
|
5
|
+
"Primary Source": "主数据源",
|
|
6
|
+
"Joins": "联合配置",
|
|
7
|
+
"Columns": "列配置",
|
|
8
|
+
"Datasource": "数据源",
|
|
9
|
+
"Collection": "数据表",
|
|
10
|
+
"Join Type": "联合类型",
|
|
11
|
+
"Left Join": "左联合",
|
|
12
|
+
"Inner Join": "内联合",
|
|
13
|
+
"Left Field": "左字段",
|
|
14
|
+
"Right Field": "右字段",
|
|
15
|
+
"Add Join": "添加联合",
|
|
16
|
+
"Field": "字段",
|
|
17
|
+
"Alias": "别名",
|
|
18
|
+
"Source": "来源",
|
|
19
|
+
"Edit mapping": "编辑映射",
|
|
20
|
+
"Expand JSON keys": "展开JSON键",
|
|
21
|
+
"No data": "暂无数据",
|
|
22
|
+
"Loading...": "加载中..."
|
|
23
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
import { Context } from '@nocobase/actions';
|
|
11
|
+
|
|
12
|
+
interface JoinConfig {
|
|
13
|
+
dataSource: string;
|
|
14
|
+
collection: string;
|
|
15
|
+
joinType: 'left' | 'inner';
|
|
16
|
+
leftField: string;
|
|
17
|
+
rightField: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ColumnConfig {
|
|
21
|
+
source: number; // 0 = primary, 1 = join[0], ...
|
|
22
|
+
field: string;
|
|
23
|
+
alias?: string;
|
|
24
|
+
jsonExpand?: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface CrossJoinConfig {
|
|
28
|
+
primarySource: {
|
|
29
|
+
dataSource: string;
|
|
30
|
+
collection: string;
|
|
31
|
+
filter?: Record<string, any>;
|
|
32
|
+
};
|
|
33
|
+
joins: JoinConfig[];
|
|
34
|
+
columns: ColumnConfig[];
|
|
35
|
+
defaultSort?: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toPlainObject(row: any): Record<string, any> {
|
|
39
|
+
if (row && typeof row.toJSON === 'function') {
|
|
40
|
+
return row.toJSON();
|
|
41
|
+
}
|
|
42
|
+
return row;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function buildJoinMap(rows: Record<string, any>[], keyField: string): Map<string, Record<string, any>[]> {
|
|
46
|
+
const map = new Map<string, Record<string, any>[]>();
|
|
47
|
+
for (const row of rows) {
|
|
48
|
+
const key = String(row[keyField] ?? '');
|
|
49
|
+
if (!map.has(key)) {
|
|
50
|
+
map.set(key, []);
|
|
51
|
+
}
|
|
52
|
+
map.get(key)!.push(row);
|
|
53
|
+
}
|
|
54
|
+
return map;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function prefixRow(row: Record<string, any>, prefix: string): Record<string, any> {
|
|
58
|
+
const result: Record<string, any> = {};
|
|
59
|
+
for (const [k, v] of Object.entries(row)) {
|
|
60
|
+
result[`${prefix}.${k}`] = v;
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function applyJoin(
|
|
66
|
+
leftRows: Record<string, any>[],
|
|
67
|
+
rightRows: Record<string, any>[],
|
|
68
|
+
joinConfig: JoinConfig,
|
|
69
|
+
joinIndex: number,
|
|
70
|
+
): Record<string, any>[] {
|
|
71
|
+
const rightMap = buildJoinMap(rightRows, joinConfig.rightField);
|
|
72
|
+
const rightPrefix = `join${joinIndex}`;
|
|
73
|
+
const result: Record<string, any>[] = [];
|
|
74
|
+
|
|
75
|
+
for (const leftRow of leftRows) {
|
|
76
|
+
const key = String(leftRow[`primary.${joinConfig.leftField}`] ?? leftRow[joinConfig.leftField] ?? '');
|
|
77
|
+
const matches = rightMap.get(key);
|
|
78
|
+
|
|
79
|
+
if (matches && matches.length > 0) {
|
|
80
|
+
for (const match of matches) {
|
|
81
|
+
result.push({ ...leftRow, ...prefixRow(match, rightPrefix) });
|
|
82
|
+
}
|
|
83
|
+
} else if (joinConfig.joinType === 'left') {
|
|
84
|
+
result.push({ ...leftRow });
|
|
85
|
+
}
|
|
86
|
+
// inner join: skip rows with no match
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function applyColumnMapping(rows: Record<string, any>[], columns: ColumnConfig[]): Record<string, any>[] {
|
|
93
|
+
return rows.map((row) => {
|
|
94
|
+
const mapped: Record<string, any> = {};
|
|
95
|
+
|
|
96
|
+
for (const col of columns) {
|
|
97
|
+
const prefix = col.source === 0 ? 'primary' : `join${col.source - 1}`;
|
|
98
|
+
const fullKey = `${prefix}.${col.field}`;
|
|
99
|
+
const value = row[fullKey];
|
|
100
|
+
|
|
101
|
+
// JSON expansion
|
|
102
|
+
if (col.jsonExpand?.length && value != null) {
|
|
103
|
+
try {
|
|
104
|
+
const parsed = typeof value === 'string' ? JSON.parse(value) : value;
|
|
105
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
106
|
+
for (const jsonKey of col.jsonExpand) {
|
|
107
|
+
const alias = col.alias ? `${col.alias}.${jsonKey}` : `${col.field}.${jsonKey}`;
|
|
108
|
+
mapped[alias] = parsed[jsonKey] ?? null;
|
|
109
|
+
}
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
} catch {
|
|
113
|
+
// not valid JSON, fall through to display raw value
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const outputKey = col.alias || col.field;
|
|
118
|
+
mapped[outputKey] = value ?? null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return mapped;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function crossJoinQuery(ctx: Context, next: () => Promise<void>) {
|
|
126
|
+
const { config, page = 1, pageSize = 20, sort } = ctx.action.params.values || ctx.action.params;
|
|
127
|
+
|
|
128
|
+
if (!config?.primarySource) {
|
|
129
|
+
ctx.throw(400, 'Missing primarySource in config');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const dsManager = ctx.app.dataSourceManager;
|
|
133
|
+
const { primarySource, joins = [], columns = [] } = config as CrossJoinConfig;
|
|
134
|
+
|
|
135
|
+
// 1. Get primary datasource and repository
|
|
136
|
+
const primaryDS = dsManager.get(primarySource.dataSource);
|
|
137
|
+
if (!primaryDS) {
|
|
138
|
+
ctx.throw(400, `Datasource "${primarySource.dataSource}" not found`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const primaryRepo = primaryDS.collectionManager.getRepository(primarySource.collection);
|
|
142
|
+
if (!primaryRepo) {
|
|
143
|
+
ctx.throw(400, `Collection "${primarySource.collection}" not found in datasource "${primarySource.dataSource}"`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 2. Determine if we need full-fetch (INNER join) or can paginate primary
|
|
147
|
+
const hasInnerJoin = joins.some((j) => j.joinType === 'inner');
|
|
148
|
+
|
|
149
|
+
let primaryRows: Record<string, any>[];
|
|
150
|
+
let totalPrimaryCount: number;
|
|
151
|
+
|
|
152
|
+
if (hasInnerJoin) {
|
|
153
|
+
// Full fetch with cap for INNER joins
|
|
154
|
+
const MAX_ROWS = 10000;
|
|
155
|
+
const rawRows = await primaryRepo.find({
|
|
156
|
+
filter: primarySource.filter || {},
|
|
157
|
+
sort: sort || config.defaultSort,
|
|
158
|
+
limit: MAX_ROWS,
|
|
159
|
+
});
|
|
160
|
+
primaryRows = rawRows.map(toPlainObject);
|
|
161
|
+
totalPrimaryCount = primaryRows.length;
|
|
162
|
+
} else {
|
|
163
|
+
// Paginate at primary level for LEFT-only joins
|
|
164
|
+
const [rawRows, count] = await Promise.all([
|
|
165
|
+
primaryRepo.find({
|
|
166
|
+
filter: primarySource.filter || {},
|
|
167
|
+
sort: sort || config.defaultSort,
|
|
168
|
+
limit: Number(pageSize),
|
|
169
|
+
offset: (Number(page) - 1) * Number(pageSize),
|
|
170
|
+
}),
|
|
171
|
+
primaryRepo.count({
|
|
172
|
+
filter: primarySource.filter || {},
|
|
173
|
+
}),
|
|
174
|
+
]);
|
|
175
|
+
primaryRows = rawRows.map(toPlainObject);
|
|
176
|
+
totalPrimaryCount = Number(count);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 3. Prefix primary rows
|
|
180
|
+
let joinedRows: Record<string, any>[] = primaryRows.map((row) => prefixRow(row, 'primary'));
|
|
181
|
+
|
|
182
|
+
// 4. Process each join
|
|
183
|
+
for (let i = 0; i < joins.length; i++) {
|
|
184
|
+
const joinDef = joins[i];
|
|
185
|
+
|
|
186
|
+
const joinDS = dsManager.get(joinDef.dataSource);
|
|
187
|
+
if (!joinDS) {
|
|
188
|
+
ctx.throw(400, `Datasource "${joinDef.dataSource}" not found`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const joinRepo = joinDS.collectionManager.getRepository(joinDef.collection);
|
|
192
|
+
if (!joinRepo) {
|
|
193
|
+
ctx.throw(400, `Collection "${joinDef.collection}" not found in datasource "${joinDef.dataSource}"`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Collect distinct join keys from current result
|
|
197
|
+
// leftField always refers to the primary source column (prefixed with "primary.")
|
|
198
|
+
const leftKeyPath = `primary.${joinDef.leftField}`;
|
|
199
|
+
const distinctKeys = [...new Set(joinedRows.map((r) => r[leftKeyPath]).filter((v) => v != null))];
|
|
200
|
+
|
|
201
|
+
if (distinctKeys.length === 0) {
|
|
202
|
+
// No keys to join on
|
|
203
|
+
joinedRows = joinDef.joinType === 'left' ? joinedRows : [];
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Batch fetch from join table
|
|
208
|
+
const joinRawRows = await joinRepo.find({
|
|
209
|
+
filter: {
|
|
210
|
+
[joinDef.rightField]: { $in: distinctKeys.map(String) },
|
|
211
|
+
},
|
|
212
|
+
limit: 10000,
|
|
213
|
+
});
|
|
214
|
+
const joinRows = joinRawRows.map(toPlainObject);
|
|
215
|
+
|
|
216
|
+
// Apply join
|
|
217
|
+
joinedRows = applyJoin(joinedRows, joinRows, joinDef, i);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 5. Paginate if we did full-fetch (INNER join case)
|
|
221
|
+
let totalCount: number;
|
|
222
|
+
let paginatedRows: Record<string, any>[];
|
|
223
|
+
|
|
224
|
+
if (hasInnerJoin) {
|
|
225
|
+
totalCount = joinedRows.length;
|
|
226
|
+
const start = (Number(page) - 1) * Number(pageSize);
|
|
227
|
+
paginatedRows = joinedRows.slice(start, start + Number(pageSize));
|
|
228
|
+
} else {
|
|
229
|
+
totalCount = totalPrimaryCount;
|
|
230
|
+
paginatedRows = joinedRows;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// 6. Apply column mapping
|
|
234
|
+
const finalRows = columns.length > 0 ? applyColumnMapping(paginatedRows, columns) : paginatedRows;
|
|
235
|
+
|
|
236
|
+
ctx.body = {
|
|
237
|
+
data: finalRows,
|
|
238
|
+
meta: {
|
|
239
|
+
page: Number(page),
|
|
240
|
+
pageSize: Number(pageSize),
|
|
241
|
+
count: totalCount,
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
await next();
|
|
246
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DUMMY COLLECTION
|
|
3
|
+
* This collection is created to prevent NocoBase workflow/ACL from throwing the error:
|
|
4
|
+
* '[Workflow pre-action]: collection "crossJoin" not found'
|
|
5
|
+
*
|
|
6
|
+
* Since 'crossJoin' is registered as a resourcer in plugin.ts, NocoBase
|
|
7
|
+
* implicitly looks for a collection with the same name.
|
|
8
|
+
* We set dumpRules: 'skip' and avoid timestamps to ensure this collection
|
|
9
|
+
* is completely ignored during backups/migrations and doesn't pollute the actual DB.
|
|
10
|
+
*/
|
|
11
|
+
export default {
|
|
12
|
+
name: 'crossJoin',
|
|
13
|
+
dumpRules: 'skip',
|
|
14
|
+
autoGenId: true,
|
|
15
|
+
createdAt: false,
|
|
16
|
+
updatedAt: false,
|
|
17
|
+
fields: [
|
|
18
|
+
{
|
|
19
|
+
name: 'name',
|
|
20
|
+
type: 'string',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
export { default } from './plugin';
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
import { Plugin } from '@nocobase/server';
|
|
11
|
+
import { crossJoinQuery } from './actions/cross-join-query';
|
|
12
|
+
|
|
13
|
+
import path from 'path';
|
|
14
|
+
|
|
15
|
+
export class PluginBlockCrossJoinServer extends Plugin {
|
|
16
|
+
async beforeLoad() {
|
|
17
|
+
await this.db.import({ directory: path.resolve(__dirname, 'collections') });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async load() {
|
|
21
|
+
this.app.resourcer.define({
|
|
22
|
+
name: 'crossJoin',
|
|
23
|
+
actions: {
|
|
24
|
+
query: crossJoinQuery,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
this.app.acl.allow('crossJoin', 'query', 'loggedIn');
|
|
29
|
+
this.app.acl.registerSnippet({
|
|
30
|
+
name: 'ui.crossJoin',
|
|
31
|
+
actions: ['crossJoin:*'],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default PluginBlockCrossJoinServer;
|