Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.77k stars 3.8k forks source link

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

Open kaushiksahu18 opened 1 month ago

kaushiksahu18 commented 1 month ago

Prerequisites

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 commented 1 month ago

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
$
github-actions[bot] commented 3 weeks ago

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

kaushiksahu18 commented 3 weeks ago

the following script works fine with Node.js.

I have used for cloudflare workers (wrangler)

TS version - latest

vkarpov15 commented 2 weeks ago

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 commented 2 weeks ago

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