@cloudbase/cloudbase-mcp 1.0.2 → 1.0.3

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.
@@ -1,90 +1,155 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
1
37
  import { z } from "zod";
2
38
  import CloudBase from "@cloudbase/manager-node";
3
39
  // 初始化CloudBase
4
- const cloudbase = new CloudBase({
40
+ var cloudbase = new CloudBase({
5
41
  secretId: process.env.TENCENTCLOUD_SECRETID,
6
42
  secretKey: process.env.TENCENTCLOUD_SECRETKEY,
7
43
  envId: process.env.CLOUDBASE_ENV_ID,
8
44
  token: process.env.TENCENTCLOUD_SESSIONTOKEN
9
45
  });
10
46
  // 获取数据库实例ID
11
- async function getDatabaseInstanceId() {
12
- const { EnvInfo } = await cloudbase.env.getEnvInfo();
13
- if (!EnvInfo?.Databases?.[0]?.InstanceId) {
14
- throw new Error("无法获取数据库实例ID");
15
- }
16
- return EnvInfo.Databases[0].InstanceId;
47
+ function getDatabaseInstanceId() {
48
+ return __awaiter(this, void 0, void 0, function () {
49
+ var EnvInfo;
50
+ var _a, _b;
51
+ return __generator(this, function (_c) {
52
+ switch (_c.label) {
53
+ case 0: return [4 /*yield*/, cloudbase.env.getEnvInfo()];
54
+ case 1:
55
+ EnvInfo = (_c.sent()).EnvInfo;
56
+ if (!((_b = (_a = EnvInfo === null || EnvInfo === void 0 ? void 0 : EnvInfo.Databases) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.InstanceId)) {
57
+ throw new Error("无法获取数据库实例ID");
58
+ }
59
+ return [2 /*return*/, EnvInfo.Databases[0].InstanceId];
60
+ }
61
+ });
62
+ });
17
63
  }
18
64
  export function registerDatabaseTools(server) {
65
+ var _this = this;
19
66
  // 创建云开发数据库集合
20
67
  server.tool("createCollection", "创建一个新的云开发数据库集合", {
21
68
  collectionName: z.string().describe("云开发数据库集合名称")
22
- }, async ({ collectionName }) => {
23
- try {
24
- const result = await cloudbase.database.createCollection(collectionName);
25
- return {
26
- content: [
27
- {
28
- type: "text",
29
- text: JSON.stringify({
30
- success: true,
31
- requestId: result.RequestId,
32
- message: "云开发数据库集合创建成功"
33
- }, null, 2)
34
- }
35
- ]
36
- };
37
- }
38
- catch (error) {
39
- return {
40
- content: [
41
- {
42
- type: "text",
43
- text: JSON.stringify({
44
- success: false,
45
- error: error.message,
46
- message: "云开发数据库集合创建失败"
47
- }, null, 2)
48
- }
49
- ]
50
- };
51
- }
52
- });
69
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
70
+ var result, error_1;
71
+ var collectionName = _b.collectionName;
72
+ return __generator(this, function (_c) {
73
+ switch (_c.label) {
74
+ case 0:
75
+ _c.trys.push([0, 2, , 3]);
76
+ return [4 /*yield*/, cloudbase.database.createCollection(collectionName)];
77
+ case 1:
78
+ result = _c.sent();
79
+ return [2 /*return*/, {
80
+ content: [
81
+ {
82
+ type: "text",
83
+ text: JSON.stringify({
84
+ success: true,
85
+ requestId: result.RequestId,
86
+ message: "云开发数据库集合创建成功"
87
+ }, null, 2)
88
+ }
89
+ ]
90
+ }];
91
+ case 2:
92
+ error_1 = _c.sent();
93
+ return [2 /*return*/, {
94
+ content: [
95
+ {
96
+ type: "text",
97
+ text: JSON.stringify({
98
+ success: false,
99
+ error: error_1.message,
100
+ message: "云开发数据库集合创建失败"
101
+ }, null, 2)
102
+ }
103
+ ]
104
+ }];
105
+ case 3: return [2 /*return*/];
106
+ }
107
+ });
108
+ }); });
53
109
  // 检查云开发数据库集合是否存在
54
110
  server.tool("checkCollectionExists", "检查云开发数据库集合是否存在", {
55
111
  collectionName: z.string().describe("云开发数据库集合名称")
56
- }, async ({ collectionName }) => {
57
- try {
58
- const result = await cloudbase.database.checkCollectionExists(collectionName);
59
- return {
60
- content: [
61
- {
62
- type: "text",
63
- text: JSON.stringify({
64
- success: true,
65
- exists: result.Exists,
66
- requestId: result.RequestId,
67
- message: result.Exists ? "云开发数据库集合已存在" : "云开发数据库集合不存在"
68
- }, null, 2)
69
- }
70
- ]
71
- };
72
- }
73
- catch (error) {
74
- return {
75
- content: [
76
- {
77
- type: "text",
78
- text: JSON.stringify({
79
- success: false,
80
- error: error.message,
81
- message: "检查云开发数据库集合失败"
82
- }, null, 2)
83
- }
84
- ]
85
- };
86
- }
87
- });
112
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
113
+ var result, error_2;
114
+ var collectionName = _b.collectionName;
115
+ return __generator(this, function (_c) {
116
+ switch (_c.label) {
117
+ case 0:
118
+ _c.trys.push([0, 2, , 3]);
119
+ return [4 /*yield*/, cloudbase.database.checkCollectionExists(collectionName)];
120
+ case 1:
121
+ result = _c.sent();
122
+ return [2 /*return*/, {
123
+ content: [
124
+ {
125
+ type: "text",
126
+ text: JSON.stringify({
127
+ success: true,
128
+ exists: result.Exists,
129
+ requestId: result.RequestId,
130
+ message: result.Exists ? "云开发数据库集合已存在" : "云开发数据库集合不存在"
131
+ }, null, 2)
132
+ }
133
+ ]
134
+ }];
135
+ case 2:
136
+ error_2 = _c.sent();
137
+ return [2 /*return*/, {
138
+ content: [
139
+ {
140
+ type: "text",
141
+ text: JSON.stringify({
142
+ success: false,
143
+ error: error_2.message,
144
+ message: "检查云开发数据库集合失败"
145
+ }, null, 2)
146
+ }
147
+ ]
148
+ }];
149
+ case 3: return [2 /*return*/];
150
+ }
151
+ });
152
+ }); });
88
153
  // // 删除云开发数据库集合
