From 550d68f2309aa77e5461aeb5f10f7ed79411b764 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 16 Apr 2021 00:49:30 +0200 Subject: [PATCH] feat(shell-api): add EJSON global MONGOSH-670 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This has the catch that it’s not going to work fully until https://jira.mongodb.org/browse/NODE-3208 is fixed (but I wouldn’t have caught that without trying it out here, so it’s still good that I did, I guess). --- packages/service-provider-core/src/index.ts | 6 ++++-- .../src/cli-service-provider.ts | 1 + packages/shell-api/src/shell-bson.spec.ts | 8 ++++++++ packages/shell-api/src/shell-bson.ts | 18 ++++++++++-------- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/service-provider-core/src/index.ts b/packages/service-provider-core/src/index.ts index 4f57e30265..2fcb101785 100644 --- a/packages/service-provider-core/src/index.ts +++ b/packages/service-provider-core/src/index.ts @@ -18,7 +18,8 @@ import { Binary, Map, calculateObjectSize, - Double + Double, + EJSON } from 'bson'; import { bsonStringifiers } from './printable-bson'; import ShellAuthOptions from './shell-auth-options'; @@ -40,7 +41,8 @@ const bson = { Binary, Map, calculateObjectSize, - Double + Double, + EJSON }; export { diff --git a/packages/service-provider-server/src/cli-service-provider.ts b/packages/service-provider-server/src/cli-service-provider.ts index cb0396a978..39270359b0 100644 --- a/packages/service-provider-server/src/cli-service-provider.ts +++ b/packages/service-provider-server/src/cli-service-provider.ts @@ -95,6 +95,7 @@ const bsonlib = { BSONSymbol, Map: BSON.Map, calculateObjectSize: BSON.calculateObjectSize, + EJSON: BSON.EJSON }; type DropDatabaseResult = { diff --git a/packages/shell-api/src/shell-bson.spec.ts b/packages/shell-api/src/shell-bson.spec.ts index a52ec5584d..73627e21d2 100644 --- a/packages/shell-api/src/shell-bson.spec.ts +++ b/packages/shell-api/src/shell-bson.spec.ts @@ -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); + }); + }); }); diff --git a/packages/shell-api/src/shell-bson.ts b/packages/shell-api/src/shell-bson.ts index e3ee2a7dac..b08bc68cc0 100644 --- a/packages/shell-api/src/shell-bson.ts +++ b/packages/shell-api/src/shell-bson.ts @@ -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) { @@ -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' ]; @@ -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) => {