Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/mongodb-ns/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
gribnoysup marked this conversation as resolved.
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);
Expand Down
11 changes: 10 additions & 1 deletion packages/mongodb-ns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type NS = {
specialish: boolean;
normal: boolean;
isNormal(): boolean;
internal: boolean;
isInternal(): boolean;
validDatabaseName: boolean;
validCollectionName: boolean;
databaseHash: number;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};
Expand Down
Loading