diff --git a/packages/mongodb-ns/src/index.spec.ts b/packages/mongodb-ns/src/index.spec.ts index f959c2f8..a0b89ab3 100644 --- a/packages/mongodb-ns/src/index.spec.ts +++ b/packages/mongodb-ns/src/index.spec.ts @@ -96,6 +96,15 @@ 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()); + }); + 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..0ac0aec7 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,9 @@ 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); this.oplog = /local\.oplog\.(\$main|rs)/.test(ns); @@ -58,7 +63,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 +103,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 +120,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; };