@nocobase/plugin-flow-engine 2.1.0-alpha.7 → 2.1.0-alpha.9
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/dist/ai/docs/runjs/context/block-model.md +35 -35
- package/dist/ai/docs/runjs/context/collection-field.md +53 -51
- package/dist/ai/docs/runjs/context/collection.md +39 -39
- package/dist/ai/docs/runjs/context/data-source-manager.md +40 -30
- package/dist/ai/docs/runjs/context/data-source.md +52 -44
- package/dist/ai/docs/runjs/context/element.md +44 -38
- package/dist/ai/docs/runjs/context/exit-all.md +37 -35
- package/dist/ai/docs/runjs/context/exit.md +38 -35
- package/dist/ai/docs/runjs/context/filter-manager.md +36 -30
- package/dist/ai/docs/runjs/context/form.md +57 -57
- package/dist/ai/docs/runjs/context/get-model.md +22 -21
- package/dist/ai/docs/runjs/context/get-value.md +20 -19
- package/dist/ai/docs/runjs/context/get-var.md +61 -55
- package/dist/ai/docs/runjs/context/i18n.md +17 -14
- package/dist/ai/docs/runjs/context/import-async.md +333 -45
- package/dist/ai/docs/runjs/context/init-resource.md +20 -20
- package/dist/ai/docs/runjs/context/libs.md +31 -31
- package/dist/ai/docs/runjs/context/location.md +34 -31
- package/dist/ai/docs/runjs/context/logger.md +41 -40
- package/dist/ai/docs/runjs/context/make-resource.md +27 -26
- package/dist/ai/docs/runjs/context/message.md +42 -41
- package/dist/ai/docs/runjs/context/modal.md +44 -44
- package/dist/ai/docs/runjs/context/model.md +36 -33
- package/dist/ai/docs/runjs/context/notification.md +41 -40
- package/dist/ai/docs/runjs/context/off.md +14 -14
- package/dist/ai/docs/runjs/context/on.md +30 -29
- package/dist/ai/docs/runjs/context/open-view.md +40 -40
- package/dist/ai/docs/runjs/context/render.md +37 -32
- package/dist/ai/docs/runjs/context/request.md +46 -45
- package/dist/ai/docs/runjs/context/require-async.md +28 -25
- package/dist/ai/docs/runjs/context/resource.md +34 -34
- package/dist/ai/docs/runjs/context/route.md +36 -34
- package/dist/ai/docs/runjs/context/router.md +43 -31
- package/dist/ai/docs/runjs/context/set-value.md +18 -17
- package/dist/ai/docs/runjs/context/sql.md +7 -15
- package/dist/ai/docs/runjs/context/t.md +20 -17
- package/dist/ai/docs/runjs/context/view.md +49 -46
- package/dist/ai/docs/runjs/document.md +1 -0
- package/dist/ai/docs/runjs/import-modules.md +32 -32
- package/dist/ai/docs/runjs/index.md +13 -13
- package/dist/ai/docs/runjs/jsx.md +19 -19
- package/dist/ai/docs/runjs/model/form-block-model.md +1 -3
- package/dist/ai/docs/runjs/render.md +15 -15
- package/dist/ai/docs/runjs/resource/api-resource.md +53 -53
- package/dist/ai/docs/runjs/resource/multi-record-resource.md +64 -64
- package/dist/ai/docs/runjs/resource/single-record-resource.md +55 -55
- package/dist/ai/docs/runjs/resource/sql-resource.md +57 -57
- package/dist/ai/docs/runjs/window.md +5 -5
- package/dist/externalVersion.js +10 -10
- package/dist/node_modules/ses/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/collections/flowsql.js +1 -0
- package/package.json +2 -2
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
# ctx.blockModel
|
|
2
2
|
|
|
3
|
-
The parent block model (BlockModel instance)
|
|
3
|
+
The parent block model (BlockModel instance) where the current JS Field / JS Block is located. In scenarios such as JSField, JSItem, and JSColumn, `ctx.blockModel` points to the form block or table block carrying the current JS logic. In a standalone JSBlock, it may be `null` or the same as `ctx.model`.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Scenarios
|
|
6
6
|
|
|
7
7
|
| Scenario | Description |
|
|
8
|
-
|
|
9
|
-
| **JSField** | Access
|
|
10
|
-
| **JSItem** | Access parent table/form block
|
|
11
|
-
| **JSColumn** | Access
|
|
12
|
-
| **Form
|
|
8
|
+
|------|------|
|
|
9
|
+
| **JSField** | Access the `form`, `collection`, and `resource` of the parent form block within a form field to implement linkage or validation. |
|
|
10
|
+
| **JSItem** | Access the resource and collection information of the parent table/form block within a sub-table item. |
|
|
11
|
+
| **JSColumn** | Access the `resource` (e.g., `getSelectedRows`) and `collection` of the parent table block within a table column. |
|
|
12
|
+
| **Form Actions / FlowEngine** | Access `form` for pre-submission validation, `resource` for refreshing, etc. |
|
|
13
13
|
|
|
14
|
-
> Note: `ctx.blockModel` is only available in RunJS contexts
|
|
14
|
+
> Note: `ctx.blockModel` is only available in RunJS contexts where a parent block exists. In standalone JSBlocks (without a parent form/table), it may be `null`. It is recommended to perform a null check before use.
|
|
15
15
|
|
|
16
|
-
## Type
|
|
16
|
+
## Type Definition
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
19
|
blockModel: BlockModel | FormBlockModel | TableBlockModel | CollectionBlockModel | DataBlockModel | null;
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
The
|
|
22
|
+
The specific type depends on the parent block type: form blocks are mostly `FormBlockModel` or `EditFormModel`, while table blocks are mostly `TableBlockModel`.
|
|
23
23
|
|
|
24
24
|
## Common Properties
|
|
25
25
|
|
|
26
26
|
| Property | Type | Description |
|
|
27
|
-
|
|
28
|
-
| `uid` | `string` |
|
|
29
|
-
| `collection` | `Collection` |
|
|
30
|
-
| `resource` | `Resource` |
|
|
31
|
-
| `form` | `FormInstance` | Form
|
|
32
|
-
| `emitter` | `EventEmitter` | Event emitter
|
|
27
|
+
|------|------|------|
|
|
28
|
+
| `uid` | `string` | Unique identifier of the block model. |
|
|
29
|
+
| `collection` | `Collection` | The collection bound to the current block. |
|
|
30
|
+
| `resource` | `Resource` | The resource instance used by the block (`SingleRecordResource` / `MultiRecordResource`, etc.). |
|
|
31
|
+
| `form` | `FormInstance` | Form Block: Ant Design Form instance, supporting `getFieldsValue`, `validateFields`, `setFieldsValue`, etc. |
|
|
32
|
+
| `emitter` | `EventEmitter` | Event emitter, used to listen for `formValuesChange`, `onFieldReset`, etc. |
|
|
33
33
|
|
|
34
|
-
##
|
|
34
|
+
## Relationship with ctx.model and ctx.form
|
|
35
35
|
|
|
36
|
-
|
|
|
37
|
-
|
|
38
|
-
| **Parent block of current JS** | `ctx.blockModel` |
|
|
39
|
-
| **Read/
|
|
40
|
-
| **Model
|
|
36
|
+
| Requirement | Recommended Usage |
|
|
37
|
+
|------|----------|
|
|
38
|
+
| **Parent block of the current JS** | `ctx.blockModel` |
|
|
39
|
+
| **Read/Write form fields** | `ctx.form` (equivalent to `ctx.blockModel?.form`, more convenient in form blocks) |
|
|
40
|
+
| **Model of the current execution context** | `ctx.model` (Field model in JSField, Block model in JSBlock) |
|
|
41
41
|
|
|
42
|
-
In JSField, `ctx.model` is the field model and `ctx.blockModel` is the form
|
|
42
|
+
In a JSField, `ctx.model` is the field model, and `ctx.blockModel` is the form or table block carrying that field; `ctx.form` is typically `ctx.blockModel.form`.
|
|
43
43
|
|
|
44
44
|
## Examples
|
|
45
45
|
|
|
46
|
-
### Table:
|
|
46
|
+
### Table: Get selected rows and process
|
|
47
47
|
|
|
48
48
|
```ts
|
|
49
49
|
const rows = ctx.blockModel?.resource?.getSelectedRows?.() || [];
|
|
@@ -53,7 +53,7 @@ if (rows.length === 0) {
|
|
|
53
53
|
}
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
### Form:
|
|
56
|
+
### Form Scenario: Validate and Refresh
|
|
57
57
|
|
|
58
58
|
```ts
|
|
59
59
|
if (ctx.blockModel?.form) {
|
|
@@ -62,15 +62,15 @@ if (ctx.blockModel?.form) {
|
|
|
62
62
|
}
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
### Listen
|
|
65
|
+
### Listen for Form Changes
|
|
66
66
|
|
|
67
67
|
```ts
|
|
68
68
|
ctx.blockModel?.emitter?.on?.('formValuesChange', (payload) => {
|
|
69
|
-
//
|
|
69
|
+
// Implement linkage or re-rendering based on the latest form values
|
|
70
70
|
});
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
### Trigger
|
|
73
|
+
### Trigger Block Re-render
|
|
74
74
|
|
|
75
75
|
```ts
|
|
76
76
|
ctx.blockModel?.rerender?.();
|
|
@@ -78,13 +78,13 @@ ctx.blockModel?.rerender?.();
|
|
|
78
78
|
|
|
79
79
|
## Notes
|
|
80
80
|
|
|
81
|
-
- In a **standalone JSBlock** (
|
|
82
|
-
- In **JSField / JSItem / JSColumn**, `ctx.blockModel`
|
|
83
|
-
- `resource` exists
|
|
81
|
+
- In a **standalone JSBlock** (without a parent form or table block), `ctx.blockModel` may be `null`. It is recommended to use optional chaining when accessing its properties: `ctx.blockModel?.resource?.refresh?.()`.
|
|
82
|
+
- In **JSField / JSItem / JSColumn**, `ctx.blockModel` refers to the form or table block carrying the current field. In a **JSBlock**, it may be itself or an upper-level block, depending on the actual hierarchy.
|
|
83
|
+
- `resource` only exists in data blocks; `form` only exists in form blocks. Table blocks typically do not have a `form`.
|
|
84
84
|
|
|
85
85
|
## Related
|
|
86
86
|
|
|
87
|
-
- [ctx.model](./model.md): model
|
|
88
|
-
- [ctx.form](./form.md):
|
|
89
|
-
- [ctx.resource](./resource.md):
|
|
90
|
-
- [ctx.getModel()](./get-model.md):
|
|
87
|
+
- [ctx.model](./model.md): The model of the current execution context.
|
|
88
|
+
- [ctx.form](./form.md): Form instance, commonly used in form blocks.
|
|
89
|
+
- [ctx.resource](./resource.md): Resource instance (equivalent to `ctx.blockModel?.resource`, use directly if available).
|
|
90
|
+
- [ctx.getModel()](./get-model.md): Get other block models by UID.
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# ctx.collectionField
|
|
2
2
|
|
|
3
|
-
The
|
|
3
|
+
The `CollectionField` instance associated with the current RunJS execution context, used to access field metadata, types, validation rules, and association information. It only exists when the field is bound to a collection definition; custom/virtual fields may be `null`.
|
|
4
4
|
|
|
5
5
|
## Use Cases
|
|
6
6
|
|
|
7
7
|
| Scenario | Description |
|
|
8
|
-
|
|
9
|
-
| **JSField** |
|
|
10
|
-
| **JSItem** | Access metadata of the column
|
|
11
|
-
| **JSColumn** |
|
|
8
|
+
|------|------|
|
|
9
|
+
| **JSField** | Perform linkage or validation in form fields based on `interface`, `enum`, `targetCollection`, etc. |
|
|
10
|
+
| **JSItem** | Access metadata of the field corresponding to the current column in sub-table items. |
|
|
11
|
+
| **JSColumn** | Select rendering methods based on `collectionField.interface` or access `targetCollection` in table columns. |
|
|
12
12
|
|
|
13
|
-
> Note: `ctx.collectionField` is only available when the field is bound to a
|
|
13
|
+
> Note: `ctx.collectionField` is only available when the field is bound to a Collection definition; it is usually `undefined` in scenarios like JSBlock independent blocks or action events without field binding. It is recommended to check for null values before use.
|
|
14
14
|
|
|
15
|
-
## Type
|
|
15
|
+
## Type Definition
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
collectionField: CollectionField | null | undefined;
|
|
@@ -21,46 +21,47 @@ collectionField: CollectionField | null | undefined;
|
|
|
21
21
|
## Common Properties
|
|
22
22
|
|
|
23
23
|
| Property | Type | Description |
|
|
24
|
-
|
|
25
|
-
| `name` | `string` | Field name (e.g
|
|
26
|
-
| `title` | `string` | Field title (
|
|
27
|
-
| `type` | `string` |
|
|
28
|
-
| `interface` | `string` |
|
|
29
|
-
| `collection` | `Collection` |
|
|
30
|
-
| `targetCollection` | `Collection` |
|
|
31
|
-
| `target` | `string` | Target collection name (association) |
|
|
32
|
-
| `enum` | `array` |
|
|
24
|
+
|------|------|------|
|
|
25
|
+
| `name` | `string` | Field name (e.g., `status`, `userId`) |
|
|
26
|
+
| `title` | `string` | Field title (including internationalization) |
|
|
27
|
+
| `type` | `string` | Field data type (`string`, `integer`, `belongsTo`, etc.) |
|
|
28
|
+
| `interface` | `string` | Field interface type (`input`, `select`, `m2o`, `o2m`, `m2m`, etc.) |
|
|
29
|
+
| `collection` | `Collection` | The collection the field belongs to |
|
|
30
|
+
| `targetCollection` | `Collection` | The target collection of the association field (only for association types) |
|
|
31
|
+
| `target` | `string` | Target collection name (for association fields) |
|
|
32
|
+
| `enum` | `array` | Enumeration options (select, radio, etc.) |
|
|
33
33
|
| `defaultValue` | `any` | Default value |
|
|
34
|
-
| `collectionName` | `string` |
|
|
35
|
-
| `foreignKey` | `string` | Foreign key (
|
|
36
|
-
| `sourceKey` | `string` |
|
|
37
|
-
| `targetKey` | `string` |
|
|
38
|
-
| `fullpath` | `string` | Full path (e.g
|
|
39
|
-
| `resourceName` | `string` | Resource name (e.g
|
|
40
|
-
| `readonly` | `boolean` |
|
|
41
|
-
| `titleable` | `boolean` |
|
|
42
|
-
| `validation` | `object` | Validation
|
|
43
|
-
| `uiSchema` | `object` | UI
|
|
44
|
-
| `targetCollectionTitleField` | `CollectionField` |
|
|
34
|
+
| `collectionName` | `string` | Name of the collection it belongs to |
|
|
35
|
+
| `foreignKey` | `string` | Foreign key field name (belongsTo, etc.) |
|
|
36
|
+
| `sourceKey` | `string` | Association source key (hasMany, etc.) |
|
|
37
|
+
| `targetKey` | `string` | Association target key |
|
|
38
|
+
| `fullpath` | `string` | Full path (e.g., `main.users.status`), used for API or variable references |
|
|
39
|
+
| `resourceName` | `string` | Resource name (e.g., `users.status`) |
|
|
40
|
+
| `readonly` | `boolean` | Whether it is read-only |
|
|
41
|
+
| `titleable` | `boolean` | Whether it can be displayed as a title |
|
|
42
|
+
| `validation` | `object` | Validation rule configuration |
|
|
43
|
+
| `uiSchema` | `object` | UI configuration |
|
|
44
|
+
| `targetCollectionTitleField` | `CollectionField` | The title field of the target collection (for association fields) |
|
|
45
45
|
|
|
46
46
|
## Common Methods
|
|
47
47
|
|
|
48
48
|
| Method | Description |
|
|
49
|
-
|
|
50
|
-
| `isAssociationField(): boolean` | Whether it is an association (belongsTo, hasMany, hasOne, belongsToMany, etc.) |
|
|
51
|
-
| `isRelationshipField(): boolean` | Whether it is a relationship (o2o, m2o, o2m, m2m, etc.) |
|
|
52
|
-
| `getComponentProps(): object` |
|
|
53
|
-
| `getFields(): CollectionField[]` |
|
|
54
|
-
| `getFilterOperators(): object[]` |
|
|
49
|
+
|------|------|
|
|
50
|
+
| `isAssociationField(): boolean` | Whether it is an association field (belongsTo, hasMany, hasOne, belongsToMany, etc.) |
|
|
51
|
+
| `isRelationshipField(): boolean` | Whether it is a relationship field (including o2o, m2o, o2m, m2m, etc.) |
|
|
52
|
+
| `getComponentProps(): object` | Get the default props of the field component |
|
|
53
|
+
| `getFields(): CollectionField[]` | Get the field list of the target collection (association fields only) |
|
|
54
|
+
| `getFilterOperators(): object[]` | Get the filter operators supported by this field (e.g., `$eq`, `$ne`, etc.) |
|
|
55
55
|
|
|
56
56
|
## Examples
|
|
57
57
|
|
|
58
|
-
### Branch
|
|
58
|
+
### Branch rendering based on field type
|
|
59
59
|
|
|
60
60
|
```ts
|
|
61
61
|
if (!ctx.collectionField) return null;
|
|
62
62
|
const { interface: iface } = ctx.collectionField;
|
|
63
63
|
if (['m2o', 'o2m', 'm2m'].includes(iface)) {
|
|
64
|
+
// Association field: display associated records
|
|
64
65
|
const target = ctx.collectionField.targetCollection;
|
|
65
66
|
// ...
|
|
66
67
|
} else if (iface === 'select' || iface === 'radioGroup') {
|
|
@@ -69,24 +70,24 @@ if (['m2o', 'o2m', 'm2m'].includes(iface)) {
|
|
|
69
70
|
}
|
|
70
71
|
```
|
|
71
72
|
|
|
72
|
-
###
|
|
73
|
+
### Determine if it is an association field and access the target collection
|
|
73
74
|
|
|
74
75
|
```ts
|
|
75
76
|
if (ctx.collectionField?.isAssociationField()) {
|
|
76
77
|
const targetCol = ctx.collectionField.targetCollection;
|
|
77
78
|
const titleField = targetCol?.titleCollectionField?.name;
|
|
78
|
-
//
|
|
79
|
+
// Process according to the target collection structure
|
|
79
80
|
}
|
|
80
81
|
```
|
|
81
82
|
|
|
82
|
-
### Get
|
|
83
|
+
### Get enumeration options
|
|
83
84
|
|
|
84
85
|
```ts
|
|
85
86
|
const options = ctx.collectionField?.enum ?? [];
|
|
86
87
|
const labels = options.map((o) => (typeof o === 'object' ? o.label : o));
|
|
87
88
|
```
|
|
88
89
|
|
|
89
|
-
### Conditional
|
|
90
|
+
### Conditional rendering based on read-only/view mode
|
|
90
91
|
|
|
91
92
|
```ts
|
|
92
93
|
const { Input } = ctx.libs.antd;
|
|
@@ -97,34 +98,35 @@ if (ctx.collectionField?.readonly) {
|
|
|
97
98
|
}
|
|
98
99
|
```
|
|
99
100
|
|
|
100
|
-
###
|
|
101
|
+
### Get the title field of the target collection
|
|
101
102
|
|
|
102
103
|
```ts
|
|
104
|
+
// When displaying an association field, use targetCollectionTitleField to get the title field name
|
|
103
105
|
const titleField = ctx.collectionField?.targetCollectionTitleField;
|
|
104
106
|
const titleKey = titleField?.name ?? 'title';
|
|
105
107
|
const assocValue = ctx.getValue?.() ?? ctx.record?.[ctx.collectionField?.name];
|
|
106
108
|
const label = assocValue?.[titleKey];
|
|
107
109
|
```
|
|
108
110
|
|
|
109
|
-
##
|
|
111
|
+
## Relationship with ctx.collection
|
|
110
112
|
|
|
111
|
-
|
|
|
112
|
-
|
|
113
|
-
| **
|
|
113
|
+
| Requirement | Recommended Usage |
|
|
114
|
+
|------|----------|
|
|
115
|
+
| **Current field's collection** | `ctx.collectionField?.collection` or `ctx.collection` |
|
|
114
116
|
| **Field metadata (name, type, interface, enum, etc.)** | `ctx.collectionField` |
|
|
115
117
|
| **Target collection** | `ctx.collectionField?.targetCollection` |
|
|
116
118
|
|
|
117
|
-
`ctx.collection`
|
|
119
|
+
`ctx.collection` usually represents the collection bound to the current block; `ctx.collectionField` represents the definition of the current field in the collection. In scenarios like sub-tables or association fields, the two may differ.
|
|
118
120
|
|
|
119
121
|
## Notes
|
|
120
122
|
|
|
121
|
-
- In **JSBlock
|
|
122
|
-
-
|
|
123
|
-
- `targetCollection` exists
|
|
123
|
+
- In scenarios such as **JSBlock** or **JSAction (without field binding)**, `ctx.collectionField` is usually `undefined`. It is recommended to use optional chaining before access.
|
|
124
|
+
- If a custom JS field is not bound to a collection field, `ctx.collectionField` may be `null`.
|
|
125
|
+
- `targetCollection` only exists for association type fields (e.g., m2o, o2m, m2m); `enum` only exists for fields with options like select or radioGroup.
|
|
124
126
|
|
|
125
127
|
## Related
|
|
126
128
|
|
|
127
|
-
- [ctx.collection](./collection.md):
|
|
128
|
-
- [ctx.model](./model.md):
|
|
129
|
-
- [ctx.blockModel](./block-model.md):
|
|
130
|
-
- [ctx.getValue()](./get-value.md), [ctx.setValue()](./set-value.md):
|
|
129
|
+
- [ctx.collection](./collection.md): Collection associated with the current context
|
|
130
|
+
- [ctx.model](./model.md): Model where the current execution context is located
|
|
131
|
+
- [ctx.blockModel](./block-model.md): Parent block carrying the current JS
|
|
132
|
+
- [ctx.getValue()](./get-value.md), [ctx.setValue()](./set-value.md): Read and write the current field value
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# ctx.collection
|
|
2
2
|
|
|
3
|
-
The
|
|
3
|
+
The Collection instance associated with the current RunJS execution context, used to access collection metadata, field definitions, primary keys, and other configurations. It usually originates from `ctx.blockModel.collection` or `ctx.collectionField?.collection`.
|
|
4
4
|
|
|
5
5
|
## Use Cases
|
|
6
6
|
|
|
7
7
|
| Scenario | Description |
|
|
8
|
-
|
|
9
|
-
| **JSBlock** |
|
|
10
|
-
| **JSField / JSItem / JSColumn** |
|
|
11
|
-
| **Table
|
|
8
|
+
|------|------|
|
|
9
|
+
| **JSBlock** | The collection bound to the block; can access `name`, `getFields`, `filterTargetKey`, etc. |
|
|
10
|
+
| **JSField / JSItem / JSColumn** | The collection the current field belongs to (or the parent block's collection), used to retrieve field lists, primary keys, etc. |
|
|
11
|
+
| **Table Column / Detail Block** | Used for rendering based on collection structure or passing `filterByTk` when opening popups. |
|
|
12
12
|
|
|
13
|
-
> Note: `ctx.collection` is available
|
|
13
|
+
> Note: `ctx.collection` is available in scenarios where a data block, form block, or table block is bound to a collection. In an independent JSBlock that is not bound to a collection, it may be `null`. It is recommended to perform a null check before use.
|
|
14
14
|
|
|
15
|
-
## Type
|
|
15
|
+
## Type Definition
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
collection: Collection | null | undefined;
|
|
@@ -21,39 +21,39 @@ collection: Collection | null | undefined;
|
|
|
21
21
|
## Common Properties
|
|
22
22
|
|
|
23
23
|
| Property | Type | Description |
|
|
24
|
-
|
|
25
|
-
| `name` | `string` | Collection name (e.g
|
|
26
|
-
| `title` | `string` | Collection title (
|
|
27
|
-
| `filterTargetKey` | `string \| string[]` | Primary key field name
|
|
28
|
-
| `dataSourceKey` | `string` | Data source key (e.g
|
|
29
|
-
| `dataSource` | `DataSource` |
|
|
30
|
-
| `template` | `string` | Collection template (e.g
|
|
31
|
-
| `titleableFields` | `CollectionField[]` |
|
|
32
|
-
| `titleCollectionField` | `CollectionField` |
|
|
24
|
+
|------|------|------|
|
|
25
|
+
| `name` | `string` | Collection name (e.g., `users`, `orders`) |
|
|
26
|
+
| `title` | `string` | Collection title (includes internationalization) |
|
|
27
|
+
| `filterTargetKey` | `string \| string[]` | Primary key field name, used for `filterByTk` and `getFilterByTK` |
|
|
28
|
+
| `dataSourceKey` | `string` | Data source key (e.g., `main`) |
|
|
29
|
+
| `dataSource` | `DataSource` | The data source instance it belongs to |
|
|
30
|
+
| `template` | `string` | Collection template (e.g., `general`, `file`, `tree`) |
|
|
31
|
+
| `titleableFields` | `CollectionField[]` | List of fields that can be displayed as titles |
|
|
32
|
+
| `titleCollectionField` | `CollectionField` | The title field instance |
|
|
33
33
|
|
|
34
34
|
## Common Methods
|
|
35
35
|
|
|
36
36
|
| Method | Description |
|
|
37
|
-
|
|
38
|
-
| `getFields(): CollectionField[]` |
|
|
39
|
-
| `getField(name: string): CollectionField \| undefined` |
|
|
40
|
-
| `getFieldByPath(path: string): CollectionField \| undefined` |
|
|
41
|
-
| `getAssociationFields(types?): CollectionField[]` |
|
|
42
|
-
| `getFilterByTK(record): any` |
|
|
37
|
+
|------|------|
|
|
38
|
+
| `getFields(): CollectionField[]` | Get all fields (including inherited ones) |
|
|
39
|
+
| `getField(name: string): CollectionField \| undefined` | Get a single field by field name |
|
|
40
|
+
| `getFieldByPath(path: string): CollectionField \| undefined` | Get a field by path (supports associations, e.g., `user.name`) |
|
|
41
|
+
| `getAssociationFields(types?): CollectionField[]` | Get association fields; `types` can be `['one']`, `['many']`, etc. |
|
|
42
|
+
| `getFilterByTK(record): any` | Extract the primary key value from a record, used for the API's `filterByTk` |
|
|
43
43
|
|
|
44
|
-
##
|
|
44
|
+
## Relationship with ctx.collectionField and ctx.blockModel
|
|
45
45
|
|
|
46
|
-
|
|
|
47
|
-
|
|
48
|
-
| **Collection
|
|
49
|
-
| **Collection of current field** | `ctx.collectionField?.collection` |
|
|
50
|
-
| **
|
|
46
|
+
| Requirement | Recommended Usage |
|
|
47
|
+
|------|----------|
|
|
48
|
+
| **Collection associated with current context** | `ctx.collection` (equivalent to `ctx.blockModel?.collection` or `ctx.collectionField?.collection`) |
|
|
49
|
+
| **Collection definition of the current field** | `ctx.collectionField?.collection` (the collection the field belongs to) |
|
|
50
|
+
| **Association target collection** | `ctx.collectionField?.targetCollection` (the target collection of an association field) |
|
|
51
51
|
|
|
52
|
-
In sub-tables
|
|
52
|
+
In scenarios like sub-tables, `ctx.collection` might be the association target collection; in standard forms/tables, it is usually the collection bound to the block.
|
|
53
53
|
|
|
54
54
|
## Examples
|
|
55
55
|
|
|
56
|
-
### Get
|
|
56
|
+
### Get Primary Key and Open Popup
|
|
57
57
|
|
|
58
58
|
```ts
|
|
59
59
|
const primaryKey = ctx.collection?.filterTargetKey ?? 'id';
|
|
@@ -66,7 +66,7 @@ await ctx.openView(popupUid, {
|
|
|
66
66
|
});
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
### Iterate
|
|
69
|
+
### Iterate Through Fields for Validation or Linkage
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
72
|
const fields = ctx.collection?.getFields() ?? [];
|
|
@@ -80,21 +80,21 @@ for (const f of requiredFields) {
|
|
|
80
80
|
}
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
### Get
|
|
83
|
+
### Get Association Fields
|
|
84
84
|
|
|
85
85
|
```ts
|
|
86
86
|
const oneToMany = ctx.collection?.getAssociationFields(['many']) ?? [];
|
|
87
|
-
//
|
|
87
|
+
// Used for building sub-tables, associated resources, etc.
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
## Notes
|
|
91
91
|
|
|
92
|
-
- `filterTargetKey` is the
|
|
93
|
-
- In **sub-tables
|
|
94
|
-
- `getFields()` merges inherited
|
|
92
|
+
- `filterTargetKey` is the primary key field name of the collection. Some collections may use a `string[]` for composite primary keys. If not configured, `'id'` is commonly used as a fallback.
|
|
93
|
+
- In scenarios like **sub-tables or association fields**, `ctx.collection` may point to the association target collection, which differs from `ctx.blockModel.collection`.
|
|
94
|
+
- `getFields()` merges fields from inherited collections; local fields override inherited fields with the same name.
|
|
95
95
|
|
|
96
96
|
## Related
|
|
97
97
|
|
|
98
|
-
- [ctx.collectionField](./collection-field.md):
|
|
99
|
-
- [ctx.blockModel](./block-model.md): parent block
|
|
100
|
-
- [ctx.model](./model.md): current model
|
|
98
|
+
- [ctx.collectionField](./collection-field.md): The collection field definition of the current field
|
|
99
|
+
- [ctx.blockModel](./block-model.md): The parent block hosting the current JS, containing `collection`
|
|
100
|
+
- [ctx.model](./model.md): The current model, which may contain `collection`
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# ctx.dataSourceManager
|
|
2
2
|
|
|
3
|
-
The
|
|
3
|
+
The Data Source Manager (`DataSourceManager` instance) is used to manage and access multiple data sources (e.g., the main database `main`, logging database `logging`, etc.). It is used when multiple data sources exist or when cross-data source metadata access is required.
|
|
4
4
|
|
|
5
5
|
## Use Cases
|
|
6
6
|
|
|
7
7
|
| Scenario | Description |
|
|
8
|
-
|
|
9
|
-
| **
|
|
10
|
-
| **Cross
|
|
11
|
-
| **
|
|
8
|
+
|------|------|
|
|
9
|
+
| **Multi-data source** | Enumerate all data sources, or get a specific data source by key. |
|
|
10
|
+
| **Cross-data source access** | Access metadata using the "data source key + collection name" format when the data source of the current context is unknown. |
|
|
11
|
+
| **Get fields by full path** | Use the `dataSourceKey.collectionName.fieldPath` format to retrieve field definitions across different data sources. |
|
|
12
12
|
|
|
13
|
-
> Note: If you only
|
|
13
|
+
> Note: If you are only operating on the current data source, prioritize using `ctx.dataSource`. Use `ctx.dataSourceManager` only when you need to enumerate or switch between data sources.
|
|
14
14
|
|
|
15
|
-
## Type
|
|
15
|
+
## Type Definition
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
dataSourceManager: DataSourceManager;
|
|
@@ -20,64 +20,74 @@ dataSourceManager: DataSourceManager;
|
|
|
20
20
|
class DataSourceManager {
|
|
21
21
|
constructor();
|
|
22
22
|
|
|
23
|
+
// Data source management
|
|
23
24
|
addDataSource(ds: DataSource | DataSourceOptions): void;
|
|
24
25
|
upsertDataSource(ds: DataSource | DataSourceOptions): void;
|
|
25
26
|
removeDataSource(key: string): void;
|
|
26
27
|
clearDataSources(): void;
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
// Read data sources
|
|
30
|
+
getDataSources(): DataSource[]; // Get all data sources
|
|
31
|
+
getDataSource(key: string): DataSource | undefined; // Get data source by key
|
|
30
32
|
|
|
33
|
+
// Access metadata directly by data source + collection
|
|
31
34
|
getCollection(dataSourceKey: string, collectionName: string): Collection | undefined;
|
|
32
35
|
getCollectionField(fieldPathWithDataSource: string): CollectionField | undefined;
|
|
33
36
|
}
|
|
34
37
|
```
|
|
35
38
|
|
|
36
|
-
##
|
|
39
|
+
## Relationship with ctx.dataSource
|
|
37
40
|
|
|
38
|
-
|
|
|
39
|
-
|
|
40
|
-
| **Single data source
|
|
41
|
-
| **Entry
|
|
41
|
+
| Requirement | Recommended Usage |
|
|
42
|
+
|------|----------|
|
|
43
|
+
| **Single data source bound to the current context** | `ctx.dataSource` (e.g., the data source of the current page/block) |
|
|
44
|
+
| **Entry point for all data sources** | `ctx.dataSourceManager` |
|
|
42
45
|
| **List or switch data sources** | `ctx.dataSourceManager.getDataSources()` / `getDataSource(key)` |
|
|
43
|
-
| **
|
|
44
|
-
| **
|
|
45
|
-
| **
|
|
46
|
-
| **
|
|
46
|
+
| **Get collection within the current data source** | `ctx.dataSource.getCollection(name)` |
|
|
47
|
+
| **Get collection across data sources** | `ctx.dataSourceManager.getCollection(dataSourceKey, collectionName)` |
|
|
48
|
+
| **Get field within the current data source** | `ctx.dataSource.getCollectionField('users.profile.avatar')` |
|
|
49
|
+
| **Get field across data sources** | `ctx.dataSourceManager.getCollectionField('main.users.profile.avatar')` |
|
|
47
50
|
|
|
48
51
|
## Examples
|
|
49
52
|
|
|
50
|
-
### Get a
|
|
53
|
+
### Get a Specific Data Source
|
|
51
54
|
|
|
52
55
|
```ts
|
|
56
|
+
// Get the data source named 'main'
|
|
53
57
|
const mainDS = ctx.dataSourceManager.getDataSource('main');
|
|
58
|
+
|
|
59
|
+
// Get all collections under this data source
|
|
54
60
|
const collections = mainDS?.getCollections();
|
|
55
61
|
```
|
|
56
62
|
|
|
57
|
-
###
|
|
63
|
+
### Access Collection Metadata Across Data Sources
|
|
58
64
|
|
|
59
65
|
```ts
|
|
66
|
+
// Get collection by dataSourceKey + collectionName
|
|
60
67
|
const users = ctx.dataSourceManager.getCollection('main', 'users');
|
|
61
68
|
const orders = ctx.dataSourceManager.getCollection('main', 'orders');
|
|
62
69
|
|
|
70
|
+
// Get the primary key of the collection
|
|
63
71
|
const primaryKey = users?.filterTargetKey ?? 'id';
|
|
64
72
|
```
|
|
65
73
|
|
|
66
|
-
### Field by
|
|
74
|
+
### Get Field Definition by Full Path
|
|
67
75
|
|
|
68
76
|
```ts
|
|
69
77
|
// Format: dataSourceKey.collectionName.fieldPath
|
|
78
|
+
// Get field definition by "data source key.collection name.field path"
|
|
70
79
|
const field = ctx.dataSourceManager.getCollectionField('main.users.profile.avatar');
|
|
71
80
|
|
|
81
|
+
// Supports association field paths
|
|
72
82
|
const userNameField = ctx.dataSourceManager.getCollectionField('main.orders.createdBy.name');
|
|
73
83
|
```
|
|
74
84
|
|
|
75
|
-
### Iterate
|
|
85
|
+
### Iterate Through All Data Sources
|
|
76
86
|
|
|
77
87
|
```ts
|
|
78
88
|
const dataSources = ctx.dataSourceManager.getDataSources();
|
|
79
89
|
for (const ds of dataSources) {
|
|
80
|
-
ctx.logger.info(`Data
|
|
90
|
+
ctx.logger.info(`Data Source: ${ds.key}, Display Name: ${ds.displayName}`);
|
|
81
91
|
const collections = ds.getCollections();
|
|
82
92
|
for (const col of collections) {
|
|
83
93
|
ctx.logger.info(` - Collection: ${col.name}`);
|
|
@@ -85,7 +95,7 @@ for (const ds of dataSources) {
|
|
|
85
95
|
}
|
|
86
96
|
```
|
|
87
97
|
|
|
88
|
-
###
|
|
98
|
+
### Dynamically Select Data Source Based on Variables
|
|
89
99
|
|
|
90
100
|
```ts
|
|
91
101
|
const dsKey = ctx.getVar('dataSourceKey') ?? 'main';
|
|
@@ -99,12 +109,12 @@ if (col) {
|
|
|
99
109
|
|
|
100
110
|
## Notes
|
|
101
111
|
|
|
102
|
-
-
|
|
103
|
-
- `getDataSource(key)` returns `undefined` if the data source
|
|
104
|
-
- `addDataSource`
|
|
112
|
+
- The path format for `getCollectionField` is `dataSourceKey.collectionName.fieldPath`, where the first segment is the data source key, followed by the collection name and the field path.
|
|
113
|
+
- `getDataSource(key)` returns `undefined` if the data source does not exist; it is recommended to perform a null check before use.
|
|
114
|
+
- `addDataSource` will throw an exception if the key already exists; `upsertDataSource` will either overwrite the existing one or add a new one.
|
|
105
115
|
|
|
106
116
|
## Related
|
|
107
117
|
|
|
108
|
-
- [ctx.dataSource](./data-source.md):
|
|
109
|
-
- [ctx.collection](./collection.md):
|
|
110
|
-
- [ctx.collectionField](./collection-field.md):
|
|
118
|
+
- [ctx.dataSource](./data-source.md): Current data source instance
|
|
119
|
+
- [ctx.collection](./collection.md): Collection associated with the current context
|
|
120
|
+
- [ctx.collectionField](./collection-field.md): Collection field definition for the current field
|