denodrivers/mongo

Should we deprecate this module since importing with `npm:mongodb` is working well with Deno?

erfanium opened this issue ยท 17 comments

npm:mongodb pros:

  • almost all driver features are working for me
  • maintained by mongodb inc
  • universal api style for both node and deno

cons:

  • requried --unstable flag it's not compatible with Deno Deploy for now
  • uses node's Buffer instead of Uint8Array
  • uses node's Stream instead of async iterator and WebStream
  • is not as fast as x/mongo because uses node compat layer

I would love a hybrid version were we would use npm as a default but also webify the interfaces nodejs brings

We can publish a new module on npm like mongodb-webified. It can benfites both worlds

Can you share how did you get the npm:mongodb to work?

I got similar error to this issue: denoland/deno#15798 when I tried using npm:mongodb in a fresh project.
error: Uncaught (in promise) Error: Cannot find module 'crypto'

import { MongoClient } from "npm:mongodb";

const client = new MongoClient("mongodb://localhost:27017/test");
await client.connect();
console.log("Connected");

console.log(await client.db().collection("test").findOne());

cmd:

deno run -A --unstable main.ts

deno 1.25.4

Interesting. When I tried the script above it works.

But when tried using it in fresh app, I got the following error:

error: Uncaught (in promise) Error: Cannot find module 'crypto'
Require stack:
- /Users/steve/Library/Caches/deno/npm/registry.npmjs.org/mongodb/4.10.0/lib/operations/add_user.js
- /Users/steve/Library/Caches/deno/npm/registry.npmjs.org/mongodb/4.10.0/lib/admin.js
- /Users/steve/Library/Caches/deno/npm/registry.npmjs.org/mongodb/4.10.0/lib/index.js
- /Users/steve/Library/Caches/deno/npm/registry.npmjs.org/mongodb/4.10.0/lib/index.js
    at Function.Module._resolveFilename (deno:ext/node/02_require.js:615:17)
    at Function.Module._load (deno:ext/node/02_require.js:447:29)
    at Module.require (deno:ext/node/02_require.js:658:21)
    at require (deno:ext/node/02_require.js:789:18)
    at Object.<anonymous> (file:///Users/steve/Library/Caches/deno/npm/registry.npmjs.org/mongodb/4.10.0/lib/operations/add_user.js:4:16)
    at Object.<anonymous> (file:///Users/steve/Library/Caches/deno/npm/registry.npmjs.org/mongodb/4.10.0/lib/operations/add_user.js:73:4)
    at Module._compile (deno:ext/node/02_require.js:719:36)
    at Object.Module._extensions..js (deno:ext/node/02_require.js:752:12)
    at Module.load (deno:ext/node/02_require.js:636:34)
    at Function.Module._load (deno:ext/node/02_require.js:493:14)

@steverandy please report it on fresh github repo. Thanks
Not related to this issue

We can publish a new module on npm like mongodb-webified. It can benfites both worlds

i prefer that it stays here. Else people could be thinking that it's not really made for Deno.

I just tried the npm: version (v4.11.0 with Deno v1.27.0) and it for some reason it hung 90% of the time and never connected (no error thrown).

const client = new MongoClient(`mongodb://${user}:${pass}@${ip}:${port}/${dbName}`);
await client.connect();
db = client.db(dbName);

Strange that it works sometimes. Seems premature to deprecate this unless I'm making some sort of weird mistake there, but that seems like a good idea in the long run - if only to have consistency of the API between Node and Deno.

Edit: Just tested with npm:mongodb@5.7.0 and it seems to work fine now.

You can follow the issues with mongoose/mongodb with deno here

denoland/deno#15824

Well, since this issue is in question format, might as well give my 2 cents.

It would be sad to see "native" deno mongo driver go, or fall into disrepair. Deno driver has the potential to be cleaner, prettier and faster, and the npm compatibility layer is big, complex and slow-ish to load.

It seems to also be true that there is a lack of maintainers to keep working on the Deno driver. And that it needs nontrivial amount of work to be good enough for widespread production use. Since the userbase is small for now, and the node driver is usable already as-is, it may be unrewarding to work on the Deno driver.

Maybe the best solution for now would be to put a big banner on the readme, with:

  • "call for maintainers"
  • a note about the deno driver being alpha/beta quality, maybe detailing some of the most important missing features (automatic reconnect etc)
  • a note to suggest regular users may want to use the NPM driver (for now)

This way regular users would not be discouraged from using Deno because of frustrating problems with unstable drivers. And there would be hope for the project to gain maintainers in the future, or even some "official" support from Mongo, eventually improving the driver beyond the NPM driver :)

@erfanium The compatibility is not quite there yet: denoland/deno#16633

@erfanium The compatibility is not quite there yet: denoland/deno#16633

This has supposedly been fixed in Deno 1.37!

I jus tried on local an deploy and seems to be working with MongoDB Atlas serverless deno v1.37

import { MongoClient } from "npm:mongodb"
import { ulid } from "https://deno.land/std@0.202.0/ulid/mod.ts";

const client = new MongoClient("<url>")

client.connect();

Deno.serve(async () => {

  const data = await client.db("deno").collection("deno").insertOne({
    name: ulid()
  })

  return new Response(JSON.stringify({
    data
  }), {
    headers: { "Content-Type": "application/json" },
  })
})

Hey all, just wanted to chime in here as I'm the Product Manager at MongoDB for the Node.js driver, as well as the reporter of denoland/deno#16633 (which was addressed directly in the driver, as well as in the runtime via denoland/deno#20314).

Ultimately the goal is to have an ideal, runtime agnostic driver that the community can use so if there are still issues with using the driver via Deno I'd love to hear them :)

@alexbevi Would be cool if the Node.js driver would be Webified and uses modern standard. Same as what happened with BSON which is almost Node.js free which is awesome. My dream would be that the deno/nodejs driver could be the same code and only need to polyfill the Direct Sockets API :D (and maybe in the future not even that)

Thanks for the feedback @lucsoft. We're currently evaluating the driver against Deno to see if there's anything that outright doesn't work (feel free to share if you're aware of features that don't work)

@alexbevi Thanks for reaching out to us! As far as I know, MongoDB's official driver for Node.js only ships with CJS modules. So, you'd need to use Deno's npm compatibility layer to use the official driver.

I'd love to see MongoDB's official driver shipped as a Deno-native package in the deno.land/x registry, just like this module. with no dependency to std/node