@terreno/api 58.0.6 → 58.1.0
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/CHANGELOG.md +52 -0
- package/dist/actions.d.ts +6 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +57 -32
- package/dist/bulk.d.ts +79 -0
- package/dist/bulk.js +449 -0
- package/dist/bulkOpenApi.d.ts +15 -0
- package/dist/bulkOpenApi.js +103 -0
- package/dist/bulkTransaction.d.ts +33 -0
- package/dist/bulkTransaction.js +126 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +8 -6
- package/dist/index.d.ts +3 -2
- package/dist/index.js +6 -3
- package/dist/mcp/actionTools.js +2 -0
- package/dist/mcp/grantAccess.d.ts +27 -0
- package/dist/mcp/grantAccess.js +29 -0
- package/dist/mcp/oauth.js +86 -5
- package/dist/mcp/oauthMetadata.d.ts +32 -1
- package/dist/mcp/oauthMetadata.js +53 -2
- package/dist/mcp/registry.d.ts +8 -1
- package/dist/mcp/server.js +6 -1
- package/dist/mcp/toolGenerator.d.ts +5 -1
- package/dist/mcp/toolGenerator.js +11 -0
- package/dist/mcp/types.d.ts +11 -0
- package/dist/modelRouterWrites.d.ts +23 -0
- package/dist/modelRouterWrites.js +54 -0
- package/dist/openApi.d.ts +7 -0
- package/dist/openApi.js +6 -6
- package/dist/permissions.js +5 -4
- package/dist/rbac/access.js +2 -0
- package/dist/rbac/routes.js +1 -0
- package/dist/rbac/statements.d.ts +12 -1
- package/dist/rbac/statements.js +89 -1
- package/dist/rbac/types.d.ts +9 -2
- package/dist/sync/executors.d.ts +6 -4
- package/dist/sync/executors.js +6 -2
- package/package.json +6 -6
- package/src/actions.ts +6 -0
- package/src/api.ts +95 -33
- package/src/bulk.test.ts +1256 -0
- package/src/bulk.ts +680 -0
- package/src/bulkOpenApi.test.ts +172 -0
- package/src/bulkOpenApi.ts +126 -0
- package/src/bulkTransaction.test.ts +197 -0
- package/src/bulkTransaction.ts +149 -0
- package/src/errors.ts +2 -2
- package/src/index.ts +6 -0
- package/src/isolated/bulkAtomic.isolated.ts +630 -0
- package/src/isolated/bulkStandalone.isolated.ts +133 -0
- package/src/isolated/mongoTopology.isolated.ts +29 -0
- package/src/mcp/actionTools.ts +2 -0
- package/src/mcp/grantAccess.test.ts +107 -0
- package/src/mcp/grantAccess.ts +49 -0
- package/src/mcp/oauth.test.ts +327 -0
- package/src/mcp/oauth.ts +113 -5
- package/src/mcp/oauthMetadata.test.ts +125 -1
- package/src/mcp/oauthMetadata.ts +99 -2
- package/src/mcp/registry.ts +8 -1
- package/src/mcp/server.test.ts +419 -2
- package/src/mcp/server.ts +8 -1
- package/src/mcp/toolGenerator.ts +17 -0
- package/src/mcp/types.ts +12 -0
- package/src/modelRouterWrites.ts +72 -0
- package/src/openApi.ts +1 -1
- package/src/permissions.ts +5 -5
- package/src/rbac/access.ts +3 -1
- package/src/rbac/routes.test.ts +15 -0
- package/src/rbac/routes.ts +1 -0
- package/src/rbac/statements.test.ts +23 -0
- package/src/rbac/statements.ts +100 -1
- package/src/rbac/types.ts +9 -2
- package/src/sync/executors.test.ts +27 -0
- package/src/sync/executors.ts +12 -4
- package/src/tests/mongoTopology.ts +53 -0
package/dist/bulk.js
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerBulkRoute = exports.toBulkItemError = exports.assertNoBulkCollision = void 0;
|
|
4
|
+
const record_1 = require("./audit/record");
|
|
5
|
+
const bulkOpenApi_1 = require("./bulkOpenApi");
|
|
6
|
+
const bulkTransaction_1 = require("./bulkTransaction");
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
const modelRouterWrites_1 = require("./modelRouterWrites");
|
|
9
|
+
/** Default cap on operations per bulk request, matching the sync batch limit. */
|
|
10
|
+
const DEFAULT_BULK_MAX_ITEMS = 100;
|
|
11
|
+
const BULK_METHODS = ["create", "update", "delete"];
|
|
12
|
+
/** Resolves `ModelRouterOptions.bulk` to concrete limits, or null when bulk is off. */
|
|
13
|
+
const resolveBulkOptions = (bulk) => {
|
|
14
|
+
if (!bulk) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const config = bulk === true ? {} : bulk;
|
|
18
|
+
return {
|
|
19
|
+
maxItems: config.maxItems ?? DEFAULT_BULK_MAX_ITEMS,
|
|
20
|
+
methods: config.methods ?? BULK_METHODS,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Throws at registration when bulk is enabled and a collection action already owns `/bulk`.
|
|
25
|
+
* Actions mount before CRUD and would silently shadow the bulk route.
|
|
26
|
+
*/
|
|
27
|
+
const assertNoBulkCollision = (options) => {
|
|
28
|
+
if (!resolveBulkOptions(options.bulk)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (options.collectionActions && Object.hasOwn(options.collectionActions, "bulk")) {
|
|
32
|
+
throw new errors_1.APIError({
|
|
33
|
+
detail: "Rename the collection action or turn off `bulk` on this router.",
|
|
34
|
+
status: 500,
|
|
35
|
+
title: "collectionAction 'bulk' collides with the bulk endpoint on /bulk",
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.assertNoBulkCollision = assertNoBulkCollision;
|
|
40
|
+
const envelopeError = (detail) => new errors_1.APIError({ code: "bulk-invalid-request", detail, status: 400, title: "Invalid bulk request" });
|
|
41
|
+
const isPlainObject = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
42
|
+
const parseOperation = (raw, index, config) => {
|
|
43
|
+
if (!isPlainObject(raw)) {
|
|
44
|
+
throw envelopeError(`operations[${index}] must be an object`);
|
|
45
|
+
}
|
|
46
|
+
const method = raw.method;
|
|
47
|
+
if (typeof method !== "string" || !BULK_METHODS.includes(method)) {
|
|
48
|
+
throw envelopeError(`operations[${index}].method must be one of ${BULK_METHODS.join(", ")}`);
|
|
49
|
+
}
|
|
50
|
+
if (!config.methods.includes(method)) {
|
|
51
|
+
throw envelopeError(`operations[${index}].method '${method}' is not enabled for bulk`);
|
|
52
|
+
}
|
|
53
|
+
if (method !== "create" && (typeof raw.id !== "string" || raw.id.length === 0)) {
|
|
54
|
+
throw envelopeError(`operations[${index}].id is required for ${method}`);
|
|
55
|
+
}
|
|
56
|
+
if (method !== "delete" && !isPlainObject(raw.body)) {
|
|
57
|
+
throw envelopeError(`operations[${index}].body must be an object for ${method}`);
|
|
58
|
+
}
|
|
59
|
+
if (method === "create") {
|
|
60
|
+
return { body: raw.body, method };
|
|
61
|
+
}
|
|
62
|
+
if (method === "update") {
|
|
63
|
+
return { body: raw.body, id: raw.id, method };
|
|
64
|
+
}
|
|
65
|
+
return { id: raw.id, method: "delete" };
|
|
66
|
+
};
|
|
67
|
+
/** Parses and checks the whole envelope before any write. Throws a 400 on any defect. */
|
|
68
|
+
const parseBulkRequest = (body, config) => {
|
|
69
|
+
if (!isPlainObject(body)) {
|
|
70
|
+
throw envelopeError("Body must be an object with an operations array");
|
|
71
|
+
}
|
|
72
|
+
if (body.atomic !== undefined && typeof body.atomic !== "boolean") {
|
|
73
|
+
throw envelopeError("atomic must be a boolean");
|
|
74
|
+
}
|
|
75
|
+
const operations = body.operations;
|
|
76
|
+
if (!Array.isArray(operations)) {
|
|
77
|
+
throw envelopeError("operations must be an array");
|
|
78
|
+
}
|
|
79
|
+
if (operations.length === 0) {
|
|
80
|
+
throw envelopeError("operations must not be empty");
|
|
81
|
+
}
|
|
82
|
+
if (operations.length > config.maxItems) {
|
|
83
|
+
throw envelopeError(`operations may hold at most ${config.maxItems} items`);
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
atomic: body.atomic ?? true,
|
|
87
|
+
operations: operations.map((raw, index) => parseOperation(raw, index, config)),
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* @internal Exported for tests.
|
|
92
|
+
* Maps any error from one item to the status and JSON body the single route's error middleware
|
|
93
|
+
* would send, and logs/captures it the same way.
|
|
94
|
+
*/
|
|
95
|
+
const toBulkItemError = (error) => {
|
|
96
|
+
let apiError = (0, errors_1.isAPIError)(error) ? error : null;
|
|
97
|
+
if (!apiError && error instanceof Error) {
|
|
98
|
+
apiError = (0, errors_1.mongooseErrorToAPIError)(error);
|
|
99
|
+
}
|
|
100
|
+
if (!apiError) {
|
|
101
|
+
// Same client body as apiFallthroughErrorMiddleware: no internal message leaks to the caller.
|
|
102
|
+
apiError = new errors_1.APIError({ cause: error, status: 500, title: "Internal server error" });
|
|
103
|
+
}
|
|
104
|
+
(0, errors_1.logAndCaptureAPIError)(apiError);
|
|
105
|
+
return { body: (0, errors_1.serializeAPIError)(apiError), status: apiError.status };
|
|
106
|
+
};
|
|
107
|
+
exports.toBulkItemError = toBulkItemError;
|
|
108
|
+
/** The request a hook sees for one item: the real request with this item's body and id. */
|
|
109
|
+
const buildItemRequest = (req, body, id) => {
|
|
110
|
+
const itemReq = Object.create(req);
|
|
111
|
+
itemReq.body = body;
|
|
112
|
+
itemReq.params = id ? { ...req.params, id } : { ...req.params };
|
|
113
|
+
return itemReq;
|
|
114
|
+
};
|
|
115
|
+
const serializeItem = async (ctx, doc, method, itemReq) => {
|
|
116
|
+
try {
|
|
117
|
+
return await ctx.responseHandler(doc, method, itemReq, ctx.options);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
121
|
+
code: "response-handler-error",
|
|
122
|
+
title: "responseHandler error",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
// Method permission before body validation, the same order as the single routes
|
|
127
|
+
// (permissionMiddleware, then validation), so an unpermitted caller gets 405, not schema details.
|
|
128
|
+
const assertMethodPermission = async (ctx, method) => {
|
|
129
|
+
if (await ctx.deps.checkPermissions(method, ctx.options.permissions[method], ctx.req.user)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
throw (0, modelRouterWrites_1.methodPermissionDeniedError)({
|
|
133
|
+
method,
|
|
134
|
+
modelName: ctx.model.modelName,
|
|
135
|
+
userId: ctx.req.user?.id,
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
const hookErrorResult = (error, wrapper, result) => {
|
|
139
|
+
const { body, status } = (0, exports.toBulkItemError)((0, errors_1.passthroughOrWrap)(error, { ...wrapper, status: 400 }));
|
|
140
|
+
return { ...result, error: body, status };
|
|
141
|
+
};
|
|
142
|
+
const writeCreateItem = async (ctx, operation, index) => {
|
|
143
|
+
await assertMethodPermission(ctx, "create");
|
|
144
|
+
const itemReq = buildItemRequest(ctx.req, operation.body);
|
|
145
|
+
const body = ctx.validateBody({ body: operation.body, method: "create", req: itemReq });
|
|
146
|
+
itemReq.body = body;
|
|
147
|
+
const { doc } = await ctx.deps.executeCreate({
|
|
148
|
+
body,
|
|
149
|
+
model: ctx.model,
|
|
150
|
+
options: ctx.options,
|
|
151
|
+
req: itemReq,
|
|
152
|
+
skipPostHooks: true,
|
|
153
|
+
user: ctx.req.user,
|
|
154
|
+
});
|
|
155
|
+
const id = String(doc._id);
|
|
156
|
+
const finish = async () => {
|
|
157
|
+
try {
|
|
158
|
+
await ctx.deps.runPostCreate({ doc, options: ctx.options, request: itemReq });
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
return hookErrorResult(error, { code: "post-create-hook-error", title: "postCreate hook error" }, { id, index, method: "create" });
|
|
162
|
+
}
|
|
163
|
+
void (0, record_1.maybeRecordModelRouterAudit)({
|
|
164
|
+
after: doc,
|
|
165
|
+
audit: ctx.options.audit,
|
|
166
|
+
modelName: ctx.model.modelName,
|
|
167
|
+
operation: "create",
|
|
168
|
+
recordId: id,
|
|
169
|
+
req: itemReq,
|
|
170
|
+
verb: "created",
|
|
171
|
+
});
|
|
172
|
+
try {
|
|
173
|
+
const data = await serializeItem(ctx, doc, "create", itemReq);
|
|
174
|
+
return { data, id, index, method: "create", status: 201 };
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
// The doc is written: keep its id so the client knows what exists before any retry.
|
|
178
|
+
return { ...failedResult(error, "create", index), id };
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
return { finish };
|
|
182
|
+
};
|
|
183
|
+
const writeUpdateItem = async (ctx, operation, index) => {
|
|
184
|
+
const { id } = operation;
|
|
185
|
+
await assertMethodPermission(ctx, "update");
|
|
186
|
+
const itemReq = buildItemRequest(ctx.req, operation.body, id);
|
|
187
|
+
const body = ctx.validateBody({ body: operation.body, method: "update", req: itemReq });
|
|
188
|
+
itemReq.body = body;
|
|
189
|
+
let result;
|
|
190
|
+
try {
|
|
191
|
+
result = await ctx.deps.executeUpdate({
|
|
192
|
+
body,
|
|
193
|
+
concurrencyCheck: (0, modelRouterWrites_1.timestampConcurrencyCheck)({ bodyUpdatedAt: body._updatedAt }),
|
|
194
|
+
id,
|
|
195
|
+
model: ctx.model,
|
|
196
|
+
options: ctx.options,
|
|
197
|
+
req: itemReq,
|
|
198
|
+
skipPostHooks: true,
|
|
199
|
+
user: ctx.req.user,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (!ctx.deps.isExecutorConflictError(error)) {
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
// Exactly the body PATCH /:id sends for a conflict, including the current server doc.
|
|
207
|
+
const data = await serializeItem(ctx, error.doc, "update", itemReq);
|
|
208
|
+
return {
|
|
209
|
+
refused: {
|
|
210
|
+
error: (0, modelRouterWrites_1.conflictResponseBody)(data),
|
|
211
|
+
id,
|
|
212
|
+
index,
|
|
213
|
+
method: "update",
|
|
214
|
+
status: 409,
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const { doc, cleanedBody, prevDoc } = result;
|
|
219
|
+
const finish = async () => {
|
|
220
|
+
try {
|
|
221
|
+
await ctx.deps.runPostUpdate({
|
|
222
|
+
cleanedBody: cleanedBody,
|
|
223
|
+
doc,
|
|
224
|
+
options: ctx.options,
|
|
225
|
+
prevDoc: prevDoc,
|
|
226
|
+
request: itemReq,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
return hookErrorResult(error, {
|
|
231
|
+
code: "post-update-hook-error",
|
|
232
|
+
detail: `postUpdate hook error on ${id}: ${(0, errors_1.errorMessage)(error)}`,
|
|
233
|
+
title: "postUpdate hook error",
|
|
234
|
+
}, { id, index, method: "update" });
|
|
235
|
+
}
|
|
236
|
+
void (0, record_1.maybeRecordModelRouterAudit)({
|
|
237
|
+
after: doc,
|
|
238
|
+
audit: ctx.options.audit,
|
|
239
|
+
before: (0, record_1.snapshotAuditBefore)({
|
|
240
|
+
audit: ctx.options.audit,
|
|
241
|
+
doc: prevDoc,
|
|
242
|
+
}),
|
|
243
|
+
modelName: ctx.model.modelName,
|
|
244
|
+
operation: "update",
|
|
245
|
+
recordId: String(doc._id),
|
|
246
|
+
req: itemReq,
|
|
247
|
+
verb: "updated",
|
|
248
|
+
});
|
|
249
|
+
try {
|
|
250
|
+
const data = await serializeItem(ctx, doc, "update", itemReq);
|
|
251
|
+
return { data, id, index, method: "update", status: 200 };
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
return { ...failedResult(error, "update", index), id };
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
return { finish };
|
|
258
|
+
};
|
|
259
|
+
const writeDeleteItem = async (ctx, operation, index) => {
|
|
260
|
+
const { id } = operation;
|
|
261
|
+
const itemReq = buildItemRequest(ctx.req, {}, id);
|
|
262
|
+
const { doc, prevDoc } = await ctx.deps.executeDelete({
|
|
263
|
+
id,
|
|
264
|
+
model: ctx.model,
|
|
265
|
+
options: ctx.options,
|
|
266
|
+
req: itemReq,
|
|
267
|
+
skipPostHooks: true,
|
|
268
|
+
user: ctx.req.user,
|
|
269
|
+
});
|
|
270
|
+
if (!prevDoc) {
|
|
271
|
+
// executeDelete treats an already soft-deleted doc as an idempotent success for the sync
|
|
272
|
+
// channel (and then returns no prevDoc). DELETE /:id answers 404 there, so bulk does too:
|
|
273
|
+
// loading the tombstone through the normal loader throws that same 404.
|
|
274
|
+
await ctx.deps.loadDocOr404(ctx.model, id, ctx.options.populatePaths);
|
|
275
|
+
}
|
|
276
|
+
const finish = async () => {
|
|
277
|
+
try {
|
|
278
|
+
await ctx.deps.runPostDelete({ doc, options: ctx.options, request: itemReq });
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
return hookErrorResult(error, { code: "post-delete-hook-error", title: "postDelete hook error" }, { id, index, method: "delete" });
|
|
282
|
+
}
|
|
283
|
+
void (0, record_1.maybeRecordModelRouterAudit)({
|
|
284
|
+
audit: ctx.options.audit,
|
|
285
|
+
before: (0, record_1.snapshotAuditBefore)({
|
|
286
|
+
audit: ctx.options.audit,
|
|
287
|
+
doc: (prevDoc ?? doc),
|
|
288
|
+
}),
|
|
289
|
+
modelName: ctx.model.modelName,
|
|
290
|
+
operation: "delete",
|
|
291
|
+
recordId: String(doc._id),
|
|
292
|
+
req: itemReq,
|
|
293
|
+
verb: "deleted",
|
|
294
|
+
});
|
|
295
|
+
return { id, index, method: "delete", status: 204 };
|
|
296
|
+
};
|
|
297
|
+
return { finish };
|
|
298
|
+
};
|
|
299
|
+
const writeItem = (ctx, operation, index) => {
|
|
300
|
+
if (operation.method === "create") {
|
|
301
|
+
return writeCreateItem(ctx, operation, index);
|
|
302
|
+
}
|
|
303
|
+
if (operation.method === "update") {
|
|
304
|
+
return writeUpdateItem(ctx, operation, index);
|
|
305
|
+
}
|
|
306
|
+
return writeDeleteItem(ctx, operation, index);
|
|
307
|
+
};
|
|
308
|
+
const failedResult = (error, method, index) => {
|
|
309
|
+
const { body, status } = (0, exports.toBulkItemError)(error);
|
|
310
|
+
return { error: body, index, method, status };
|
|
311
|
+
};
|
|
312
|
+
const failedItemResult = (error, operation, index) => {
|
|
313
|
+
const result = failedResult(error, operation.method, index);
|
|
314
|
+
return operation.method === "create" ? result : { ...result, id: operation.id };
|
|
315
|
+
};
|
|
316
|
+
/** Partial mode: each item commits on its own; a failure is recorded and the batch continues. */
|
|
317
|
+
const runPartial = async (ctx, operations) => {
|
|
318
|
+
const results = [];
|
|
319
|
+
for (const [index, operation] of operations.entries()) {
|
|
320
|
+
try {
|
|
321
|
+
const written = await writeItem(ctx, operation, index);
|
|
322
|
+
results.push("refused" in written ? written.refused : await written.finish());
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
results.push(failedItemResult(error, operation, index));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return results;
|
|
329
|
+
};
|
|
330
|
+
/** Thrown inside the atomic transaction to abort it with one item's response. */
|
|
331
|
+
class BulkItemAbort extends Error {
|
|
332
|
+
result;
|
|
333
|
+
constructor(result) {
|
|
334
|
+
super("Bulk item failed");
|
|
335
|
+
this.result = result;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const transactionConflictError = (cause) => new errors_1.APIError({
|
|
339
|
+
cause,
|
|
340
|
+
code: "bulk-transaction-conflict",
|
|
341
|
+
detail: "Another write touched the same documents. Nothing was written; retry the request.",
|
|
342
|
+
status: 409,
|
|
343
|
+
title: "Bulk transaction conflict",
|
|
344
|
+
});
|
|
345
|
+
/**
|
|
346
|
+
* Atomic mode: every item's write runs in one transaction. The first failure aborts it and
|
|
347
|
+
* becomes the whole response (that item's status and body plus `meta.index`). After a commit,
|
|
348
|
+
* post hooks, audit, and `responseHandler` run per item, in order.
|
|
349
|
+
*/
|
|
350
|
+
const runAtomic = async (ctx, operations, res) => {
|
|
351
|
+
if (!(await (0, bulkTransaction_1.supportsTransactions)(ctx.model.db))) {
|
|
352
|
+
throw new errors_1.APIError({
|
|
353
|
+
code: "bulk-atomic-unsupported",
|
|
354
|
+
detail: "Atomic bulk mode requires a replica set; send atomic:false for per-item writes.",
|
|
355
|
+
status: 400,
|
|
356
|
+
title: "Atomic bulk writes are not supported by this database",
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
let written;
|
|
360
|
+
try {
|
|
361
|
+
written = await (0, bulkTransaction_1.runInBulkTransaction)({
|
|
362
|
+
base: ctx.model.base,
|
|
363
|
+
connection: ctx.model.db,
|
|
364
|
+
fn: async () => {
|
|
365
|
+
const items = [];
|
|
366
|
+
for (const [index, operation] of operations.entries()) {
|
|
367
|
+
let write;
|
|
368
|
+
try {
|
|
369
|
+
write = await writeItem(ctx, operation, index);
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
if ((0, bulkTransaction_1.isTransientTransactionError)(error)) {
|
|
373
|
+
throw error;
|
|
374
|
+
}
|
|
375
|
+
throw new BulkItemAbort(failedItemResult(error, operation, index));
|
|
376
|
+
}
|
|
377
|
+
if ("refused" in write) {
|
|
378
|
+
throw new BulkItemAbort(write.refused);
|
|
379
|
+
}
|
|
380
|
+
items.push(write);
|
|
381
|
+
}
|
|
382
|
+
return items;
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
if (error instanceof BulkItemAbort) {
|
|
388
|
+
const { error: body, index, status } = error.result;
|
|
389
|
+
const errorBody = (body ?? {});
|
|
390
|
+
const meta = (errorBody.meta ?? {});
|
|
391
|
+
res.status(status).json({ ...errorBody, meta: { ...meta, index } });
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if ((0, bulkTransaction_1.isTransientTransactionError)(error)) {
|
|
395
|
+
throw transactionConflictError(error);
|
|
396
|
+
}
|
|
397
|
+
if ((0, bulkTransaction_1.isBulkCommitUnknownError)(error)) {
|
|
398
|
+
throw new errors_1.APIError({
|
|
399
|
+
cause: error,
|
|
400
|
+
code: "bulk-commit-unknown",
|
|
401
|
+
detail: "The database did not confirm the commit. The writes may or may not have been applied; " +
|
|
402
|
+
"read the documents before retrying.",
|
|
403
|
+
status: 500,
|
|
404
|
+
title: "Bulk transaction outcome unknown",
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
// The writes are committed: one item's post-commit failure (e.g. its responseHandler) must not
|
|
410
|
+
// hide the others' results or skip their post hooks and audit.
|
|
411
|
+
const results = [];
|
|
412
|
+
for (const [position, item] of written.entries()) {
|
|
413
|
+
try {
|
|
414
|
+
results.push(await item.finish());
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
results.push(failedItemResult(error, operations[position], position));
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
res.json({ data: results });
|
|
421
|
+
};
|
|
422
|
+
/**
|
|
423
|
+
* Mounts `POST /bulk` on a modelRouter's router when `options.bulk` is set. Call with the
|
|
424
|
+
* collection actions, before user `endpoints` and the `/:id` routes.
|
|
425
|
+
*/
|
|
426
|
+
const registerBulkRoute = ({ deps, router, model, options, responseHandler, validateBody, }) => {
|
|
427
|
+
const config = resolveBulkOptions(options.bulk);
|
|
428
|
+
if (!config) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
// Atomic requests join every Mongoose operation (hook writes included) to their transaction.
|
|
432
|
+
(0, bulkTransaction_1.enableTransactionAsyncLocalStorage)(model.base);
|
|
433
|
+
const ctxBase = { deps, model, options, responseHandler, validateBody };
|
|
434
|
+
router.post("/bulk", deps.authenticateMiddleware(options.allowAnonymous), (0, bulkOpenApi_1.bulkOpenApiMiddleware)({ model, options, ...config }), async (req, res, next) => {
|
|
435
|
+
try {
|
|
436
|
+
const request = parseBulkRequest(req.body, config);
|
|
437
|
+
const ctx = { ...ctxBase, req };
|
|
438
|
+
if (request.atomic) {
|
|
439
|
+
await runAtomic(ctx, request.operations, res);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
res.json({ data: await runPartial(ctx, request.operations) });
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
next(error);
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
exports.registerBulkRoute = registerBulkRoute;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI documentation for a modelRouter's `POST /bulk` (see `bulk.ts`), so generated SDKs
|
|
3
|
+
* (syncdb `--api-out`) get a typed hook for it.
|
|
4
|
+
*/
|
|
5
|
+
import type express from "express";
|
|
6
|
+
import type { Model } from "mongoose";
|
|
7
|
+
import type { ModelRouterOptions } from "./api";
|
|
8
|
+
import type { BulkMethod } from "./bulk";
|
|
9
|
+
/** Builds the OpenAPI middleware for `POST /bulk`, or a pass-through when OpenAPI is off. */
|
|
10
|
+
export declare const bulkOpenApiMiddleware: <T>({ model, options, maxItems, methods, }: {
|
|
11
|
+
model: Model<T>;
|
|
12
|
+
options: Partial<ModelRouterOptions<T>>;
|
|
13
|
+
maxItems: number;
|
|
14
|
+
methods: BulkMethod[];
|
|
15
|
+
}) => express.RequestHandler;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.bulkOpenApiMiddleware = void 0;
|
|
7
|
+
const merge_1 = __importDefault(require("lodash/merge"));
|
|
8
|
+
const openApi_1 = require("./openApi");
|
|
9
|
+
const populate_1 = require("./populate");
|
|
10
|
+
const passThrough = (_req, _res, next) => next();
|
|
11
|
+
/** Builds the OpenAPI middleware for `POST /bulk`, or a pass-through when OpenAPI is off. */
|
|
12
|
+
const bulkOpenApiMiddleware = ({ model, options, maxItems, methods, }) => {
|
|
13
|
+
if (!options.openApi?.path) {
|
|
14
|
+
return passThrough;
|
|
15
|
+
}
|
|
16
|
+
const tag = model.collection.collectionName;
|
|
17
|
+
const { properties, required } = (0, populate_1.getOpenApiSpecForModel)(model, {
|
|
18
|
+
extraModelProperties: options.openApiExtraModelProperties,
|
|
19
|
+
populatePaths: options.populatePaths,
|
|
20
|
+
});
|
|
21
|
+
const modelResponse = {
|
|
22
|
+
properties,
|
|
23
|
+
required: [...required, "_id", "created", "updated"],
|
|
24
|
+
type: "object",
|
|
25
|
+
};
|
|
26
|
+
return options.openApi.path((0, merge_1.default)({
|
|
27
|
+
description: "Run many create/update/delete operations through this router's permissions, hooks, " +
|
|
28
|
+
"and responseHandler. With atomic (default true) the batch commits or rolls back " +
|
|
29
|
+
"together; with atomic: false each item reports its own status.",
|
|
30
|
+
operationId: `${tag}_bulk`,
|
|
31
|
+
requestBody: {
|
|
32
|
+
content: {
|
|
33
|
+
"application/json": {
|
|
34
|
+
schema: {
|
|
35
|
+
properties: {
|
|
36
|
+
atomic: {
|
|
37
|
+
default: true,
|
|
38
|
+
description: "Commit every operation in one transaction (requires a replica set)",
|
|
39
|
+
type: "boolean",
|
|
40
|
+
},
|
|
41
|
+
operations: {
|
|
42
|
+
items: {
|
|
43
|
+
properties: {
|
|
44
|
+
body: {
|
|
45
|
+
description: "Document fields for create and update",
|
|
46
|
+
properties,
|
|
47
|
+
type: "object",
|
|
48
|
+
},
|
|
49
|
+
id: { description: "Document id for update and delete", type: "string" },
|
|
50
|
+
method: { enum: methods, type: "string" },
|
|
51
|
+
},
|
|
52
|
+
required: ["method"],
|
|
53
|
+
type: "object",
|
|
54
|
+
},
|
|
55
|
+
maxItems,
|
|
56
|
+
minItems: 1,
|
|
57
|
+
type: "array",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
required: ["operations"],
|
|
61
|
+
type: "object",
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
required: true,
|
|
66
|
+
},
|
|
67
|
+
responses: {
|
|
68
|
+
200: {
|
|
69
|
+
content: {
|
|
70
|
+
"application/json": {
|
|
71
|
+
schema: {
|
|
72
|
+
properties: {
|
|
73
|
+
data: {
|
|
74
|
+
items: {
|
|
75
|
+
properties: {
|
|
76
|
+
data: modelResponse,
|
|
77
|
+
error: { $ref: "#/components/schemas/APIError" },
|
|
78
|
+
id: { type: "string" },
|
|
79
|
+
index: { type: "number" },
|
|
80
|
+
method: { enum: methods, type: "string" },
|
|
81
|
+
status: { type: "number" },
|
|
82
|
+
},
|
|
83
|
+
required: ["index", "method", "status"],
|
|
84
|
+
type: "object",
|
|
85
|
+
},
|
|
86
|
+
type: "array",
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
required: ["data"],
|
|
90
|
+
type: "object",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
description: "One result per operation, in input order",
|
|
95
|
+
},
|
|
96
|
+
...openApi_1.defaultOpenApiErrorResponses,
|
|
97
|
+
401: { content: openApi_1.apiErrorContent, description: "The user must be authenticated" },
|
|
98
|
+
},
|
|
99
|
+
summary: `Bulk write ${tag}`,
|
|
100
|
+
tags: [tag],
|
|
101
|
+
}, options.openApiOverwrite?.bulk ?? {}));
|
|
102
|
+
};
|
|
103
|
+
exports.bulkOpenApiMiddleware = bulkOpenApiMiddleware;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Connection, Mongoose } from "mongoose";
|
|
2
|
+
/**
|
|
3
|
+
* Turns on `transactionAsyncLocalStorage` for a Mongoose instance. This is app-wide: from then on
|
|
4
|
+
* every Mongoose operation inside any transaction store joins that transaction, including stores
|
|
5
|
+
* opened by the app's own `connection.transaction()` calls. Operations outside a store are
|
|
6
|
+
* unaffected.
|
|
7
|
+
*/
|
|
8
|
+
export declare const enableTransactionAsyncLocalStorage: (base: Mongoose) => void;
|
|
9
|
+
/** Whether the connection's deployment supports transactions (replica set or sharded). */
|
|
10
|
+
export declare const supportsTransactions: (connection: Connection) => Promise<boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Whether an error (or any error in its `cause` chain, since executors wrap driver errors in
|
|
13
|
+
* APIErrors) is a transient transaction failure, such as a write conflict: the transaction was
|
|
14
|
+
* aborted and nothing it wrote is visible.
|
|
15
|
+
*/
|
|
16
|
+
export declare const isTransientTransactionError: (error: unknown) => boolean;
|
|
17
|
+
/** Thrown when a commit's outcome stays unknown after one retry: the writes may have landed. */
|
|
18
|
+
export declare class BulkCommitUnknownError extends Error {
|
|
19
|
+
readonly cause: unknown;
|
|
20
|
+
readonly isBulkCommitUnknown = true;
|
|
21
|
+
constructor(cause: unknown);
|
|
22
|
+
}
|
|
23
|
+
export declare const isBulkCommitUnknownError: (error: unknown) => error is BulkCommitUnknownError;
|
|
24
|
+
/**
|
|
25
|
+
* Runs `fn` inside one transaction on `connection`, with every Mongoose operation of `base`
|
|
26
|
+
* joined to it. Commits when `fn` resolves; aborts and rethrows when it throws. Work `fn` starts
|
|
27
|
+
* without awaiting keeps the session after the transaction ends, so it must leave the store.
|
|
28
|
+
*/
|
|
29
|
+
export declare const runInBulkTransaction: <R>({ base, connection, fn, }: {
|
|
30
|
+
base: Mongoose;
|
|
31
|
+
connection: Connection;
|
|
32
|
+
fn: () => Promise<R>;
|
|
33
|
+
}) => Promise<R>;
|