mongodb #2721
Replies: 15 comments 68 replies
-
@xofyarg reported an issue on the unenv repo which @pi0 fixed. It should be integrated in unenv with cloudflare/workers-sdk#6920 |
Beta Was this translation helpful? Give feedback.
-
it's been forever now... I can't remember when I encountered this the first time but it must be at least 2 years ago. thanks! |
Beta Was this translation helpful? Give feedback.
-
I saw the thread on the forum which says that the only problem is that workers don't support TLS |
Beta Was this translation helpful? Give feedback.
-
I've started a new Mongodb instance, and I can't use it with my Cloudflare workers; I want to use a Cloudflare environment with queues, kv, etc., but I can't use it if workers don't actually work... |
Beta Was this translation helpful? Give feedback.
-
December 6th |
Beta Was this translation helpful? Give feedback.
-
How can one use mongodb on Workers, now that node:net is merged? |
Beta Was this translation helpful? Give feedback.
-
As promised, here is an update: workerd now implements wrangler 3.106 which was just released contains the latest version of Meaning that you should be able to use Please test and report your experiences! 🙏 edit: You need to use the I have tested with: # wrangler.toml
compatibility_date = "2024-12-05"
compatibility_flags = ["nodejs_compat"] |
Beta Was this translation helpful? Give feedback.
-
/cc @alexbevi ☝️ |
Beta Was this translation helpful? Give feedback.
-
The moment i finally give up and switch to D1 from mongo you release THIS. But big thank you for finally doing this!! |
Beta Was this translation helpful? Give feedback.
-
I want to raise another related issue. First off there is a workaround see: mongodb polyfill Another important point: @alexbevi, seeing as you are from Mongodb, do you have any insight about this error, have you encountered it? The Finally I'm not the only one that is getting these errors, see polyfill issue 4 |
Beta Was this translation helpful? Give feedback.
-
If everything's working fine in a production deployed worker, can we kindly have a blog or official documentation on it to so that googling "how to use mongodb with Cloudflare worker" shows that article and not GH discussion? 😊🙏🏻 Thanks for all your amazing work everyone. |
Beta Was this translation helpful? Give feedback.
-
Has anyone gotten this to work with Pages Functions as oppose to native workers.. I think CF Pages Functions use a different version of wrangler which still fails. |
Beta Was this translation helpful? Give feedback.
-
Starting a new thread for issues with connection pooling. We’d like to re-use connections across invocations, because otherwise we add a ~1500ms cold start to every invocation. However, @gerhardcit noted that you can’t re-use connection pools (created by
This isn’t that informative, because the actual issue is that a deeper exception gets caught, which causes the promises to not resolve. Firing up the debugger and breaking on caught exceptions gives us:
Okay, so it seems like a non-starter to actually re-use a connection in a Worker. Instead, let’s try use a singleton Durable Object to manage the connection pool. Durable Objects shut down about a minute after their last client disconnects, which should be enough to keep a connection pool around across multiple requests. I added this to {
"durable_objects": {
"bindings": [
{
"name": "MONGO_DB_CONNECTION",
"class_name": "MongoDBConnection"
}
]
},
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["MongoDBConnection"]
}
]
} My export { MongoDBConnection } from "./MongoDBConnection.js";
export default {
async fetch(request, { MONGO_DB_CONNECTION, MONGO_DB_URI }) {
// FWIW, you’ll probably want to do an auth check or similar here, to prevent wasting resources on fuzzing attacks
// We can just forward the request straight onto the Durable Object
const id = MONGO_DB_CONNECTION.idFromName(MONGO_DB_URI);
const connection = MONGO_DB_CONNECTION.get(id, { locationHint: "apac" });
return await connection.fetch(request);
}
}; Then 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,
// Time out idle connections after ~20 seconds, to avoid using up too much wall time (tweak this as needed for your use case)
maxIdleTimeMS: 20 * 1000,
});
}
async fetch(request) {
// Wait for the connection to become available
await this.connection.asPromise();
// Do work here
return new Response("OK");
}
} This code just transparently forwards all requests straight onto the DO, which can hold onto the connection pool across multiple requests and re-use it. My end-to-end timings went from ~2000ms to ~300ms after doing this! |
Beta Was this translation helpful? Give feedback.
-
someone can tell me how to make a connection between cloudflare worker with mongoDB Atlas? i don't know how to get public IP of cloudflare worker to be added into mongoDB Atlas |
Beta Was this translation helpful? Give feedback.
-
The
mongodb
does not work even withnode_compat_v2
on.It can be imported, but it does not properly connect to a DB. The package relies on
net.Socket
andtls.TLSSocket
.See Issue for more context: cloudflare/workers-sdk#6684
See Blog for more context: https://alexbevi.com/blog/2024/09/11/why-cloudflare-workers-dont-work-with-mongodb/
Beta Was this translation helpful? Give feedback.
All reactions