Fly Postgres Replication with Sequelize

Hi guys,

We’re trying to use Sequelize replication to configure read/write for our Postgres cluster. Going by this guide, we configured the read and write values but this still won’t connect.

This is a piece of code we’ve tried:

sequelize.useCLS(namespace); // automatically add transaction to every request

const primary = process.env.PRIMARY_REGION;
const current = process.env.FLY_REGION;
const dbUrl = process.env.DATABASE_URL ;

const isWrite = primary === current;

const u = new URL(dbUrl);
const replication = [
  {
    host: u.hostname,
    port: isWrite ? 5432 : 5433,
    username: u.username,
    password: u.password,
    dialect: "postgres",
    database: u.pathname.substring(1),
    pool: {},
  },
];
const { database, host, password, port, username } = replication[0];

const db = new sequelize(database, null, null, {
  dialect: "postgres",
  replication: {
    read: replication,
    write: replication[0],
  },

  logging: console.log,
  retry: {
    max: Infinity,
    match: [
      /ConnectionError/,
      /SequelizeConnectionError/
    ],
  },
  define: {
    timestamps: true,
  },
});

(async () => {
  try {
    await db.authenticate();
    console.log("Connection established");
  } catch (error) {
    console.log(error);
  }
})();

Perhaps someone have some thoughts or have gotten this to work in their project.

Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.