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

feat(core): maintain transaction context automatically #959

Merged
merged 1 commit into from
Oct 25, 2020
Merged

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Oct 25, 2020

Domain API is now used behind the scenes to automatically bare the tx context. This means
we can now use any EM, repository or a service (from DI), regardless of what EM instance
they were created from.

// this repo is based on `orm.em`, but thanks `TransactionContext` helper,
// it will use the right EM behind the scenes when used inside `em.transactional()`
const repo = orm.em.getRepository(Author4);

await orm.em.transactional(async () => {
  const a = await repo.findOneOrFail(god1);
  a.name = 'abc';
});

@lgtm-com
Copy link

lgtm-com bot commented Oct 25, 2020

This pull request introduces 1 alert when merging bf38e7c into 7248762 - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

Domain API is now used behind the scenes to automatically bare the tx context. This means
we can now use any EM, repository or a service (from DI), regardless of what EM instance
they were created from.

```ts
// this repo is based on `orm.em`, but thanks `TransactionContext` helper,
// it will use the right EM behind the scenes when used inside `em.transactional()`
const repo = orm.em.getRepository(Author4);

await orm.em.transactional(async () => {
  const a = await repo.findOneOrFail(god1);
  a.name = 'abc';
});
```
@merceyz
Copy link
Contributor

merceyz commented Oct 25, 2020

This will override the current domain.active meaning any custom things in there will be lost when using transactional, could it instead attach itself to the current active domain or copy the data from the active domain?

import domain from 'domain';

(async () => {
	const userDomain = domain.create();
	userDomain.__requestId = 1;

	userDomain.run(async () => {
		console.log(domain.active.__requestId);
		// 1

		const transactionalDomain = domain.create();
		transactionalDomain.__transactional = 2;
		transactionalDomain.run(async () => {
			console.log(domain.active.__requestId, domain.active.__transactional);
			// undefined, 2
		});
	});
})();

@B4nan
Copy link
Member Author

B4nan commented Oct 25, 2020

Sure, good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants