3
3
const CommandOperation = require ( './command' ) ;
4
4
const ReadPreference = require ( '../read_preference' ) ;
5
5
const { Aspect, defineAspects } = require ( './operation' ) ;
6
- const { applyWriteConcern, handleCallback } = require ( '../utils' ) ;
6
+ const { applyWriteConcern } = require ( '../utils' ) ;
7
7
const { loadCollection } = require ( '../dynamic_loaders' ) ;
8
8
const { MongoError } = require ( '../error' ) ;
9
9
10
- // Filter out any write concern options
11
- const illegalCommandFields = [
10
+ const ILLEGAL_COMMAND_FIELDS = new Set ( [
12
11
'w' ,
13
12
'wtimeout' ,
14
13
'j' ,
@@ -22,27 +21,24 @@ const illegalCommandFields = [
22
21
'session' ,
23
22
'readConcern' ,
24
23
'writeConcern'
25
- ] ;
24
+ ] ) ;
26
25
27
26
class CreateCollectionOperation extends CommandOperation {
28
27
constructor ( db , name , options ) {
29
28
super ( db , options ) ;
30
-
31
29
this . name = name ;
32
30
}
33
31
34
32
_buildCommand ( ) {
35
33
const name = this . name ;
36
34
const options = this . options ;
37
35
38
- // Create collection command
39
36
const cmd = { create : name } ;
40
- // Add all optional parameters
41
37
for ( let n in options ) {
42
38
if (
43
39
options [ n ] != null &&
44
40
typeof options [ n ] !== 'function' &&
45
- illegalCommandFields . indexOf ( n ) === - 1
41
+ ! ILLEGAL_COMMAND_FIELDS . has ( n )
46
42
) {
47
43
cmd [ n ] = options [ n ] ;
48
44
}
@@ -55,61 +51,51 @@ class CreateCollectionOperation extends CommandOperation {
55
51
const db = this . db ;
56
52
const name = this . name ;
57
53
const options = this . options ;
54
+ const Collection = loadCollection ( ) ;
58
55
59
- let Collection = loadCollection ( ) ;
56
+ let listCollectionOptions = Object . assign ( { nameOnly : true , strict : false } , options ) ;
57
+ listCollectionOptions = applyWriteConcern ( listCollectionOptions , { db } , listCollectionOptions ) ;
60
58
61
- // Did the user destroy the topology
62
- if ( db . serverConfig && db . serverConfig . isDestroyed ( ) ) {
63
- return callback ( new MongoError ( 'topology was destroyed' ) ) ;
64
- }
59
+ function done ( err ) {
60
+ if ( err ) {
61
+ return callback ( err ) ;
62
+ }
65
63
66
- let listCollectionOptions = Object . assign ( { } , options , { nameOnly : true } ) ;
67
- listCollectionOptions = applyWriteConcern ( listCollectionOptions , { db } , listCollectionOptions ) ;
64
+ try {
65
+ callback (
66
+ null ,
67
+ new Collection ( db , db . s . topology , db . databaseName , name , db . s . pkFactory , options )
68
+ ) ;
69
+ } catch ( err ) {
70
+ callback ( err ) ;
71
+ }
72
+ }
68
73
69
- // Check if we have the name
70
- db . listCollections ( { name } , listCollectionOptions )
71
- . setReadPreference ( ReadPreference . PRIMARY )
72
- . toArray ( ( err , collections ) => {
73
- if ( err != null ) return handleCallback ( callback , err , null ) ;
74
- if ( collections . length > 0 && listCollectionOptions . strict ) {
75
- return handleCallback (
76
- callback ,
77
- MongoError . create ( {
78
- message : `Collection ${ name } already exists. Currently in strict mode.` ,
79
- driver : true
80
- } ) ,
81
- null
82
- ) ;
83
- } else if ( collections . length > 0 ) {
84
- try {
85
- return handleCallback (
86
- callback ,
87
- null ,
88
- new Collection ( db , db . s . topology , db . databaseName , name , db . s . pkFactory , options )
89
- ) ;
90
- } catch ( err ) {
91
- return handleCallback ( callback , err ) ;
74
+ const strictMode = listCollectionOptions . strict ;
75
+ if ( strictMode ) {
76
+ db . listCollections ( { name } , listCollectionOptions )
77
+ . setReadPreference ( ReadPreference . PRIMARY )
78
+ . toArray ( ( err , collections ) => {
79
+ if ( err ) {
80
+ return callback ( err ) ;
92
81
}
93
- }
94
-
95
- // Execute command
96
- super . execute ( err => {
97
- if ( err ) return handleCallback ( callback , err ) ;
98
82
99
- try {
100
- return handleCallback (
101
- callback ,
102
- null ,
103
- new Collection ( db , db . s . topology , db . databaseName , name , db . s . pkFactory , options )
83
+ if ( collections . length > 0 ) {
84
+ return callback (
85
+ new MongoError ( `Collection ${ name } already exists. Currently in strict mode.` )
104
86
) ;
105
- } catch ( err ) {
106
- return handleCallback ( callback , err ) ;
107
87
}
88
+
89
+ super . execute ( done ) ;
108
90
} ) ;
109
- } ) ;
91
+
92
+ return ;
93
+ }
94
+
95
+ // otherwise just execute the command
96
+ super . execute ( done ) ;
110
97
}
111
98
}
112
99
113
100
defineAspects ( CreateCollectionOperation , Aspect . WRITE_OPERATION ) ;
114
-
115
101
module . exports = CreateCollectionOperation ;
0 commit comments