Skip to content

Support MongoDB's standard multi-host replica-set URIs #578

@RaschidJFR

Description

@RaschidJFR

What problem are you trying to solve?

The Mongo runtime's URL binding validates connection strings with the WHATWG URL constructor, which doesn't accept the multi-host form of the standard MongoDB Connection String Format:

import mongo from '@prisma-next/mongo/runtime';
import type { Contract } from './contract.d';
import contractJson from './contract.json' with { type: 'json' };

export const db = mongo<Contract>({
  contractJson,
  url: 'mongodb://h1:27017,h2:27017,h3:27017/mydb?replicaSet=rs0',
});
// throws at import time: "Mongo URL must be a valid URL"

### Proposed solution

Use [`mongodb-connection-string-url`](https://www.npmjs.com/package/mongodb-connection-string-url) (a dependency of `mongodb`) in place of the raw `new URL()` call:

```ts
// node_modules/@prisma-next/mongo/src/runtime/binding.ts
import { ConnectionString } from 'mongodb-connection-string-url';

function validateMongoUrl(url: string): ConnectionString {
  const trimmed = url.trim();
  if (trimmed.length === 0) {
    throw new Error('Mongo URL must be a non-empty string');
  }
  let parsed: ConnectionString;
  try {
    parsed = new ConnectionString(trimmed);
  } catch {
    throw new Error('Mongo URL must be a valid MongoDB connection string');
  }
  // protocol / dbName extraction stays the same; ConnectionString is URL-compatible.
  return parsed;
}

Alternatives considered

Rewriting the URI in user code before passing it in — collapsing the seed list to a single host:

const cs = new ConnectionString(originalUri);
cs.hosts = [cs.hosts[0]];
const url = cs.toString();

This works because the driver's SDAM still discovers the remaining replica members from the seed, but:

  • every consumer has to know about and apply the workaround
  • it silently loses initial-connection fault tolerance (if the chosen seed happens to be down, the driver has no fallback during the handshake)
  • it's surprising that prisma-next's Mongo runtime rejects the URI format that MongoDB itself documents as canonical and that the underlying driver accepts

Scope and impact

  • Package: @prisma-next/mongo (runtime binding only — src/runtime/binding.ts)
  • Not a breaking change: every URI accepted today is also accepted by ConnectionString (it's a URL-compatible superset)
  • Mongo-target-specific; no impact on Postgres / SQLite targets
  • No new direct dependency required: mongodb-connection-string-url is already pulled in transitively via the mongodb driver, though promoting it to a direct dependency would also be reasonable

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestneeds-triageAwaiting maintainer triage

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions