Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bulk/ordered.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -453,7 +456,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
Original file line number Diff line number Diff line change
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;
};
161 changes: 161 additions & 0 deletions test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { expect } from 'chai';

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

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

['GridFSBucketReadStream', 'reason'],
['GridFSBucketWriteStream', 'reason'],
['OrderedBulkOperation', 'reason'],
['UnorderedBulkOperation', 'reason'],

// TS-NODE Adds these keys but they are undefined... weird
['AnyBulkWriteOperation', 'ts-node'],
['BulkWriteOptions', 'ts-node'],
]);

describe('mongodb entrypoint', () => {
it('should export all keys in our exports list', () => {
expect(sorted(Object.keys(mongodb), byStrings)).to.deep.equal(
sorted(expectedExports.keys(), byStrings)
);
});

it('should export only keys in our exports list', () => {
const currentExports = Object.keys(mongodb);
const difference = setDifference(currentExports, expectedExports.keys());
expect(
difference,
`Found extra exports [${Array.from(difference).join(
', '
)}], if these are expected, just add them to the list in this file`
).to.have.lengthOf(0);
});

it('meta test: ts-node adds keys to the module that point to undefined', () => {
const tsNodeKeys = Array.from(expectedExports.entries()).filter(
([, value]) => value === 'ts-node'
);

for (const [tsNodeKey] of tsNodeKeys) {
expect(mongodb).to.have.property(tsNodeKey, undefined);
}
});
});