Skip to content

Commit 8778808

Browse files
authored
feat(shell-api): expose BSONRegExp to users MONGOSH-859 (#977)
1 parent d629647 commit 8778808

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

packages/service-provider-core/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
Map,
2121
calculateObjectSize,
2222
Double,
23-
EJSON
23+
EJSON,
24+
BSONRegExp
2425
} from 'bson';
2526
import { bsonStringifiers } from './printable-bson';
2627
import ShellAuthOptions from './shell-auth-options';
@@ -43,7 +44,8 @@ const bson = {
4344
Map,
4445
calculateObjectSize,
4546
Double,
46-
EJSON
47+
EJSON,
48+
BSONRegExp
4749
};
4850

4951
export {

packages/service-provider-core/src/printable-bson.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ describe('BSON printers', function() {
5555
expect(inspect(new bson.Binary('abc'))).to.equal('Binary(Buffer.from("616263", "hex"), 0)');
5656
});
5757

58+
it('formats BSONRegExp correctly', function() {
59+
expect(inspect(new bson.BSONRegExp('(?-i)AA_', 'im'))).to.equal('BSONRegExp("(?-i)AA_", "im")');
60+
});
61+
5862
it('formats UUIDs correctly', function() {
5963
expect(inspect(new bson.Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4)))
6064
.to.equal('UUID("01234567-89ab-cdef-0123-456789abcdef")');

packages/service-provider-core/src/printable-bson.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export const bsonStringifiers: Record<string, (this: any) => string> = {
4848
return `Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
4949
},
5050

51+
BSONRegExp: function(): string {
52+
return `BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
53+
},
54+
5155
Binary: function(): string {
5256
const asBuffer = this.value(true);
5357
switch (this.sub_type) {

packages/service-provider-server/src/cli-service-provider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
AuthMechanism,
44
MongoClient,
55
ReadPreference,
6+
BSONRegExp,
67
Binary,
78
Code,
89
DBRef,
@@ -101,7 +102,8 @@ const bsonlib = {
101102
BSONSymbol,
102103
Map: BSON.Map,
103104
calculateObjectSize: BSON.calculateObjectSize,
104-
EJSON: BSON.EJSON
105+
EJSON: BSON.EJSON,
106+
BSONRegExp
105107
};
106108

107109
type DropDatabaseResult = {

packages/shell-api/src/shell-bson.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function constructHelp(className: string): Help {
2323
export default function constructShellBson(bson: typeof BSON, printWarning: (msg: string) => void): any {
2424
const bsonNames = [
2525
'Binary', 'Code', 'DBRef', 'Decimal128', 'Double', 'Int32', 'Long',
26-
'MaxKey', 'MinKey', 'ObjectId', 'Timestamp', 'Map', 'BSONSymbol'
26+
'MaxKey', 'MinKey', 'ObjectId', 'Timestamp', 'Map', 'BSONSymbol',
27+
'BSONRegExp'
2728
] as const; // Statically set this so we can error if any are missing
2829

2930
// If the service provider doesn't provide a BSON version, use service-provider-core's BSON package (js-bson 4.x)
@@ -153,7 +154,8 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg
153154
Long: bson.Long,
154155
Binary: bson.Binary,
155156
Double: bson.Double,
156-
EJSON: bson.EJSON
157+
EJSON: bson.EJSON,
158+
BSONRegExp: bson.BSONRegExp
157159
} as any;
158160

159161
Object.keys(bsonPkg).forEach((className) => {

0 commit comments

Comments
 (0)