cassanknex
Advanced tools
Comparing version 1.12.1 to 1.13.0
@@ -61,4 +61,4 @@ /** | ||
"if": {"name": "if", "grouping": "if"}, | ||
"ifExists": {"name": "ifExists", "grouping": "if"}, | ||
"ifNotExists": {"name": "ifNotExists", "grouping": "if"}, | ||
"ifExists": {"name": "ifExists", "grouping": "ifExists"}, | ||
"ifNotExists": {"name": "ifNotExists", "grouping": "ifNotExists"}, | ||
@@ -65,0 +65,0 @@ "usingTTL": {"name": "usingTTL", "grouping": "using"}, |
{ | ||
"name": "cassanknex", | ||
"version": "1.12.1", | ||
"version": "1.13.0", | ||
"description": "An Apache Cassandra CQL query builder with support for the DataStax NodeJS driver, written in the spirit of Knex.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -51,2 +51,17 @@ /** | ||
break; | ||
case "ifNotExists": | ||
queryBuilder[method] = function () { | ||
return this._wrapMethod(null, methods[method].name, _getIfNotExists(methods[method].name), arguments); | ||
}; | ||
break; | ||
case "ifExists": | ||
queryBuilder[method] = function () { | ||
return this._wrapMethod(null, methods[method].name, _getIfExists(methods[method].name), arguments); | ||
}; | ||
break; | ||
case "if": | ||
queryBuilder[method] = function () { | ||
return this._wrapMethod(null, methods[method].name, _getIf(methods[method].name), arguments); | ||
}; | ||
break; | ||
} | ||
@@ -124,2 +139,18 @@ }); | ||
function _getIfNotExists() { | ||
return function () { | ||
this._single.ifNotExists = {grouping: "ifNotExists", ifNotExists: true}; | ||
return this; | ||
}; | ||
} | ||
function _getIfExists() { | ||
return function () { | ||
this._single.ifExists = {grouping: "ifExists", ifExists: true}; | ||
return this; | ||
}; | ||
} | ||
function _getLimit() { | ||
@@ -168,1 +199,17 @@ return function (limit) { | ||
} | ||
function _getIf(type) { | ||
return function (identifier, op, value) { | ||
var statement = { | ||
grouping: "if", | ||
type: type, | ||
op: op, | ||
key: identifier, | ||
val: value | ||
}; | ||
this._statements.push(statement); | ||
return this; | ||
}; | ||
} |
@@ -49,6 +49,9 @@ /** | ||
cql += "(" + formatter.parameterize(compiling.value.values, this) + ")"; | ||
if (_.has(this._single, "ifNotExists")) { | ||
cql += " IF NOT EXISTS"; | ||
} | ||
if (_.has(this._grouped, "using")) { | ||
cql += " " + _compileUsing(this, this._grouped.using); | ||
} | ||
this.query({ | ||
@@ -138,2 +141,8 @@ cql: cql + ";" | ||
} | ||
if (_.has(this._grouped, "if")) { | ||
cql += " IF " + _compileIf(this, this._grouped.if); | ||
} | ||
if (_.has(this._single, "ifExists")) { | ||
cql += " IF EXISTS"; | ||
} | ||
@@ -221,2 +230,37 @@ this.query({ | ||
function _compileIf(client, whereStatements) { | ||
var groupedIf = _.groupBy(whereStatements, "type"); | ||
var cql = "" | ||
, relationsStart = true; | ||
_.each(Object.keys(groupedIf), function (type) { | ||
var relations = []; | ||
relations.length = 0; | ||
_.each(groupedIf[type], function (statement) { | ||
var key = statement.key; | ||
// test for nested columns | ||
if ((/\[.*\]$/).test(key)) { | ||
key = formatter.wrapQuotes(key.replace(/\[.*/g, "")) + key.replace(/.+?(?=\[)/, ""); | ||
} | ||
else { | ||
key = formatter.wrapQuotes(key); | ||
} | ||
switch (statement.op.toLowerCase()) { | ||
case "in": | ||
relations.push([key, statement.op, "(" + formatter.parameterize(statement.val, client) + ")"].join(" ")); | ||
break; | ||
default: | ||
relations.push([key, statement.op, formatter.parameterize(statement.val, client)].join(" ")); | ||
} | ||
}); | ||
var joinClause = " AND "; | ||
cql += (!relationsStart ? joinClause : "") + relations.join(joinClause); | ||
relationsStart = false; | ||
}); | ||
return cql; | ||
} | ||
function _compileSet(client, setStatements) { | ||
@@ -223,0 +267,0 @@ |
@@ -592,2 +592,4 @@ | ||
- 1.13.0 | ||
- Add `if` (for `update`), `ifExists` (for `update`), and `ifNotExists` (for `insert`) per PR [#28](https://github.com/azuqua/cassanknex/pull/28). | ||
- 1.12.1 | ||
@@ -594,0 +596,0 @@ - Update DataStax Driver module from `2.2.2` to `3.1.1`. |
@@ -33,3 +33,21 @@ /** | ||
}); | ||
it("should compile an insert query string, w/ if not exists", function () { | ||
var cql = 'INSERT INTO "cassanKnexy"."columnFamily" ("id","bar","baz") VALUES (?, ?, ?) IF NOT EXISTS USING TIMESTAMP ? AND USING TTL ?;' | ||
, qb = cassanKnex("cassanKnexy") | ||
, values = { | ||
"id": "foo" | ||
, "bar": "baz" | ||
, "baz": ["foo", "bar"] | ||
}; | ||
qb.insert(values) | ||
.ifNotExists() | ||
.usingTimestamp(250000) | ||
.usingTTL(50000) | ||
.into("columnFamily"); | ||
var _cql = qb.cql(); | ||
assert(_cql === cql, "Expected compilation: '" + cql + "' but compiled: " + _cql); | ||
}); | ||
// SELECT | ||
@@ -182,3 +200,34 @@ | ||
}); | ||
it("should compile an update query string using an object param /w if exists", function () { | ||
var cql = 'UPDATE "cassanKnexy"."columnFamily" SET "bar" = ?,"foo" = ? WHERE "foo"[bar] = ? AND "id" in (?, ?, ?, ?, ?) IF EXISTS;' | ||
, qb = cassanKnex("cassanKnexy"); | ||
qb.update("columnFamily") | ||
.set({ | ||
"bar": "baz", | ||
"foo": ["bar", "baz"] | ||
}) | ||
.where("foo[bar]", "=", "baz") | ||
.where("id", "in", ["1", "1", "2", "3", "5"]) | ||
.ifExists(); | ||
var _cql = qb.cql(); | ||
assert(_cql === cql, "Expected compilation: '" + cql + "' but compiled: " + _cql); | ||
}); | ||
it("should compile an update query string using an object param /w if conditions", function () { | ||
var cql = 'UPDATE "cassanKnexy"."columnFamily" SET "bar" = ?,"foo" = ? IF "bar" = ? AND "foo" = ?;' | ||
, qb = cassanKnex("cassanKnexy"); | ||
qb.update("columnFamily") | ||
.set({ | ||
"bar": "baz", | ||
"foo": "bar" | ||
}) | ||
.if("bar", "=", "baz") | ||
.if("foo", "=", "bar"); | ||
var _cql = qb.cql(); | ||
assert(_cql === cql, "Expected compilation: '" + cql + "' but compiled: " + _cql); | ||
}); | ||
// DELETE | ||
@@ -185,0 +234,0 @@ |
114365
2670
644