plugin-data-source-postgres 0.0.0-stage → 2.0.1

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