From 465722c743bd737a4bac44b838e84e44960d5d1b Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Tue, 5 May 2026 10:38:58 +0200 Subject: [PATCH 1/3] feat(mongodb-ns): add explicit flag for internal dbs --- packages/mongodb-ns/src/index.spec.ts | 7 +++++++ packages/mongodb-ns/src/index.ts | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/mongodb-ns/src/index.spec.ts b/packages/mongodb-ns/src/index.spec.ts index f959c2f8..8cced8b6 100644 --- a/packages/mongodb-ns/src/index.spec.ts +++ b/packages/mongodb-ns/src/index.spec.ts @@ -96,6 +96,13 @@ describe('ns', function () { }); }); + it('should identify internal namespaces', function () { + assert.equal(ns('a').internal, false); + assert.equal(ns('a.b').isInternal(), false); + assert(ns('__mdb_internal').internal); + assert(ns('__mdb_internal_foo_bar').isInternal()); + }); + describe('database name validation', function () { it('should accept `foo` as a valid database name', function () { assert.equal(ns('foo').validDatabaseName, true); diff --git a/packages/mongodb-ns/src/index.ts b/packages/mongodb-ns/src/index.ts index 33a76e8c..0f5c5bbc 100644 --- a/packages/mongodb-ns/src/index.ts +++ b/packages/mongodb-ns/src/index.ts @@ -14,6 +14,8 @@ type NS = { specialish: boolean; normal: boolean; isNormal(): boolean; + internal: boolean; + isInternal(): boolean; validDatabaseName: boolean; validCollectionName: boolean; databaseHash: number; @@ -47,6 +49,8 @@ const NS: NSConstructor = function (this: NS, ns: string | NS): NS { this.collection = ns.slice(this.dotIndex + 1); } + this.internal = /^__mdb_internal_\w/.test(this.database); + this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection); this.oplog = /local\.oplog\.(\$main|rs)/.test(ns); @@ -58,7 +62,7 @@ const NS: NSConstructor = function (this: NS, ns: string | NS): NS { this.command || this.system || this.database === 'config' || - /^__mdb_internal_\w/.test(this.database); + this.internal; this.specialish = this.special || ['local', 'admin'].indexOf(this.database) > -1; @@ -98,6 +102,7 @@ NS.prototype.system = false; NS.prototype.oplog = false; NS.prototype.normal = false; NS.prototype.specialish = false; +NS.prototype.internal = false; NS.prototype.isCommand = function () { return this.command; @@ -114,6 +119,9 @@ NS.prototype.isNormal = function () { NS.prototype.isOplog = function () { return this.oplog; }; +NS.prototype.isInternal = function () { + return this.internal; +}; NS.prototype.isConf = function () { return undefined; }; From 2b0b13160074cae564095e966b79b943f6e5ed3a Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Tue, 5 May 2026 11:43:11 +0200 Subject: [PATCH 2/3] chore: adjust test and add doc link --- packages/mongodb-ns/src/index.spec.ts | 2 +- packages/mongodb-ns/src/index.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/mongodb-ns/src/index.spec.ts b/packages/mongodb-ns/src/index.spec.ts index 8cced8b6..a83a7bcd 100644 --- a/packages/mongodb-ns/src/index.spec.ts +++ b/packages/mongodb-ns/src/index.spec.ts @@ -99,7 +99,7 @@ describe('ns', function () { it('should identify internal namespaces', function () { assert.equal(ns('a').internal, false); assert.equal(ns('a.b').isInternal(), false); - assert(ns('__mdb_internal').internal); + assert(ns('__mdb_internal_bla').internal); assert(ns('__mdb_internal_foo_bar').isInternal()); }); diff --git a/packages/mongodb-ns/src/index.ts b/packages/mongodb-ns/src/index.ts index 0f5c5bbc..0ac0aec7 100644 --- a/packages/mongodb-ns/src/index.ts +++ b/packages/mongodb-ns/src/index.ts @@ -49,6 +49,7 @@ const NS: NSConstructor = function (this: NS, ns: string | NS): NS { this.collection = ns.slice(this.dotIndex + 1); } + // https://www.mongodb.com/docs/atlas/reference/internal-database/#internal-databases this.internal = /^__mdb_internal_\w/.test(this.database); this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection); From 028dcb3dfe9c069865ca86e8830483ae46499d1d Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Tue, 5 May 2026 15:33:23 +0200 Subject: [PATCH 3/3] chore: add a test case for mdb_internal not followed by service name Co-authored-by: Neal Beeken --- packages/mongodb-ns/src/index.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/mongodb-ns/src/index.spec.ts b/packages/mongodb-ns/src/index.spec.ts index a83a7bcd..a0b89ab3 100644 --- a/packages/mongodb-ns/src/index.spec.ts +++ b/packages/mongodb-ns/src/index.spec.ts @@ -99,6 +99,8 @@ describe('ns', function () { it('should identify internal namespaces', function () { assert.equal(ns('a').internal, false); assert.equal(ns('a.b').isInternal(), false); + assert.equal(ns('__mdb_internal.b').isInternal(), false); + assert.equal(ns('__mdb_internal').isInternal(), false); assert(ns('__mdb_internal_bla').internal); assert(ns('__mdb_internal_foo_bar').isInternal()); });