89
154
  // server.tool(
90
155
  // "deleteCollection",
@@ -142,149 +207,185 @@ export function registerDatabaseTools(server) {
142
207
  IndexName: z.string()
143
208
  })).optional()
144
209
  }).describe("更新选项,支持创建和删除索引")
145
- }, async ({ collectionName, options }) => {
146
- try {
147
- const result = await cloudbase.database.updateCollection(collectionName, options);
148
- return {
149
- content: [
150
- {
151
- type: "text",
152
- text: JSON.stringify({
153
- success: true,
154
- requestId: result.RequestId,
155
- message: "云开发数据库集合更新成功"
156
- }, null, 2)
157
- }
158
- ]
159
- };
160
- }
161
- catch (error) {
162
- return {
163
- content: [
164
- {
165
- type: "text",
166
- text: JSON.stringify({
167
- success: false,
168
- error: error.message,
169
- message: "云开发数据库集合更新失败"
170
- }, null, 2)
171
- }
172
- ]
173
- };
174
- }
175
- });
210
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
211
+ var result, error_3;
212
+ var collectionName = _b.collectionName, options = _b.options;
213
+ return __generator(this, function (_c) {
214
+ switch (_c.label) {
215
+ case 0:
216
+ _c.trys.push([0, 2, , 3]);
217
+ return [4 /*yield*/, cloudbase.database.updateCollection(collectionName, options)];
218
+ case 1:
219
+ result = _c.sent();
220
+ return [2 /*return*/, {
221
+ content: [
222
+ {
223
+ type: "text",
224
+ text: JSON.stringify({
225
+ success: true,
226
+ requestId: result.RequestId,
227
+ message: "云开发数据库集合更新成功"
228
+ }, null, 2)
229
+ }
230
+ ]
231
+ }];
232
+ case 2:
233
+ error_3 = _c.sent();
234
+ return [2 /*return*/, {
235
+ content: [
236
+ {
237
+ type: "text",
238
+ text: JSON.stringify({
239
+ success: false,
240
+ error: error_3.message,
241
+ message: "云开发数据库集合更新失败"
242
+ }, null, 2)
243
+ }
244
+ ]
245
+ }];
246
+ case 3: return [2 /*return*/];
247
+ }
248
+ });
249
+ }); });
176
250
  // 查询云开发数据库集合详情
