@@ -4,13 +4,11 @@ const Aspect = require('./operation').Aspect;
4
4
const defineAspects = require ( './operation' ) . defineAspects ;
5
5
const CommandOperation = require ( './command' ) ;
6
6
const applyWriteConcern = require ( '../utils' ) . applyWriteConcern ;
7
- const handleCallback = require ( '../utils' ) . handleCallback ;
8
7
const loadCollection = require ( '../dynamic_loaders' ) . loadCollection ;
9
8
const MongoError = require ( '../core' ) . MongoError ;
10
9
const ReadPreference = require ( '../core' ) . ReadPreference ;
11
10
12
- // Filter out any write concern options
13
- const illegalCommandFields = [
11
+ const ILLEGAL_COMMAND_FIELDS = new Set ( [
14
12
'w' ,
15
13
'wtimeout' ,
16
14
'j' ,
@@ -24,27 +22,24 @@ const illegalCommandFields = [
24
22
'session' ,
25
23
'readConcern' ,
26
24
'writeConcern'
27
- ] ;
25
+ ] ) ;
28
26
29
27
class CreateCollectionOperation extends CommandOperation {
30
28
constructor ( db , name , options ) {
31
29
super ( db , options ) ;
32
-
33
30
this . name = name ;
34
31
}
35
32
36
33
_buildCommand ( ) {
37
34
const name = this . name ;
38
35
const options = this . options ;
39
36
40
- // Create collection command
41
37
const cmd = { create : name } ;
42
- // Add all optional parameters
43
38
for ( let n in options ) {
44
39
if (
45
40
options [ n ] != null &&
46
41
typeof options [ n ] !== 'function' &&
47
- illegalCommandFields . indexOf ( n ) === - 1
42
+ ! ILLEGAL_COMMAND_FIELDS . has ( n )
48
43
) {
49
44
cmd [ n ] = options [ n ] ;
50
45
}
@@ -57,61 +52,51 @@ class CreateCollectionOperation extends CommandOperation {
57
52
const db = this . db ;
58
53
const name = this . name ;
59
54
const options = this . options ;
55
+ const Collection = loadCollection ( ) ;
60
56
61
- let Collection = loadCollection ( ) ;
57
+ let listCollectionOptions = Object . assign ( { nameOnly : true , strict : false } , options ) ;
58
+ listCollectionOptions = applyWriteConcern ( listCollectionOptions , { db } , listCollectionOptions ) ;
62
59
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
+ }
67
64
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
+ }
70
74
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 ) ;
94
82
}
95
- }
96
-
97
- // Execute command
98
- super . execute ( err => {
99
- if ( err ) return handleCallback ( callback , err ) ;
100
83
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.` )
106
87
) ;
107
- } catch ( err ) {
108
- return handleCallback ( callback , err ) ;
109
88
}
89
+
90
+ super . execute ( done ) ;
110
91
} ) ;
111
- } ) ;
92
+
93
+ return ;
94
+ }
95
+
96
+ // otherwise just execute the command
97
+ super . execute ( done ) ;
112
98
}
113
99
}
114
100
115
101
defineAspects ( CreateCollectionOperation , Aspect . WRITE_OPERATION ) ;
116
-
117
102
module . exports = CreateCollectionOperation ;
0 commit comments