-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBatchApi.js
More file actions
133 lines (124 loc) · 4.52 KB
/
testBatchApi.js
File metadata and controls
133 lines (124 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import BosBase from "bosbase";
const baseUrl = process.env.BOSBASE_BASE_URL ?? "http://127.0.0.1:8090";
const authEmail =
process.env.BOSBASE_EMAIL ??
process.env.BOSBASE_SUPERUSER_EMAIL ??
"try@bosbase.com";
const authPassword =
process.env.BOSBASE_PASSWORD ??
process.env.BOSBASE_SUPERUSER_PASSWORD ??
"bosbasepass";
async function main() {
let pb;
let collectionName;
let originalBatchSettings;
let batchSettingsChanged = false;
try {
console.log("[INFO] BATCH_API doc test starting...");
pb = new BosBase(baseUrl);
await pb.collection("_superusers").authWithPassword(authEmail, authPassword);
console.log("[SUCCESS] Authenticated as superuser");
originalBatchSettings = await pb.settings.getCategory("batch");
const maxRequests =
originalBatchSettings?.maxRequests && originalBatchSettings.maxRequests > 0
? originalBatchSettings.maxRequests
: 50;
const timeout =
originalBatchSettings?.timeout && originalBatchSettings.timeout > 0
? originalBatchSettings.timeout
: 3;
if (!originalBatchSettings?.enabled || maxRequests <= 0) {
await pb.settings.updateBatch({
enabled: true,
maxRequests,
timeout,
maxBodySize: originalBatchSettings?.maxBodySize ?? 0,
});
batchSettingsChanged = true;
console.log("[INFO] Enabled batch API for test run");
} else {
console.log("[INFO] Batch API already enabled");
}
collectionName = `batch_demo_${Date.now()}`;
await pb.collections.create({
name: collectionName,
type: "base",
schema: [
{ name: "title", type: "text", required: true },
{ name: "flag", type: "bool" },
],
});
console.log(`[SUCCESS] Collection "${collectionName}" created`);
const createBatch = pb.createBatch();
createBatch.collection(collectionName).create({ title: "First", flag: true });
createBatch.collection(collectionName).create({ title: "Second", flag: false });
const createResults = await createBatch.send();
const createdIds = (createResults || [])
.map((entry) => entry?.body?.id)
.filter(Boolean);
console.log("[SUCCESS] Batch created records:", createdIds.join(", ") || "none");
if (createdIds.length < 2) {
throw new Error("Batch create did not return at least 2 record ids");
}
const updateBatch = pb.createBatch();
updateBatch
.collection(collectionName)
.update(createdIds[0], { title: "Updated First" });
updateBatch
.collection(collectionName)
.upsert({ id: createdIds[0], flag: false });
updateBatch.collection(collectionName).delete(createdIds[1]);
const updateResults = await updateBatch.send();
console.log(
"[SUCCESS] Batch update/upsert/delete statuses:",
(updateResults || []).map((r) => r?.status).join(", "),
);
const list = await pb.collection(collectionName).getList(1, 10, {
sort: "-created",
});
const remainingTitles = list?.items?.map((item) => item.title) ?? [];
console.log("[SUCCESS] Records after batch:", remainingTitles.join(", ") || "none");
console.log("\n========== BATCH_API doc test completed ==========");
} catch (error) {
console.error("[ERROR] BATCH_API doc test failed:");
if (error?.response) {
console.error("Status:", error.response.status);
console.error("Data:", JSON.stringify(error.response.data, null, 2));
if (error.response.data?.message) {
console.error("Message:", error.response.data.message);
}
} else {
console.error(error);
}
process.exit(1);
} finally {
if (pb && collectionName) {
try {
await pb.collections.delete(collectionName);
console.log(`[CLEANUP] Collection "${collectionName}" deleted`);
} catch (cleanupError) {
console.warn(
"[WARN] Failed to delete test collection:",
cleanupError?.message || cleanupError,
);
}
}
if (pb && batchSettingsChanged && originalBatchSettings) {
try {
await pb.settings.updateBatch({
enabled: !!originalBatchSettings.enabled,
maxRequests: originalBatchSettings.maxRequests,
timeout: originalBatchSettings.timeout,
maxBodySize: originalBatchSettings.maxBodySize,
});
console.log("[CLEANUP] Batch settings restored");
} catch (batchCleanupError) {
console.warn(
"[WARN] Failed to restore batch settings:",
batchCleanupError?.message || batchCleanupError,
);
}
}
}
}
main();