@@ -4,11 +4,13 @@ const Aspect = require('./operation').Aspect;
44const defineAspects = require ( './operation' ) . defineAspects ;
55const CommandOperation = require ( './command' ) ;
66const applyWriteConcern = require ( '../utils' ) . applyWriteConcern ;
7+ const handleCallback = require ( '../utils' ) . handleCallback ;
78const loadCollection = require ( '../dynamic_loaders' ) . loadCollection ;
89const MongoError = require ( '../core' ) . MongoError ;
910const ReadPreference = require ( '../core' ) . ReadPreference ;
1011
11- const ILLEGAL_COMMAND_FIELDS = new Set ( [
12+ // Filter out any write concern options
13+ const illegalCommandFields = [
1214 'w' ,
1315 'wtimeout' ,
1416 'j' ,
@@ -22,24 +24,27 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
2224 'session' ,
2325 'readConcern' ,
2426 'writeConcern'
25- ] ) ;
27+ ] ;
2628
2729class CreateCollectionOperation extends CommandOperation {
2830 constructor ( db , name , options ) {
2931 super ( db , options ) ;
32+
3033 this . name = name ;
3134 }
3235
3336 _buildCommand ( ) {
3437 const name = this . name ;
3538 const options = this . options ;
3639
40+ // Create collection command
3741 const cmd = { create : name } ;
42+ // Add all optional parameters
3843 for ( let n in options ) {
3944 if (
4045 options [ n ] != null &&
4146 typeof options [ n ] !== 'function' &&
42- ! ILLEGAL_COMMAND_FIELDS . has ( n )
47+ illegalCommandFields . indexOf ( n ) === - 1
4348 ) {
4449 cmd [ n ] = options [ n ] ;
4550 }
@@ -52,51 +57,61 @@ class CreateCollectionOperation extends CommandOperation {
5257 const db = this . db ;
5358 const name = this . name ;
5459 const options = this . options ;
55- const Collection = loadCollection ( ) ;
56-
57- let listCollectionOptions = Object . assign ( { nameOnly : true , strict : false } , options ) ;
58- listCollectionOptions = applyWriteConcern ( listCollectionOptions , { db } , listCollectionOptions ) ;
5960
60- function done ( err ) {
61- if ( err ) {
62- return callback ( err ) ;
63- }
61+ let Collection = loadCollection ( ) ;
6462
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- }
63+ // Did the user destroy the topology
64+ if ( db . serverConfig && db . serverConfig . isDestroyed ( ) ) {
65+ return callback ( new MongoError ( 'topology was destroyed' ) ) ;
7366 }
7467
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 ) ;
82- }
68+ let listCollectionOptions = Object . assign ( { } , options , { nameOnly : true } ) ;
69+ listCollectionOptions = applyWriteConcern ( listCollectionOptions , { db } , listCollectionOptions ) ;
8370
84- if ( collections . length > 0 ) {
85- return callback (
86- new MongoError ( `Collection ${ name } already exists. Currently in strict mode.` )
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 )
8791 ) ;
92+ } catch ( err ) {
93+ return handleCallback ( callback , err ) ;
8894 }
95+ }
8996
90- super . execute ( done ) ;
91- } ) ;
92-
93- return ;
94- }
97+ // Execute command
98+ super . execute ( err => {
99+ if ( err ) return handleCallback ( callback , err ) ;
95100
96- // otherwise just execute the command
97- super . execute ( done ) ;
101+ try {
102+ return handleCallback (
103+ callback ,
104+ null ,
105+ new Collection ( db , db . s . topology , db . databaseName , name , db . s . pkFactory , options )
106+ ) ;
107+ } catch ( err ) {
108+ return handleCallback ( callback , err ) ;
109+ }
110+ } ) ;
111+ } ) ;
98112 }
99113}
100114
101115defineAspects ( CreateCollectionOperation , Aspect . WRITE_OPERATION ) ;
116+
102117module . exports = CreateCollectionOperation ;
0 commit comments