timgit / pg-boss

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

Documentation for PgBoss constructor db option. #444

Closed airbender-1 closed 1 month ago

airbender-1 commented 5 months ago

This is a documentation improvement idea

I think the Database Install chapter of readme may be improved to include example of initializing PgBoss with external db connection.

In my case I have Node + Typescript app that uses Sequelize for db connection and ORM. Db is configured differently for local dev environment (no ssl) and production (with SSL).

I was struggling to find the correct way to use Sequelize for db access for PgBoss. Here is what worked for me:

//// db contains all the parameters for connection string, ssl options, etc. and is defined elsewhere
//// The same instance of Sequelize is reused in the code that initializes PgBoss
//   const db = new Sequelize(...)

   const executeSql = async (text: string, values: string[]): Promise<{ rows: any[], rowCount: number }> => {
      const [rows] = await db.query(text, { bind: values })

      return { rows, rowCount: rows.length }
    }

    const pgDb = { executeSql }

    const boss = new PgBoss({ db: pgDb })

If this was included in the documentation it will allow saving time on looking into PgBoss code, debugging PgBoss code + Sequelize to understand the requirements for executeSql and how to pass values from executeSql to Sequelize db via bind options.

timgit commented 1 month ago

The expected interface is in the send() docs