keviiii
keviiii

Reputation: 11

How to use mongoose in cloudflare workers

Is there any way to use mongodb in cloudflare wokers? Thanks. I want to use flare wokers cloud but use mongodb. I have searched many places and tried many ways but it doesn't work, currently cloudflare workers don't support mongoose but is there any way? Thanks.

Upvotes: 1

Views: 664

Answers (1)

Greg Wozniak
Greg Wozniak

Reputation: 7202

You can't connect directly to MongoDB database using Mongoose and Cloudflare Workers.

Cloudflare Workers support only outgoing TCP connections. It's related to the fact that as for right now, Cloudflare does not provide a dedicated IP address to each worker what would solve the routing problem.

From the other side - the only way to communicate directly with the MongoDB server is through MongoDB Wire Protocol which requires both ways communication. Mongoose uses the official MongoDB Node.js driver.

The alternative ways to use MongoDB are through their HTTP API without needing for fixed connection. Might be worth looking into something like Cloudflare Tunnel or Prisma Accelerate.

You can read more here about Cloudflare and alternative solutions to connect to databases. Also this article that sheds more light on Socket Workers it's important but make sure you distinguish WebSockets from TCP type connection.

Upvotes: 1

Related Questions