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
6 changes: 4 additions & 2 deletions packages/service-provider-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
Binary,
Map,
calculateObjectSize,
Double
Double,
EJSON
} from 'bson';
import { bsonStringifiers } from './printable-bson';
import ShellAuthOptions from './shell-auth-options';
Expand All @@ -40,7 +41,8 @@ const bson = {
Binary,
Map,
calculateObjectSize,
Double
Double,
EJSON
};

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const bsonlib = {
BSONSymbol,
Map: BSON.Map,
calculateObjectSize: BSON.calculateObjectSize,
EJSON: BSON.EJSON
};

type DropDatabaseResult = {
Expand Down
8 changes: 8 additions & 0 deletions packages/shell-api/src/shell-bson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,12 @@ describe('Shell BSON', () => {
expect.fail('Expecting error, nothing thrown');
});
});

describe('EJSON', () => {
it('serializes and de-serializes data', () => {
const input = { a: new Date() };
const output = shellBson.EJSON.parse(shellBson.EJSON.stringify(input));
expect(input).to.deep.equal(output);
});
});
});
18 changes: 10 additions & 8 deletions packages/shell-api/src/shell-bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function constructHelp(className: string): Help {
* we can have help, serverVersions, and other metadata on the bson classes constructed by the user.
*/
export default function constructShellBson(bson: typeof BSON, printWarning: (msg: string) => void): any {
const bsonNames: (keyof typeof BSON)[] = [
const bsonNames = [
'Binary', 'Code', 'DBRef', 'Decimal128', 'Double', 'Int32', 'Long',
'MaxKey', 'MinKey', 'ObjectId', 'Timestamp', 'Map', 'BSONSymbol'
]; // Statically set this so we can error if any are missing
] as const; // Statically set this so we can error if any are missing

// If the service provider doesn't provide a BSON version, use service-provider-core's BSON package (js-bson 4.x)
if (bson === undefined) {
Expand All @@ -35,14 +35,15 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg
if (!(className in bson)) {
throw new MongoshInternalError(`${className} does not exist in provided BSON package.`);
}
bson[className].prototype.serverVersions = ALL_SERVER_VERSIONS;
bson[className].prototype.platforms = ALL_PLATFORMS;
bson[className].prototype.topologies = ALL_TOPOLOGIES;
const proto = bson[className].prototype as any;
proto.serverVersions = ALL_SERVER_VERSIONS;
proto.platforms = ALL_PLATFORMS;
proto.topologies = ALL_TOPOLOGIES;

const help = constructHelp(className);
helps[className] = help;
bson[className].prototype.help = (): Help => (help);
Object.setPrototypeOf(bson[className].prototype.help, help);
proto.help = (): Help => (help);
Object.setPrototypeOf(proto.help, help);
});
// Symbol is deprecated
(bson.BSONSymbol as any).prototype.serverVersions = [ ServerVersions.earliest, '1.6.0' ];
Expand Down Expand Up @@ -154,7 +155,8 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg
Int32: bson.Int32,
Long: bson.Long,
Binary: bson.Binary,
Double: bson.Double
Double: bson.Double,
EJSON: bson.EJSON
} as any;

Object.keys(bsonPkg).forEach((className) => {
Expand Down