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
What problem are you trying to solve?
The Mongo runtime's URL binding validates connection strings with the WHATWG
URLconstructor, which doesn't accept the multi-host form of the standard MongoDB Connection String Format:Alternatives considered
Rewriting the URI in user code before passing it in — collapsing the seed list to a single host:
This works because the driver's SDAM still discovers the remaining replica members from the seed, but:
Scope and impact
@prisma-next/mongo(runtime binding only —src/runtime/binding.ts)ConnectionString(it's a URL-compatible superset)mongodb-connection-string-urlis already pulled in transitively via themongodbdriver, though promoting it to a direct dependency would also be reasonable