177
251
  server.tool("describeCollection", "获取云开发数据库集合的详细信息", {
178
252
  collectionName: z.string().describe("云开发数据库集合名称")
179
- }, async ({ collectionName }) => {
180
- try {
181
- const result = await cloudbase.database.describeCollection(collectionName);
182
- return {
183
- content: [
184
- {
185
- type: "text",
186
- text: JSON.stringify({
187
- success: true,
188
- requestId: result.RequestId,
189
- indexNum: result.IndexNum,
190
- indexes: result.Indexes,
191
- message: "获取云开发数据库集合信息成功"
192
- }, null, 2)
193
- }
194
- ]
195
- };
196
- }
197
- catch (error) {
198
- return {
199
- content: [
200
- {
201
- type: "text",
202
- text: JSON.stringify({
203
- success: false,
204
- error: error.message,
205
- message: "获取云开发数据库集合信息失败"
206
- }, null, 2)
207
- }
208
- ]
209
- };
210
- }
211
- });
253
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
254
+ var result, error_4;
255
+ var collectionName = _b.collectionName;
256
+ return __generator(this, function (_c) {
257
+ switch (_c.label) {
258
+ case 0:
259
+ _c.trys.push([0, 2, , 3]);
260
+ return [4 /*yield*/, cloudbase.database.describeCollection(collectionName)];
261
+ case 1:
262
+ result = _c.sent();
263
+ return [2 /*return*/, {
264
+ content: [
265
+ {
266
+ type: "text",
267
+ text: JSON.stringify({
268
+ success: true,
269
+ requestId: result.RequestId,
270
+ indexNum: result.IndexNum,
271
+ indexes: result.Indexes,
272
+ message: "获取云开发数据库集合信息成功"
273
+ }, null, 2)
274
+ }
275
+ ]
276
+ }];
277
+ case 2:
278
+ error_4 = _c.sent();
279
+ return [2 /*return*/, {
280
+ content: [
281
+ {
282
+ type: "text",
283
+ text: JSON.stringify({
284
+ success: false,
285
+ error: error_4.message,
286
+ message: "获取云开发数据库集合信息失败"
287
+ }, null, 2)
288
+ }
289
+ ]
290
+ }];
291
+ case 3: return [2 /*return*/];
292
+ }
293
+ });
294
+ }); });
212
295
  // 获取云开发数据库集合列表
