Skip to content

Commit d750b53

Browse files
committed
Merge branch 'release/7.2.0'
2 parents 10a6256 + fd2f8a0 commit d750b53

6 files changed

Lines changed: 288 additions & 36 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ node_modules/
33
repomix*
44
CLAUDE.md
55
ROADMAP.md
6-
.mongodb-lens.json*
6+
.mongodb-lens*.json*

README.md

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ MongoDB Lens is now installed and ready to accept MCP requests.
319319

320320
- [MongoDB Connection String](#configuration-mongodb-connection-string)
321321
- [Config File](#configuration-config-file)
322+
- [Config File Generation](#configuration-config-file-generation)
322323
- [Multiple MongoDB Connections](#configuration-multiple-mongodb-connections)
323324
- [Environment Variable Overrides](#configuration-environment-variable-overrides)
325+
- [Cross-Platform Environment Variables](#configuration-cross-platform-environment-variables)
324326

325327
### Configuration: MongoDB Connection String
326328

@@ -357,7 +359,10 @@ MongoDB Lens supports extensive customization via JSON configuration file.
357359
> The config file is optional. MongoDB Lens will run with default settings if no config file is provided.
358360
359361
> [!TIP]<br>
360-
> MongoDB Lens supports both `.json` and `.jsonc` (JSON with comments) file formats.
362+
> You only need to include the settings you want to customize in the config file. MongoDB Lens will use default settings for any omitted values.
363+
364+
> [!TIP]<br>
365+
> MongoDB Lens supports both `.json` and `.jsonc` (JSON with comments) config file formats.
361366
362367
<details>
363368
<summary><strong>Example configuration file</strong></summary>
@@ -449,7 +454,10 @@ MongoDB Lens supports extensive customization via JSON configuration file.
449454

450455
</details>
451456

452-
By default, MongoDB Lens looks for the config file at: `~/.mongodb-lens.json`
457+
By default, MongoDB Lens looks for the config file at:
458+
459+
- `~/.mongodb-lens.jsonc` first, then falls back to
460+
- `~/.mongodb-lens.json` if the former doesn't exist
453461

454462
To customize the config file path, set the environment variable `CONFIG_PATH` to the desired file path.
455463

@@ -465,6 +473,66 @@ Example Docker Hub usage:
465473
docker run --rm -i --network=host --pull=always -v /path/to/config.json:/root/.mongodb-lens.json furey/mongodb-lens
466474
```
467475

476+
### Configuration: Config File Generation
477+
478+
You can generate a configuration file automatically using the `config:create` script:
479+
480+
```console
481+
# Create config file
482+
npm run config:create
483+
484+
# Create config file (force overwrite existing)
485+
npm run config:create -- --force
486+
```
487+
488+
This script extracts the [example configuration file](#configuration-config-file) above and saves it to: `~/.mongodb-lens.jsonc`
489+
490+
#### Config File Generation: Custom Path
491+
492+
You can specify a custom configuration file output location using the `CONFIG_PATH` environment variable:
493+
494+
```console
495+
# Save to a specific file path
496+
CONFIG_PATH=/path/to/config.jsonc npm run config:create
497+
498+
# Save to a specific directory (will append .mongodb-lens.jsonc to the path)
499+
CONFIG_PATH=/path/to/directory npm run config:create
500+
501+
# Save as JSON without comments
502+
CONFIG_PATH=/path/to/config.json npm run config:create
503+
```
504+
505+
The script handles paths intelligently:
506+
507+
- If `CONFIG_PATH` has no file extension, it's treated as a directory and `.mongodb-lens.jsonc` is appended
508+
- If `CONFIG_PATH` ends with `.json` (not `.jsonc`), comments are automatically stripped from the output
509+
510+
### Configuration: Multiple MongoDB Connections
511+
512+
MongoDB Lens supports multiple MongoDB URIs with aliases in your [config file](#configuration-config-file), allowing you to easily switch between different MongoDB instances using simple names.
513+
514+
To configure multiple connections, set the `mongoUri` config setting to an object with alias-URI pairs:
515+
516+
```json
517+
{
518+
"mongoUri": {
519+
"main": "mongodb://localhost:27017",
520+
"backup": "mongodb://localhost:27018",
521+
"atlas": "mongodb+srv://username:password@cluster.mongodb.net/mydb"
522+
}
523+
}
524+
```
525+
526+
With this configuration:
527+
528+
- The first URI in the list (e.g. `main`) becomes the default connection at startup
529+
- You can switch connections using natural language: `"Connect to backup"` or `"Connect to atlas"`
530+
- The original syntax still works: `"Connect to mongodb://localhost:27018"`
531+
- The `list-connections` tool shows all available connection aliases
532+
533+
> [!NOTE]<br>
534+
> When using the command-line argument to specify a connection, you can use either a full MongoDB URI or an alias defined in your configuration file.
535+
468536
### Configuration: Environment Variable Overrides
469537

470538
MongoDB Lens supports environment variable overrides for configuration settings.
@@ -507,31 +575,26 @@ Example Docker Hub usage:
507575
docker run --rm -i --network=host --pull=always -e CONFIG_DEFAULTS_QUERY_LIMIT='25' furey/mongodb-lens
508576
```
509577

510-
### Configuration: Multiple MongoDB Connections
578+
### Configuration: Cross-Platform Environment Variables
511579

512-
MongoDB Lens supports defining multiple MongoDB URIs with aliases in your [config file](#configuration-config-file), allowing you to easily switch between different MongoDB instances using simple names.
580+
For consistent environment variable usage across Windows, macOS, and Linux, consider using `cross-env`:
513581

514-
To configure multiple connections, set the `mongoUri` setting to an object with alias-URI pairs:
582+
1. Install cross-env globally:<br>
583+
```console
584+
# Using NPM
585+
npm install -g cross-env
515586

516-
```json
517-
{
518-
"mongoUri": {
519-
"main": "mongodb://localhost:27017",
520-
"backup": "mongodb://localhost:27018",
521-
"atlas": "mongodb+srv://username:password@cluster.mongodb.net/mydb"
522-
}
523-
}
524-
```
525-
526-
With this configuration:
587+
# Using Volta (see: https://volta.sh)
588+
volta install cross-env
589+
```
590+
1. Prefix any NPX or Node.js environment variables in this document's examples:<br>
591+
```console
592+
# Example NPX usage with cross-env
593+
cross-env CONFIG_DEFAULTS_QUERY_LIMIT='25' npx -y mongodb-lens@latest
527594

528-
- The first URI in the list (e.g. `main`) becomes the default connection at startup
529-
- You can switch connections using natural language: `"Connect to backup"` or `"Connect to atlas"`
530-
- The original syntax still works: `"Connect to mongodb://localhost:27018"`
531-
- The `list-connections` tool shows all available connection aliases
532-
533-
> [!NOTE]<br>
534-
> When using the command-line argument to specify a connection, you can use either a full MongoDB URI or an alias defined in your configuration file.
595+
# Example Node.js usage with cross-env
596+
cross-env CONFIG_DEFAULTS_QUERY_LIMIT='25' node mongodb-lens.js
597+
```
535598

536599
## Client Setup
537600

@@ -563,7 +626,7 @@ For each option:
563626
- Replace `mongodb://your-connection-string` with your MongoDB connection string or omit it to use the default `mongodb://localhost:27017`.
564627
- To use a custom config file, set [`CONFIG_PATH`](#configuration-config-file) environment variable.
565628
- To include environment variables:
566-
- For NPX or Node add `"env": {}` with key-value pairs, for example:<br>
629+
- For NPX or Node.js add `"env": {}` with key-value pairs, for example:<br>
567630
```json
568631
"command": "/path/to/npx",
569632
"args": [

config-create.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env node
2+
3+
import { join, dirname, extname, resolve } from 'path'
4+
import stripJsonComments from 'strip-json-comments'
5+
import { readFile, writeFile } from 'fs/promises'
6+
import { fileURLToPath } from 'url'
7+
import { existsSync } from 'fs'
8+
9+
const __filename = fileURLToPath(import.meta.url)
10+
const __dirname = dirname(__filename)
11+
12+
const run = async () => {
13+
const homeDir = process.env.HOME || process.env.USERPROFILE
14+
15+
let configPath = process.env.CONFIG_PATH || join(homeDir, '.mongodb-lens.jsonc')
16+
17+
if (!extname(configPath)) configPath = join(configPath, '.mongodb-lens.jsonc')
18+
19+
if (!configPath.endsWith('.json') && !configPath.endsWith('.jsonc')) {
20+
console.error('Error: Configuration file must have .json or .jsonc extension')
21+
process.exit(1)
22+
}
23+
24+
configPath = resolve(configPath)
25+
26+
try {
27+
const configContent = await extractConfigFromReadme()
28+
29+
if (existsSync(configPath)) {
30+
const overwrite = process.argv.includes('--force')
31+
if (!overwrite) {
32+
console.log(`Configuration file already exists at: ${configPath}`)
33+
console.log('Use --force to overwrite it.')
34+
return
35+
}
36+
console.log(`Overwriting existing configuration file at: ${configPath}`)
37+
}
38+
39+
let finalContent = configContent
40+
if (configPath.endsWith('.json')) finalContent = stripJsonComments(configContent)
41+
42+
finalContent = finalContent
43+
.split('\n')
44+
.map(line => line.trimEnd())
45+
.join('\n')
46+
47+
await writeFile(configPath, finalContent, 'utf8')
48+
console.log(`Configuration file created successfully at: ${configPath}`)
49+
} catch (error) {
50+
console.error('Error creating configuration file:', error.message)
51+
process.exit(1)
52+
}
53+
}
54+
55+
const extractConfigFromReadme = async () => {
56+
try {
57+
const possiblePaths = [
58+
join(__dirname, 'README.md'),
59+
join(__dirname, '..', 'README.md'),
60+
join(process.cwd(), 'README.md')
61+
]
62+
63+
let readmeContent = null
64+
65+
for (const path of possiblePaths) {
66+
if (existsSync(path)) {
67+
readmeContent = await readFile(path, 'utf8')
68+
console.log(`Found README at: ${path}`)
69+
break
70+
}
71+
}
72+
73+
if (!readmeContent) throw new Error('README.md not found in expected locations')
74+
75+
const configRegex = /Example configuration file[\s\S]*?```jsonc\s*([\s\S]*?)```/
76+
const match = readmeContent.match(configRegex)
77+
78+
if (!match || !match[1]) throw new Error('Could not find example configuration in README.md')
79+
80+
return match[1].trim()
81+
} catch (error) {
82+
console.error('Error extracting configuration from README:', error.message)
83+
throw error
84+
}
85+
}
86+
87+
run()

mongodb-lens.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5698,9 +5698,25 @@ const validateDropIndexToken = (collectionName, indexName, token) => {
56985698
const loadConfig = () => {
56995699
let config = { ...defaultConfig }
57005700

5701-
const configPath = process.env.CONFIG_PATH || join(process.env.HOME || __dirname, '.mongodb-lens.json')
5701+
const configPathEnv = process.env.CONFIG_PATH
57025702

5703-
if (existsSync(configPath)) {
5703+
let configPath = null
5704+
if (configPathEnv) {
5705+
configPath = configPathEnv
5706+
} else {
5707+
const homeDir = process.env.HOME || process.env.USERPROFILE || __dirname
5708+
5709+
const jsoncPath = join(homeDir, '.mongodb-lens.jsonc')
5710+
const jsonPath = join(homeDir, '.mongodb-lens.json')
5711+
5712+
if (existsSync(jsoncPath)) {
5713+
configPath = jsoncPath
5714+
} else if (existsSync(jsonPath)) {
5715+
configPath = jsonPath
5716+
}
5717+
}
5718+
5719+
if (configPath && existsSync(configPath)) {
57045720
try {
57055721
log(`Loading config file: ${configPath}`)
57065722
const fileContent = readFileSync(configPath, 'utf8')

0 commit comments

Comments
 (0)