Skip to content

Commit a5acfe0

Browse files
committed
Merge branch '3.5' into 3.6
2 parents f8a7c63 + d368f12 commit a5acfe0

13 files changed

+478
-324
lines changed

lib/core/sessions.js

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ function isUnknownTransactionCommitResult(err) {
304304
}
305305

306306
function isMaxTimeMSExpiredError(err) {
307+
if (err == null) return false;
307308
return (
308309
err.code === MAX_TIME_MS_EXPIRED_CODE ||
309310
(err.writeConcernError && err.writeConcernError.code === MAX_TIME_MS_EXPIRED_CODE)

lib/operations/create_collection.js

+37-52
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ const Aspect = require('./operation').Aspect;
44
const defineAspects = require('./operation').defineAspects;
55
const CommandOperation = require('./command');
66
const applyWriteConcern = require('../utils').applyWriteConcern;
7-
const handleCallback = require('../utils').handleCallback;
87
const loadCollection = require('../dynamic_loaders').loadCollection;
98
const MongoError = require('../core').MongoError;
109
const ReadPreference = require('../core').ReadPreference;
1110

12-
// Filter out any write concern options
13-
const illegalCommandFields = [
11+
const ILLEGAL_COMMAND_FIELDS = new Set([
1412
'w',
1513
'wtimeout',
1614
'j',
@@ -24,27 +22,24 @@ const illegalCommandFields = [
2422
'session',
2523
'readConcern',
2624
'writeConcern'
27-
];
25+
]);
2826

2927
class CreateCollectionOperation extends CommandOperation {
3028
constructor(db, name, options) {
3129
super(db, options);
32-
3330
this.name = name;
3431
}
3532

3633
_buildCommand() {
3734
const name = this.name;
3835
const options = this.options;
3936

40-
// Create collection command
4137
const cmd = { create: name };
42-
// Add all optional parameters
4338
for (let n in options) {
4439
if (
4540
options[n] != null &&
4641
typeof options[n] !== 'function' &&
47-
illegalCommandFields.indexOf(n) === -1
42+
!ILLEGAL_COMMAND_FIELDS.has(n)
4843
) {
4944
cmd[n] = options[n];
5045
}
@@ -57,61 +52,51 @@ class CreateCollectionOperation extends CommandOperation {
5752
const db = this.db;
5853
const name = this.name;
5954
const options = this.options;
55+
const Collection = loadCollection();
6056

61-
let Collection = loadCollection();
57+
let listCollectionOptions = Object.assign({ nameOnly: true, strict: false }, options);
58+
listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);
6259

63-
// Did the user destroy the topology
64-
if (db.serverConfig && db.serverConfig.isDestroyed()) {
65-
return callback(new MongoError('topology was destroyed'));
66-
}
60+
function done(err) {
61+
if (err) {
62+
return callback(err);
63+
}
6764

68-
let listCollectionOptions = Object.assign({}, options, { nameOnly: true });
69-
listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);
65+
try {
66+
callback(
67+
null,
68+
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
69+
);
70+
} catch (err) {
71+
callback(err);
72+
}
73+
}
7074

71-
// Check if we have the name
72-
db.listCollections({ name }, listCollectionOptions)
73-
.setReadPreference(ReadPreference.PRIMARY)
74-
.toArray((err, collections) => {
75-
if (err != null) return handleCallback(callback, err, null);
76-
if (collections.length > 0 && listCollectionOptions.strict) {
77-
return handleCallback(
78-
callback,
79-
MongoError.create({
80-
message: `Collection ${name} already exists. Currently in strict mode.`,
81-
driver: true
82-
}),
83-
null
84-
);
85-
} else if (collections.length > 0) {
86-
try {
87-
return handleCallback(
88-
callback,
89-
null,
90-
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
91-
);
92-
} catch (err) {
93-
return handleCallback(callback, err);
75+
const strictMode = listCollectionOptions.strict;
76+
if (strictMode) {
77+
db.listCollections({ name }, listCollectionOptions)
78+
.setReadPreference(ReadPreference.PRIMARY)
79+
.toArray((err, collections) => {
80+
if (err) {
81+
return callback(err);
9482
}
95-
}
96-
97-
// Execute command
98-
super.execute(err => {
99-
if (err) return handleCallback(callback, err);
10083

101-
try {
102-
return handleCallback(
103-
callback,
104-
null,
105-
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
84+
if (collections.length > 0) {
85+
return callback(
86+
new MongoError(`Collection ${name} already exists. Currently in strict mode.`)
10687
);
107-
} catch (err) {
108-
return handleCallback(callback, err);
10988
}
89+
90+
super.execute(done);
11091
});
111-
});
92+
93+
return;
94+
}
95+
96+
// otherwise just execute the command
97+
super.execute(done);
11298
}
11399
}
114100

115101
defineAspects(CreateCollectionOperation, Aspect.WRITE_OPERATION);
116-
117102
module.exports = CreateCollectionOperation;

0 commit comments

Comments
 (0)