Skip to content
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

Emit nested action events after the transaction completes #14981

Merged
merged 7 commits into from
Aug 18, 2022

Conversation

licitdev
Copy link
Member

@licitdev licitdev commented Aug 9, 2022

Description

Fixes #11172 by storing a queue of nested actionEvents that gets emitted after the main transaction completes. The top level action event will be emitted, followed by the nested action events.

Newly created nested items can be retrieved in the action event.

The database in the context is updated from this.knex || getDatabase() to getDatabase() as the emitting of action runs outside of the transaction now.

Sample extension

module.exports = function registerHooks({ action }) {
	async function callback(data, { database, schema }) {
		console.dir({ data }, { depth: null });
		console.dir(
			await database
				.select('*')
				.from(data.collection)
				.where(schema.collections[data.collection].primary, '=', data.key ?? data.keys[0]),
			{ depth: null }
		);
	}

	action('items.create', callback);
	action('items.update', callback);
	action('items.delete', callback);
};

Sample Flow

image

Type of Change

  • Bugfix
  • Improvement
  • New Feature
  • Refactor / codestyle updates
  • Other, please describe:

Requirements Checklist

  • New / updated tests are included
  • All tests are passing locally
  • Performed a self-review of the submitted code

If adding a new feature:

  • Documentation was added/updated

@licitdev licitdev marked this pull request as ready for review August 9, 2022 16:18
@rijkvanzanten rijkvanzanten merged commit 73fa8d6 into main Aug 18, 2022
@rijkvanzanten rijkvanzanten deleted the fix/nested-action-events branch August 18, 2022 20:07
qborisb pushed a commit to qborisb/directus that referenced this pull request Aug 31, 2022
…4981)

* Emit nested action events after the transaction completes

* Propagate mutation options for the disabling of nested emits

* Disable emit in test

* Shift emitting
qborisb pushed a commit to qborisb/directus that referenced this pull request Aug 31, 2022
…4981)

* Emit nested action events after the transaction completes

* Propagate mutation options for the disabling of nested emits

* Disable emit in test

* Shift emitting
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Action hook is triggered before the item is created
3 participants