question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: using subscribers on vanilla javascript

See original GitHub issue

Issue type:

[X] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [X] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [X] 0.2.6 (or put your version here)

I’m trying to configure subscribers using Javascript, but the beforeInsert method on my Subscriber class never get called… Maybe I’m missing some step or configuration?

Doc: https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md

My code looks like this:

class Subscriber {
  beforeInsert (event) {
    console.log('fooo', event)
  }
}

module.exports = {
  name: 'default',
  type: 'postgres',
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  entities: schemas.map(schema => new EntitySchema(schema)),
  synchronize: false,
  logging: true,
  subscribers: [ Subscriber ],
  extra: { max: 1 },
  migrations: [path.join(__dirname, '../../migrations/*.js')],
  cli: { migrationsDir: path.join(__dirname, '../../migrations/*.js') },
  logger: new QueryLogger()
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JoeCasecommented, Sep 28, 2021

Has anyone advanced this? This is a bit old but running into this issued right now for listeners and subscribers in javascript.

1reaction
jkiyocommented, Jul 11, 2018

For those who still have some trouble to use Subscribers in vanilla, here is my solution:

I’ve created an utility function to apply decorators into classes:

const DecorateClass = (decorators, target) => {
  decorators = Array.isArray(decorators) ? decorators : [decorators]
  decorators.forEach(decorator => decorator(target))
  return target
}

And then use the decorators:

const { EventSubscriber } = require('typeorm')

class EverythingSubscriber {
  /**
   * Returns the class of the entity to which events will listen.
   * If this method is omitted, then subscriber will listen to events of all entities.
   *
   * listenTo?(): Function;
   */

  /* afterLoad?(entity: Entity): Promise<any>|void; */
  afterLoad ({ entity }) { }

  /* beforeInsert?(event: InsertEvent<Entity>): Promise<any>|void; */
  beforeInsert ({ connection, queryRunner, manager, entity }) { }

  /* beforeUpdate?(event: UpdateEvent<Entity>): Promise<any>|void; */
  beforeUpdate ({ connection, queryRunner, manager, entity, databaseEntity, updatedColumns, updatedRelations }) { }

  /* beforeRemove?(event: RemoveEvent<Entity>): Promise<any>|void; */
  beforeRemove ({ connection, queryRunner, manager, entity, databaseEntity, entityId }) { }

  /* afterInsert?(event: InsertEvent<Entity>): Promise<any>|void; */
  afterInsert ({ connection, queryRunner, manager, entity }) { }

  /* afterUpdate?(event: UpdateEvent<Entity>): Promise<any>|void; */
  afterUpdate ({ connection, queryRunner, manager, entity }) { }

  /* afterRemove?(event: RemoveEvent<Entity>): Promise<any>|void; */
  afterRemove ({ connection, queryRunner, manager, entity }) { }
}

module.exports = DecorateClass(EventSubscriber(/* Decorator params */), EverythingSubscriber)

The DecorateClass function will only work on Classes, to decorate a method the implementation must be different…

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Design Patterns Part 2: The Publisher/Subscriber ...
The Publisher/Subscriber pattern, or “PubSub” for short, is a pattern that allows us to create modules that can communicate with each other ...
Read more >
Use Observables with Vanilla JavaScript | Ahmed Tarek
This Subscription object should expose an unsubscribe function. unsubscribe should be called by the observer whenever he wants to stop listening ...
Read more >
Observer Pattern with Vanilla JS - Under The Hood Learning
The Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen...
Read more >
What are your vanilla JavaScript knowledge standards that ...
I take issue with some of these questions insofar as "vanilla JS" is pure JS and not necessarily DOM API (I define vanilla...
Read more >
How to create an ApolloClient subscription in vanilla js
... in the ApolloClient 'link' meant it was setup only for http, I can confirm that the code above works just fine with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found