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.
- 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 +29 -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 +149 -0
- package/dist/server/collections/tree-collection.js +118 -0
- package/dist/server/hooks/field-hooks.js +412 -0
- package/dist/server/index.js +38 -0
- package/dist/server/plugin.js +1273 -0
- package/dist/server/postgres-collection-manager.js +868 -0
- package/dist/server/postgres-datasource.js +361 -0
- package/dist/server/utils/sql-validator.js +253 -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 +2423 -0
- package/src/server/__tests__/sql-model.test.ts +159 -0
- package/src/server/__tests__/sql-validator.test.ts +126 -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 +548 -0
- package/src/server/index.ts +13 -0
- package/src/server/plugin.ts +1499 -0
- package/src/server/postgres-collection-manager.ts +1027 -0
- package/src/server/postgres-datasource.ts +410 -0
- package/src/server/utils/sql-validator.ts +258 -0
- package/src/server/utils/validation-error.ts +38 -0
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "plugin-data-source-postgres",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"displayName": "Data Source: External PostgreSQL",
|
|
4
|
+
"displayName.zh-CN": "数据源:外部PostgreSQL",
|
|
5
|
+
"description": "Connect to external PostgreSQL databases as data sources",
|
|
6
|
+
"description.zh-CN": "连接外部PostgreSQL数据库作为数据源",
|
|
7
|
+
"version": "2.0.1",
|
|
8
|
+
"license": "AGPL-3.0",
|
|
9
|
+
"main": "dist/server/index.js",
|
|
10
|
+
"nocobase": {
|
|
11
|
+
"supportedVersions": [
|
|
12
|
+
"2.x"
|
|
13
|
+
],
|
|
14
|
+
"editionLevel": 0
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"client.js",
|
|
20
|
+
"client-v2.js",
|
|
21
|
+
"server.js",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"homepage": "https://docs.nocobase.com/handbook/data-source-postgres",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"data-sources",
|
|
27
|
+
"PostgreSQL",
|
|
28
|
+
"External Database"
|
|
29
|
+
],
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@nocobase/actions": "2.x",
|
|
32
|
+
"@nocobase/client": "2.x",
|
|
33
|
+
"@nocobase/client-v2": "2.x",
|
|
34
|
+
"@nocobase/data-source-manager": "2.x",
|
|
35
|
+
"@nocobase/database": "2.x",
|
|
36
|
+
"@nocobase/flow-engine": "2.x",
|
|
37
|
+
"@nocobase/plugin-data-source-manager": "2.x",
|
|
38
|
+
"@nocobase/server": "2.x"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"pg": "^8.16.3"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/OrgGem/nocobase-beta.git",
|
|
46
|
+
"directory": "packages/plugins/plugin-data-source-postgres"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "5b21f0f8498a767052d7ee569b54c3e5378ca04f",
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"registry": "https://registry.npmjs.org/"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/server.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/server');
|
|
@@ -0,0 +1,307 @@
|
|
|
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 React, { useMemo, useState } from 'react';
|
|
11
|
+
import { ArrayItems, Space } from '@formily/antd-v5';
|
|
12
|
+
import { type ISchema, useForm, useField } from '@formily/react';
|
|
13
|
+
import { cloneDeep, uniqueId } from 'lodash';
|
|
14
|
+
import {
|
|
15
|
+
useAPIClient,
|
|
16
|
+
useActionContext,
|
|
17
|
+
useDataSourceManager,
|
|
18
|
+
useRecord,
|
|
19
|
+
useCompile,
|
|
20
|
+
useCollectionManager_deprecated,
|
|
21
|
+
useCancelAction,
|
|
22
|
+
useResourceActionContext,
|
|
23
|
+
SchemaComponent,
|
|
24
|
+
ActionContextProvider,
|
|
25
|
+
RecordProvider,
|
|
26
|
+
useRequest,
|
|
27
|
+
type CollectionTemplate,
|
|
28
|
+
} from '@nocobase/client';
|
|
29
|
+
import { useParams } from 'react-router-dom';
|
|
30
|
+
import { Button, Dropdown, message, type MenuProps } from 'antd';
|
|
31
|
+
import { PlusOutlined, DownOutlined } from '@ant-design/icons';
|
|
32
|
+
import {
|
|
33
|
+
PostgresGeneralCollectionTemplate,
|
|
34
|
+
PostgresCalendarCollectionTemplate,
|
|
35
|
+
PostgresTreeCollectionTemplate,
|
|
36
|
+
PostgresFileCollectionTemplate,
|
|
37
|
+
PostgresSQLCollectionTemplate,
|
|
38
|
+
} from './collection-templates';
|
|
39
|
+
import { nsT } from './collection-templates/base-template-helper';
|
|
40
|
+
|
|
41
|
+
type SqlFieldType = 'string' | 'text' | 'integer' | 'bigInt' | 'decimal' | 'boolean' | 'date' | 'json' | 'uuid';
|
|
42
|
+
|
|
43
|
+
const SQL_FIELD_INTERFACES: Record<SqlFieldType, string> = {
|
|
44
|
+
string: 'input',
|
|
45
|
+
text: 'textarea',
|
|
46
|
+
integer: 'integer',
|
|
47
|
+
bigInt: 'integer',
|
|
48
|
+
decimal: 'number',
|
|
49
|
+
boolean: 'checkbox',
|
|
50
|
+
date: 'datetime',
|
|
51
|
+
json: 'json',
|
|
52
|
+
uuid: 'uuid',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
type SqlFieldInput = { name?: string; type?: SqlFieldType };
|
|
56
|
+
type FieldLike = Record<string, unknown> & { name?: string; type?: string; target?: string };
|
|
57
|
+
type CollectionRecordLike = Record<string, unknown> & { key?: string; name?: string };
|
|
58
|
+
type PostgresAddCollectionProps = {
|
|
59
|
+
scope?: Record<string, unknown>;
|
|
60
|
+
getContainer?: () => HTMLElement;
|
|
61
|
+
item?: CollectionRecordLike;
|
|
62
|
+
children?: React.ReactNode;
|
|
63
|
+
trigger?: ('click' | 'hover' | 'contextMenu')[];
|
|
64
|
+
type?: 'primary' | 'default' | 'dashed' | 'link' | 'text';
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get schema for collection creation form
|
|
69
|
+
*/
|
|
70
|
+
const getSchema = (schema: CollectionTemplate | undefined, category: string | null): ISchema | undefined => {
|
|
71
|
+
if (!schema) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const properties = cloneDeep(schema.configurableProperties || {}) as Record<string, Record<string, unknown>>;
|
|
76
|
+
|
|
77
|
+
if (properties.name) {
|
|
78
|
+
properties.name['x-disabled'] = false;
|
|
79
|
+
properties.name['x-read-only'] = false;
|
|
80
|
+
const componentProps = (properties.name['x-component-props'] as Record<string, unknown> | undefined) || {};
|
|
81
|
+
properties.name['x-component-props'] = {
|
|
82
|
+
...componentProps,
|
|
83
|
+
disabled: false,
|
|
84
|
+
readOnly: false,
|
|
85
|
+
placeholder: `t_${uniqueId()}`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const initialValue: Record<string, unknown> = {
|
|
90
|
+
template: schema.name,
|
|
91
|
+
category: category || null,
|
|
92
|
+
...cloneDeep(schema.default || {}),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
type: 'object',
|
|
97
|
+
properties: {
|
|
98
|
+
[uniqueId()]: {
|
|
99
|
+
type: 'void',
|
|
100
|
+
'x-component': 'Action.Drawer',
|
|
101
|
+
'x-component-props': {
|
|
102
|
+
getContainer: '{{ getContainer }}',
|
|
103
|
+
},
|
|
104
|
+
'x-decorator': 'Form',
|
|
105
|
+
'x-decorator-props': {
|
|
106
|
+
useValues(options: Record<string, unknown>) {
|
|
107
|
+
return useRequest(
|
|
108
|
+
() =>
|
|
109
|
+
Promise.resolve({
|
|
110
|
+
data: initialValue,
|
|
111
|
+
}),
|
|
112
|
+
options,
|
|
113
|
+
);
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
title: nsT('Create collection'),
|
|
117
|
+
properties: {
|
|
118
|
+
summary: {
|
|
119
|
+
type: 'void',
|
|
120
|
+
'x-component': 'TemplateSummay',
|
|
121
|
+
'x-component-props': {
|
|
122
|
+
schemaKey: schema.name,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
...(properties as Record<string, ISchema>),
|
|
126
|
+
footer: {
|
|
127
|
+
type: 'void',
|
|
128
|
+
'x-component': 'Action.Drawer.Footer',
|
|
129
|
+
properties: {
|
|
130
|
+
action1: {
|
|
131
|
+
title: nsT('Cancel'),
|
|
132
|
+
'x-component': 'Action',
|
|
133
|
+
'x-component-props': {
|
|
134
|
+
useAction: '{{ useCancelAction }}',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
action2: {
|
|
138
|
+
title: nsT('Submit'),
|
|
139
|
+
'x-component': 'Action',
|
|
140
|
+
'x-component-props': {
|
|
141
|
+
type: 'primary',
|
|
142
|
+
useAction: '{{ useCreatePostgresCollection }}',
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Custom hook for creating collections in PostgreSQL datasources
|
|
155
|
+
*/
|
|
156
|
+
const useCreatePostgresCollection = () => {
|
|
157
|
+
const form = useForm();
|
|
158
|
+
const { refreshCM } = useCollectionManager_deprecated();
|
|
159
|
+
const resourceAction = useResourceActionContext();
|
|
160
|
+
const ctx = useActionContext();
|
|
161
|
+
const { name: dataSourceKey } = useParams<{ name?: string }>();
|
|
162
|
+
const api = useAPIClient();
|
|
163
|
+
const dm = useDataSourceManager();
|
|
164
|
+
const field = useField();
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
async run() {
|
|
168
|
+
field.data = field.data || {};
|
|
169
|
+
field.data.loading = true;
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
await form.submit();
|
|
173
|
+
const values = cloneDeep(form.values) as Record<string, unknown> & {
|
|
174
|
+
name?: string;
|
|
175
|
+
template?: string;
|
|
176
|
+
fields?: (FieldLike | string)[];
|
|
177
|
+
sqlFields?: SqlFieldInput[];
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
if (values.template === 'sql' && Array.isArray(values.sqlFields)) {
|
|
181
|
+
values.fields = values.sqlFields
|
|
182
|
+
.filter((item): item is { name: string; type?: SqlFieldType } => Boolean(item?.name?.trim()))
|
|
183
|
+
.map(({ name, type }) => {
|
|
184
|
+
const fieldType: SqlFieldType = type && type in SQL_FIELD_INTERFACES ? type : 'string';
|
|
185
|
+
return {
|
|
186
|
+
name: name.trim(),
|
|
187
|
+
type: fieldType,
|
|
188
|
+
interface: SQL_FIELD_INTERFACES[fieldType],
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
delete values.sqlFields;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (values.template === 'general' && Array.isArray(values.fields)) {
|
|
195
|
+
const selected = new Set(values.fields.map((item) => (typeof item === 'string' ? item : item?.name)));
|
|
196
|
+
values.autoGenId = selected.has('id');
|
|
197
|
+
values.createdAt = selected.has('createdAt');
|
|
198
|
+
values.updatedAt = selected.has('updatedAt');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (values.template === 'tree' && Array.isArray(values.fields) && typeof values.name === 'string') {
|
|
202
|
+
values.fields = values.fields.map((item) => {
|
|
203
|
+
if (typeof item === 'object' && item !== null && (item.type === 'belongsTo' || item.type === 'hasMany')) {
|
|
204
|
+
return item.target ? item : { ...item, target: values.name };
|
|
205
|
+
}
|
|
206
|
+
return item;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
await api.request({
|
|
211
|
+
url: `dataSources/${encodeURIComponent(dataSourceKey || '')}/collections:create`,
|
|
212
|
+
method: 'post',
|
|
213
|
+
data: {
|
|
214
|
+
logging: true,
|
|
215
|
+
...values,
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
ctx.setVisible(false);
|
|
220
|
+
await form.reset();
|
|
221
|
+
|
|
222
|
+
const dataSource = dm.getDataSource(dataSourceKey);
|
|
223
|
+
if (dataSource) {
|
|
224
|
+
await dataSource.reload();
|
|
225
|
+
}
|
|
226
|
+
await refreshCM();
|
|
227
|
+
await resourceAction?.refresh?.();
|
|
228
|
+
} catch (error) {
|
|
229
|
+
const errorText = error instanceof Error ? error.message : String(error || 'Failed to create collection');
|
|
230
|
+
message.error(errorText);
|
|
231
|
+
} finally {
|
|
232
|
+
field.data.loading = false;
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Custom AddCollection component for PostgreSQL datasources
|
|
240
|
+
*/
|
|
241
|
+
export const PostgresAddCollectionAction: React.FC<PostgresAddCollectionProps> = (props) => {
|
|
242
|
+
const { scope, getContainer, item: record, children, trigger } = props;
|
|
243
|
+
const dataSourceManager = useDataSourceManager();
|
|
244
|
+
const [visible, setVisible] = useState(false);
|
|
245
|
+
const [schema, setSchema] = useState<ISchema>({});
|
|
246
|
+
const compile = useCompile();
|
|
247
|
+
|
|
248
|
+
const allTemplates = useMemo<CollectionTemplate[]>(() => {
|
|
249
|
+
const templateManager = dataSourceManager?.collectionTemplateManager;
|
|
250
|
+
return [
|
|
251
|
+
new PostgresGeneralCollectionTemplate(templateManager),
|
|
252
|
+
new PostgresCalendarCollectionTemplate(templateManager),
|
|
253
|
+
new PostgresTreeCollectionTemplate(templateManager),
|
|
254
|
+
new PostgresFileCollectionTemplate(templateManager),
|
|
255
|
+
new PostgresSQLCollectionTemplate(templateManager),
|
|
256
|
+
];
|
|
257
|
+
}, [dataSourceManager]);
|
|
258
|
+
|
|
259
|
+
const items = useMemo<MenuProps['items']>(() => {
|
|
260
|
+
return allTemplates.map((template) => ({
|
|
261
|
+
label: compile(template.title),
|
|
262
|
+
key: template.name,
|
|
263
|
+
}));
|
|
264
|
+
}, [allTemplates, compile]);
|
|
265
|
+
|
|
266
|
+
return (
|
|
267
|
+
<RecordProvider record={record}>
|
|
268
|
+
<ActionContextProvider value={{ visible, setVisible }}>
|
|
269
|
+
<Dropdown
|
|
270
|
+
trigger={trigger}
|
|
271
|
+
menu={{
|
|
272
|
+
onClick: (info) => {
|
|
273
|
+
const selectedTemplate = allTemplates.find((t) => t.name === info.key);
|
|
274
|
+
const nextSchema = getSchema(selectedTemplate, record?.key || record?.name || null);
|
|
275
|
+
if (nextSchema) {
|
|
276
|
+
setSchema(nextSchema);
|
|
277
|
+
setVisible(true);
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
items,
|
|
281
|
+
}}
|
|
282
|
+
>
|
|
283
|
+
{children || (
|
|
284
|
+
<Button icon={<PlusOutlined />} type={props.type}>
|
|
285
|
+
{compile(nsT('Create collection'))} <DownOutlined />
|
|
286
|
+
</Button>
|
|
287
|
+
)}
|
|
288
|
+
</Dropdown>
|
|
289
|
+
<SchemaComponent
|
|
290
|
+
schema={schema}
|
|
291
|
+
components={{ ArrayItems, Space }}
|
|
292
|
+
scope={{
|
|
293
|
+
getContainer,
|
|
294
|
+
useCancelAction,
|
|
295
|
+
useCreatePostgresCollection,
|
|
296
|
+
...scope,
|
|
297
|
+
}}
|
|
298
|
+
/>
|
|
299
|
+
</ActionContextProvider>
|
|
300
|
+
</RecordProvider>
|
|
301
|
+
);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
export const PostgresAddCollection: React.FC<PostgresAddCollectionProps> = (props) => {
|
|
305
|
+
const record = useRecord() as CollectionRecordLike | undefined;
|
|
306
|
+
return <PostgresAddCollectionAction item={record} {...props} />;
|
|
307
|
+
};
|
|
@@ -0,0 +1,203 @@
|
|
|
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 { FormTab } from '@formily/antd-v5';
|
|
11
|
+
import { useForm } from '@formily/react';
|
|
12
|
+
import { SchemaComponent } from '@nocobase/client';
|
|
13
|
+
import React, { useEffect } from 'react';
|
|
14
|
+
import { DEFAULT_PORT, DEFAULT_SCHEMA, NAMESPACE } from '../constants';
|
|
15
|
+
|
|
16
|
+
const nsT = (key: string) => `{{t(${JSON.stringify(key)}, { ns: "${NAMESPACE}" })}}`;
|
|
17
|
+
|
|
18
|
+
interface ConfigurationFormProps {
|
|
19
|
+
from?: 'create' | 'edit';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const ConfigurationForm: React.FC<ConfigurationFormProps> = ({ from }) => {
|
|
23
|
+
const form = useForm();
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const options = (form?.values?.options || {}) as Record<string, unknown>;
|
|
27
|
+
const loadAll =
|
|
28
|
+
typeof options.addAllCollections === 'boolean' ? options.addAllCollections : options.autoDiscover !== false;
|
|
29
|
+
if (options.addAllCollections !== loadAll) {
|
|
30
|
+
form.setValuesIn('options.addAllCollections', loadAll);
|
|
31
|
+
}
|
|
32
|
+
if (options.autoDiscover !== loadAll) {
|
|
33
|
+
form.setValuesIn('options.autoDiscover', loadAll);
|
|
34
|
+
}
|
|
35
|
+
if (from !== 'create') {
|
|
36
|
+
if (Array.isArray(options.selectedCollections) && !Array.isArray(form.values.collections)) {
|
|
37
|
+
form.setValuesIn(
|
|
38
|
+
'collections',
|
|
39
|
+
options.selectedCollections
|
|
40
|
+
.filter((name): name is string => typeof name === 'string' && name.length > 0)
|
|
41
|
+
.map((name) => ({ name, selected: true })),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
if (from === 'edit' || !loadAll) {
|
|
45
|
+
form.setValuesIn('__postgresPreserveSelection', true);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, [form, from]);
|
|
49
|
+
|
|
50
|
+
const schema = {
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties: {
|
|
53
|
+
displayName: {
|
|
54
|
+
type: 'string',
|
|
55
|
+
title: nsT('Display Name'),
|
|
56
|
+
'x-decorator': 'FormItem',
|
|
57
|
+
'x-component': 'Input',
|
|
58
|
+
'x-decorator-props': {
|
|
59
|
+
tooltip: nsT('The connection name to display in NocoBase'),
|
|
60
|
+
},
|
|
61
|
+
required: true,
|
|
62
|
+
},
|
|
63
|
+
options: {
|
|
64
|
+
type: 'object',
|
|
65
|
+
properties: {
|
|
66
|
+
tabs: {
|
|
67
|
+
type: 'void',
|
|
68
|
+
'x-component': 'FormTab',
|
|
69
|
+
'x-component-props': {},
|
|
70
|
+
properties: {
|
|
71
|
+
connection: {
|
|
72
|
+
type: 'void',
|
|
73
|
+
'x-component': 'FormTab.TabPane',
|
|
74
|
+
'x-component-props': {
|
|
75
|
+
tab: nsT('Connection Settings'),
|
|
76
|
+
},
|
|
77
|
+
properties: {
|
|
78
|
+
host: {
|
|
79
|
+
type: 'string',
|
|
80
|
+
title: nsT('Host'),
|
|
81
|
+
'x-decorator': 'FormItem',
|
|
82
|
+
'x-component': 'Input',
|
|
83
|
+
'x-decorator-props': {
|
|
84
|
+
tooltip: nsT('PostgreSQL server hostname or IP address'),
|
|
85
|
+
},
|
|
86
|
+
default: 'localhost',
|
|
87
|
+
required: true,
|
|
88
|
+
},
|
|
89
|
+
port: {
|
|
90
|
+
type: 'number',
|
|
91
|
+
title: nsT('Port'),
|
|
92
|
+
'x-decorator': 'FormItem',
|
|
93
|
+
'x-component': 'InputNumber',
|
|
94
|
+
'x-decorator-props': {
|
|
95
|
+
tooltip: nsT('PostgreSQL server port number'),
|
|
96
|
+
},
|
|
97
|
+
'x-component-props': {
|
|
98
|
+
min: 1,
|
|
99
|
+
max: 65535,
|
|
100
|
+
style: { width: '100%' },
|
|
101
|
+
},
|
|
102
|
+
default: DEFAULT_PORT,
|
|
103
|
+
required: true,
|
|
104
|
+
},
|
|
105
|
+
database: {
|
|
106
|
+
type: 'string',
|
|
107
|
+
title: nsT('Database'),
|
|
108
|
+
'x-decorator': 'FormItem',
|
|
109
|
+
'x-component': 'Input',
|
|
110
|
+
'x-decorator-props': {
|
|
111
|
+
tooltip: nsT('Name of the PostgreSQL database'),
|
|
112
|
+
},
|
|
113
|
+
required: true,
|
|
114
|
+
},
|
|
115
|
+
username: {
|
|
116
|
+
type: 'string',
|
|
117
|
+
title: nsT('Username'),
|
|
118
|
+
'x-decorator': 'FormItem',
|
|
119
|
+
'x-component': 'Input',
|
|
120
|
+
'x-decorator-props': {
|
|
121
|
+
tooltip: nsT('Database username for authentication'),
|
|
122
|
+
},
|
|
123
|
+
required: true,
|
|
124
|
+
},
|
|
125
|
+
password: {
|
|
126
|
+
type: 'string',
|
|
127
|
+
title: nsT('Password'),
|
|
128
|
+
'x-decorator': 'FormItem',
|
|
129
|
+
'x-component': 'Password',
|
|
130
|
+
'x-decorator-props': {
|
|
131
|
+
tooltip: nsT('Database password for authentication'),
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
advanced: {
|
|
137
|
+
type: 'void',
|
|
138
|
+
'x-component': 'FormTab.TabPane',
|
|
139
|
+
'x-component-props': {
|
|
140
|
+
tab: nsT('Advanced Settings'),
|
|
141
|
+
},
|
|
142
|
+
properties: {
|
|
143
|
+
schema: {
|
|
144
|
+
type: 'string',
|
|
145
|
+
title: nsT('Schema'),
|
|
146
|
+
'x-decorator': 'FormItem',
|
|
147
|
+
'x-component': 'Input',
|
|
148
|
+
'x-decorator-props': {
|
|
149
|
+
tooltip: nsT('PostgreSQL schema name, defaults to public'),
|
|
150
|
+
},
|
|
151
|
+
default: DEFAULT_SCHEMA,
|
|
152
|
+
},
|
|
153
|
+
ssl: {
|
|
154
|
+
type: 'boolean',
|
|
155
|
+
title: nsT('Use SSL'),
|
|
156
|
+
'x-decorator': 'FormItem',
|
|
157
|
+
'x-component': 'Checkbox',
|
|
158
|
+
'x-decorator-props': {
|
|
159
|
+
tooltip: nsT('Enable SSL/TLS connection'),
|
|
160
|
+
},
|
|
161
|
+
default: false,
|
|
162
|
+
},
|
|
163
|
+
sslCa: {
|
|
164
|
+
type: 'string',
|
|
165
|
+
title: nsT('CA certificate (PEM)'),
|
|
166
|
+
'x-decorator': 'FormItem',
|
|
167
|
+
'x-component': 'Input.TextArea',
|
|
168
|
+
'x-component-props': { rows: 4 },
|
|
169
|
+
},
|
|
170
|
+
addAllCollections: {
|
|
171
|
+
type: 'boolean',
|
|
172
|
+
title: nsT('Auto Discover Tables'),
|
|
173
|
+
'x-decorator': 'FormItem',
|
|
174
|
+
'x-component': 'Checkbox',
|
|
175
|
+
'x-decorator-props': {
|
|
176
|
+
tooltip: nsT('Automatically discover tables from the database'),
|
|
177
|
+
},
|
|
178
|
+
'x-reactions': {
|
|
179
|
+
target: 'options.autoDiscover',
|
|
180
|
+
fulfill: {
|
|
181
|
+
state: {
|
|
182
|
+
value: '{{$self.value}}',
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
default: true,
|
|
187
|
+
},
|
|
188
|
+
autoDiscover: {
|
|
189
|
+
type: 'boolean',
|
|
190
|
+
'x-hidden': true,
|
|
191
|
+
default: true,
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
return <SchemaComponent schema={schema} components={{ FormTab }} />;
|
|
203
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
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 type { ISchema } from '@formily/react';
|
|
11
|
+
import { CollectionTemplate } from '@nocobase/client';
|
|
12
|
+
import { nsT } from './collection-templates/base-template-helper';
|
|
13
|
+
import { POSTGRES_AVAILABLE_INTERFACES } from './collection-templates/field-interfaces-config';
|
|
14
|
+
|
|
15
|
+
type TemplateManagerArg = ConstructorParameters<typeof CollectionTemplate>[0];
|
|
16
|
+
type TemplateDefault = NonNullable<CollectionTemplate['default']>;
|
|
17
|
+
|
|
18
|
+
export class PostgresGeneralCollectionTemplate extends CollectionTemplate {
|
|
19
|
+
name = 'general';
|
|
20
|
+
title = nsT('General collection');
|
|
21
|
+
order = 1;
|
|
22
|
+
color = 'blue';
|
|
23
|
+
supportDataSourceType = ['postgres-external'];
|
|
24
|
+
|
|
25
|
+
constructor(collectionTemplateManager: TemplateManagerArg) {
|
|
26
|
+
super(collectionTemplateManager);
|
|
27
|
+
|
|
28
|
+
this.availableFieldInterfaces = POSTGRES_AVAILABLE_INTERFACES;
|
|
29
|
+
this.configurableProperties = this._getConfigurableProperties();
|
|
30
|
+
this.default = this._getDefault();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
_getConfigurableProperties(): Record<string, ISchema> {
|
|
34
|
+
return {
|
|
35
|
+
title: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
title: nsT('Collection display name'),
|
|
38
|
+
required: true,
|
|
39
|
+
'x-decorator': 'FormItem',
|
|
40
|
+
'x-component': 'Input',
|
|
41
|
+
},
|
|
42
|
+
name: {
|
|
43
|
+
type: 'string',
|
|
44
|
+
title: nsT('Collection name'),
|
|
45
|
+
required: true,
|
|
46
|
+
'x-decorator': 'FormItem',
|
|
47
|
+
'x-component': 'Input',
|
|
48
|
+
'x-disabled': false,
|
|
49
|
+
'x-read-only': false,
|
|
50
|
+
'x-component-props': {
|
|
51
|
+
disabled: false,
|
|
52
|
+
placeholder: nsT('Please input collection name'),
|
|
53
|
+
},
|
|
54
|
+
'x-validator': [
|
|
55
|
+
{
|
|
56
|
+
pattern: /^[a-zA-Z][a-zA-Z0-9_]*$/,
|
|
57
|
+
message: nsT('Must start with a letter and can only contain letters, numbers, and underscores'),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
description: nsT(
|
|
61
|
+
'Use letters, numbers and underscores, must start with a letter. Example: users, products, orders',
|
|
62
|
+
),
|
|
63
|
+
},
|
|
64
|
+
description: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
title: nsT('Description'),
|
|
67
|
+
'x-decorator': 'FormItem',
|
|
68
|
+
'x-component': 'Input.TextArea',
|
|
69
|
+
},
|
|
70
|
+
fields: {
|
|
71
|
+
type: 'array',
|
|
72
|
+
title: nsT('Preset fields'),
|
|
73
|
+
description: nsT('Select default fields to include in this collection'),
|
|
74
|
+
'x-decorator': 'FormItem',
|
|
75
|
+
'x-component': 'Checkbox.Group',
|
|
76
|
+
'x-component-props': {
|
|
77
|
+
options: [
|
|
78
|
+
{
|
|
79
|
+
label: nsT('ID - Primary key (auto-increment)'),
|
|
80
|
+
value: 'id',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
label: nsT('Created At - Timestamp when record is created'),
|
|
84
|
+
value: 'createdAt',
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
label: nsT('Updated At - Timestamp when record is updated'),
|
|
88
|
+
value: 'updatedAt',
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
default: ['id', 'createdAt', 'updatedAt'],
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_getDefault(): TemplateDefault {
|
|
98
|
+
return {
|
|
99
|
+
fields: ['id', 'createdAt', 'updatedAt'] as unknown as TemplateDefault['fields'],
|
|
100
|
+
autoGenId: true,
|
|
101
|
+
createdBy: false,
|
|
102
|
+
updatedBy: false,
|
|
103
|
+
createdAt: true,
|
|
104
|
+
updatedAt: true,
|
|
105
|
+
sortable: false,
|
|
106
|
+
logging: false,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|