Batch transaction (#5849)
* Batch transaction boilerplate * Refactoring transaction boilerplate * Independent sessions test * Transactions - partial * Missing only one test * All tests passing for mongo db * Tests on Travis * Transactions on postgres * Fix travis to restart mongodb * Remove mongodb service and keep only mongodb runner * MongoDB service back * Initialize replicaset * Remove mongodb runner again * Again only with mongodb-runner and removing cache * Trying with pretest and posttest * WiredTiger * Pretest and posttest again * Removing inexistent scripts * wiredTiger * One more attempt * Trying another way to run mongodb-runner * Fixing tests * Include batch transaction on direct access * Add tests to direct access
This commit is contained in:
committed by
GitHub
parent
fe18fe0f61
commit
8b97c1380b
71
src/batch.js
71
src/batch.js
@@ -83,30 +83,57 @@ function handleBatch(router, req) {
|
||||
req.config.publicServerURL
|
||||
);
|
||||
|
||||
const promises = req.body.requests.map(restRequest => {
|
||||
const routablePath = makeRoutablePath(restRequest.path);
|
||||
// Construct a request that we can send to a handler
|
||||
const request = {
|
||||
body: restRequest.body,
|
||||
config: req.config,
|
||||
auth: req.auth,
|
||||
info: req.info,
|
||||
};
|
||||
let initialPromise = Promise.resolve();
|
||||
if (req.body.transaction === true) {
|
||||
initialPromise = req.config.database.createTransactionalSession();
|
||||
}
|
||||
|
||||
return router
|
||||
.tryRouteRequest(restRequest.method, routablePath, request)
|
||||
.then(
|
||||
response => {
|
||||
return { success: response.response };
|
||||
},
|
||||
error => {
|
||||
return { error: { code: error.code, error: error.message } };
|
||||
return initialPromise.then(() => {
|
||||
const promises = req.body.requests.map(restRequest => {
|
||||
const routablePath = makeRoutablePath(restRequest.path);
|
||||
// Construct a request that we can send to a handler
|
||||
|
||||
const request = {
|
||||
body: restRequest.body,
|
||||
config: req.config,
|
||||
auth: req.auth,
|
||||
info: req.info,
|
||||
};
|
||||
|
||||
return router
|
||||
.tryRouteRequest(restRequest.method, routablePath, request)
|
||||
.then(
|
||||
response => {
|
||||
return { success: response.response };
|
||||
},
|
||||
error => {
|
||||
if (req.body.transaction === true) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
return { error: { code: error.code, error: error.message } };
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
return Promise.all(promises)
|
||||
.catch(error => {
|
||||
if (req.body.transaction === true) {
|
||||
return req.config.database.abortTransactionalSession().then(() => {
|
||||
throw error;
|
||||
});
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
return Promise.all(promises).then(results => {
|
||||
return { response: results };
|
||||
})
|
||||
.then(results => {
|
||||
if (req.body.transaction === true) {
|
||||
return req.config.database.commitTransactionalSession().then(() => {
|
||||
return { response: results };
|
||||
});
|
||||
} else {
|
||||
return { response: results };
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user