Skip to content

Conversation

@goastler
Copy link
Member

@goastler 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

Copilot AI review requested due to automatic review settings December 4, 2025 14:10
@goastler goastler self-assigned this Dec 4, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 getMongoConnectionOptions utility function with comprehensive default connection settings
  • Implemented getMongoCompressors to automatically determine compression settings based on URL (remote connections use compression, local/container connections don't)
  • Updated connection setup in MongoDatabase and client-example-server to 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.

Comment on lines +32 to +78
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);
});
});
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);
});
});

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +57
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(":")
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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(":"))

Copilot uses AI. Check for mistakes.
} = options;

if (!url || url.trim() === "") {
throw new Error("MongoDB connection URL is required and cannot be empty");
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Suggested change
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");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants