Skip to content

Commit

Permalink
feat(db): remove createCollection strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Aug 12, 2020
1 parent 3becdc0 commit bb13764
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,6 @@ const collectionKeys = [
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.strict=false] Returns an error if the collection does not exist
* @param {boolean} [options.capped=false] Create a capped collection.
* @param {boolean} [options.autoIndexId=true] DEPRECATED: Create an index on the _id field of the document, True by default on MongoDB 2.6 - 3.0
* @param {number} [options.size] The size of the capped collection in bytes.
Expand Down
28 changes: 0 additions & 28 deletions src/operations/create_collection.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import CommandOperation = require('./command');
import { ReadPreference } from '../read_preference';
import { Aspect, defineAspects } from './operation';
import { applyWriteConcern } from '../utils';
import { loadCollection } from '../dynamic_loaders';
import { MongoError } from '../error';

const ILLEGAL_COMMAND_FIELDS = new Set([
'w',
'wtimeout',
'j',
'fsync',
'autoIndexId',
'strict',
'serializeFunctions',
'pkFactory',
'raw',
Expand All @@ -37,9 +33,6 @@ class CreateCollectionOperation extends CommandOperation {
const options = this.options;
const Collection = loadCollection();

let listCollectionOptions = Object.assign({ nameOnly: true, strict: false }, options);
listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);

function done(err: any) {
if (err) {
return callback(err);
Expand All @@ -66,27 +59,6 @@ class CreateCollectionOperation extends CommandOperation {
}
}

const strictMode = listCollectionOptions.strict;
if (strictMode) {
db.listCollections({ name }, listCollectionOptions)
.setReadPreference(ReadPreference.PRIMARY)
.toArray((err?: any, collections?: any) => {
if (err) {
return callback(err);
}

if (collections.length > 0) {
return callback(
new MongoError(`Collection ${name} already exists. Currently in strict mode.`)
);
}

super.executeCommand(server, cmd, done);
});

return;
}

// otherwise just execute the command
super.executeCommand(server, cmd, done);
}
Expand Down
32 changes: 0 additions & 32 deletions test/functional/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,6 @@ describe('Collection', function () {
});
});

it('should perform strict create collection', function (done) {
db.createCollection('test_strict_create_collection', (err, collection) => {
expect(err).to.not.exist;
expect(collection.collectionName).to.equal('test_strict_create_collection');

// Creating an existing collection should fail
db.createCollection('test_strict_create_collection', { strict: true }, err => {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal(
'Collection test_strict_create_collection already exists. Currently in strict mode.'
);

done();
});
});
});

it('should fail to insert due to illegal keys', function (done) {
db.createCollection('test_invalid_key_names', (err, collection) => {
// Legal inserts
Expand Down Expand Up @@ -480,21 +463,6 @@ describe('Collection', function () {
});
});

it('should fail due to existing collection', function (done) {
db.createCollection('shouldFailDueToExistingCollection', { strict: true }, (err, coll) => {
expect(err).to.not.exist;
expect(coll).to.exist;

db.createCollection('shouldFailDueToExistingCollection', { strict: true }, err => {
expect(err).to.exist;
expect(err.message).to.equal(
'Collection shouldFailDueToExistingCollection already exists. Currently in strict mode.'
);
done();
});
});
});

const listCollectionsTests = [
{
title: 'should filter correctly during list',
Expand Down
5 changes: 0 additions & 5 deletions test/unit/db_list_collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ describe('db.listCollections', function () {
command: db => db.listCollections({}, { nameOnly: true }).toArray(() => {}),
listCollectionsValue: true
},
{
description: 'should send nameOnly: true for db.createCollection',
command: db => db.createCollection('foo', { strict: true }, () => {}),
listCollectionsValue: true
},
{
description: 'should send nameOnly: true for db.collections',
command: db => db.collections(() => {}),
Expand Down

0 comments on commit bb13764

Please sign in to comment.