Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudflare Workers support (maybe via a HTTP Driver) #14613

Open
2 tasks done
kaushiksahu18 opened this issue May 24, 2024 · 15 comments
Open
2 tasks done

Cloudflare Workers support (maybe via a HTTP Driver) #14613

kaushiksahu18 opened this issue May 24, 2024 · 15 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@kaushiksahu18
Copy link

kaushiksahu18 commented May 24, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.0

Node.js version

20.11.1

MongoDB server version

7.0.8

Typescript version (if applicable)

No response

Description

i was trying to use mongodb with honojs and i have installed mongoos but
connectDB function is showing the error import_mongoose.default.connect is not a function

import { z } from "zod";
import mongoose from "mongoose";

import { USERS } from "./index.js";

// this function
async function connectDB(url: string) {
  await mongoose.connect(String(url));
}

log

Error in connecting to DB TypeError: import_mongoose.default.connect is not a function
    at connectDB (file:///home/user/hono_tododb/src/utils.ts:7:18)
    at null.<anonymous> (file:///home/user/hono_tododb/src/index.ts:26:13)
    at dispatch (file:///home/user/hono_tododb/node_modules/hono/dist/compose.js:29:23)
    at null.<anonymous> (file:///home/user/hono_tododb/node_modules/hono/dist/compose.js:30:20)
    at cors2 (file:///home/user/hono_tododb/node_modules/hono/dist/middleware/cors/index.js:65:11)
    at dispatch (file:///home/user/hono_tododb/node_modules/hono/dist/compose.js:29:23)
    at null.<anonymous> (file:///home/user/hono_tododb/node_modules/hono/dist/compose.js:6:12)
    at null.<anonymous> (file:///home/user/hono_tododb/node_modules/hono/dist/hono-base.js:188:31)
    at Hono2.dispatch (file:///home/user/hono_tododb/node_modules/hono/dist/hono-base.js:198:5)
    at Hono2.fetch (file:///home/user/hono_tododb/node_modules/hono/dist/hono-base.js:201:17)

Steps to Reproduce

import { z } from "zod";
import mongoose from "mongoose";

import { USERS } from "./index.js";

// this function
async function connectDB(url: string) {
  await mongoose.connect(String(url));
}
@vkarpov15
Copy link
Collaborator

I'm unable to repro, the following script works fine with Node.js. Please modify the following script to demonstrate your issue. Also, can you please clarify whether you're using Next.js or some environment other than Node.js, and also which version of TypeScript you're using and your TypeScript config? I see you have an src/utils.ts file.

import { serve } from '@hono/node-server';
import { Hono } from 'hono';
import mongoose from 'mongoose';

const app = new Hono()
app.get('/', async (c) => {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
  return c.text('Hono meets Node.js')
})

serve(app, (info) => {
  console.log(`Listening on http://localhost:${info.port}`) // Listening on http://localhost:3000
})

Output:

$ node gh-14613.mjs 
Listening on http://localhost:3000

Curl command:

$ curl http://localhost:3000
Hono meets Node.js
$

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jun 2, 2024
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Jun 17, 2024
@kaushiksahu18
Copy link
Author

the following script works fine with Node.js.

I have used for cloudflare workers (wrangler)

TS version - latest

@kaushiksahu18 kaushiksahu18 reopened this Jun 17, 2024
@github-actions github-actions bot removed the Stale label Jun 18, 2024
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jun 21, 2024
@vkarpov15 vkarpov15 added this to the 8.4.4 milestone Jun 21, 2024
@vkarpov15
Copy link
Collaborator

The easiest way to fix this particular error is to add the following to your wrangler.toml file:

node_compat = true

However, with that, we still get the following errors:

▲ [WARNING] The package "node:async_hooks" wasn't found on the file system but is built into node.

  Your Worker may throw errors at runtime unless you enable the "nodejs_compat" compatibility flag.
  Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details. Imported
  from:
   - node_modules/mongoose/lib/mongoose.js


✘ [ERROR] Could not resolve "fs/promises"

    node_modules/mongodb/lib/client-side-encryption/state_machine.js:4:19:
      4 │ const fs = require("fs/promises");
        ╵                    ~~~~~~~~~~~~~

  The package "fs/promises" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

I'm investigating a fix. But it's worth mentioning that we haven't tested Mongoose with Cloudflare Workers at all.

@vkarpov15
Copy link
Collaborator

I took some time to play whack-a-mole with all the build issues that pop up with Cloudflare Workers, but still no luck, Mongoose can't connect. It looks like Cloudflare Workers has some support for TCP sockets, but looks like that doesn't quite work with the MongoDB driver.

image

It looks like the MongoDB driver doesn't support Cloudflare Workers, the officially supported way to connect to MongoDB from Cloudflare Workers is Realm. Mongoose doesn't have a Realm driver, so we can't quite use that yet. But we may consider adding a Realm driver in the future.

image

@vkarpov15 vkarpov15 changed the title log : import_mongoose.default.connect is not a function Cloudflare Workers support (maybe via a Realm Driver) Jun 24, 2024
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jun 24, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.4.4, Parking Lot Jun 24, 2024
@jdgamble555
Copy link

Cloudflare doesn't support TCP protocol, there would need to be an HTTP implementation using REST API. Is this something mongoose can do, connect differently?

Firebase has a lite version for this.

J

@vkarpov15
Copy link
Collaborator

Yes, Mongoose can in theory use HTTP or some alternative protocol via the Driver API.

@ceddybi
Copy link

ceddybi commented Sep 13, 2024

It looks like the MongoDB driver doesn't support Cloudflare Workers, the officially supported way to connect to MongoDB from Cloudflare Workers is Realm. Mongoose doesn't have a Realm driver, so we can't quite use that yet. But we may consider adding a Realm driver in the future.

@vkarpov15 Well Realm is deprecated soon

@vkarpov15
Copy link
Collaborator

In that case, we will update the issue title to remove reference to Realm: https://www.mongodb.com/products/updates/product-support-deprecation?tck=notice_product_deprecation_2024.

@vkarpov15 vkarpov15 changed the title Cloudflare Workers support (maybe via a Realm Driver) Cloudflare Workers support (maybe via a HTTP Driver) Sep 15, 2024
@ceddybi
Copy link

ceddybi commented Sep 15, 2024

In that case, we will update the issue title to remove reference to Realm: https://www.mongodb.com/products/updates/product-support-deprecation?tck=notice_product_deprecation_2024.

@vkarpov15 Okok, so how will mongo run in cloudflare workers?

@jdgamble555
Copy link

jdgamble555 commented Sep 15, 2024

It's not just Cloudflare, it is Vercel Edge, Deno, Bun, and other non-node environments fwi. This is the new norm.

@ceddybi
Copy link

ceddybi commented Sep 15, 2024

@jdgamble555 no mongoose doesn't work on cloudflare, but others where there's a full nodejs environment it works, e.g Vercel (not edge, vercel edge is just like cloudflare), Bun, Deno

@jdgamble555
Copy link

Right, Vercel Edge uses Cloudflare under the hood. What I'm saying is the new normal is multiple environments. Mongoose should work in ALL JavaScript environments.

The only way to do this is to have on option that uses HTTP under-the-hood. This is how firestore/lite works, and there should be an option for Mongoose to work that way as well.

J

@ceddybi
Copy link

ceddybi commented Sep 15, 2024

@jdgamble555

Vercel Edge uses Cloudflare under the hood

You mean they are re-selling the cloudflare workers platform ? i thought they were using something from aws/gcp.

I used the full Vercel functions (without edge) and it works.

anyways mongo is cooked! without a light weight HTTP client (Realm)

who ever deprecated realm shouldn't get promoted.

@vkarpov15
Copy link
Collaborator

We won't run MongoDB in cloudflare workers, but Mongoose's driver API makes it possible for Mongoose to speak HTTP under the hood. For example, that's how stargate-mongoose works: subs out default MongoDB driver for an HTTP-based driver that talks to DataStax's Data API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

4 participants