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:
- 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
? - 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!