Skip to content

Commit 972f760

Browse files
authored
feat(NODE-4607): add exports needed by legacy client (#3396)
1 parent 5676f81 commit 972f760

File tree

7 files changed

+174
-12
lines changed

7 files changed

+174
-12
lines changed

src/bulk/ordered.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Batch, BatchType, BulkOperationBase, BulkWriteOptions } from './common'
88

99
/** @public */
1010
export class OrderedBulkOperation extends BulkOperationBase {
11+
/** @internal */
1112
constructor(collection: Collection, options: BulkWriteOptions) {
1213
super(collection, options, true);
1314
}

src/bulk/unordered.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Batch, BatchType, BulkOperationBase, BulkWriteOptions, BulkWriteResult
99

1010
/** @public */
1111
export class UnorderedBulkOperation extends BulkOperationBase {
12+
/** @internal */
1213
constructor(collection: Collection, options: BulkWriteOptions) {
1314
super(collection, options, false);
1415
}

src/gridfs/download.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableS
103103
*/
104104
static readonly CLOSE = 'close' as const;
105105

106-
/** @internal
106+
/**
107107
* @param chunks - Handle for chunks collection
108108
* @param files - Handle for files collection
109109
* @param readPreference - The read preference to use
110110
* @param filter - The filter to use to find the file document
111+
* @internal
111112
*/
112113
constructor(
113114
chunks: Collection<GridFSChunk>,

src/gridfs/upload.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ export class GridFSBucketWriteStream extends Writable implements NodeJS.Writable
6969
*/
7070
static readonly FINISH = 'finish';
7171

72-
/** @internal
72+
/**
7373
* @param bucket - Handle for this stream's corresponding bucket
7474
* @param filename - The value of the 'filename' key in the files doc
7575
* @param options - Optional settings.
76+
* @internal
7677
*/
7778
constructor(bucket: GridFSBucket, filename: string, options?: GridFSBucketWriteStreamOptions) {
7879
super();

src/index.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Admin } from './admin';
22
import { ObjectId } from './bson';
3+
import { OrderedBulkOperation } from './bulk/ordered';
4+
import { UnorderedBulkOperation } from './bulk/unordered';
35
import { ChangeStream } from './change_stream';
46
import { Collection } from './collection';
57
import { AbstractCursor } from './cursor/abstract_cursor';
@@ -9,10 +11,13 @@ import { ListCollectionsCursor } from './cursor/list_collections_cursor';
911
import { ListIndexesCursor } from './cursor/list_indexes_cursor';
1012
import { Db } from './db';
1113
import { GridFSBucket } from './gridfs';
14+
import { GridFSBucketReadStream } from './gridfs/download';
15+
import { GridFSBucketWriteStream } from './gridfs/upload';
1216
import { Logger } from './logger';
1317
import { MongoClient } from './mongo_client';
1418
import { CancellationToken } from './mongo_types';
1519
import { PromiseProvider } from './promise_provider';
20+
import { ClientSession } from './sessions';
1621

1722
/** @internal */
1823
export { BSON } from './bson';
@@ -80,16 +85,21 @@ export {
8085
AggregationCursor,
8186
CancellationToken,
8287
ChangeStream,
88+
ClientSession,
8389
Collection,
8490
Db,
8591
FindCursor,
8692
GridFSBucket,
93+
GridFSBucketReadStream,
94+
GridFSBucketWriteStream,
8795
ListCollectionsCursor,
8896
ListIndexesCursor,
8997
Logger,
9098
MongoClient,
99+
OrderedBulkOperation,
91100
// Utils
92-
PromiseProvider as Promise
101+
PromiseProvider as Promise,
102+
UnorderedBulkOperation
93103
};
94104

95105
// enums
@@ -172,8 +182,6 @@ export type {
172182
FindOperators,
173183
WriteConcernErrorData
174184
} from './bulk/common';
175-
export type { OrderedBulkOperation } from './bulk/ordered';
176-
export type { UnorderedBulkOperation } from './bulk/unordered';
177185
export type {
178186
ChangeStreamCollModDocument,
179187
ChangeStreamCreateDocument,
@@ -269,18 +277,13 @@ export type { Encrypter, EncrypterOptions } from './encrypter';
269277
export type { AnyError, ErrorDescription, MongoNetworkErrorOptions } from './error';
270278
export type { Explain, ExplainOptions, ExplainVerbosityLike } from './explain';
271279
export type {
272-
GridFSBucketReadStream,
273280
GridFSBucketReadStreamOptions,
274281
GridFSBucketReadStreamOptionsWithRevision,
275282
GridFSBucketReadStreamPrivate,
276283
GridFSFile
277284
} from './gridfs/download';
278285
export type { GridFSBucketEvents, GridFSBucketOptions, GridFSBucketPrivate } from './gridfs/index';
279-
export type {
280-
GridFSBucketWriteStream,
281-
GridFSBucketWriteStreamOptions,
282-
GridFSChunk
283-
} from './gridfs/upload';
286+
export type { GridFSBucketWriteStreamOptions, GridFSChunk } from './gridfs/upload';
284287
export type { LoggerFunction, LoggerOptions } from './logger';
285288
export type {
286289
Auth,
@@ -454,7 +457,6 @@ export type {
454457
} from './sdam/topology';
455458
export type { TopologyDescription, TopologyDescriptionOptions } from './sdam/topology_description';
456459
export type {
457-
ClientSession,
458460
ClientSessionEvents,
459461
ClientSessionOptions,
460462
EndSessionOptions,

test/tools/utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,17 @@ export function isBSONExtImported() {
503503
const driverBSON = require('../../src/bson');
504504
return driverBSON.deserialize.toString().includes('[native code]');
505505
}
506+
507+
export const byStrings = (a: any, b: any) => {
508+
const res = `${a}`.localeCompare(`${b}`);
509+
return res < 0 ? -1 : res > 0 ? 1 : 0;
510+
};
511+
512+
export const sorted = <T>(iterable: Iterable<T>, how: (a: T, b: T) => 0 | 1 | -1) => {
513+
if (typeof how !== 'function') {
514+
throw new TypeError('must provide a "how" function to sorted');
515+
}
516+
const items = Array.from(iterable);
517+
items.sort(how);
518+
return items;
519+
};

test/unit/index.test.ts

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { expect } from 'chai';
2+
3+
import * as mongodb from '../../src/index';
4+
import { byStrings, sorted } from '../tools/utils';
5+
6+
/**
7+
* TS-NODE Adds these keys but they are undefined, they are not present when you import from lib
8+
* We did not think this strangeness was worth investigating so we just make sure they remain set to undefined
9+
*/
10+
const TS_NODE_EXPORTS = ['AnyBulkWriteOperation', 'BulkWriteOptions'];
11+
12+
const EXPECTED_EXPORTS = [
13+
...TS_NODE_EXPORTS,
14+
'AbstractCursor',
15+
'Admin',
16+
'AggregationCursor',
17+
'AuthMechanism',
18+
'AutoEncryptionLoggerLevel',
19+
'BatchType',
20+
'Binary',
21+
'BSON',
22+
'BSONRegExp',
23+
'BSONSymbol',
24+
'BSONType',
25+
'CancellationToken',
26+
'ChangeStream',
27+
'ChangeStreamCursor',
28+
'ClientSession',
29+
'Code',
30+
'Collection',
31+
'CommandFailedEvent',
32+
'CommandStartedEvent',
33+
'CommandSucceededEvent',
34+
'Compressor',
35+
'ConnectionCheckedInEvent',
36+
'ConnectionCheckedOutEvent',
37+
'ConnectionCheckOutFailedEvent',
38+
'ConnectionCheckOutStartedEvent',
39+
'ConnectionClosedEvent',
40+
'ConnectionCreatedEvent',
41+
'ConnectionPoolClearedEvent',
42+
'ConnectionPoolClosedEvent',
43+
'ConnectionPoolCreatedEvent',
44+
'ConnectionPoolMonitoringEvent',
45+
'ConnectionReadyEvent',
46+
'CURSOR_FLAGS',
47+
'Db',
48+
'DBRef',
49+
'Decimal128',
50+
'Double',
51+
'ExplainVerbosity',
52+
'FindCursor',
53+
'GridFSBucket',
54+
'GridFSBucketReadStream',
55+
'GridFSBucketWriteStream',
56+
'GSSAPICanonicalizationValue',
57+
'Int32',
58+
'ListCollectionsCursor',
59+
'ListIndexesCursor',
60+
'Logger',
61+
'LoggerLevel',
62+
'Long',
63+
'Map',
64+
'MaxKey',
65+
'MinKey',
66+
'MongoAPIError',
67+
'MongoAWSError',
68+
'MongoBatchReExecutionError',
69+
'MongoBulkWriteError',
70+
'MongoChangeStreamError',
71+
'MongoClient',
72+
'MongoCompatibilityError',
73+
'MongoCursorExhaustedError',
74+
'MongoCursorInUseError',
75+
'MongoDecompressionError',
76+
'MongoDriverError',
77+
'MongoError',
78+
'MongoErrorLabel',
79+
'MongoExpiredSessionError',
80+
'MongoGridFSChunkError',
81+
'MongoGridFSStreamError',
82+
'MongoInvalidArgumentError',
83+
'MongoKerberosError',
84+
'MongoMissingCredentialsError',
85+
'MongoMissingDependencyError',
86+
'MongoNetworkError',
87+
'MongoNetworkTimeoutError',
88+
'MongoNotConnectedError',
89+
'MongoParseError',
90+
'MongoRuntimeError',
91+
'MongoServerClosedError',
92+
'MongoServerError',
93+
'MongoServerSelectionError',
94+
'MongoSystemError',
95+
'MongoTailableCursorError',
96+
'MongoTopologyClosedError',
97+
'MongoTransactionError',
98+
'MongoUnexpectedServerResponseError',
99+
'MongoWriteConcernError',
100+
'ObjectId',
101+
'ObjectID',
102+
'OrderedBulkOperation',
103+
'ProfilingLevel',
104+
'Promise',
105+
'ReadConcern',
106+
'ReadConcernLevel',
107+
'ReadPreference',
108+
'ReadPreferenceMode',
109+
'ReturnDocument',
110+
'ServerApiVersion',
111+
'ServerClosedEvent',
112+
'ServerDescriptionChangedEvent',
113+
'ServerHeartbeatFailedEvent',
114+
'ServerHeartbeatStartedEvent',
115+
'ServerHeartbeatSucceededEvent',
116+
'ServerOpeningEvent',
117+
'ServerType',
118+
'SrvPollingEvent',
119+
'Timestamp',
120+
'TopologyClosedEvent',
121+
'TopologyDescriptionChangedEvent',
122+
'TopologyOpeningEvent',
123+
'TopologyType',
124+
'UnorderedBulkOperation',
125+
'WriteConcern'
126+
];
127+
128+
describe('mongodb entrypoint', () => {
129+
it('should export all and only the expected keys in expected_exports', () => {
130+
expect(sorted(Object.keys(mongodb), byStrings)).to.deep.equal(
131+
sorted(EXPECTED_EXPORTS, byStrings)
132+
);
133+
});
134+
135+
it('should export keys added by ts-node as undefined', () => {
136+
// If the array is empty, this test would be a no-op so we should remove it
137+
expect(TS_NODE_EXPORTS).to.have.length.greaterThan(0);
138+
for (const tsNodeExportKey of TS_NODE_EXPORTS) {
139+
expect(mongodb).to.have.property(tsNodeExportKey, undefined);
140+
}
141+
});
142+
});

0 commit comments

Comments
 (0)