Conversation
goastler
commented
Dec 4, 2025
- fix mongoose connection setup parameters
- make util fn for getting mongoose connection options, deduce compressors from url
- fix deps
- lint
- docs(changeset): make util fn for mongoose connection config, standardise mongoose connections
There was a problem hiding this comment.
Pull request overview
This PR standardizes MongoDB/Mongoose connection configuration across the codebase by creating a centralized utility function for connection options and implementing intelligent compression settings based on connection type (remote vs. local).
Key Changes:
- Created
getMongoConnectionOptionsutility function with comprehensive default connection settings - Implemented
getMongoCompressorsto automatically determine compression settings based on URL (remote connections use compression, local/container connections don't) - Updated connection setup in
MongoDatabaseandclient-example-serverto use the new utilities
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/database/src/mongooseOptions.ts | New utility module with connection options and compressor detection logic |
| packages/database/src/tests/mongooseOptions.unit.test.ts | Comprehensive test suite for the compressor detection function |
| packages/database/src/base/mongo.ts | Updated to use new connection options utility |
| demos/client-example-server/src/utils/connection.ts | Updated to use new connection options utility |
| packages/database/vite.test.config.ts | New test configuration with environment file loading |
| packages/database/src/index.ts | Export new mongooseOptions module |
| packages/database/package.json | Added dotenv dependency and test script |
| demos/client-example-server/package.json | Added @prosopo/database dependency |
| demos/client-example-server/tsconfig.json | Added database package reference |
| demos/client-example-server/tsconfig.cjs.json | Added database package reference |
| .changeset/humble-lions-appear.md | Changeset documenting the standardization |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| it("returns empty array for local hostnames without dots", () => { | ||
| const urls = [ | ||
| "mongodb://database:27017/db", | ||
| "mongodb://db/db", | ||
| "mongodb://mongo:27017/db", | ||
| "mongodb://container-name/db", | ||
| ]; | ||
| urls.forEach((url) => { | ||
| expect(getMongoCompressors(url)).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it("returns empty array for localhost variants", () => { | ||
| const urls = [ | ||
| "mongodb://localhost:27017/db", | ||
| "mongodb://127.0.0.1:27017/db", | ||
| "mongodb://[::1]:27017/db", | ||
| "mongodb://::1:27017/db", | ||
| ]; | ||
| urls.forEach((url) => { | ||
| expect(getMongoCompressors(url)).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it("returns compressors for IP addresses (treated as remote)", () => { | ||
| const urls = [ | ||
| "mongodb://192.168.1.1:27017/db", | ||
| "mongodb://10.0.0.1/db", | ||
| "mongodb://172.16.0.1:27017/db", | ||
| ]; | ||
| const expected = ["zstd", "snappy", "zlib", "none"]; | ||
| urls.forEach((url) => { | ||
| expect(getMongoCompressors(url)).toEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| it("returns compressors for IPv6 addresses (treated as remote)", () => { | ||
| const urls = [ | ||
| "mongodb://[2001:0db8::1]:27017/db", | ||
| "mongodb://[2001:0db8:85a3::8a2e:0370:7334]/db", | ||
| "mongodb://[::ffff:192.168.1.1]:27017/db", | ||
| ]; | ||
| const expected = ["zstd", "snappy", "zlib", "none"]; | ||
| urls.forEach((url) => { | ||
| expect(getMongoCompressors(url)).toEqual(expected); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Inconsistent indentation: this test block and several others use tabs while line 18 uses spaces. Use consistent indentation (spaces) throughout the file to match the initial test.
| it("returns empty array for local hostnames without dots", () => { | |
| const urls = [ | |
| "mongodb://database:27017/db", | |
| "mongodb://db/db", | |
| "mongodb://mongo:27017/db", | |
| "mongodb://container-name/db", | |
| ]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual([]); | |
| }); | |
| }); | |
| it("returns empty array for localhost variants", () => { | |
| const urls = [ | |
| "mongodb://localhost:27017/db", | |
| "mongodb://127.0.0.1:27017/db", | |
| "mongodb://[::1]:27017/db", | |
| "mongodb://::1:27017/db", | |
| ]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual([]); | |
| }); | |
| }); | |
| it("returns compressors for IP addresses (treated as remote)", () => { | |
| const urls = [ | |
| "mongodb://192.168.1.1:27017/db", | |
| "mongodb://10.0.0.1/db", | |
| "mongodb://172.16.0.1:27017/db", | |
| ]; | |
| const expected = ["zstd", "snappy", "zlib", "none"]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual(expected); | |
| }); | |
| }); | |
| it("returns compressors for IPv6 addresses (treated as remote)", () => { | |
| const urls = [ | |
| "mongodb://[2001:0db8::1]:27017/db", | |
| "mongodb://[2001:0db8:85a3::8a2e:0370:7334]/db", | |
| "mongodb://[::ffff:192.168.1.1]:27017/db", | |
| ]; | |
| const expected = ["zstd", "snappy", "zlib", "none"]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual(expected); | |
| }); | |
| }); | |
| it("returns empty array for local hostnames without dots", () => { | |
| const urls = [ | |
| "mongodb://database:27017/db", | |
| "mongodb://db/db", | |
| "mongodb://mongo:27017/db", | |
| "mongodb://container-name/db", | |
| ]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual([]); | |
| }); | |
| }); | |
| it("returns empty array for localhost variants", () => { | |
| const urls = [ | |
| "mongodb://localhost:27017/db", | |
| "mongodb://127.0.0.1:27017/db", | |
| "mongodb://[::1]:27017/db", | |
| "mongodb://::1:27017/db", | |
| ]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual([]); | |
| }); | |
| }); | |
| it("returns compressors for IP addresses (treated as remote)", () => { | |
| const urls = [ | |
| "mongodb://192.168.1.1:27017/db", | |
| "mongodb://10.0.0.1/db", | |
| "mongodb://172.16.0.1:27017/db", | |
| ]; | |
| const expected = ["zstd", "snappy", "zlib", "none"]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual(expected); | |
| }); | |
| }); | |
| it("returns compressors for IPv6 addresses (treated as remote)", () => { | |
| const urls = [ | |
| "mongodb://[2001:0db8::1]:27017/db", | |
| "mongodb://[2001:0db8:85a3::8a2e:0370:7334]/db", | |
| "mongodb://[::ffff:192.168.1.1]:27017/db", | |
| ]; | |
| const expected = ["zstd", "snappy", "zlib", "none"]; | |
| urls.forEach((url) => { | |
| expect(getMongoCompressors(url)).toEqual(expected); | |
| }); | |
| }); |
| hostname === "::1" || | ||
| hostname === "[::1]"; | ||
|
|
||
| // If it's localhost, no compression | ||
| if (isLocalhost) { | ||
| return []; | ||
| } | ||
|
|
||
| // Domains and IP addresses (IPv4/IPv6) include dots or colons | ||
| return hostname.includes(".") || | ||
| hostname.includes(":") |
There was a problem hiding this comment.
The logic for detecting IPv6 addresses is flawed. Hostnames containing colons will trigger compression, but the parsedUrl.hostname will have brackets stripped for IPv6 addresses (e.g., [::1] becomes ::1). This means link-local IPv6 addresses like ::1 won't match the localhost check on line 47 (hostname === "[::1]"), causing ::1 to incorrectly trigger compression instead of being treated as localhost.
| hostname === "::1" || | |
| hostname === "[::1]"; | |
| // If it's localhost, no compression | |
| if (isLocalhost) { | |
| return []; | |
| } | |
| // Domains and IP addresses (IPv4/IPv6) include dots or colons | |
| return hostname.includes(".") || | |
| hostname.includes(":") | |
| hostname === "::1"; | |
| // If it's localhost, no compression | |
| if (isLocalhost) { | |
| return []; | |
| } | |
| // Domains and IP addresses (IPv4/IPv6) include dots or colons, | |
| // but exclude localhost variants | |
| return (hostname.includes(".") || hostname.includes(":")) |
| } = options; | ||
|
|
||
| if (!url || url.trim() === "") { | ||
| throw new Error("MongoDB connection URL is required and cannot be empty"); |
There was a problem hiding this comment.
The error message mentions "cannot be empty" but the check url.trim() === "" actually validates whitespace-only strings, not empty strings. Update the message to: "MongoDB connection URL is required and cannot be empty or whitespace-only".
| throw new Error("MongoDB connection URL is required and cannot be empty"); | |
| throw new Error("MongoDB connection URL is required and cannot be empty or whitespace-only"); |