213
296
  server.tool("listCollections", "获取云开发数据库集合列表", {
214
297
  offset: z.number().optional().describe("偏移量"),
215
298
  limit: z.number().optional().describe("返回数量限制")
216
- }, async ({ offset, limit }) => {
217
- try {
218
- const result = await cloudbase.database.listCollections({
219
- MgoOffset: offset,
220
- MgoLimit: limit
221
- });
222
- return {
223
- content: [
224
- {
225
- type: "text",
226
- text: JSON.stringify({
227
- success: true,
228
- requestId: result.RequestId,
229
- collections: result.Collections,
230
- pager: result.Pager,
231
- message: "获取云开发数据库集合列表成功"
232
- }, null, 2)
233
- }
234
- ]
235
- };
236
- }
237
- catch (error) {
238
- return {
239
- content: [
240
- {
241
- type: "text",
242
- text: JSON.stringify({
243
- success: false,
244
- error: error.message,
245
- message: "获取云开发数据库集合列表失败"
246
- }, null, 2)
247
- }
248
- ]
249
- };
250
- }
251
- });
299
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
300
+ var result, error_5;
301
+ var offset = _b.offset, limit = _b.limit;
302
+ return __generator(this, function (_c) {
303
+ switch (_c.label) {
304
+ case 0:
305
+ _c.trys.push([0, 2, , 3]);
306
+ return [4 /*yield*/, cloudbase.database.listCollections({
307
+ MgoOffset: offset,
308
+ MgoLimit: limit
309
+ })];
310
+ case 1:
311
+ result = _c.sent();
312
+ return [2 /*return*/, {
313
+ content: [
314
+ {
315
+ type: "text",
316
+ text: JSON.stringify({
317
+ success: true,
318
+ requestId: result.RequestId,
319
+ collections: result.Collections,
320
+ pager: result.Pager,
321
+ message: "获取云开发数据库集合列表成功"
322
+ }, null, 2)
323
+ }
324
+ ]
325
+ }];
326
+ case 2:
327
+ error_5 = _c.sent();
328
+ return [2 /*return*/, {
329
+ content: [
330
+ {
331
+ type: "text",
332
+ text: JSON.stringify({
333
+ success: false,
334
+ error: error_5.message,
335
+ message: "获取云开发数据库集合列表失败"
336
+ }, null, 2)
337
+ }
338
+ ]
339
+ }];
340
+ case 3: return [2 /*return*/];
341
+ }
342
+ });
343
+ }); });
252
344
  // 检查索引是否存在
253
345
  server.tool("checkIndexExists", "检查索引是否存在", {
254
346
  collectionName: z.string().describe("云开发数据库集合名称"),
255
347
  indexName: z.string().describe("索引名称")
256
- }, async ({ collectionName, indexName }) => {
257
- try {
258
- const result = await cloudbase.database.checkIndexExists(collectionName, indexName);
259
- return {
260
- content: [
261
- {
262
- type: "text",
263
- text: JSON.stringify({
264
- success: true,
265
- exists: result.Exists,
266
- requestId: result.RequestId,
267
- message: result.Exists ? "索引已存在" : "索引不存在"
268
- }, null, 2)
269
- }
270
- ]
271
- };
272
- }
273
- catch (error) {
274
- return {
275
- content: [
276
- {
277
- type: "text",
278
- text: JSON.stringify({
279
- success: false,
280
- error: error.message,
281
- message: "检查索引失败"
282
- }, null, 2)
283
- }
284
- ]
285
- };
286
- }
287
- });
348
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
349
+ var result, error_6;
350
+ var collectionName = _b.collectionName, indexName = _b.indexName;
351
+ return __generator(this, function (_c) {
352
+ switch (_c.label) {
353
+ case 0:
354
+ _c.trys.push([0, 2, , 3]);
355
+ return [4 /*yield*/, cloudbase.database.checkIndexExists(collectionName, indexName)];
356
+ case 1:
357
+ result = _c.sent();
358
+ return [2 /*return*/, {
359
+ content: [
360
+ {
361
+ type: "text",
362
+ text: JSON.stringify({
363
+ success: true,
364
+ exists: result.Exists,
365
+ requestId: result.RequestId,
366
+ message: result.Exists ? "索引已存在" : "索引不存在"
367
+ }, null, 2)
368
+ }
369
+ ]
370
+ }];
371
+ case 2:
372
+ error_6 = _c.sent();
373
+ return [2 /*return*/, {
374
+ content: [
375
+ {
376
+ type: "text",
377
+ text: JSON.stringify({
378
+ success: false,
379
+ error: error_6.message,
380
+ message: "检查索引失败"
381
+ }, null, 2)
382
+ }
383
+ ]
384
+ }];
385
+ case 3: return [2 /*return*/];
386
+ }
387
+ });
388
+ }); });
288
389
  // // 导入数据
