plugin-data-source-postgres 0.0.0-stage → 2.0.0
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 +9 -3
- package/client-v2.js +1 -0
- package/client.js +1 -0
- package/dist/client/index.js +10 -0
- package/dist/client-v2/985.34da95acdf7743ab.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/constants.js +45 -0
- package/dist/externalVersion.js +28 -0
- package/dist/locale/en-US.json +141 -0
- package/dist/locale/zh-CN.json +141 -0
- package/dist/server/collections/adjacency-list-repository.js +340 -0
- package/dist/server/collections/index.js +45 -0
- package/dist/server/collections/sql-collection.js +84 -0
- package/dist/server/collections/sql-model.js +147 -0
- package/dist/server/collections/tree-collection.js +118 -0
- package/dist/server/hooks/field-hooks.js +392 -0
- package/dist/server/index.js +38 -0
- package/dist/server/plugin.js +690 -0
- package/dist/server/postgres-collection-manager.js +701 -0
- package/dist/server/postgres-datasource.js +327 -0
- package/dist/server/utils/sql-validator.js +203 -0
- package/dist/server/utils/validation-error.js +59 -0
- package/package.json +51 -4
- package/server.js +1 -0
- package/src/client/AddCollectionAction.tsx +307 -0
- package/src/client/ConfigurationForm.tsx +203 -0
- package/src/client/PostgresCollectionTemplate.tsx +109 -0
- package/src/client/__tests__/client-v1.test.tsx +265 -0
- package/src/client/collection-templates/CalendarCollectionTemplate.tsx +141 -0
- package/src/client/collection-templates/FileCollectionTemplate.tsx +154 -0
- package/src/client/collection-templates/SQLCollectionTemplate.tsx +184 -0
- package/src/client/collection-templates/TreeCollectionTemplate.tsx +136 -0
- package/src/client/collection-templates/base-template-helper.ts +62 -0
- package/src/client/collection-templates/field-interfaces-config.ts +73 -0
- package/src/client/collection-templates/index.ts +19 -0
- package/src/client/index.tsx +69 -0
- package/src/client-v2/PostgresAddCollection.tsx +422 -0
- package/src/client-v2/PostgresConfigForm.tsx +178 -0
- package/src/client-v2/PostgresDeleteCollection.tsx +83 -0
- package/src/client-v2/__tests__/plugin.test.tsx +372 -0
- package/src/client-v2/collection-options.ts +22 -0
- package/src/client-v2/index.tsx +10 -0
- package/src/client-v2/plugin.tsx +71 -0
- package/src/constants.ts +20 -0
- package/src/locale/en-US.json +141 -0
- package/src/locale/zh-CN.json +141 -0
- package/src/server/__tests__/field-hooks.test.ts +357 -0
- package/src/server/__tests__/postgres-data-source.integration.test.ts +543 -0
- package/src/server/__tests__/postgres-datasource.unit.test.ts +1538 -0
- package/src/server/__tests__/sql-model.test.ts +115 -0
- package/src/server/__tests__/sql-validator.test.ts +119 -0
- package/src/server/collections/adjacency-list-repository.ts +378 -0
- package/src/server/collections/index.ts +19 -0
- package/src/server/collections/sql-collection.ts +66 -0
- package/src/server/collections/sql-model.ts +158 -0
- package/src/server/collections/tree-collection.ts +102 -0
- package/src/server/hooks/field-hooks.ts +514 -0
- package/src/server/index.ts +13 -0
- package/src/server/plugin.ts +764 -0
- package/src/server/postgres-collection-manager.ts +827 -0
- package/src/server/postgres-datasource.ts +365 -0
- package/src/server/utils/sql-validator.ts +203 -0
- package/src/server/utils/validation-error.ts +38 -0
|
@@ -0,0 +1,514 @@
|
|
|
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 { Collection } from '@nocobase/database';
|
|
11
|
+
import { Transaction, Utils } from 'sequelize';
|
|
12
|
+
import { isEqual } from 'lodash';
|
|
13
|
+
import { PostgresCollectionManager } from '../postgres-collection-manager';
|
|
14
|
+
import { PostgresValidationError } from '../utils/validation-error';
|
|
15
|
+
|
|
16
|
+
type FieldValues = {
|
|
17
|
+
name?: string;
|
|
18
|
+
type?: string;
|
|
19
|
+
field?: string;
|
|
20
|
+
interface?: string;
|
|
21
|
+
allowNull?: boolean;
|
|
22
|
+
defaultValue?: unknown;
|
|
23
|
+
unique?: boolean;
|
|
24
|
+
autoIncrement?: boolean;
|
|
25
|
+
primaryKey?: boolean;
|
|
26
|
+
length?: number;
|
|
27
|
+
precision?: number;
|
|
28
|
+
scale?: number;
|
|
29
|
+
elementType?: string;
|
|
30
|
+
settings?: FieldValues;
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const RELATION_TYPES = new Set(['belongsTo', 'belongsToMany', 'belongsToArray', 'hasOne', 'hasMany']);
|
|
35
|
+
const RELATION_INTERFACES: Record<string, string> = {
|
|
36
|
+
m2o: 'belongsTo',
|
|
37
|
+
obo: 'belongsTo',
|
|
38
|
+
o2m: 'hasMany',
|
|
39
|
+
oho: 'hasOne',
|
|
40
|
+
m2m: 'belongsToMany',
|
|
41
|
+
m2m_array: 'belongsToArray',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function removesPhysicalColumn(collection: Collection, name: string): boolean {
|
|
45
|
+
const field = collection.getField(name);
|
|
46
|
+
if (!field || field.isRelationField()) return false;
|
|
47
|
+
const columnName = field.columnName();
|
|
48
|
+
return !collection
|
|
49
|
+
.getFields()
|
|
50
|
+
.some((other) => other.name !== name && !other.isRelationField() && other.columnName() === columnName);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const ARRAY_ELEMENT_TYPE_MAP: Record<string, string> = {
|
|
54
|
+
nanoid: 'string',
|
|
55
|
+
sequence: 'string',
|
|
56
|
+
uid: 'string',
|
|
57
|
+
snowflakeId: 'bigInt',
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function fieldReferencesColumn(
|
|
61
|
+
values: FieldValues,
|
|
62
|
+
sourceCollection: string,
|
|
63
|
+
targetCollection: string,
|
|
64
|
+
columns: string | string[],
|
|
65
|
+
): boolean {
|
|
66
|
+
const candidates = new Set(Array.isArray(columns) ? columns : [columns]);
|
|
67
|
+
const type = values.type;
|
|
68
|
+
if (values.target === targetCollection) {
|
|
69
|
+
if (
|
|
70
|
+
(type === 'belongsTo' || type === 'belongsToMany' || type === 'belongsToArray') &&
|
|
71
|
+
candidates.has(String(values.targetKey || 'id'))
|
|
72
|
+
) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if ((type === 'hasOne' || type === 'hasMany') && values.foreignKey && candidates.has(String(values.foreignKey))) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (sourceCollection === targetCollection) {
|
|
80
|
+
if (
|
|
81
|
+
(type === 'belongsTo' || type === 'belongsToArray') &&
|
|
82
|
+
values.foreignKey &&
|
|
83
|
+
candidates.has(String(values.foreignKey))
|
|
84
|
+
) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if (
|
|
88
|
+
(type === 'hasOne' || type === 'hasMany' || type === 'belongsToMany') &&
|
|
89
|
+
candidates.has(String(values.sourceKey || 'id'))
|
|
90
|
+
) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return (
|
|
95
|
+
values.through === targetCollection &&
|
|
96
|
+
((Boolean(values.foreignKey) && candidates.has(String(values.foreignKey))) ||
|
|
97
|
+
(Boolean(values.otherKey) && candidates.has(String(values.otherKey))))
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function buildArrayForeignKeyField(
|
|
102
|
+
manager: PostgresCollectionManager,
|
|
103
|
+
collectionName: string,
|
|
104
|
+
relationOptions: FieldValues,
|
|
105
|
+
): FieldValues | undefined {
|
|
106
|
+
if (relationOptions.type !== 'belongsToArray') return undefined;
|
|
107
|
+
const foreignKey = typeof relationOptions.foreignKey === 'string' ? relationOptions.foreignKey : undefined;
|
|
108
|
+
const target = typeof relationOptions.target === 'string' ? relationOptions.target : undefined;
|
|
109
|
+
const targetKey = typeof relationOptions.targetKey === 'string' ? relationOptions.targetKey : 'id';
|
|
110
|
+
if (!foreignKey || !target || foreignKey === relationOptions.name) return undefined;
|
|
111
|
+
const sourceCollection = manager.getCollection(collectionName);
|
|
112
|
+
if (!sourceCollection || sourceCollection.getField(foreignKey)) return undefined;
|
|
113
|
+
const targetFieldType = String(manager.getCollection(target)?.getField(targetKey)?.options?.type || 'bigInt');
|
|
114
|
+
const elementType = ARRAY_ELEMENT_TYPE_MAP[targetFieldType] || targetFieldType;
|
|
115
|
+
return {
|
|
116
|
+
name: foreignKey,
|
|
117
|
+
field: foreignKey,
|
|
118
|
+
type: 'set',
|
|
119
|
+
dataType: 'array',
|
|
120
|
+
elementType,
|
|
121
|
+
interface: 'json',
|
|
122
|
+
isForeignKey: true,
|
|
123
|
+
uiSchema: {
|
|
124
|
+
type: 'object',
|
|
125
|
+
title: foreignKey,
|
|
126
|
+
'x-component': 'Input.JSON',
|
|
127
|
+
'x-read-pretty': true,
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function normalizeValues(input: FieldValues): FieldValues {
|
|
133
|
+
const values = { ...(input.settings || {}), ...input };
|
|
134
|
+
delete values.settings;
|
|
135
|
+
if (!values.type && values.interface && RELATION_INTERFACES[values.interface]) {
|
|
136
|
+
values.type = RELATION_INTERFACES[values.interface];
|
|
137
|
+
}
|
|
138
|
+
return values;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function defaultThroughTable(collectionName: string, target: string): string {
|
|
142
|
+
return Utils.camelize(
|
|
143
|
+
[collectionName, target]
|
|
144
|
+
.map((item) => item.toLowerCase())
|
|
145
|
+
.sort()
|
|
146
|
+
.join('_'),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function getRelationLockTables(
|
|
151
|
+
manager: PostgresCollectionManager,
|
|
152
|
+
collectionName: string,
|
|
153
|
+
input: FieldValues,
|
|
154
|
+
fieldName?: string,
|
|
155
|
+
): string[] {
|
|
156
|
+
const schema = manager.db?.options?.schema || 'public';
|
|
157
|
+
const tables = new Set<string>();
|
|
158
|
+
const addTable = (name: string) => {
|
|
159
|
+
const col = manager.getCollection(name);
|
|
160
|
+
const tableName = col?.options?.tableName || name;
|
|
161
|
+
tables.add(`${schema}.${tableName}`);
|
|
162
|
+
};
|
|
163
|
+
addTable(collectionName);
|
|
164
|
+
const values = normalizeValues(input);
|
|
165
|
+
const resolvedName = fieldName || (typeof values.name === 'string' ? values.name : undefined);
|
|
166
|
+
const existing = resolvedName
|
|
167
|
+
? (manager.getCollection(collectionName)?.getField(resolvedName)?.options as FieldValues | undefined)
|
|
168
|
+
: undefined;
|
|
169
|
+
const addRelationTables = (candidate?: FieldValues) => {
|
|
170
|
+
if (!candidate) return;
|
|
171
|
+
const type = candidate.type;
|
|
172
|
+
if (typeof candidate.target === 'string' && candidate.target && type && RELATION_TYPES.has(type)) {
|
|
173
|
+
addTable(candidate.target);
|
|
174
|
+
if (type === 'belongsToMany') {
|
|
175
|
+
const through =
|
|
176
|
+
typeof candidate.through === 'string' && candidate.through
|
|
177
|
+
? candidate.through
|
|
178
|
+
: defaultThroughTable(collectionName, candidate.target);
|
|
179
|
+
addTable(through);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
addRelationTables(existing);
|
|
184
|
+
addRelationTables(existing ? { ...existing, ...values } : values);
|
|
185
|
+
return [...tables].sort();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function ensureBelongsTo(
|
|
189
|
+
manager: PostgresCollectionManager,
|
|
190
|
+
collectionName: string,
|
|
191
|
+
name: string,
|
|
192
|
+
values: FieldValues,
|
|
193
|
+
input: FieldValues,
|
|
194
|
+
transaction: Transaction,
|
|
195
|
+
replaceActions = false,
|
|
196
|
+
) {
|
|
197
|
+
const target = typeof values.target === 'string' ? values.target : undefined;
|
|
198
|
+
if (!target) throw new PostgresValidationError('Relation field {{name}} requires a target', { name });
|
|
199
|
+
const targetCollection = manager.getCollection(target);
|
|
200
|
+
if (targetCollection?.options?.view || targetCollection?.options?.sql) {
|
|
201
|
+
throw new PostgresValidationError('This collection is read-only');
|
|
202
|
+
}
|
|
203
|
+
const targetKey =
|
|
204
|
+
typeof values.targetKey === 'string' && values.targetKey
|
|
205
|
+
? values.targetKey
|
|
206
|
+
: targetCollection?.model?.primaryKeyAttribute || 'id';
|
|
207
|
+
const foreignKey =
|
|
208
|
+
typeof values.foreignKey === 'string' && values.foreignKey
|
|
209
|
+
? values.foreignKey
|
|
210
|
+
: Utils.camelize([Utils.singularize(name), targetKey].join('_'));
|
|
211
|
+
await manager.ensureForeignKey(
|
|
212
|
+
collectionName,
|
|
213
|
+
{
|
|
214
|
+
foreignKey,
|
|
215
|
+
targetKey,
|
|
216
|
+
target,
|
|
217
|
+
onDelete: typeof values.onDelete === 'string' && values.onDelete ? values.onDelete : undefined,
|
|
218
|
+
onUpdate: typeof values.onUpdate === 'string' && values.onUpdate ? values.onUpdate : undefined,
|
|
219
|
+
replaceActions,
|
|
220
|
+
},
|
|
221
|
+
transaction,
|
|
222
|
+
);
|
|
223
|
+
if (!input.type) input.type = 'belongsTo';
|
|
224
|
+
if (!input.target) input.target = target;
|
|
225
|
+
if (!input.targetKey) input.targetKey = targetKey;
|
|
226
|
+
if (!input.foreignKey) input.foreignKey = foreignKey;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function ensureHasRelation(
|
|
230
|
+
manager: PostgresCollectionManager,
|
|
231
|
+
collectionName: string,
|
|
232
|
+
name: string,
|
|
233
|
+
type: 'hasMany' | 'hasOne',
|
|
234
|
+
values: FieldValues,
|
|
235
|
+
input: FieldValues,
|
|
236
|
+
transaction: Transaction,
|
|
237
|
+
replaceActions = false,
|
|
238
|
+
) {
|
|
239
|
+
const target = typeof values.target === 'string' ? values.target : undefined;
|
|
240
|
+
if (!target) throw new PostgresValidationError('Relation field {{name}} requires a target', { name });
|
|
241
|
+
const sourceCollection = manager.getCollection(collectionName);
|
|
242
|
+
const targetCollection = manager.getCollection(target);
|
|
243
|
+
if (!targetCollection) {
|
|
244
|
+
throw new PostgresValidationError('Collection {{name}} does not exist', { name: target });
|
|
245
|
+
}
|
|
246
|
+
const sourceKey =
|
|
247
|
+
typeof values.sourceKey === 'string' && values.sourceKey
|
|
248
|
+
? values.sourceKey
|
|
249
|
+
: sourceCollection?.model?.primaryKeyAttribute || 'id';
|
|
250
|
+
const foreignKey =
|
|
251
|
+
typeof values.foreignKey === 'string' && values.foreignKey
|
|
252
|
+
? values.foreignKey
|
|
253
|
+
: Utils.camelize([Utils.singularize(collectionName), sourceKey].join('_'));
|
|
254
|
+
if (targetCollection.options?.view || targetCollection.options?.sql) {
|
|
255
|
+
if (!targetCollection.getField(foreignKey)) {
|
|
256
|
+
throw new PostgresValidationError('This collection is read-only');
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
await manager.ensureForeignKey(
|
|
260
|
+
target,
|
|
261
|
+
{
|
|
262
|
+
foreignKey,
|
|
263
|
+
targetKey: sourceKey,
|
|
264
|
+
target: collectionName,
|
|
265
|
+
onDelete: typeof values.onDelete === 'string' && values.onDelete ? values.onDelete : undefined,
|
|
266
|
+
onUpdate: typeof values.onUpdate === 'string' && values.onUpdate ? values.onUpdate : undefined,
|
|
267
|
+
replaceActions,
|
|
268
|
+
},
|
|
269
|
+
transaction,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
if (!input.type) input.type = type;
|
|
273
|
+
if (!input.target) input.target = target;
|
|
274
|
+
if (!input.sourceKey) input.sourceKey = sourceKey;
|
|
275
|
+
if (!input.foreignKey) input.foreignKey = foreignKey;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function ensureBelongsToMany(
|
|
279
|
+
manager: PostgresCollectionManager,
|
|
280
|
+
collectionName: string,
|
|
281
|
+
name: string,
|
|
282
|
+
values: FieldValues,
|
|
283
|
+
input: FieldValues,
|
|
284
|
+
transaction: Transaction,
|
|
285
|
+
replaceActions = false,
|
|
286
|
+
) {
|
|
287
|
+
const target = typeof values.target === 'string' ? values.target : undefined;
|
|
288
|
+
if (!target) throw new PostgresValidationError('Relation field {{name}} requires a target', { name });
|
|
289
|
+
const sourceCollection = manager.getCollection(collectionName);
|
|
290
|
+
const targetCollection = manager.getCollection(target);
|
|
291
|
+
if (!targetCollection) {
|
|
292
|
+
throw new PostgresValidationError('Collection {{name}} does not exist', { name: target });
|
|
293
|
+
}
|
|
294
|
+
if (targetCollection.options?.view || targetCollection.options?.sql) {
|
|
295
|
+
throw new PostgresValidationError('This collection is read-only');
|
|
296
|
+
}
|
|
297
|
+
const sourceKey =
|
|
298
|
+
typeof values.sourceKey === 'string' && values.sourceKey
|
|
299
|
+
? values.sourceKey
|
|
300
|
+
: sourceCollection?.model?.primaryKeyAttribute || 'id';
|
|
301
|
+
const targetKey =
|
|
302
|
+
typeof values.targetKey === 'string' && values.targetKey
|
|
303
|
+
? values.targetKey
|
|
304
|
+
: targetCollection.model?.primaryKeyAttribute || 'id';
|
|
305
|
+
const foreignKey =
|
|
306
|
+
typeof values.foreignKey === 'string' && values.foreignKey
|
|
307
|
+
? values.foreignKey
|
|
308
|
+
: Utils.camelize([Utils.singularize(collectionName), sourceKey].join('_'));
|
|
309
|
+
const otherKey =
|
|
310
|
+
typeof values.otherKey === 'string' && values.otherKey
|
|
311
|
+
? values.otherKey
|
|
312
|
+
: Utils.camelize([Utils.singularize(target), targetKey].join('_'));
|
|
313
|
+
const through =
|
|
314
|
+
typeof values.through === 'string' && values.through ? values.through : defaultThroughTable(collectionName, target);
|
|
315
|
+
await manager.ensureJoinTable(
|
|
316
|
+
collectionName,
|
|
317
|
+
{
|
|
318
|
+
through,
|
|
319
|
+
foreignKey,
|
|
320
|
+
otherKey,
|
|
321
|
+
sourceKey,
|
|
322
|
+
targetKey,
|
|
323
|
+
target,
|
|
324
|
+
onDelete: typeof values.onDelete === 'string' && values.onDelete ? values.onDelete : undefined,
|
|
325
|
+
onUpdate: typeof values.onUpdate === 'string' && values.onUpdate ? values.onUpdate : undefined,
|
|
326
|
+
replaceActions,
|
|
327
|
+
},
|
|
328
|
+
transaction,
|
|
329
|
+
);
|
|
330
|
+
if (!input.type) input.type = 'belongsToMany';
|
|
331
|
+
if (!input.target) input.target = target;
|
|
332
|
+
if (!input.sourceKey) input.sourceKey = sourceKey;
|
|
333
|
+
if (!input.targetKey) input.targetKey = targetKey;
|
|
334
|
+
if (!input.foreignKey) input.foreignKey = foreignKey;
|
|
335
|
+
if (!input.otherKey) input.otherKey = otherKey;
|
|
336
|
+
if (!input.through) input.through = through;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async function ensureBelongsToArray(
|
|
340
|
+
manager: PostgresCollectionManager,
|
|
341
|
+
collectionName: string,
|
|
342
|
+
name: string,
|
|
343
|
+
values: FieldValues,
|
|
344
|
+
input: FieldValues,
|
|
345
|
+
transaction: Transaction,
|
|
346
|
+
) {
|
|
347
|
+
const target = typeof values.target === 'string' ? values.target : undefined;
|
|
348
|
+
if (!target) throw new PostgresValidationError('Relation field {{name}} requires a target', { name });
|
|
349
|
+
const targetCollection = manager.getCollection(target);
|
|
350
|
+
if (!targetCollection) {
|
|
351
|
+
throw new PostgresValidationError('Collection {{name}} does not exist', { name: target });
|
|
352
|
+
}
|
|
353
|
+
if (targetCollection.options?.sql) {
|
|
354
|
+
throw new PostgresValidationError('This collection is read-only');
|
|
355
|
+
}
|
|
356
|
+
const targetKey =
|
|
357
|
+
typeof values.targetKey === 'string' && values.targetKey
|
|
358
|
+
? values.targetKey
|
|
359
|
+
: targetCollection.model?.primaryKeyAttribute || 'id';
|
|
360
|
+
const foreignKey =
|
|
361
|
+
typeof values.foreignKey === 'string' && values.foreignKey
|
|
362
|
+
? values.foreignKey
|
|
363
|
+
: Utils.camelize([Utils.singularize(name), targetKey].join('_'));
|
|
364
|
+
if (name === foreignKey) {
|
|
365
|
+
throw new PostgresValidationError(
|
|
366
|
+
'Foreign key {{foreignKey}} cannot have the same name as relation field {{name}}',
|
|
367
|
+
{
|
|
368
|
+
foreignKey,
|
|
369
|
+
name,
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
await manager.ensureForeignKeyArray(collectionName, { foreignKey, targetKey, target }, transaction);
|
|
374
|
+
if (!input.type) input.type = 'belongsToArray';
|
|
375
|
+
if (!input.target) input.target = target;
|
|
376
|
+
if (!input.targetKey) input.targetKey = targetKey;
|
|
377
|
+
if (!input.foreignKey) input.foreignKey = foreignKey;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export async function syncFieldAction(
|
|
381
|
+
manager: PostgresCollectionManager,
|
|
382
|
+
action: string,
|
|
383
|
+
collectionName: string,
|
|
384
|
+
fieldName: string | undefined,
|
|
385
|
+
input: FieldValues,
|
|
386
|
+
transaction: Transaction,
|
|
387
|
+
): Promise<void> {
|
|
388
|
+
const collection = manager.getCollection(collectionName);
|
|
389
|
+
if (!collection) {
|
|
390
|
+
throw new PostgresValidationError('Collection {{name}} does not exist', { name: collectionName });
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const values = normalizeValues(input);
|
|
394
|
+
const name = fieldName || values.name;
|
|
395
|
+
if (!name) {
|
|
396
|
+
throw new PostgresValidationError('Field name is required');
|
|
397
|
+
}
|
|
398
|
+
const field = collection.getField(name);
|
|
399
|
+
|
|
400
|
+
if (action === 'destroy') {
|
|
401
|
+
if (!field) {
|
|
402
|
+
throw new PostgresValidationError('Field {{name}} does not exist', { name });
|
|
403
|
+
}
|
|
404
|
+
if (!removesPhysicalColumn(collection, name)) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
const columnNames = [name, field.columnName()];
|
|
408
|
+
for (const candidate of manager.getCollections()) {
|
|
409
|
+
for (const reference of candidate.getFields()) {
|
|
410
|
+
if (fieldReferencesColumn(reference.options, candidate.name, collectionName, columnNames)) {
|
|
411
|
+
throw new PostgresValidationError('Field {{name}} is referenced by {{collection}}.{{field}}', {
|
|
412
|
+
name,
|
|
413
|
+
collection: candidate.name,
|
|
414
|
+
field: reference.name,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
await manager.removeField(collectionName, name, false, transaction);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (!field) {
|
|
424
|
+
if (values.type === 'belongsTo') {
|
|
425
|
+
await ensureBelongsTo(manager, collectionName, name, values, input, transaction);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (values.type === 'hasMany' || values.type === 'hasOne') {
|
|
429
|
+
await ensureHasRelation(manager, collectionName, name, values.type, values, input, transaction);
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (values.type === 'belongsToMany') {
|
|
433
|
+
await ensureBelongsToMany(manager, collectionName, name, values, input, transaction);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (values.type === 'belongsToArray') {
|
|
437
|
+
await ensureBelongsToArray(manager, collectionName, name, values, input, transaction);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
await manager.addField(collectionName, { ...values, name }, transaction);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (action === 'create') {
|
|
444
|
+
throw new PostgresValidationError('Field {{name}} already exists', { name });
|
|
445
|
+
}
|
|
446
|
+
if (
|
|
447
|
+
['autoIncrement', 'primaryKey'].some(
|
|
448
|
+
(key) =>
|
|
449
|
+
Object.prototype.hasOwnProperty.call(values, key) && Boolean(values[key]) !== Boolean(field.options[key]),
|
|
450
|
+
)
|
|
451
|
+
) {
|
|
452
|
+
throw new PostgresValidationError('Field {{name}} cannot change generated-id or primary-key settings', { name });
|
|
453
|
+
}
|
|
454
|
+
if (values.type && RELATION_TYPES.has(field.options.type) !== RELATION_TYPES.has(values.type)) {
|
|
455
|
+
throw new PostgresValidationError('Field {{name}} cannot be changed between a relation and a physical column', {
|
|
456
|
+
name,
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
if (RELATION_TYPES.has(field.options.type)) {
|
|
460
|
+
const merged = { ...field.options, ...values };
|
|
461
|
+
if (
|
|
462
|
+
field.options.type === 'belongsTo' &&
|
|
463
|
+
['target', 'foreignKey', 'targetKey', 'onDelete', 'onUpdate'].some(
|
|
464
|
+
(key) => Object.prototype.hasOwnProperty.call(values, key) && !isEqual(values[key], field.options[key]),
|
|
465
|
+
)
|
|
466
|
+
) {
|
|
467
|
+
await ensureBelongsTo(manager, collectionName, name, merged, input, transaction, true);
|
|
468
|
+
} else if (
|
|
469
|
+
(field.options.type === 'hasMany' || field.options.type === 'hasOne') &&
|
|
470
|
+
['target', 'foreignKey', 'sourceKey', 'onDelete', 'onUpdate'].some(
|
|
471
|
+
(key) => Object.prototype.hasOwnProperty.call(values, key) && !isEqual(values[key], field.options[key]),
|
|
472
|
+
)
|
|
473
|
+
) {
|
|
474
|
+
await ensureHasRelation(manager, collectionName, name, field.options.type, merged, input, transaction, true);
|
|
475
|
+
} else if (
|
|
476
|
+
field.options.type === 'belongsToMany' &&
|
|
477
|
+
['target', 'through', 'foreignKey', 'otherKey', 'sourceKey', 'targetKey', 'onDelete', 'onUpdate'].some(
|
|
478
|
+
(key) => Object.prototype.hasOwnProperty.call(values, key) && !isEqual(values[key], field.options[key]),
|
|
479
|
+
)
|
|
480
|
+
) {
|
|
481
|
+
await ensureBelongsToMany(manager, collectionName, name, merged, input, transaction, true);
|
|
482
|
+
} else if (
|
|
483
|
+
field.options.type === 'belongsToArray' &&
|
|
484
|
+
['target', 'foreignKey', 'targetKey'].some(
|
|
485
|
+
(key) => Object.prototype.hasOwnProperty.call(values, key) && !isEqual(values[key], field.options[key]),
|
|
486
|
+
)
|
|
487
|
+
) {
|
|
488
|
+
await ensureBelongsToArray(manager, collectionName, name, merged, input, transaction);
|
|
489
|
+
}
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const changes: Pick<
|
|
494
|
+
FieldValues,
|
|
495
|
+
'type' | 'allowNull' | 'defaultValue' | 'unique' | 'length' | 'precision' | 'scale' | 'elementType'
|
|
496
|
+
> = {};
|
|
497
|
+
for (const key of [
|
|
498
|
+
'type',
|
|
499
|
+
'allowNull',
|
|
500
|
+
'defaultValue',
|
|
501
|
+
'unique',
|
|
502
|
+
'length',
|
|
503
|
+
'precision',
|
|
504
|
+
'scale',
|
|
505
|
+
'elementType',
|
|
506
|
+
] as const) {
|
|
507
|
+
if (Object.prototype.hasOwnProperty.call(values, key) && !isEqual(values[key], field.options[key])) {
|
|
508
|
+
Object.assign(changes, { [key]: values[key] });
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (Object.keys(changes).length) {
|
|
512
|
+
await manager.updateField(collectionName, name, changes, transaction);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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 { PluginDataSourcePostgresServer } from './plugin';
|
|
11
|
+
|
|
12
|
+
export { PluginDataSourcePostgresServer };
|
|
13
|
+
export default PluginDataSourcePostgresServer;
|