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:
Antonio Davi Macedo Coelho de Castro
2019-07-31 02:41:07 -07:00
committed by GitHub
parent fe18fe0f61
commit 8b97c1380b
15 changed files with 931 additions and 106 deletions

View File

@@ -33,11 +33,13 @@ function getAuth(options = {}, config) {
}
function ParseServerRESTController(applicationId, router) {
function handleRequest(method, path, data = {}, options = {}) {
function handleRequest(method, path, data = {}, options = {}, config) {
// Store the arguments, for later use if internal fails
const args = arguments;
const config = Config.get(applicationId);
if (!config) {
config = Config.get(applicationId);
}
const serverURL = URL.parse(config.serverURL);
if (path.indexOf(serverURL.path) === 0) {
path = path.slice(serverURL.path.length, path.length);
@@ -48,24 +50,52 @@ function ParseServerRESTController(applicationId, router) {
}
if (path === '/batch') {
const promises = data.requests.map(request => {
return handleRequest(
request.method,
request.path,
request.body,
options
).then(
response => {
return Promise.resolve({ success: response });
},
error => {
return Promise.resolve({
error: { code: error.code, error: error.message },
});
}
);
let initialPromise = Promise.resolve();
if (data.transaction === true) {
initialPromise = config.database.createTransactionalSession();
}
return initialPromise.then(() => {
const promises = data.requests.map(request => {
return handleRequest(
request.method,
request.path,
request.body,
options,
config
).then(
response => {
return Promise.resolve({ success: response });
},
error => {
if (data.transaction === true) {
return Promise.reject(error);
}
return Promise.resolve({
error: { code: error.code, error: error.message },
});
}
);
});
return Promise.all(promises)
.catch(error => {
if (data.transaction === true) {
return config.database.abortTransactionalSession().then(() => {
throw error;
});
} else {
throw error;
}
})
.then(result => {
if (data.transaction === true) {
return config.database.commitTransactionalSession().then(() => {
return result;
});
} else {
return result;
}
});
});
return Promise.all(promises);
}
let query;