`TypeORM` and `@fastly/http-compute-js`

Hi Fastly community,

I’m attempting to integrate TypeORM with @fastly/http-compute-js, but I’m encountering an error when trying to connect to the database. Here’s my code snippet and the error message:

Code:

import http from '@fastly/http-compute-js';

import { createConnection, getManager } from "typeorm";
 class User {
    id
    firstName
    lastName
    age

    constructor(firstName, lastName, age) {
        this.firstName = firstName
        this.lastName = lastName
        this.age = age
    }

    async getUsers() {
      let connection
      try {
        connection= await createConnection()
        const manager = getManager()
      const result = await manager.query('SELECT * FROM users')
        return result
      }
      catch (error) {
        return error
      } finally {
        await connection?.close()
      }
    }
}

const server = http.createServer(async (req, res) => {
  const user = new User('John', 'Doe', 25)
  const users = await user.getUsers()
  console.log(users)
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    data: 'Hello World!'
  }));
});

server.listen();

Error:

2023-12-21T11:26:32.026885Z  INFO request{id=1}: handling request GET http://127.0.0.1:7676/favicon.ico
Log: (new Error("Cannot read connection options in a browser context.", "<stdin>", 19010))
2023-12-21T11:26:32.035390Z  INFO request{id=1}: request completed using 23.4 MB of WebAssembly heap
2023-12-21T11:26:32.035406Z  INFO request{id=1}: request completed in 8ms

Questions:

  1. Can anyone please help me understand the reason for this error and suggest ways to resolve it? Is there a specific configuration or approach needed for database connections within @fastly/http-compute-js?
  2. If direct TypeORM integration isn’t feasible, are there alternative methods or libraries recommended for connecting to databases within Fastly Compute@Edge?

Thank you in advance for your guidance!

Hi @rahul1

Thanks for reaching out to us (and for your patience while you wait for a response, as most people reviewing the forums were on leave over the Christmas and New Year period).

I’ve pinged our Compute JS team to get some possible feedback. Do please feel free to also reach out to support@fastly.com as the Customer Support team might also be able to assist.

Thanks!

While this is not the direct cause of the ‘Cannot read connection options in a browser context.’ error, you will likely run into a complication using TypeORM because Compute services can only make HTTP/HTTPS outbound connections; when you succeed in getting TypeORM to read the connection configuration it will have to be that type of connection, not a ‘raw’ TCP socket like would usually be used with common databases like PostgreSQL, MariaDB, etc.

Which type of database are you trying to connect to?