@@ -414,6 +414,7 @@ function _sendNotification(appId, res, reqType) {
414
414
415
415
var _isSchemaValid = function ( appId , collectionName , document , accessList , isMasterKey ) {
416
416
var mainPromise = q . defer ( ) ;
417
+ var columnNotFound = false
417
418
418
419
try {
419
420
var promises = [ ] ;
@@ -523,7 +524,7 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
523
524
524
525
// if column does not exist create a new column
525
526
if ( ! col ) {
526
-
527
+ columnNotFound = true
527
528
try {
528
529
let detectedDataType = type . inferDataType ( document [ key ] ) ;
529
530
let newCol = {
@@ -544,35 +545,6 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
544
545
//push the new column to the old schema
545
546
table . columns . push ( newCol ) ;
546
547
547
- // update the table schema
548
- var createNewColumnPromise = q . defer ( ) ;
549
- var schemaCursor = global . mongoClient . db ( appId ) . collection ( "_Schema" ) ;
550
- schemaCursor . findOneAndUpdate (
551
- {
552
- name : document . _tableName
553
- } ,
554
- {
555
- $set : table
556
- } ,
557
- {
558
- upsert : true ,
559
- returnOriginal : false
560
- } ,
561
- function ( err , response ) {
562
- var table = null ;
563
- if ( response && response . value )
564
- table = response . value ;
565
-
566
- if ( err ) {
567
- createNewColumnPromise . reject ( "Error : Failed to update the table with the new column. " ) ;
568
- } else if ( table ) {
569
- createNewColumnPromise . resolve ( ) ;
570
- console . log ( "Column " + key + " created." ) ;
571
- }
572
- }
573
- )
574
-
575
- promises . push ( createNewColumnPromise . promise )
576
548
}
577
549
catch ( err ) {
578
550
global . winston . log ( 'error' , {
@@ -669,7 +641,37 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
669
641
}
670
642
}
671
643
}
644
+ if ( columnNotFound ) {
645
+ // update the table schema
646
+ var createNewColumnPromise = q . defer ( ) ;
647
+ var schemaCursor = global . mongoClient . db ( appId ) . collection ( "_Schema" ) ;
648
+ schemaCursor . findOneAndUpdate (
649
+ {
650
+ name : document . _tableName
651
+ } ,
652
+ {
653
+ $set : table
654
+ } ,
655
+ {
656
+ upsert : true ,
657
+ returnOriginal : false
658
+ } ,
659
+ function ( err , response ) {
660
+ var table = null ;
661
+ if ( response && response . value )
662
+ table = response . value ;
663
+
664
+ if ( err ) {
665
+ createNewColumnPromise . reject ( "Error : Failed to update the table with the new column. " ) ;
666
+ } else if ( table ) {
667
+ createNewColumnPromise . resolve ( ) ;
668
+ console . log ( "Column " + key + " created." ) ;
669
+ }
670
+ }
671
+ )
672
672
673
+ promises . push ( createNewColumnPromise . promise )
674
+ }
673
675
if ( promises . length > 0 ) {
674
676
//you have related documents or unique queries.
675
677
q . all ( promises ) . then ( function ( results ) {
0 commit comments