Skip to content

feat: Enable cross-trigger transactions via shared context #9794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: npm install && npm run build
command: npm run start


5 changes: 5 additions & 0 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,9 +1686,14 @@ class DatabaseController {
return protectedKeys;
}

setTransactionalSession(transactionalSession) {
this._transactionalSession = transactionalSession;
}

createTransactionalSession() {
return this.adapter.createTransactionalSession().then(transactionalSession => {
this._transactionalSession = transactionalSession;
return this._transactionalSession;
});
}

Expand Down
11 changes: 10 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ function RestWrite(config, auth, className, query, data, originalData, clientSDK
// Returns a promise for a {response, status, location} object.
// status and location are optional.
RestWrite.prototype.execute = function () {
if (this.context.transaction) {
this.config.database.setTransactionalSession(this.context.transaction)
}

return Promise.resolve()
.then(() => {
return this.getUserAndRoleACL();
Expand Down Expand Up @@ -165,7 +169,12 @@ RestWrite.prototype.execute = function () {
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, 'User email is not verified.');
}
return this.response;
});
}).finally(() => {
if (this.context.transaction) {
// Ensure isolation even on uncaught errors
this.config.database.setTransactionalSession(null);
}
});;
};

// Uses the Auth object to get the list of roles, adds the user id
Expand Down
10 changes: 9 additions & 1 deletion src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,15 @@ export function getRequestObject(
triggerType === Types.afterFind
) {
// Set a copy of the context on the request object.
request.context = Object.assign({}, context);
request.context = Object.assign(
{},
context,
{
createTransactionalSession: config.database.createTransactionalSession.bind(config.database),
commitTransactionalSession: config.database.commitTransactionalSession.bind(config.database),
abortTransactionalSession: config.database.abortTransactionalSession.bind(config.database),
}
);
}

if (!auth) {
Expand Down