Comments (9)
Generally it's fine to do db calls in the event dispatcher.
Use Case: Gathering data for email which will be dispatched over the event dispatcher.
If you provide your use case, we could give you more help.
from express-typescript-boilerplate.
Hi @dweber019 thanks for reply.
actually right now im calling database queries in EventSubcriber like this:
@On(events.postActions.created)
public async onPostActionsCreate(postActions: PostActions): Promise<void> {
try {
const connection: Connection = Container.get('connection');
const em = connection.createEntityManager();
const post: any = await em.findOne<Posts>(Posts, {
where: {id: postActions.postId},
relations: ['user'],
});
console.log(post);
} catch (err) {
console.log('error', err);
}
}
actually i dont like it, is it possible to call service or repository directly here?
my use case is when when post action is created im dispatching email and emitting socket io
Thanks in advance
from express-typescript-boilerplate.
@jasonvillalon you could try to use the normal typedi annotation for injecting a service.
constructor(
private postService: PostService
) { }
Never tested but maybe it works as event-dispatch does some things with a container and a meta registry.
from express-typescript-boilerplate.
@jasonvillalon did it work? If you don't mind I close this issue.
We/you can reopen if needed.
from express-typescript-boilerplate.
@dweber019 no it didnt work. when im injecting dependencies in subscribers its always undefined.
from express-typescript-boilerplate.
@dweber019 can we reopen this? i really need this feature now :) my event dispatcher modules are so bloated now.
from express-typescript-boilerplate.
ok i figure out a way to call service inside event dispatcher. i think this will work for me:
const em = connection.createEntityManager();
const myService = new MyService(em.getCustomRepository(MyRepository), log);
is this the only way?
from express-typescript-boilerplate.
@dweber019 It didn't work for me too. Can you reopen it? It's really must have thing.
from express-typescript-boilerplate.
@jasonvillalon Maybe this can help you? Directly getting service from container, instead of building it buy yourself.
from express-typescript-boilerplate.
Related Issues (20)
- Docker build fails HOT 2
- Seeding out of date
- File upload using multer integration
- Errors when installing on Ubuntu 18 LTS HOT 2
- How can i integrate JWT Authentication in this boiler plate HOT 1
- How can i integrate JWT Authentication in this boiler plate HOT 2
- Error when installing (bcrypt) HOT 1
- Error connecting to PostgreSQL with SSL
- error while yarn run setup command HOT 5
- Integrate with Relative Path
- How to set Elastic search Loader?
- About public route without authentication
- Update version of bcrypt and sqlite3 to 5.0.1 and 5.0.0 respectively HOT 8
- How to use winston?
- Why use CommonJS?
- 'yarn start serve' does not start the server HOT 1
- Bcrypt installation errors on M1 Mac HOT 4
- docker not started nps not found
- Cannot create migrations
- Witch branch Should I use ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from express-typescript-boilerplate.