Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-2032

Type BSON expressions and include them in shell-api TS definitions

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Developer Tools

      We extend the Shell API TS definitions created in MONGOSH-2031 with definitions for the shell variants of the BSON constructors. This requires properly typing the bsonPkg object in order to provide reasonable definitions to consumers of it.

      When post-processing api-raw.d.ts into api-processed.d.ts, declare global functions for all the methods inside the ShellBsonBase interface since we assign those straight onto the shell's global context.

      Example:

      ~/mongo/mongosh % cat packages/shell-api/lib/shell-bson.d.ts
      /// <reference types="node" />
      import Help from './help';
      import type { BinaryType, Document } from '@mongosh/service-provider-core';
      import { bson as BSON } from '@mongosh/service-provider-core';
      type LongWithoutAccidentallyExposedMethods = Omit<typeof BSON.Long, 'fromExtendedJSON'>;
      interface ShellBsonBase {
          DBRef: (namespace: string, oid: any, db?: string, fields?: Document) => typeof BSON.DBRef.prototype;
          bsonsize: (object: any) => number;
          MaxKey: () => typeof BSON.MaxKey.prototype;
          MinKey: () => typeof BSON.MinKey.prototype;
          ObjectId: (id?: string | number | typeof BSON.ObjectId.prototype | Buffer) => typeof BSON.ObjectId.prototype;
          Timestamp: (t?: number | typeof BSON.Long.prototype | {
              t: number;
              i: number;
          }, i?: number) => typeof BSON.Timestamp.prototype;
          Code: (c?: string | Function, s?: any) => typeof BSON.Code.prototype;
          NumberDecimal: (s?: string) => typeof BSON.Decimal128.prototype;
          NumberInt: (v?: string) => typeof BSON.Int32.prototype;
          NumberLong: (s?: string | number) => typeof BSON.Long.prototype;
          ISODate: (input?: string) => Date;
          BinData: (subtype: number, b64string: string) => BinaryType;
          HexData: (subtype: number, hexstr: string) => BinaryType;
          UUID: (hexstr?: string) => BinaryType;
          MD5: (hexstr: string) => BinaryType;
          Decimal128: typeof BSON.Decimal128;
          BSONSymbol: typeof BSON.BSONSymbol;
          Int32: typeof BSON.Int32;
          Long: LongWithoutAccidentallyExposedMethods;
          Binary: typeof BSON.Binary;
          Double: typeof BSON.Double;
          EJSON: typeof BSON.EJSON;
          BSONRegExp: typeof BSON.BSONRegExp;
      }
      type WithHelp<T> = {
          [prop in keyof T]: T[prop] & {
              help?: () => Help;
          };
      };
      export type ShellBson = WithHelp<ShellBsonBase>;
      export default function constructShellBson(bson: typeof BSON, printWarning: (msg: string) => void): ShellBson;
      export {}
      

      Then we'll append the following code to api-processed.d.ts:

      declare global {
         const UUID: (hexstr?: string) => BinaryType;
         // etc.
      }
      

      Probably with some added

      & { help?: () => Help }
      

            Assignee:
            leroux.bodenstein@mongodb.com Le Roux Bodenstein
            Reporter:
            anna.henningsen@mongodb.com Anna Henningsen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: