timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
2.05k stars 157 forks source link

Use PostgreSQL client directly #48

Closed adriaandotcom closed 6 years ago

adriaandotcom commented 6 years ago

I'm using pg-boss as a part of an express app. In that express app I use sequelize for interacting with the database. When I run pg-boss it does setup a new connection to my database. Can I setup pg-boss in such a way it uses the same database connection as sequelize does?

timgit commented 6 years ago

Adriaan, I currently have this on my 3.0 list but I think it's achievable before then with some small constructor changes.

adriaandotcom commented 6 years ago

That would be awesome. Hopefully the sequelize team will do the same (https://github.com/sequelize/sequelize/issues/8967).

Edit: it's possible to export a connection from sequelize already https://github.com/sequelize/sequelize/issues/8967#issuecomment-361472210. like this:

const connection =  sequelize.connectionManager.getConnection();
terehov commented 6 years ago

We are working on a postgres-centric node framework and were planning to integrate PGBoss. Since we have a pool manager across multiple nodes, we would need this functionality as well. Thumbs up!

timgit commented 6 years ago

The above commit is included in 2.4.0. It's sort of "underdocumented" atm. The response pg-boss expects is that of the pg module. rows array, rowCount property when an update was run, etc.

adriaandotcom commented 6 years ago

Thanks, it works! How I did it (with sequelize), it maybe help others

// initializers/pg-boss.js
const db = require('../models'); // Your sequelize db export
const PgBoss = require('pg-boss');

const start = new Promise((resolve, reject) => {
  const getConnection = db.sequelize.connectionManager.getConnection();

  getConnection.then(connection => {
    // Add `close` and `executeSql` functions for PgBoss to function
    const PgBossDB = Object.assign(connection, {
      close: connection.end, // Not required
      executeSql: connection.query
    });

    const boss = new PgBoss({ db: PgBossDB });
    boss.on('error', console.error);
    boss.start().then(resolve).catch(reject);
  }, reject);
});

module.exports = start.then(pgBoss => pgBoss).catch(console.error);

And run it in another file

// runners/jobs.js
const pgBoss = require('../initializers/pg-boss');
const addJob = job => {
  // Something with the job
  job.done();
};

pgBoss.then(jobs => {
  jobs.subscribe('add-job', addJob).catch(error => {
    console.error('[JOB] Subscribe:', error);
  });
}).catch('[PG BOSS]', console.error);
timgit commented 6 years ago

Great! Btw, close() is not required.