Skip to content

Commit

Permalink
push error check to execute operation
Browse files Browse the repository at this point in the history
  • Loading branch information
HanaPearlman committed Oct 21, 2020
1 parent 147e78b commit 5797beb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 61 deletions.
6 changes: 0 additions & 6 deletions src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type { Callback } from './utils';
import type { Document } from './bson';
import type { CommandOperationOptions } from './operations/command';
import type { Db } from './db';
import { MongoClientClosedError } from './error';

/** @internal */
export interface AdminPrivate {
Expand Down Expand Up @@ -80,7 +79,6 @@ export class Admin {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({ dbName: 'admin' }, options);

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new RunCommandOperation(this.s.db, command, options),
Expand Down Expand Up @@ -205,7 +203,6 @@ export class Admin {

options = Object.assign({ dbName: 'admin' }, options);

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new AddUserOperation(this.s.db, username, password, options),
Expand All @@ -232,7 +229,6 @@ export class Admin {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({ dbName: 'admin' }, options);

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new RemoveUserOperation(this.s.db, username, options),
Expand Down Expand Up @@ -263,7 +259,6 @@ export class Admin {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new ValidateCollectionOperation(this, collectionName, options),
Expand All @@ -288,7 +283,6 @@ export class Admin {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new ListDatabasesOperation(this.s.db, options),
Expand Down
33 changes: 0 additions & 33 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ export class Collection implements OperationParent {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new InsertOneOperation(this, doc, options),
Expand Down Expand Up @@ -346,7 +345,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options ? Object.assign({}, options) : { ordered: true };

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new InsertManyOperation(this, docs, options),
Expand Down Expand Up @@ -407,7 +405,6 @@ export class Collection implements OperationParent {
throw new MongoError('operations must be an array of documents');
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new BulkWriteOperation(this, operations, options),
Expand Down Expand Up @@ -447,7 +444,6 @@ export class Collection implements OperationParent {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new UpdateOneOperation(this, filter, update, options),
Expand Down Expand Up @@ -491,7 +487,6 @@ export class Collection implements OperationParent {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new ReplaceOneOperation(this, filter, replacement, options),
Expand Down Expand Up @@ -531,7 +526,6 @@ export class Collection implements OperationParent {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new UpdateManyOperation(this, filter, update, options),
Expand Down Expand Up @@ -564,7 +558,6 @@ export class Collection implements OperationParent {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new DeleteOneOperation(this, filter, options),
Expand Down Expand Up @@ -609,7 +602,6 @@ export class Collection implements OperationParent {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new DeleteManyOperation(this, filter, options),
Expand All @@ -636,7 +628,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY });

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new RenameOperation(this, newName, options),
Expand All @@ -661,7 +652,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new DropCollectionOperation(this.s.db, this.collectionName, options),
Expand Down Expand Up @@ -697,7 +687,6 @@ export class Collection implements OperationParent {
query = query || {};
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new FindOneOperation(this, query, options),
Expand Down Expand Up @@ -746,7 +735,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(this.s.db.topology, new OptionsOperation(this, options), callback);
}

Expand All @@ -767,7 +755,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(this.s.db.topology, new IsCappedOperation(this, options), callback);
}

Expand Down Expand Up @@ -816,7 +803,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new CreateIndexOperation(this, this.collectionName, indexSpec, options),
Expand Down Expand Up @@ -873,7 +859,6 @@ export class Collection implements OperationParent {
options = options ? Object.assign({}, options) : {};
if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new CreateIndexesOperation(this, this.collectionName, indexSpecs, options),
Expand Down Expand Up @@ -903,7 +888,6 @@ export class Collection implements OperationParent {
// Run only against primary
options.readPreference = ReadPreference.primary;

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new DropIndexOperation(this, indexName, options),
Expand All @@ -928,7 +912,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options ? Object.assign({}, options) : {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(this.s.db.topology, new DropIndexesOperation(this, options), callback);
}

Expand Down Expand Up @@ -971,7 +954,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new IndexExistsOperation(this, indexes, options),
Expand All @@ -996,7 +978,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new IndexInformationOperation(this.s.db, this.collectionName, options),
Expand All @@ -1021,7 +1002,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new EstimatedDocumentCountOperation(this, options),
Expand Down Expand Up @@ -1079,7 +1059,6 @@ export class Collection implements OperationParent {
query = query || {};
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new CountDocumentsOperation(this, query as Document, options as CountDocumentsOptions),
Expand Down Expand Up @@ -1123,7 +1102,6 @@ export class Collection implements OperationParent {
query = query || {};
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new DistinctOperation(this, key, query as Document, options as DistinctOptions),
Expand All @@ -1148,7 +1126,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(this.s.db.topology, new IndexesOperation(this, options), callback);
}

Expand All @@ -1169,7 +1146,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(this.s.db.topology, new CollStatsOperation(this, options), callback);
}

Expand All @@ -1196,7 +1172,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new FindOneAndDeleteOperation(this, filter, options),
Expand Down Expand Up @@ -1234,7 +1209,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new FindOneAndReplaceOperation(this, filter, replacement, options),
Expand Down Expand Up @@ -1272,7 +1246,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new FindOneAndUpdateOperation(this, filter, update, options),
Expand Down Expand Up @@ -1383,7 +1356,6 @@ export class Collection implements OperationParent {
options.finalize = options.finalize.toString();
}

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new MapReduceOperation(this, map, reduce, options),
Expand Down Expand Up @@ -1516,7 +1488,6 @@ export class Collection implements OperationParent {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new EnsureIndexOperation(this.s.db, this.collectionName, fieldOrSpec, options),
Expand Down Expand Up @@ -1557,7 +1528,6 @@ export class Collection implements OperationParent {
query = query || {};
options = options || {};

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new EstimatedDocumentCountOperation(this, query, options),
Expand Down Expand Up @@ -1607,7 +1577,6 @@ export class Collection implements OperationParent {
// Add the remove option
options.remove = true;

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new FindAndModifyOperation(this, query, sort as Sort, undefined, options),
Expand Down Expand Up @@ -1668,7 +1637,6 @@ export class Collection implements OperationParent {
// Set up the command as default
command = command == null ? true : command;

if (!this.s.db.topology) throw new MongoClientClosedError();
if (command == null) {
return executeOperation(
this.s.db.topology,
Expand Down Expand Up @@ -1728,7 +1696,6 @@ export class Collection implements OperationParent {
// Force read preference primary
options.readPreference = ReadPreference.primary;

if (!this.s.db.topology) throw new MongoClientClosedError();
return executeOperation(
this.s.db.topology,
new FindAndModifyOperation(this, query, sort, doc, options),
Expand Down
Loading

0 comments on commit 5797beb

Please sign in to comment.