@nocobase/plugin-data-source-main 2.1.0-alpha.13 → 2.1.0-alpha.15

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.
@@ -0,0 +1,162 @@
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
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var service_exports = {};
28
+ __export(service_exports, {
29
+ applyCollectionDefinition: () => applyCollectionDefinition,
30
+ applyFieldDefinition: () => applyFieldDefinition,
31
+ findCollection: () => findCollection,
32
+ pickCollectionApplyValues: () => pickCollectionApplyValues,
33
+ upsertFieldDefinition: () => upsertFieldDefinition
34
+ });
35
+ module.exports = __toCommonJS(service_exports);
36
+ var import_capabilities = require("./capabilities");
37
+ var import_collections = require("./collections");
38
+ var import_constants = require("./constants");
39
+ var import_fields = require("./fields");
40
+ function mergeSettings(input = {}) {
41
+ const { settings, ...rest } = input;
42
+ return {
43
+ ...settings || {},
44
+ ...rest
45
+ };
46
+ }
47
+ async function findCollection(ctx, collectionName) {
48
+ return ctx.app.db.getRepository("collections").findOne({
49
+ filter: {
50
+ name: collectionName
51
+ },
52
+ appends: ["fields"]
53
+ });
54
+ }
55
+ function pickCollectionApplyValues(values) {
56
+ const normalized = { ...values };
57
+ delete normalized.fields;
58
+ delete normalized.name;
59
+ delete normalized.replaceFields;
60
+ delete normalized.verify;
61
+ delete normalized.settings;
62
+ return normalized;
63
+ }
64
+ async function upsertFieldDefinition(ctx, rawInput) {
65
+ const input = mergeSettings(rawInput);
66
+ const collectionName = input.collectionName;
67
+ if (!collectionName) {
68
+ throw new Error("field apply requires collectionName");
69
+ }
70
+ const values = (0, import_fields.normalizeFieldInput)(input, { collectionName });
71
+ await (0, import_capabilities.assertModelingSupport)(ctx.app, { fields: [values] });
72
+ const repository = ctx.app.db.getRepository("fields");
73
+ const existing = await repository.findOne({
74
+ filter: {
75
+ collectionName,
76
+ name: values.name
77
+ }
78
+ });
79
+ if (existing) {
80
+ await repository.update({
81
+ filterByTk: existing.key,
82
+ values,
83
+ context: ctx
84
+ });
85
+ } else {
86
+ await repository.create({
87
+ values,
88
+ context: ctx
89
+ });
90
+ }
91
+ return repository.findOne({
92
+ filter: {
93
+ collectionName,
94
+ name: values.name
95
+ }
96
+ });
97
+ }
98
+ async function applyFieldDefinition(ctx, rawInput, options = {}) {
99
+ const input = mergeSettings(rawInput);
100
+ if (options.relationOnly && !import_constants.RELATION_INTERFACES.has(input.interface)) {
101
+ throw new Error(`fields:apply relation mode requires relation interface, got ${input.interface || "empty"}`);
102
+ }
103
+ return upsertFieldDefinition(ctx, input);
104
+ }
105
+ async function applyCollectionDefinition(ctx, rawInput, syncCollectionFields) {
106
+ const input = mergeSettings(rawInput);
107
+ const collectionName = input.name || ctx.action.params.filterByTk;
108
+ const replaceFields = input.replaceFields === true;
109
+ const shouldVerify = input.verify !== false;
110
+ const repository = ctx.app.db.getRepository("collections");
111
+ if (!collectionName) {
112
+ throw new Error("collections:apply requires name");
113
+ }
114
+ const existing = await findCollection(ctx, collectionName);
115
+ if (!existing) {
116
+ const values = (0, import_collections.composeCollectionDefinition)({ ...input, name: collectionName }, { mode: "create" });
117
+ await (0, import_capabilities.assertModelingSupport)(ctx.app, values);
118
+ await repository.create({
119
+ values,
120
+ context: ctx
121
+ });
122
+ } else {
123
+ const values = (0, import_collections.normalizeCollectionInput)({ ...input, name: collectionName }, { mode: "update" });
124
+ await (0, import_capabilities.assertModelingSupport)(ctx.app, values);
125
+ const collectionValues = pickCollectionApplyValues(values);
126
+ if (Object.keys(collectionValues).length > 0) {
127
+ await repository.update({
128
+ filterByTk: collectionName,
129
+ values: collectionValues,
130
+ context: ctx
131
+ });
132
+ }
133
+ if (Array.isArray(values.fields) && values.fields.length > 0) {
134
+ if (replaceFields) {
135
+ await syncCollectionFields(ctx, collectionName, {
136
+ fields: (0, import_fields.normalizeFieldList)(values.fields, collectionName)
137
+ });
138
+ } else {
139
+ for (const field of values.fields) {
140
+ await upsertFieldDefinition(ctx, {
141
+ ...field,
142
+ collectionName
143
+ });
144
+ }
145
+ }
146
+ }
147
+ }
148
+ const collection = await findCollection(ctx, collectionName);
149
+ const verify = shouldVerify ? (0, import_capabilities.verifyCollectionDefinition)(ctx.app, collectionName) : void 0;
150
+ return {
151
+ data: collection,
152
+ verify
153
+ };
154
+ }
155
+ // Annotate the CommonJS export names for ESM import in node:
156
+ 0 && (module.exports = {
157
+ applyCollectionDefinition,
158
+ applyFieldDefinition,
159
+ findCollection,
160
+ pickCollectionApplyValues,
161
+ upsertFieldDefinition
162
+ });
@@ -6,8 +6,13 @@
6
6
  * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
