Skip to content

Commit

Permalink
Merge 1beb8ea into 394ba20
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Obrovac committed Feb 11, 2015
2 parents 394ba20 + 1beb8ea commit 8cd40f5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/dbutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ dbu.buildPutQuery = function(req, keyspace, table, schema) {

// switch between insert & update / upsert
// - insert for 'if not exists', or when no non-primary-key attributes are
// specified
// specified, or they are all null (as Cassandra does not distinguish the two)
// - update when any non-primary key attributes are supplied
// - Need to verify that all primary key members are supplied as well,
// else error.
Expand All @@ -626,8 +626,26 @@ dbu.buildPutQuery = function(req, keyspace, table, schema) {

var condRes = dbu.buildCondition(indexKVMap, schema);

// determine if we should use INSERT INTO
var shouldUseInsert = !keys.length || req.if === 'not exists' || keys.every(function(key) {
var val = req.attributes[key];
// if the value is null or undefined, it is not considered a value
if (val === null || val === undefined) {
return true;
}
// if the value is an empty array, it is not considered a value
if (Array.isArray(val) && !val.length) {
return true;
}
// if the value is an empty object, it is not considered a value
if (val.constructor === Object && !Object.keys(val).length) {
return true;
}
return false;
});

var cond = '';
if (!keys.length || req.if === 'not exists') {
if (shouldUseInsert) {
if (req.if === 'not exists') {
cond = ' if not exists ';
}
Expand Down

0 comments on commit 8cd40f5

Please sign in to comment.