Skip to content

Commit 230d9c0

Browse files
Merge pull request #185 from CloudBoost/staging
auto create fix
2 parents e76fd4a + 4bed8c5 commit 230d9c0

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

services/cloudObjects.js

+32-30
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ function _sendNotification(appId, res, reqType) {
414414

415415
var _isSchemaValid = function(appId, collectionName, document, accessList, isMasterKey) {
416416
var mainPromise = q.defer();
417+
var columnNotFound = false
417418

418419
try {
419420
var promises = [];
@@ -523,7 +524,7 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
523524

524525
// if column does not exist create a new column
525526
if (!col) {
526-
527+
columnNotFound = true
527528
try{
528529
let detectedDataType = type.inferDataType(document[key]);
529530
let newCol = {
@@ -544,35 +545,6 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
544545
//push the new column to the old schema
545546
table.columns.push(newCol);
546547

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)
576548
}
577549
catch (err) {
578550
global.winston.log('error', {
@@ -669,7 +641,37 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
669641
}
670642
}
671643
}
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+
)
672672

673+
promises.push(createNewColumnPromise.promise)
674+
}
673675
if (promises.length > 0) {
674676
//you have related documents or unique queries.
675677
q.all(promises).then(function(results) {

0 commit comments

Comments
 (0)