+ export declare function syncCollectionFields(ctx: any, filterByTk: string, values: {
10
+ fields?: any[];
11
+ }): Promise<void>;
9
12
  declare const _default: {
10
13
  "collections:listMeta"(ctx: any, next: any): Promise<void>;
11
14
  "collections:setFields"(ctx: any, next: any): Promise<void>;
15
+ "collections:apply"(ctx: any, next: any): Promise<void>;
16
+ "fields:apply"(ctx: any, next: any): Promise<void>;
12
17
  };
13
18
  export default _default;
@@ -36,10 +36,79 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
36
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
37
  var collections_exports = {};
38
38
  __export(collections_exports, {
39
- default: () => collections_default
39
+ default: () => collections_default,
40
+ syncCollectionFields: () => syncCollectionFields
40
41
  });
41
42
  module.exports = __toCommonJS(collections_exports);
42
43
  var import_lodash = __toESM(require("lodash"));
44
+ var import_service = require("../modeling/service");
45
+ async function syncCollectionFields(ctx, filterByTk, values) {
46
+ var _a;
47
+ const transaction = await ctx.app.db.sequelize.transaction();
48
+ try {
49
+ const fields = (_a = values.fields) == null ? void 0 : _a.map((f) => {
50
+ delete f.key;
51
+ return f;
52
+ });
53
+ const db = ctx.app.db;
54
+ const collectionModel = await db.getRepository("collections").findOne({
55
+ filter: {
56
+ name: filterByTk
57
+ },
58
+ transaction
59
+ });
60
+ const existFields = await collectionModel.getFields({
61
+ transaction
62
+ });
63
+ const needUpdateFields = fields.filter((f) => {
64
+ return existFields.find((ef) => ef.name === f.name);
65
+ }).map((f) => {
66
+ return {
67
+ ...f,
68
+ key: existFields.find((ef) => ef.name === f.name).key
69
+ };
70
+ });
71
+ const needDestroyFields = existFields.filter((ef) => {
72
+ return !fields.find((f) => f.name === ef.name);
73
+ });
74
+ const needCreatedFields = fields.filter((f) => {
75
+ return !existFields.find((ef) => ef.name === f.name);
76
+ });
77
+ if (needDestroyFields.length) {
78
+ await db.getRepository("fields").destroy({
79
+ filterByTk: needDestroyFields.map((f) => f.key),
80
+ transaction
81
+ });
82
+ }
83
+ if (needUpdateFields.length) {
84
+ await db.getRepository("fields").updateMany({
85
+ records: needUpdateFields,
86
+ transaction
87
+ });
88
+ }
89
+ if (needCreatedFields.length) {
90
+ await db.getRepository("collections.fields", filterByTk).create({
91
+ values: needCreatedFields,
92
+ transaction
93
+ });
94
+ }
95
+ await collectionModel.loadFields({
96
+ transaction
97
+ });
98
+ const collection = db.getCollection(filterByTk);
99
+ await collection.sync({
100
+ force: false,
101
+ alter: {
102
+ drop: false
103
+ },
104
+ transaction
105
+ });
106
+ await transaction.commit();
107
+ } catch (e) {
108
+ await transaction.rollback();
109
+ throw e;
110
+ }
111
+ }
43
112
  var collections_default = {
44
113
  async ["collections:listMeta"](ctx, next) {
45
114
  const db = ctx.app.db;
@@ -69,72 +138,24 @@ var collections_default = {
69
138
  await next();
70
139
  },
71
140
  async ["collections:setFields"](ctx, next) {
72
- var _a;
73
141
  const { filterByTk, values } = ctx.action.params;
74
- const transaction = await ctx.app.db.sequelize.transaction();
75
- try {
76
- const fields = (_a = values.fields) == null ? void 0 : _a.map((f) => {
77
- delete f.key;
78
- return f;
79
- });
80
- const db = ctx.app.db;
81
- const collectionModel = await db.getRepository("collections").findOne({
82
- filter: {
83
- name: filterByTk
84
- },
85
- transaction
86
- });
87
- const existFields = await collectionModel.getFields({
88
- transaction
89
- });
90
- const needUpdateFields = fields.filter((f) => {
91
- return existFields.find((ef) => ef.name === f.name);
92
- }).map((f) => {
93
- return {
94
- ...f,
95
- key: existFields.find((ef) => ef.name === f.name).key
96
- };
97
- });
98
- const needDestroyFields = existFields.filter((ef) => {
99
- return !fields.find((f) => f.name === ef.name);
100
- });
101
- const needCreatedFields = fields.filter((f) => {
102
- return !existFields.find((ef) => ef.name === f.name);
103
- });
104
- if (needDestroyFields.length) {
105
- await db.getRepository("fields").destroy({
106
- filterByTk: needDestroyFields.map((f) => f.key),
107
- transaction
108
- });
109
- }
110
- if (needUpdateFields.length) {
111
- await db.getRepository("fields").updateMany({
112
- records: needUpdateFields,
113
- transaction
114
- });
115
- }
116
- if (needCreatedFields.length) {
117
- await db.getRepository("collections.fields", filterByTk).create({
118
- values: needCreatedFields,
119
- transaction
120
- });
121
- }
122
- await collectionModel.loadFields({
123
- transaction
124
- });
125
- const collection = db.getCollection(filterByTk);
126
- await collection.sync({
127
- force: false,
128
- alter: {
129
- drop: false
130
- },
131
- transaction
132
- });
133
- await transaction.commit();
134
- } catch (e) {
135
- await transaction.rollback();
136
- throw e;
137
- }
142
+ await syncCollectionFields(ctx, filterByTk, values);
143
+ await next();
144
+ },
145
+ async ["collections:apply"](ctx, next) {
146
+ const rawValues = ctx.action.params.values || ctx.request.body || {};
147
+ ctx.body = await (0, import_service.applyCollectionDefinition)(ctx, rawValues, syncCollectionFields);
148
+ await next();
149
+ },
150
+ async ["fields:apply"](ctx, next) {
151
+ const rawValues = ctx.action.params.values || ctx.request.body || {};
152
+ ctx.body = {
153
+ data: await (0, import_service.applyFieldDefinition)(ctx, rawValues)
154
+ };
138
155
  await next();
139
156
  }
140
157
  };
158
+ // Annotate the CommonJS export names for ESM import in node:
159
+ 0 && (module.exports = {
160
+ syncCollectionFields
161
+ });
@@ -56,6 +56,7 @@ var import_models = require("./models");
56
56
  var import_collections = __toESM(require("./resourcers/collections"));
57
57
  var import_views = __toESM(require("./resourcers/views"));
58
58
  var import_main_data_source = __toESM(require("./resourcers/main-data-source"));
59
+ var import_mcp_post_processors = require("./mcp-post-processors");
59
60
  var import_constants = require("./constants");
60
61
  var import_json_schema = require("@formily/json-schema");
61
62
  var import_lodash2 = __toESM(require("lodash"));
@@ -378,7 +379,7 @@ class PluginDataSourceMainServer extends import_server.Plugin {
378
379
  this.app.acl.allow("collectionCategories", "list", "loggedIn");
379
380
  this.app.acl.registerSnippet({
380
381
  name: `pm.data-source-manager.data-source-main`,
381
- actions: ["collections:*", "collections.fields:*", "collectionCategories:*", "mainDataSource:*"]
382
+ actions: ["collections:*", "collections.fields:*", "fields:*", "collectionCategories:*", "mainDataSource:*"]
382
383
  });
383
384
  this.app.acl.registerSnippet({
384
385
  name: `pm.data-source-manager.collection-view `,
@@ -404,6 +405,7 @@ class PluginDataSourceMainServer extends import_server.Plugin {
404
405
  async load() {
405
406
  this.db.getRepository("collections").setApp(this.app);
406
407
  this.registerErrorHandler();
408
+ (0, import_mcp_post_processors.registerDataSourceMainMcpPostProcessors)(this.ai.mcpToolsManager);
407
409
  this.app.resourceManager.use(async function mergeReverseFieldWhenSaveCollectionField(ctx, next) {
408
410
  if (ctx.action.resourceName === "collections.fields" && ["create", "update"].includes(ctx.action.actionName)) {
409
411
  ctx.action.mergeParams({
@@ -272,6 +272,43 @@ declare const _default: {
272
272
  };
273
273
  };
274
274
  };
275
+ '/collections:apply': {
276
+ post: {
277
+ tags: string[];
278
+ summary: string;
279
+ description: string;
280
+ requestBody: {
281
+ content: {
282
+ 'application/json': {
283
+ schema: {
284
+ $ref: string;
285
+ };
286
+ };
287
+ };
288
+ };
289
+ responses: {
290
+ '200': {
291
+ description: string;
292
+ content: {
293
+ 'application/json': {
294
+ schema: {
295
+ type: string;
296
+ properties: {
297
+ data: {
298
+ $ref: string;
299
+ };
300
+ verify: {
301
+ type: string;
302
+ additionalProperties: boolean;
303
+ };
304
+ };
305
+ };
306
+ };
307
+ };
308
+ };
309
+ };
310
+ };
311
+ };
275
312
  '/collections/{collectionName}/fields:get': {
276
313
  get: {
277
314
  tags: string[];
@@ -446,6 +483,39 @@ declare const _default: {
446
483
  };
447
484
  };
448
485
  };
486
+ '/fields:apply': {
487
+ post: {
488
+ tags: string[];
489
+ summary: string;
490
+ description: string;
491
+ requestBody: {
492
+ content: {
493
+ 'application/json': {
494
+ schema: {
495
+ $ref: string;
496
+ };
497
+ };
498
+ };
499
+ };
500
+ responses: {
501
+ '200': {
502
+ description: string;
503
+ content: {
504
+ 'application/json': {
505
+ schema: {
506
+ type: string;
507
+ properties: {
508
+ data: {
509
+ $ref: string;
510
+ };
511
+ };
512
+ };
513
+ };
514
+ };
515
+ };
516
+ };
517
+ };
518
+ };
449
519
  '/collectionCategories:list': {
450
520
  post: {
451
521
  tags: string[];
@@ -1030,6 +1100,64 @@ declare const _default: {
1030
1100
  };
1031
1101
  additionalProperties: boolean;
1032
1102
  };
1103
+ apply: {
1104
+ type: string;
1105
+ description: string;
1106
+ properties: {
1107
+ name: {
1108
+ type: string;
1109
+ description: string;
1110
+ };
1111
+ title: {
1112
+ type: string;
1113
+ description: string;
1114
+ };
1115
+ description: {
1116
+ type: string;
1117
+ };
1118
+ template: {
1119
+ type: string;
1120
+ description: string;
1121
+ };
1122
+ viewName: {
1123
+ type: string;
1124
+ description: string;
1125
+ };
1126
+ inherits: {
1127
+ oneOf: ({
1128
+ type: string;
1129
+ items?: undefined;
1130
+ } | {
1131
+ type: string;
1132
+ items: {
1133
+ type: string;
1134
+ };
1135
+ })[];
1136
+ description: string;
1137
+ };
1138
+ replaceFields: {
1139
+ type: string;
1140
+ description: string;
1141
+ };
1142
+ verify: {
1143
+ type: string;
1144
+ description: string;
1145
+ };
1146
+ fields: {
1147
+ type: string;
1148
+ description: string;
1149
+ items: {
1150
+ $ref: string;
1151
+ };
1152
+ };
1153
+ settings: {
1154
+ type: string;
1155
+ description: string;
1156
+ additionalProperties: boolean;
1157
+ };
1158
+ };
1159
+ additionalProperties: boolean;
1160
+ };
1033
1161
  update: {
1034
1162
  type: string;
1035
1163
  properties: {
@@ -1397,6 +1525,174 @@ declare const _default: {
1397
1525
  $ref: string;
1398
1526
  }[];
1399
1527
  };
1528
+ apply: {
1529
+ type: string;
1530
+ description: string;
1531
+ properties: {
1532
+ collectionName: {
1533
+ type: string;
1534
+ description: string;
1535
+ };
1536
+ name: {
1537
+ type: string;
1538
+ };
1539
+ title: {
1540
+ type: string;
1541
+ };
1542
+ interface: {
1543
+ type: string;
1544
+ };
1545
+ description: {
1546
+ type: string;
1547
+ };
1548
+ target: {
1549
+ type: string;
1550
+ };
1551
+ defaultValue: {
1552
+ description: string;
1553
+ };
1554
+ enum: {
1555
+ type: string;
1556
+ description: string;
1557
+ items: {
1558
+ anyOf: ({
1559
+ type: string;
1560
+ properties?: undefined;
1561
+ required?: undefined;
1562
+ additionalProperties?: undefined;
1563
+ } | {
1564
+ type: string;
1565
+ properties: {
1566
+ label: {
1567
+ type: string;
1568
+ };
1569
+ value: {};
1570
+ color: {
1571
+ type: string;
1572
+ };
1573
+ };
1574
+ required: string[];
1575
+ additionalProperties: boolean;
1576
+ })[];
1577
+ };
1578
+ };
1579
+ reverseName: {
1580
+ type: string;
1581
+ };
1582
+ reverseTitle: {
1583
+ type: string;
1584
+ };
1585
+ reverseInterface: {
1586
+ type: string;
1587
+ };
1588
+ settings: {
1589
+ type: string;
1590
+ description: string;
1591
+ additionalProperties: boolean;
1592
+ };
1593
+ reverseField: {
1594
+ allOf: {
1595
+ $ref: string;
1596
+ }[];
1597
+ description: string;
1598
+ };
1599
+ };
1600
+ required: string[];
1601
+ additionalProperties: boolean;
1602
+ };
1603
+ applyShallow: {
1604
+ type: string;
1605
+ description: string;
1606
+ properties: {
1607
+ name: {
1608
+ type: string;
1609
+ };
1610
+ title: {
1611
+ type: string;
1612
+ };
1613
+ interface: {
1614
+ type: string;
1615
+ };
1616
+ target: {
1617
+ type: string;
1618
+ };
1619
+ settings: {
1620
+ type: string;
1621
+ additionalProperties: boolean;
1622
+ };
1623
+ };
1624
+ additionalProperties: boolean;
1625
+ };
1626
+ applyNested: {
1627
+ type: string;
1628
+ description: string;
1629
+ properties: {
1630
+ name: {
1631
+ type: string;
1632
+ };
1633
+ title: {
1634
+ type: string;
1635
+ };
1636
+ interface: {
1637
+ type: string;
1638
+ };
1639
+ description: {
1640
+ type: string;
1641
+ };
1642
+ target: {
1643
+ type: string;
1644
+ };
1645
+ defaultValue: {
1646
+ description: string;
1647
+ };
1648
+ enum: {
1649
+ type: string;
1650
+ description: string;
1651
+ items: {
1652
+ anyOf: ({
1653
+ type: string;
1654
+ properties?: undefined;
1655
+ required?: undefined;
1656
+ additionalProperties?: undefined;
1657
+ } | {
1658
+ type: string;
1659
+ properties: {
1660
+ label: {
1661
+ type: string;
1662
+ };
1663
+ value: {};
1664
+ color: {
1665
+ type: string;
1666
+ };
1667
+ };
1668
+ required: string[];
1669
+ additionalProperties: boolean;
1670
+ })[];
1671
+ };
1672
+ };
1673
+ reverseName: {
1674
+ type: string;
1675
+ };
1676
+ reverseTitle: {
1677
+ type: string;
1678
+ };
1679
+ reverseInterface: {
1680
+ type: string;
1681
+ };
1682
+ settings: {
1683
+ type: string;
1684
+ description: string;
1685
+ additionalProperties: boolean;
1686
+ };
1687
+ reverseField: {
1688
+ allOf: {
1689
+ $ref: string;
1690
+ }[];
1691
+ description: string;
1692
+ };
1693
+ };
1694
+ additionalProperties: boolean;
1695
+ };
1400
1696
  update: {
1401
1697
  allOf: {
1402
1698
  $ref: string;