import { DurableObject } from "cloudflare:workers";
import mongoose from "mongoose";
export class MongoDBConnection extends DurableObject {
connection;
constructor(ctx, env) {
super(ctx, env);
this.connection = mongoose.createConnection(env.MONGO_DB_URI, {
// Decrease the selection timeout from 30s (default) to 5s, so that if something breaks, we don't wait around for it.
serverSelectionTimeoutMS: 5000,
});
}
async fetch(request) {
// Wait for the connection to become available
await this.connection.asPromise();
// Do work here
return new Response("OK");
}
}
import { DurableObject } from "cloudflare:workers";
import mongoose from "mongoose";
export class MongoDBConnection extends DurableObject {
connection;
constructor(ctx, env) {
super(ctx, env);
this.connection = mongoose.createConnection(env.MONGO_DB_URI, {
// Decrease the selection timeout from 30s (default) to 5s, so that if something breaks, we don't wait around for it.
serverSelectionTimeoutMS: 5000,
});
}
async fetch(request) {
// Wait for the connection to become available
await this.connection.asPromise();
// Do work here
return new Response("OK");
}
}