289
390
  // server.tool(
290
391
  // "importData",
@@ -427,82 +528,101 @@ export function registerDatabaseTools(server) {
427
528
  // }
428
529
  // );
429
530
  // 查询数据分布
430
- server.tool("distribution", "查询数据库中云开发数据库集合的数据分布情况", {}, async () => {
431
- try {
432
- const result = await cloudbase.database.distribution();
433
- return {
434
- content: [
435
- {
436
- type: "text",
437
- text: JSON.stringify({
438
- success: true,
439
- requestId: result.RequestId,
440
- collections: result.Collections,
441
- message: "获取数据分布成功"
442
- }, null, 2)
443
- }
444
- ]
445
- };
446
- }
447
- catch (error) {
448
- return {
449
- content: [
450
- {
451
- type: "text",
452
- text: JSON.stringify({
453
- success: false,
454
- error: error.message,
455
- message: "获取数据分布失败"
456
- }, null, 2)
457
- }
458
- ]
459
- };
460
- }
461
- });
531
+ server.tool("distribution", "查询数据库中云开发数据库集合的数据分布情况", {}, function () { return __awaiter(_this, void 0, void 0, function () {
532
+ var result, error_7;
533
+ return __generator(this, function (_a) {
534
+ switch (_a.label) {
535
+ case 0:
536
+ _a.trys.push([0, 2, , 3]);
537
+ return [4 /*yield*/, cloudbase.database.distribution()];
538
+ case 1:
539
+ result = _a.sent();
540
+ return [2 /*return*/, {
541
+ content: [
542
+ {
543
+ type: "text",
544
+ text: JSON.stringify({
545
+ success: true,
546
+ requestId: result.RequestId,
547
+ collections: result.Collections,
548
+ message: "获取数据分布成功"
549
+ }, null, 2)
550
+ }
551
+ ]
552
+ }];
553
+ case 2:
554
+ error_7 = _a.sent();
555
+ return [2 /*return*/, {
556
+ content: [
557
+ {
558
+ type: "text",
559
+ text: JSON.stringify({
560
+ success: false,
561
+ error: error_7.message,
562
+ message: "获取数据分布失败"
563
+ }, null, 2)
564
+ }
565
+ ]
566
+ }];
567
+ case 3: return [2 /*return*/];
568
+ }
569
+ });
570
+ }); });
462
571
  // 插入文档
463
572
  server.tool("insertDocuments", "向云开发数据库集合中插入一个或多个文档", {
464
573
  collectionName: z.string().describe("云开发数据库集合名称"),
465
574
  documents: z.array(z.string()).describe("要插入的文档数组,每个文档都是JSON字符串")
466
- }, async ({ collectionName, documents }) => {
467
- try {
468
- const instanceId = await getDatabaseInstanceId();
469
- const result = await cloudbase.commonService('flexdb').call({
470
- Action: 'PutItem',
471
- Param: {
472
- TableName: collectionName,
473
- MgoDocs: documents,
474
- Tag: instanceId
475
- }
476
- });
477
- return {
478
- content: [
479
- {
480
- type: "text",
481
- text: JSON.stringify({
482
- success: true,
483
- requestId: result.RequestId,
484
- insertedIds: result.InsertedIds,
485
- message: "文档插入成功"
486
- }, null, 2)
487
- }
488
- ]
489
- };
490
- }
491
- catch (error) {
492
- return {
493
- content: [
494
- {
495
- type: "text",
496
- text: JSON.stringify({
497
- success: false,
498
- error: error.message,
499
- message: "文档插入失败"
500
- }, null, 2)
501
- }
502
- ]
503
- };
504
- }
505
- });
575
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
576
+ var instanceId, result, error_8;
577
+ var collectionName = _b.collectionName, documents = _b.documents;
578
+ return __generator(this, function (_c) {
579
+ switch (_c.label) {
580
+ case 0:
581
+ _c.trys.push([0, 3, , 4]);
582
+ return [4 /*yield*/, getDatabaseInstanceId()];
583
+ case 1:
584
+ instanceId = _c.sent();
585
+ return [4 /*yield*/, cloudbase.commonService('flexdb').call({
586
+ Action: 'PutItem',
587
+ Param: {
588
+ TableName: collectionName,
589
+ MgoDocs: documents,
590
+ Tag: instanceId
591
+ }
592
+ })];
593
+ case 2:
594
+ result = _c.sent();
595
+ return [2 /*return*/, {
596
+ content: [
597
+ {
598
+ type: "text",
599
+ text: JSON.stringify({
600
+ success: true,
601
+ requestId: result.RequestId,
602
+ insertedIds: result.InsertedIds,
603
+ message: "文档插入成功"
604
+ }, null, 2)
605
+ }
606
+ ]
607
+ }];
608
+ case 3:
609
+ error_8 = _c.sent();
610
+ return [2 /*return*/, {
611
+ content: [
612
+ {
613
+ type: "text",
614
+ text: JSON.stringify({
615
+ success: false,
616
+ error: error_8.message,
617
+ message: "文档插入失败"
618
+ }, null, 2)
619
+ }
620
+ ]
621
+ }];
622
+ case 4: return [2 /*return*/];
623
+ }
624
+ });
625
+ }); });
506
626
  // 查询文档
507
627
  server.tool("queryDocuments", "查询云开发数据库集合中的文档", {
508
628
  collectionName: z.string().describe("云开发数据库集合名称"),
@@ -511,51 +631,62 @@ export function registerDatabaseTools(server) {
511
631
  sort: z.string().optional().describe("排序条件(JSON字符串)"),
512
632
  limit: z.number().optional().describe("返回数量限制"),
513
633
  offset: z.number().optional().describe("跳过的记录数")
514
- }, async ({ collectionName, query, projection, sort, limit, offset }) => {
515
- try {
516
- const instanceId = await getDatabaseInstanceId();
517
- const result = await cloudbase.commonService('flexdb').call({
518
- Action: 'Query',
519
- Param: {
520
- TableName: collectionName,
521
- MgoQuery: query,
522
- MgoProjection: projection,
523
- MgoSort: sort,
524
- MgoLimit: limit,
525
- MgoOffset: offset,
526
- Tag: instanceId
527
- }
528
- });
529
- return {
530
- content: [
531
- {
532
- type: "text",
533
- text: JSON.stringify({
534
- success: true,
535
- requestId: result.RequestId,
536
- data: result.Data,
537
- pager: result.Pager,
538
- message: "文档查询成功"
539
- }, null, 2)
540
- }
541
- ]
542
- };
543
- }
544
- catch (error) {
545
- return {
546
- content: [
547
- {
548
- type: "text",
549
- text: JSON.stringify({
550
- success: false,
551
- error: error.message,
552
- message: "文档查询失败"
553
- }, null, 2)
554
- }
555
- ]
556
- };
557
- }
558
- });
634
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
635
+ var instanceId, result, error_9;
636
+ var collectionName = _b.collectionName, query = _b.query, projection = _b.projection, sort = _b.sort, limit = _b.limit, offset = _b.offset;
637
+ return __generator(this, function (_c) {
638
+ switch (_c.label) {
639
+ case 0:
640
+ _c.trys.push([0, 3, , 4]);
641
+ return [4 /*yield*/, getDatabaseInstanceId()];
642
+ case 1:
643
+ instanceId = _c.sent();
644
+ return [4 /*yield*/, cloudbase.commonService('flexdb').call({
645
+ Action: 'Query',
646
+ Param: {
647
+ TableName: collectionName,
648
+ MgoQuery: query,
649
+ MgoProjection: projection,
650
+ MgoSort: sort,
651
+ MgoLimit: limit,
652
+ MgoOffset: offset,
653
+ Tag: instanceId
654
+ }
655
+ })];
656
+ case 2:
657
+ result = _c.sent();
658
+ return [2 /*return*/, {
659
+ content: [
660
+ {
661
+ type: "text",
662
+ text: JSON.stringify({
663
+ success: true,
664
+ requestId: result.RequestId,
665
+ data: result.Data,
666
+ pager: result.Pager,
667
+ message: "文档查询成功"
668
+ }, null, 2)
669
+ }
670
+ ]
671
+ }];
672
+ case 3:
673
+ error_9 = _c.sent();
674
+ return [2 /*return*/, {
675
+ content: [
676
+ {
677
+ type: "text",
678
+ text: JSON.stringify({
679
+ success: false,
680
+ error: error_9.message,
681
+ message: "文档查询失败"
682
+ }, null, 2)
683
+ }
684
+ ]
685
+ }];
686
+ case 4: return [2 /*return*/];
687
+ }
688
+ });
689
+ }); });
559
690
  // 更新文档
