Skip to content

Commit

Permalink
feat(NODE-4607): add exports needed by legacy client (#3396)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Sep 8, 2022
1 parent 5676f81 commit 972f760
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/bulk/ordered.ts
Expand Up @@ -8,6 +8,7 @@ import { Batch, BatchType, BulkOperationBase, BulkWriteOptions } from './common'

/** @public */
export class OrderedBulkOperation extends BulkOperationBase {
/** @internal */
constructor(collection: Collection, options: BulkWriteOptions) {
super(collection, options, true);
}
Expand Down
1 change: 1 addition & 0 deletions src/bulk/unordered.ts
Expand Up @@ -9,6 +9,7 @@ import { Batch, BatchType, BulkOperationBase, BulkWriteOptions, BulkWriteResult

/** @public */
export class UnorderedBulkOperation extends BulkOperationBase {
/** @internal */
constructor(collection: Collection, options: BulkWriteOptions) {
super(collection, options, false);
}
Expand Down
3 changes: 2 additions & 1 deletion src/gridfs/download.ts
Expand Up @@ -103,11 +103,12 @@ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableS
*/
static readonly CLOSE = 'close' as const;

/** @internal
/**
* @param chunks - Handle for chunks collection
* @param files - Handle for files collection
* @param readPreference - The read preference to use
* @param filter - The filter to use to find the file document
* @internal
*/
constructor(
chunks: Collection<GridFSChunk>,
Expand Down
3 changes: 2 additions & 1 deletion src/gridfs/upload.ts
Expand Up @@ -69,10 +69,11 @@ export class GridFSBucketWriteStream extends Writable implements NodeJS.Writable
*/
static readonly FINISH = 'finish';

/** @internal
/**
* @param bucket - Handle for this stream's corresponding bucket
* @param filename - The value of the 'filename' key in the files doc
* @param options - Optional settings.
* @internal
*/
constructor(bucket: GridFSBucket, filename: string, options?: GridFSBucketWriteStreamOptions) {
super();
Expand Down
22 changes: 12 additions & 10 deletions src/index.ts
@@ -1,5 +1,7 @@
import { Admin } from './admin';
import { ObjectId } from './bson';
import { OrderedBulkOperation } from './bulk/ordered';
import { UnorderedBulkOperation } from './bulk/unordered';
import { ChangeStream } from './change_stream';
import { Collection } from './collection';
import { AbstractCursor } from './cursor/abstract_cursor';
Expand All @@ -9,10 +11,13 @@ import { ListCollectionsCursor } from './cursor/list_collections_cursor';
import { ListIndexesCursor } from './cursor/list_indexes_cursor';
import { Db } from './db';
import { GridFSBucket } from './gridfs';
import { GridFSBucketReadStream } from './gridfs/download';
import { GridFSBucketWriteStream } from './gridfs/upload';
import { Logger } from './logger';
import { MongoClient } from './mongo_client';
import { CancellationToken } from './mongo_types';
import { PromiseProvider } from './promise_provider';
import { ClientSession } from './sessions';

/** @internal */
export { BSON } from './bson';
Expand Down Expand Up @@ -80,16 +85,21 @@ export {
AggregationCursor,
CancellationToken,
ChangeStream,
ClientSession,
Collection,
Db,
FindCursor,
GridFSBucket,
GridFSBucketReadStream,
GridFSBucketWriteStream,
ListCollectionsCursor,
ListIndexesCursor,
Logger,
MongoClient,
OrderedBulkOperation,
// Utils
PromiseProvider as Promise
PromiseProvider as Promise,
UnorderedBulkOperation
};

// enums
Expand Down Expand Up @@ -172,8 +182,6 @@ export type {
FindOperators,
WriteConcernErrorData
} from './bulk/common';
export type { OrderedBulkOperation } from './bulk/ordered';
export type { UnorderedBulkOperation } from './bulk/unordered';
export type {
ChangeStreamCollModDocument,
ChangeStreamCreateDocument,
Expand Down Expand Up @@ -269,18 +277,13 @@ export type { Encrypter, EncrypterOptions } from './encrypter';
export type { AnyError, ErrorDescription, MongoNetworkErrorOptions } from './error';
export type { Explain, ExplainOptions, ExplainVerbosityLike } from './explain';
export type {
GridFSBucketReadStream,
GridFSBucketReadStreamOptions,
GridFSBucketReadStreamOptionsWithRevision,
GridFSBucketReadStreamPrivate,
GridFSFile
} from './gridfs/download';
export type { GridFSBucketEvents, GridFSBucketOptions, GridFSBucketPrivate } from './gridfs/index';
export type {
GridFSBucketWriteStream,
GridFSBucketWriteStreamOptions,
GridFSChunk
} from './gridfs/upload';
export type { GridFSBucketWriteStreamOptions, GridFSChunk } from './gridfs/upload';
export type { LoggerFunction, LoggerOptions } from './logger';
export type {
Auth,
Expand Down Expand Up @@ -454,7 +457,6 @@ export type {
} from './sdam/topology';
export type { TopologyDescription, TopologyDescriptionOptions } from './sdam/topology_description';
export type {
ClientSession,
ClientSessionEvents,
ClientSessionOptions,
EndSessionOptions,
Expand Down
14 changes: 14 additions & 0 deletions test/tools/utils.ts
Expand Up @@ -503,3 +503,17 @@ export function isBSONExtImported() {
const driverBSON = require('../../src/bson');
return driverBSON.deserialize.toString().includes('[native code]');
}

export const byStrings = (a: any, b: any) => {
const res = `${a}`.localeCompare(`${b}`);
return res < 0 ? -1 : res > 0 ? 1 : 0;
};

export const sorted = <T>(iterable: Iterable<T>, how: (a: T, b: T) => 0 | 1 | -1) => {
if (typeof how !== 'function') {
throw new TypeError('must provide a "how" function to sorted');
}
const items = Array.from(iterable);
items.sort(how);
return items;
};
142 changes: 142 additions & 0 deletions test/unit/index.test.ts
@@ -0,0 +1,142 @@
import { expect } from 'chai';

import * as mongodb from '../../src/index';
import { byStrings, sorted } from '../tools/utils';

/**
* TS-NODE Adds these keys but they are undefined, they are not present when you import from lib
* We did not think this strangeness was worth investigating so we just make sure they remain set to undefined
*/
const TS_NODE_EXPORTS = ['AnyBulkWriteOperation', 'BulkWriteOptions'];

const EXPECTED_EXPORTS = [
...TS_NODE_EXPORTS,
'AbstractCursor',
'Admin',
'AggregationCursor',
'AuthMechanism',
'AutoEncryptionLoggerLevel',
'BatchType',
'Binary',
'BSON',
'BSONRegExp',
'BSONSymbol',
'BSONType',
'CancellationToken',
'ChangeStream',
'ChangeStreamCursor',
'ClientSession',
'Code',
'Collection',
'CommandFailedEvent',
'CommandStartedEvent',
'CommandSucceededEvent',
'Compressor',
'ConnectionCheckedInEvent',
'ConnectionCheckedOutEvent',
'ConnectionCheckOutFailedEvent',
'ConnectionCheckOutStartedEvent',
'ConnectionClosedEvent',
'ConnectionCreatedEvent',
'ConnectionPoolClearedEvent',
'ConnectionPoolClosedEvent',
'ConnectionPoolCreatedEvent',
'ConnectionPoolMonitoringEvent',
'ConnectionReadyEvent',
'CURSOR_FLAGS',
'Db',
'DBRef',
'Decimal128',
'Double',
'ExplainVerbosity',
'FindCursor',
'GridFSBucket',
'GridFSBucketReadStream',
'GridFSBucketWriteStream',
'GSSAPICanonicalizationValue',
'Int32',
'ListCollectionsCursor',
'ListIndexesCursor',
'Logger',
'LoggerLevel',
'Long',
'Map',
'MaxKey',
'MinKey',
'MongoAPIError',
'MongoAWSError',
'MongoBatchReExecutionError',
'MongoBulkWriteError',
'MongoChangeStreamError',
'MongoClient',
'MongoCompatibilityError',
'MongoCursorExhaustedError',
'MongoCursorInUseError',
'MongoDecompressionError',
'MongoDriverError',
'MongoError',
'MongoErrorLabel',
'MongoExpiredSessionError',
'MongoGridFSChunkError',
'MongoGridFSStreamError',
'MongoInvalidArgumentError',
'MongoKerberosError',
'MongoMissingCredentialsError',
'MongoMissingDependencyError',
'MongoNetworkError',
'MongoNetworkTimeoutError',
'MongoNotConnectedError',
'MongoParseError',
'MongoRuntimeError',
'MongoServerClosedError',
'MongoServerError',
'MongoServerSelectionError',
'MongoSystemError',
'MongoTailableCursorError',
'MongoTopologyClosedError',
'MongoTransactionError',
'MongoUnexpectedServerResponseError',
'MongoWriteConcernError',
'ObjectId',
'ObjectID',
'OrderedBulkOperation',
'ProfilingLevel',
'Promise',
'ReadConcern',
'ReadConcernLevel',
'ReadPreference',
'ReadPreferenceMode',
'ReturnDocument',
'ServerApiVersion',
'ServerClosedEvent',
'ServerDescriptionChangedEvent',
'ServerHeartbeatFailedEvent',
'ServerHeartbeatStartedEvent',
'ServerHeartbeatSucceededEvent',
'ServerOpeningEvent',
'ServerType',
'SrvPollingEvent',
'Timestamp',
'TopologyClosedEvent',
'TopologyDescriptionChangedEvent',
'TopologyOpeningEvent',
'TopologyType',
'UnorderedBulkOperation',
'WriteConcern'
];

describe('mongodb entrypoint', () => {
it('should export all and only the expected keys in expected_exports', () => {
expect(sorted(Object.keys(mongodb), byStrings)).to.deep.equal(
sorted(EXPECTED_EXPORTS, byStrings)
);
});

it('should export keys added by ts-node as undefined', () => {
// If the array is empty, this test would be a no-op so we should remove it
expect(TS_NODE_EXPORTS).to.have.length.greaterThan(0);
for (const tsNodeExportKey of TS_NODE_EXPORTS) {
expect(mongodb).to.have.property(tsNodeExportKey, undefined);
}
});
});

0 comments on commit 972f760

Please sign in to comment.