@cloudbase/cloudbase-mcp 1.0.1 → 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.
- package/dist/index.js +79 -0
- package/dist/tools/database.js +810 -0
- package/dist/tools/env.js +248 -0
- package/dist/tools/file.js +251 -0
- package/dist/tools/functions.js +491 -0
- package/dist/tools/hosting.js +328 -0
- package/dist/types.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,810 @@
|
|
|
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
|
+
};
|
|
37
|
+
import { z } from "zod";
|
|
38
|
+
import CloudBase from "@cloudbase/manager-node";
|
|
39
|
+
// 初始化CloudBase
|
|
40
|
+
var cloudbase = new CloudBase({
|
|
41
|
+
secretId: process.env.TENCENTCLOUD_SECRETID,
|
|
42
|
+
secretKey: process.env.TENCENTCLOUD_SECRETKEY,
|
|
43
|
+
envId: process.env.CLOUDBASE_ENV_ID,
|
|
44
|
+
token: process.env.TENCENTCLOUD_SESSIONTOKEN
|
|
45
|
+
});
|
|
46
|
+
// 获取数据库实例ID
|
|
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
|
+
});
|
|
63
|
+
}
|
|
64
|
+
export function registerDatabaseTools(server) {
|
|
65
|
+
var _this = this;
|
|
66
|
+
// 创建云开发数据库集合
|
|
67
|
+
server.tool("createCollection", "创建一个新的云开发数据库集合", {
|
|
68
|
+
collectionName: z.string().describe("云开发数据库集合名称")
|
|
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
|
+
}); });
|
|
109
|
+
// 检查云开发数据库集合是否存在
|
|
110
|
+
server.tool("checkCollectionExists", "检查云开发数据库集合是否存在", {
|
|
111
|
+
collectionName: z.string().describe("云开发数据库集合名称")
|
|
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
|
+
}); });
|
|
153
|
+
// // 删除云开发数据库集合
|
|
154
|
+
// server.tool(
|
|
155
|
+
// "deleteCollection",
|
|
156
|
+
// "删除一个云开发数据库集合",
|
|
157
|
+
// {
|
|
158
|
+
// collectionName: z.string().describe("云开发数据库集合名称")
|
|
159
|
+
// },
|
|
160
|
+
// async ({ collectionName }) => {
|
|
161
|
+
// try {
|
|
162
|
+
// const result = await cloudbase.database.deleteCollection(collectionName);
|
|
163
|
+
// return {
|
|
164
|
+
// content: [
|
|
165
|
+
// {
|
|
166
|
+
// type: "text",
|
|
167
|
+
// text: JSON.stringify({
|
|
168
|
+
// success: true,
|
|
169
|
+
// requestId: result.RequestId,
|
|
170
|
+
// exists: result.Exists,
|
|
171
|
+
// message: "云开发数据库集合删除成功"
|
|
172
|
+
// }, null, 2)
|
|
173
|
+
// }
|
|
174
|
+
// ]
|
|
175
|
+
// };
|
|
176
|
+
// } catch (error: any) {
|
|
177
|
+
// return {
|
|
178
|
+
// content: [
|
|
179
|
+
// {
|
|
180
|
+
// type: "text",
|
|
181
|
+
// text: JSON.stringify({
|
|
182
|
+
// success: false,
|
|
183
|
+
// error: error.message,
|
|
184
|
+
// message: "云开发数据库集合删除失败"
|
|
185
|
+
// }, null, 2)
|
|
186
|
+
// }
|
|
187
|
+
// ]
|
|
188
|
+
// };
|
|
189
|
+
// }
|
|
190
|
+
// }
|
|
191
|
+
// );
|
|
192
|
+
// 更新云开发数据库集合(创建/删除索引)
|
|
193
|
+
server.tool("updateCollection", "更新云开发数据库集合配置(创建或删除索引)", {
|
|
194
|
+
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
195
|
+
options: z.object({
|
|
196
|
+
CreateIndexes: z.array(z.object({
|
|
197
|
+
IndexName: z.string(),
|
|
198
|
+
MgoKeySchema: z.object({
|
|
199
|
+
MgoIsUnique: z.boolean(),
|
|
200
|
+
MgoIndexKeys: z.array(z.object({
|
|
201
|
+
Name: z.string(),
|
|
202
|
+
Direction: z.string()
|
|
203
|
+
}))
|
|
204
|
+
})
|
|
205
|
+
})).optional(),
|
|
206
|
+
DropIndexes: z.array(z.object({
|
|
207
|
+
IndexName: z.string()
|
|
208
|
+
})).optional()
|
|
209
|
+
}).describe("更新选项,支持创建和删除索引")
|
|
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
|
+
}); });
|
|
250
|
+
// 查询云开发数据库集合详情
|
|
251
|
+
server.tool("describeCollection", "获取云开发数据库集合的详细信息", {
|
|
252
|
+
collectionName: z.string().describe("云开发数据库集合名称")
|
|
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
|
+
}); });
|
|
295
|
+
// 获取云开发数据库集合列表
|
|
296
|
+
server.tool("listCollections", "获取云开发数据库集合列表", {
|
|
297
|
+
offset: z.number().optional().describe("偏移量"),
|
|
298
|
+
limit: z.number().optional().describe("返回数量限制")
|
|
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
|
+
}); });
|
|
344
|
+
// 检查索引是否存在
|
|
345
|
+
server.tool("checkIndexExists", "检查索引是否存在", {
|
|
346
|
+
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
347
|
+
indexName: z.string().describe("索引名称")
|
|
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
|
+
}); });
|
|
389
|
+
// // 导入数据
|
|
390
|
+
// server.tool(
|
|
391
|
+
// "importData",
|
|
392
|
+
// "导入数据到云开发数据库集合中",
|
|
393
|
+
// {
|
|
394
|
+
// collectionName: z.string().describe("云开发数据库集合名称"),
|
|
395
|
+
// file: z.object({
|
|
396
|
+
// ObjectKey: z.string().optional().describe("对象存储中的文件路径"),
|
|
397
|
+
// FilePath: z.string().optional().describe("本地文件路径")
|
|
398
|
+
// }).describe("导入文件信息"),
|
|
399
|
+
// options: z.object({
|
|
400
|
+
// FileType: z.enum(["csv", "json"]).optional().describe("文件类型"),
|
|
401
|
+
// StopOnError: z.boolean().optional().describe("遇到错误时是否停止导入"),
|
|
402
|
+
// ConflictMode: z.enum(["insert", "upsert"]).optional().describe("冲突处理方式")
|
|
403
|
+
// }).optional().describe("导入选项")
|
|
404
|
+
// },
|
|
405
|
+
// async ({ collectionName, file, options }) => {
|
|
406
|
+
// try {
|
|
407
|
+
// const result = await cloudbase.database.import(collectionName, file, options);
|
|
408
|
+
// return {
|
|
409
|
+
// content: [
|
|
410
|
+
// {
|
|
411
|
+
// type: "text",
|
|
412
|
+
// text: JSON.stringify({
|
|
413
|
+
// success: true,
|
|
414
|
+
// requestId: result.RequestId,
|
|
415
|
+
// jobId: result.JobId,
|
|
416
|
+
// message: "数据导入任务创建成功"
|
|
417
|
+
// }, null, 2)
|
|
418
|
+
// }
|
|
419
|
+
// ]
|
|
420
|
+
// };
|
|
421
|
+
// } catch (error: any) {
|
|
422
|
+
// return {
|
|
423
|
+
// content: [
|
|
424
|
+
// {
|
|
425
|
+
// type: "text",
|
|
426
|
+
// text: JSON.stringify({
|
|
427
|
+
// success: false,
|
|
428
|
+
// error: error.message,
|
|
429
|
+
// message: "数据导入任务创建失败"
|
|
430
|
+
// }, null, 2)
|
|
431
|
+
// }
|
|
432
|
+
// ]
|
|
433
|
+
// };
|
|
434
|
+
// }
|
|
435
|
+
// }
|
|
436
|
+
// );
|
|
437
|
+
// // 导出数据
|
|
438
|
+
// server.tool(
|
|
439
|
+
// "exportData",
|
|
440
|
+
// "从云开发数据库集合中导出数据",
|
|
441
|
+
// {
|
|
442
|
+
// collectionName: z.string().describe("云开发数据库集合名称"),
|
|
443
|
+
// file: z.object({
|
|
444
|
+
// ObjectKey: z.string().describe("导出到对象存储的文件路径")
|
|
445
|
+
// }).describe("导出文件信息"),
|
|
446
|
+
// options: z.object({
|
|
447
|
+
// FileType: z.enum(["csv", "json"]).optional().describe("文件类型"),
|
|
448
|
+
// Query: z.string().optional().describe("查询条件(JSON字符串)"),
|
|
449
|
+
// Sort: z.string().optional().describe("排序条件(JSON字符串)"),
|
|
450
|
+
// Skip: z.number().optional().describe("跳过的记录数"),
|
|
451
|
+
// Limit: z.number().optional().describe("限制返回的记录数"),
|
|
452
|
+
// Fields: z.string().optional().describe("导出字段列表,以逗号分隔")
|
|
453
|
+
// }).optional().describe("导出选项")
|
|
454
|
+
// },
|
|
455
|
+
// async ({ collectionName, file, options }) => {
|
|
456
|
+
// try {
|
|
457
|
+
// const result = await cloudbase.database.export(collectionName, file, options);
|
|
458
|
+
// return {
|
|
459
|
+
// content: [
|
|
460
|
+
// {
|
|
461
|
+
// type: "text",
|
|
462
|
+
// text: JSON.stringify({
|
|
463
|
+
// success: true,
|
|
464
|
+
// requestId: result.RequestId,
|
|
465
|
+
// jobId: result.JobId,
|
|
466
|
+
// message: "数据导出任务创建成功"
|
|
467
|
+
// }, null, 2)
|
|
468
|
+
// }
|
|
469
|
+
// ]
|
|
470
|
+
// };
|
|
471
|
+
// } catch (error: any) {
|
|
472
|
+
// return {
|
|
473
|
+
// content: [
|
|
474
|
+
// {
|
|
475
|
+
// type: "text",
|
|
476
|
+
// text: JSON.stringify({
|
|
477
|
+
// success: false,
|
|
478
|
+
// error: error.message,
|
|
479
|
+
// message: "数据导出任务创建失败"
|
|
480
|
+
// }, null, 2)
|
|
481
|
+
// }
|
|
482
|
+
// ]
|
|
483
|
+
// };
|
|
484
|
+
// }
|
|
485
|
+
// }
|
|
486
|
+
// );
|
|
487
|
+
// // 查询迁移状态
|
|
488
|
+
// server.tool(
|
|
489
|
+
// "migrateStatus",
|
|
490
|
+
// "查询数据迁移(导入/导出)任务的状态",
|
|
491
|
+
// {
|
|
492
|
+
// jobId: z.number().describe("任务ID")
|
|
493
|
+
// },
|
|
494
|
+
// async ({ jobId }) => {
|
|
495
|
+
// try {
|
|
496
|
+
// const result = await cloudbase.database.migrateStatus(jobId);
|
|
497
|
+
// return {
|
|
498
|
+
// content: [
|
|
499
|
+
// {
|
|
500
|
+
// type: "text",
|
|
501
|
+
// text: JSON.stringify({
|
|
502
|
+
// success: true,
|
|
503
|
+
// requestId: result.RequestId,
|
|
504
|
+
// status: result.Status,
|
|
505
|
+
// recordSuccess: result.RecordSuccess,
|
|
506
|
+
// recordFail: result.RecordFail,
|
|
507
|
+
// errorMsg: result.ErrorMsg,
|
|
508
|
+
// fileUrl: result.FileUrl,
|
|
509
|
+
// message: "获取迁移状态成功"
|
|
510
|
+
// }, null, 2)
|
|
511
|
+
// }
|
|
512
|
+
// ]
|
|
513
|
+
// };
|
|
514
|
+
// } catch (error: any) {
|
|
515
|
+
// return {
|
|
516
|
+
// content: [
|
|
517
|
+
// {
|
|
518
|
+
// type: "text",
|
|
519
|
+
// text: JSON.stringify({
|
|
520
|
+
// success: false,
|
|
521
|
+
// error: error.message,
|
|
522
|
+
// message: "获取迁移状态失败"
|
|
523
|
+
// }, null, 2)
|
|
524
|
+
// }
|
|
525
|
+
// ]
|
|
526
|
+
// };
|
|
527
|
+
// }
|
|
528
|
+
// }
|
|
529
|
+
// );
|
|
530
|
+
// 查询数据分布
|
|
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
|
+
}); });
|
|
571
|
+
// 插入文档
|
|
572
|
+
server.tool("insertDocuments", "向云开发数据库集合中插入一个或多个文档", {
|
|
573
|
+
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
574
|
+
documents: z.array(z.string()).describe("要插入的文档数组,每个文档都是JSON字符串")
|
|
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
|
+
}); });
|
|
626
|
+
// 查询文档
|
|
627
|
+
server.tool("queryDocuments", "查询云开发数据库集合中的文档", {
|
|
628
|
+
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
629
|
+
query: z.string().optional().describe("查询条件(JSON字符串)"),
|
|
630
|
+
projection: z.string().optional().describe("返回字段投影(JSON字符串)"),
|
|
631
|
+
sort: z.string().optional().describe("排序条件(JSON字符串)"),
|
|
632
|
+
limit: z.number().optional().describe("返回数量限制"),
|
|
633
|
+
offset: z.number().optional().describe("跳过的记录数")
|
|
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
|
+
}); });
|
|
690
|
+
// 更新文档
|
|
691
|
+
server.tool("updateDocuments", "更新云开发数据库集合中的文档", {
|
|
692
|
+
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
693
|
+
query: z.string().describe("查询条件(JSON字符串)"),
|
|
694
|
+
update: z.string().describe("更新内容(JSON字符串)"),
|
|
695
|
+
isMulti: z.boolean().optional().describe("是否更新多条记录"),
|
|
696
|
+
upsert: z.boolean().optional().describe("是否在不存在时插入")
|
|
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
|
+
}); });
|
|
753
|
+
// 删除文档
|
|
754
|
+
server.tool("deleteDocuments", "删除云开发数据库集合中的文档", {
|
|
755
|
+
collectionName: z.string().describe("云开发数据库集合名称"),
|
|
756
|
+
query: z.string().describe("查询条件(JSON字符串)"),
|
|
757
|
+
isMulti: z.boolean().optional().describe("是否删除多条记录")
|
|
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
|
+
}); });
|
|
810
|
+
}
|