diff --git a/src/bulk/ordered.ts b/src/bulk/ordered.ts index e0dbf9b7e3..97ee6444d8 100644 --- a/src/bulk/ordered.ts +++ b/src/bulk/ordered.ts @@ -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); } diff --git a/src/bulk/unordered.ts b/src/bulk/unordered.ts index b4227e8d03..5f2e562f3c 100644 --- a/src/bulk/unordered.ts +++ b/src/bulk/unordered.ts @@ -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); } diff --git a/src/gridfs/download.ts b/src/gridfs/download.ts index a13cdac39d..c161333854 100644 --- a/src/gridfs/download.ts +++ b/src/gridfs/download.ts @@ -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, diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index ae0d678998..9e9b40aa07 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -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(); diff --git a/src/index.ts b/src/index.ts index 933be8772e..f432443c03 100644 --- a/src/index.ts +++ b/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'; @@ -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'; @@ -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 @@ -171,8 +181,6 @@ export type { FindOperators, WriteConcernErrorData } from './bulk/common'; -export type { OrderedBulkOperation } from './bulk/ordered'; -export type { UnorderedBulkOperation } from './bulk/unordered'; export type { ChangeStreamCollModDocument, ChangeStreamCreateDocument, @@ -268,18 +276,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, @@ -453,7 +456,6 @@ export type { } from './sdam/topology'; export type { TopologyDescription, TopologyDescriptionOptions } from './sdam/topology_description'; export type { - ClientSession, ClientSessionEvents, ClientSessionOptions, EndSessionOptions, diff --git a/test/tools/utils.ts b/test/tools/utils.ts index 24bed64c47..a32fa15803 100644 --- a/test/tools/utils.ts +++ b/test/tools/utils.ts @@ -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 = (iterable: Iterable, 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; +}; diff --git a/test/unit/index.test.ts b/test/unit/index.test.ts new file mode 100644 index 0000000000..98c2ced75e --- /dev/null +++ b/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); + } + }); +});