# Fly postgres multiregion TypeORM

**URL:** <https://community.fly.io/t/fly-postgres-multiregion-typeorm/4633>\
**Category:** Questions / Help\
**Tags:** postgres\
**Created:** [April 5, 2022, 6:35pm UTC](https://community.fly.io/t/fly-postgres-multiregion-typeorm/4633 "2022-04-05T18:35:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pollux](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/pollux/32/17724_2.png) [@pollux](https://community.fly.io/u/pollux)\
**Post date:** [April 5, 2022, 6:35pm UTC](https://community.fly.io/t/fly-postgres-multiregion-typeorm/4633/1 "2022-04-05T18:35:07Z")

</div>

Hi,

I just started setting up Postgres multiregion for typeorm.

So I’m confused on how to handle the fly-replay header?

how do I detect for database read replica write errors?

I’m using TypeORM for my db calls. Also using Graphql.

I was thinking I can lisn to lisners on every “Before Insert” or “Before Update” event? this is just a thought, havnt tried it out [https://typeorm.io/listeners-and-subscribers](https://typeorm.io/listeners-and-subscribers)

Has anybody implemented this with typeorm?

Thanks

---

<div class="post-metadata">

**Author:** ![greg](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/greg/32/2119_2.png) [@greg](https://community.fly.io/u/greg)\
**Post date:** [April 5, 2022, 9:10pm UTC](https://community.fly.io/t/fly-postgres-multiregion-typeorm/4633/2 "2022-04-05T21:10:20Z")

</div>

I haven’t _but_ in case nobody else has either (and says how they did it) I would _think_ you could either:

1. have a listener on any request that might change the data. Like an insert/update/delete (as you suggest). And return a response including the `fly-replay` header

or

1. listen for a database error caused by the write to a read replica. It’s not entirely clear what the recommended approach is to do that but here’s one approach:

> <https://github.com/typeorm/typeorm/issues/5057#issuecomment-734442426>
>
> \*\*Issue type:\*\*
> 
> \[x\] question
> \[\] bug report
> \[\] feature request
> \[\] docume…ntation issue
> 
> \*\*Database system/driver:\*\*
> 
> \[\] \`cordova\`
> \[\] \`mongodb\`
> \[\] \`mssql\`
> \[x\] \`mysql\` / \`mariadb\`
> \[\] \`oracle\`
> \[\] \`postgres\`
> \[\] \`cockroachdb\`
> \[\] \`sqlite\`
> \[\] \`sqljs\`
> \[\] \`react-native\`
> \[\] \`expo\`
> 
> \*\*TypeORM version:\*\*
> 
> \[\] \`latest\`
> \[\] \`@next\`
> \[x\] \`0.x.x\` (0.2.20)
> 
> \*\*Steps to reproduce or a small repository showing the problem:\*\*
> 
> I see no hint of being able to handle database calls error properly. In my case, I have to catch duplicate entry error as follow:
> 
> \#### // user.repository.ts
> \`\`\`ts
> 
> const result = this.save(user)
> .catch((err: any) =\> {
> console.log(err);
> });
> \`\`\`
> ...and this is what I got
> 
> \`\`\`js 
> {
> message: 'ER\_DUP\_ENTRY: Duplicate entry ' +
> "'a.xxxxxxxx@xxxxx.com' for key " +
> "'IDX\_97672ac88f789774dd47f7c8be'",
> code: 'ER\_DUP\_ENTRY',
> errno: 1062,
> sqlMessage: "Duplicate entry 'a.xxxxxxxx@xxxxx.com' " +
> "for key 'IDX\_97672ac88f789774dd47f7c8be'",
> sqlState: '23000',
> index: 0,
> sql: 'INSERT INTO \`users\`(\`id\`, \`email\`, \`password\`, \`firstname\`, ' +
> '\`lastname\`, \`status\`, \`token\`) VALUES ' +
> "('3433c6a6-6a64-4071-bf77-6a752a09879f', 'a.xxxxxxxx@xxxxx.com', " +
> "'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', " +
> "'Aldee', 'XXXXXX', 'active', 'aa86b69a0b1f5')",
> name: 'QueryFailedError',
> query: 'INSERT INTO \`users\`(\`id\`, \`email\`, \`password\`, \`firstname\`, ' +
> '\`lastname\`, \`status\`, \`token\`) VALUES (?, ?, ?, ?, ?, ?, ?)',
> parameters: \[
> '3433c6a6-6a64-4071-bf77-6a752a09879f',
> 'a.xxxxxxxx@xxxxx.com',
> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
> 'Aldee',
> 'XXXXXX',
> 'active',
> 'aa86b69a0b1f5'
> \]
> }
> \`\`\`
> 
> As you can see, the \`code\` property is available as well as the \`errno\`. 
> The exception thrown says that it is an instance of \`QueryFailedError\`. 
> 
> Ok, so now, in the case above, I can modify my code and handle duplicate error exception like this:
> 
> \`\`\`ts
> const result = this.save(user)
> .catch((err: any) =\> {
> if (err instanceof QueryFailedError) {
> switch (err.code) { 
> case: 'ER\_DUP\_ENTRY':
> // do something
> break;
> }
> }
> });
> \`\`\`
> 
> But not so fast! The argument of the \`switch\` throws an error because the \`QueryFailedError\` class doesn't contain \`code\` property.
> 
> To bypass that issue, I just omitted the \`instanceof\` checking and \`switch\` part.
> 
> \`\`\`ts
> const result = this.save(user)
> .catch((err: any) =\> {
> switch (err.code) {
> case 'ER\_DUP\_ENTRY':
> // do something....
> break;
> }
> });
> \`\`\`
> 
> It's working if I bypass the typing (using \`any\`) but it adds some risk if the API changes in the future. Is there any neat way to do this?

These being the available errors:

> **[typeorm/src/error at master · typeorm/typeorm](https://github.com/typeorm/typeorm/tree/master/src/error)**
>
> ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms. -...

Not sure which it would throw … I would _guess_ a `QueryFailedError` as I can’t see a write-to-a-read-replica one 🙂

And return a `fly-replay` header which Fly’s proxy will see. That should contain the region the primary database is in e.g

```auto
res.setHeader('Fly-Replay', 'region=lhr')

```

---

<div class="post-metadata">

**Author:** ![pollux](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/pollux/32/17724_2.png) [@pollux](https://community.fly.io/u/pollux)\
**Post date:** [April 5, 2022, 9:26pm UTC](https://community.fly.io/t/fly-postgres-multiregion-typeorm/4633/3 "2022-04-05T21:26:33Z")

</div>

OMG! Yes this is what I was looking for.

Let me give it a try. I think this is a better apporach. Just to look for errors rather than handle all write requests.

---

<div class="post-metadata">

**Author:** ![pollux](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/pollux/32/17724_2.png) [@pollux](https://community.fly.io/u/pollux)\
**Post date:** [April 6, 2022, 6:40pm UTC](https://community.fly.io/t/fly-postgres-multiregion-typeorm/4633/4 "2022-04-06T18:40:35Z")

</div>

Okay I implemnted it.

I dint use fly-replay request. TypeORM has a replication strategy here [https://typeorm.io/multiple-data-sources#replication](https://typeorm.io/multiple-data-sources#replication)

here is a gist of my code if anybody needs for TypeORM

```auto
const databaseUrl = process.env.DATABASE_URL as string;
      let options: ConnectionOptions = {
        type: 'postgres',
        name: 'default',
        logging: false,
        synchronize: false,
        entities: [__dirname + '/../modules/**/*.js'],
        migrations: [__dirname + '/../migration/*.js'],
      };
      if (process.env.PRIMARY_REGION !== process.env.FLY_REGION) {
        options = {
          ...options,
          replication: {
            master: {
              url: databaseUrl,
            },
            slaves: [
              {
                url: databaseUrl.replace('5432', '5433'),
              },
            ],
          },
        };
      } else {
        options = {
          ...options,
          url: databaseUrl,
        };
      }
      
        return createConnection(options)

```

use this while you are creating a connection for produciton
