GithubHelp home page GithubHelp logo

Comments (9)

dweber019 avatar dweber019 commented on May 3, 2024

Hi @jasonvillalon

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.

jasonvillalon avatar jasonvillalon commented on May 3, 2024

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.

dweber019 avatar dweber019 commented on May 3, 2024

@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.

dweber019 avatar dweber019 commented on May 3, 2024

@jasonvillalon did it work? If you don't mind I close this issue.
We/you can reopen if needed.

from express-typescript-boilerplate.

jasonvillalon avatar jasonvillalon commented on May 3, 2024

@dweber019 no it didnt work. when im injecting dependencies in subscribers its always undefined.

from express-typescript-boilerplate.

jasonvillalon avatar jasonvillalon commented on May 3, 2024

@dweber019 can we reopen this? i really need this feature now :) my event dispatcher modules are so bloated now.

from express-typescript-boilerplate.

jasonvillalon avatar jasonvillalon commented on May 3, 2024

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.

LogansUA avatar LogansUA commented on May 3, 2024

@dweber019 It didn't work for me too. Can you reopen it? It's really must have thing.

from express-typescript-boilerplate.

LogansUA avatar LogansUA commented on May 3, 2024

@jasonvillalon Maybe this can help you? Directly getting service from container, instead of building it buy yourself.
screen shot 2018-09-24 at 12 52 21

from express-typescript-boilerplate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.