Skip to content

Commit

Permalink
Merge pull request #185 from CloudBoost/staging
Browse files Browse the repository at this point in the history
auto create fix
  • Loading branch information
shubhamqweasd authored May 10, 2017
2 parents e76fd4a + 4bed8c5 commit 230d9c0
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions services/cloudObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ function _sendNotification(appId, res, reqType) {

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

try {
var promises = [];
Expand Down Expand Up @@ -523,7 +524,7 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas

// if column does not exist create a new column
if (!col) {

columnNotFound = true
try{
let detectedDataType = type.inferDataType(document[key]);
let newCol = {
Expand All @@ -544,35 +545,6 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
//push the new column to the old schema
table.columns.push(newCol);

// update the table schema
var createNewColumnPromise = q.defer();
var schemaCursor = global.mongoClient.db(appId).collection("_Schema");
schemaCursor.findOneAndUpdate(
{
name: document._tableName
},
{
$set: table
},
{
upsert: true,
returnOriginal: false
},
function(err, response) {
var table = null;
if (response && response.value)
table = response.value;

if (err) {
createNewColumnPromise.reject("Error : Failed to update the table with the new column. ");
} else if (table) {
createNewColumnPromise.resolve();
console.log("Column " + key + " created.");
}
}
)

promises.push(createNewColumnPromise.promise)
}
catch (err) {
global.winston.log('error', {
Expand Down Expand Up @@ -669,7 +641,37 @@ var _isSchemaValid = function(appId, collectionName, document, accessList, isMas
}
}
}
if(columnNotFound){
// update the table schema
var createNewColumnPromise = q.defer();
var schemaCursor = global.mongoClient.db(appId).collection("_Schema");
schemaCursor.findOneAndUpdate(
{
name: document._tableName
},
{
$set: table
},
{
upsert: true,
returnOriginal: false
},
function(err, response) {
var table = null;
if (response && response.value)
table = response.value;

if (err) {
createNewColumnPromise.reject("Error : Failed to update the table with the new column. ");
} else if (table) {
createNewColumnPromise.resolve();
console.log("Column " + key + " created.");
}
}
)

promises.push(createNewColumnPromise.promise)
}
if (promises.length > 0) {
//you have related documents or unique queries.
q.all(promises).then(function(results) {
Expand Down

0 comments on commit 230d9c0

Please sign in to comment.