560
691
  server.tool("updateDocuments", "更新云开发数据库集合中的文档", {
561
692
  collectionName: z.string().describe("云开发数据库集合名称"),
@@ -563,95 +694,117 @@ export function registerDatabaseTools(server) {
563
694
  update: z.string().describe("更新内容(JSON字符串)"),
564
695
  isMulti: z.boolean().optional().describe("是否更新多条记录"),
565
696
  upsert: z.boolean().optional().describe("是否在不存在时插入")
566
- }, async ({ collectionName, query, update, isMulti, upsert }) => {
567
- try {
568
- const instanceId = await getDatabaseInstanceId();
569
- const result = await cloudbase.commonService('flexdb').call({
570
- Action: 'UpdateItem',
571
- Param: {
572
- TableName: collectionName,
573
- MgoQuery: query,
574
- MgoUpdate: update,
575
- MgoIsMulti: isMulti,
576
- MgoUpsert: upsert,
577
- Tag: instanceId
578
- }
579
- });
580
- return {
581
- content: [
582
- {
583
- type: "text",
584
- text: JSON.stringify({
585
- success: true,
586
- requestId: result.RequestId,
587
- modifiedCount: result.ModifiedNum,
588
- matchedCount: result.MatchedNum,
589
- upsertedId: result.UpsertedId,
590
- message: "文档更新成功"
591
- }, null, 2)
592
- }
593
- ]
594
- };
595
- }
596
- catch (error) {
597
- return {
598
- content: [
599
- {
600
- type: "text",
601
- text: JSON.stringify({
602
- success: false,
603
- error: error.message,
604
- message: "文档更新失败"
605
- }, null, 2)
606
- }
607
- ]
608
- };
609
- }
610
- });
697
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
698
+ var instanceId, result, error_10;
699
+ var collectionName = _b.collectionName, query = _b.query, update = _b.update, isMulti = _b.isMulti, upsert = _b.upsert;
700
+ return __generator(this, function (_c) {
701
+ switch (_c.label) {
702
+ case 0:
703
+ _c.trys.push([0, 3, , 4]);
704
+ return [4 /*yield*/, getDatabaseInstanceId()];
705
+ case 1:
706
+ instanceId = _c.sent();
707
+ return [4 /*yield*/, cloudbase.commonService('flexdb').call({
708
+ Action: 'UpdateItem',
709
+ Param: {
710
+ TableName: collectionName,
711
+ MgoQuery: query,
712
+ MgoUpdate: update,
713
+ MgoIsMulti: isMulti,
714
+ MgoUpsert: upsert,
715
+ Tag: instanceId
716
+ }
717
+ })];
718
+ case 2:
719
+ result = _c.sent();
720
+ return [2 /*return*/, {
721
+ content: [
722
+ {
723
+ type: "text",
724
+ text: JSON.stringify({
725
+ success: true,
726
+ requestId: result.RequestId,
727
+ modifiedCount: result.ModifiedNum,
728
+ matchedCount: result.MatchedNum,
729
+ upsertedId: result.UpsertedId,
730
+ message: "文档更新成功"
731
+ }, null, 2)
732
+ }
733
+ ]
734
+ }];
735
+ case 3:
736
+ error_10 = _c.sent();
737
+ return [2 /*return*/, {
738
+ content: [
739
+ {
740
+ type: "text",
741
+ text: JSON.stringify({
742
+ success: false,
743
+ error: error_10.message,
744
+ message: "文档更新失败"
745
+ }, null, 2)
746
+ }
747
+ ]
748
+ }];
749
+ case 4: return [2 /*return*/];
750
+ }
751
+ });
752
+ }); });
611
753
  // 删除文档
612
754
  server.tool("deleteDocuments", "删除云开发数据库集合中的文档", {
613
755
  collectionName: z.string().describe("云开发数据库集合名称"),
614
756
  query: z.string().describe("查询条件(JSON字符串)"),
615
757
  isMulti: z.boolean().optional().describe("是否删除多条记录")
616
- }, async ({ collectionName, query, isMulti }) => {
617
- try {
618
- const instanceId = await getDatabaseInstanceId();
619
- const result = await cloudbase.commonService('flexdb').call({
620
- Action: 'DeleteItem',
621
- Param: {
622
- TableName: collectionName,
623
- MgoQuery: query,
624
- MgoIsMulti: isMulti,
625
- Tag: instanceId
626
- }
627
- });
628
- return {
629
- content: [
630
- {
631
- type: "text",
632
- text: JSON.stringify({
633
- success: true,
634
- requestId: result.RequestId,
635
- deleted: result.Deleted,
636
- message: "文档删除成功"
637
- }, null, 2)
638
- }
639
- ]
640
- };
641
- }
642
- catch (error) {
643
- return {
644
- content: [
645
- {
646
- type: "text",
647
- text: JSON.stringify({
648
- success: false,
649
- error: error.message,
650
- message: "文档删除失败"
651
- }, null, 2)
652
- }
653
- ]
654
- };
655
- }
656
- });
758
+ }, function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
759
+ var instanceId, result, error_11;
760
+ var collectionName = _b.collectionName, query = _b.query, isMulti = _b.isMulti;
761
+ return __generator(this, function (_c) {
762
+ switch (_c.label) {
763
+ case 0:
764
+ _c.trys.push([0, 3, , 4]);
765
+ return [4 /*yield*/, getDatabaseInstanceId()];
766
+ case 1:
767
+ instanceId = _c.sent();
768
+ return [4 /*yield*/, cloudbase.commonService('flexdb').call({
769
+ Action: 'DeleteItem',
770
+ Param: {
771
+ TableName: collectionName,
772
+ MgoQuery: query,
773
+ MgoIsMulti: isMulti,
774
+ Tag: instanceId
775
+ }
776
+ })];
777
+ case 2:
778
+ result = _c.sent();
779
+ return [2 /*return*/, {
780
+ content: [
781
+ {
782
+ type: "text",
783
+ text: JSON.stringify({
784
+ success: true,
785
+ requestId: result.RequestId,
786
+ deleted: result.Deleted,
787
+ message: "文档删除成功"
788
+ }, null, 2)
789
+ }
790
+ ]
791
+ }];
792
+ case 3:
793
+ error_11 = _c.sent();
794
+ return [2 /*return*/, {
795
+ content: [
796
+ {
797
+ type: "text",
798
+ text: JSON.stringify({
799
+ success: false,
800
+ error: error_11.message,
801
+ message: "文档删除失败"
802
+ }, null, 2)
803
+ }
804
+ ]
805
+ }];
806
+ case 4: return [2 /*return*/];
807
+ }
808
+ });
809
+ }); });
657
810
  }