json-sql
Advanced tools
Comparing version 0.3.9 to 0.3.10
@@ -45,5 +45,6 @@ 'use strict'; | ||
return String(value); | ||
} else if (_.isString(value)) { | ||
} else if (_.isString(value) || _.isDate(value)) { | ||
if (this.options.separatedValues) { | ||
var placeholder = this._getPlaceholder(); | ||
if (this.options.namedValues) { | ||
@@ -54,4 +55,7 @@ this._values[placeholder] = value; | ||
} | ||
return this._wrapPlaceholder(placeholder); | ||
} else { | ||
if (_.isDate(value)) value = value.toISOString(); | ||
return '\'' + value + '\''; | ||
@@ -58,0 +62,0 @@ } |
'use strict'; | ||
var _ = require('underscore'); | ||
var objectUtils = require('../../utils/object'); | ||
@@ -14,27 +15,7 @@ var removeTopBrackets = function(condition) { | ||
// check if object contains any of expected keys | ||
var hasSome = function(obj, keys) { | ||
var objKeys = _(obj).keys(); | ||
return _(keys).some(function(key) { | ||
return _(objKeys).contains(key); | ||
}); | ||
}; | ||
var termKeys = ['select', 'query', 'field', 'value', 'func', 'expression']; | ||
var isTerm = function(obj) { | ||
return _.isObject(obj) && !_.isArray(obj) && hasSome(obj, termKeys); | ||
return objectUtils.isObjectObject(obj) && objectUtils.hasSome(obj, termKeys); | ||
}; | ||
var isSimpleValue = function(value) { | ||
return ( | ||
_.isString(value) || | ||
_.isNumber(value) || | ||
_.isBoolean(value) || | ||
_.isNull(value) || | ||
_.isUndefined(value) || | ||
_.isRegExp(value) || | ||
_.isDate(value) | ||
); | ||
}; | ||
module.exports = function(dialect) { | ||
@@ -57,3 +38,3 @@ dialect.blocks.add('distinct', function() { | ||
fields = _(fields).map(function(field) { | ||
if (isSimpleValue(field) || isTerm(field) || _.has(field, 'name')) { | ||
if (objectUtils.isSimpleValue(field) || isTerm(field) || _.has(field, 'name')) { | ||
// if field has simple type or is field object: {name: '', table: 't', alias: 'r'} | ||
@@ -88,7 +69,8 @@ return dialect.buildBlock('term', {term: field, type: 'field'}); | ||
var notObject = isSimpleValue(term) || _.isArray(term); | ||
var isSimpleValue = objectUtils.isSimpleValue(term); | ||
var isArray = _.isArray(term); | ||
if (notObject && !_.isString(term)) type = 'value'; | ||
if (isSimpleValue && !_.isString(term) || isArray) type = 'value'; | ||
if (notObject || !isTerm(term)) { | ||
if (isSimpleValue || !isTerm(term) || isArray) { | ||
term = _(term).chain().pick('cast', 'alias').extend(_.object([type], [term])).value(); | ||
@@ -259,3 +241,3 @@ } | ||
result = _(join).map(function(joinItem, table) { | ||
if (!hasSome(joinItem, ['table', 'query', 'select', 'expression'])) { | ||
if (!objectUtils.hasSome(joinItem, ['table', 'query', 'select', 'expression'])) { | ||
joinItem = _.defaults({table: table}, joinItem); | ||
@@ -262,0 +244,0 @@ } |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var ValuesStore = require('../../utils/valuesStore'); | ||
var objectUtils = require('../../utils/object'); | ||
@@ -76,3 +77,5 @@ var templatesInit = require('./templates'); | ||
if (!_.isObject(value)) value = _.object([params.defaultFetchingOperator], [value]); | ||
if (objectUtils.isSimpleValue(value)) { | ||
value = _.object([params.defaultFetchingOperator], [value]); | ||
} | ||
@@ -99,3 +102,5 @@ if (_.isEmpty(value)) return ''; | ||
if (field[0] !== '$') { | ||
if (!_.isObject(item) || _.isArray(item)) item = {$eq: item}; | ||
if (objectUtils.isSimpleValue(item) || _.isArray(item)) { | ||
item = {$eq: item}; | ||
} | ||
item = _.defaults({name: field}, item); | ||
@@ -149,3 +154,3 @@ field = '$field'; | ||
var result; | ||
if (params.end || !_.isObject(value)) { | ||
if (params.end || objectUtils.isSimpleValue(value)) { | ||
result = field; | ||
@@ -173,3 +178,3 @@ } else { | ||
if (_.isObject(value) && !_.isArray(value)) { | ||
if (objectUtils.isObjectObject(value)) { | ||
// get first query operator | ||
@@ -207,3 +212,3 @@ operator = _(value).findKey(function(item, operator) { | ||
var result; | ||
if (_.isObject(value) && !_.isArray(value)) { | ||
if (objectUtils.isObjectObject(value)) { | ||
result = this.operators.logical.get(params.operator).fn( | ||
@@ -210,0 +215,0 @@ _(value) |
{ | ||
"name": "json-sql", | ||
"description": "node.js json to sql queries mapper", | ||
"version": "0.3.9", | ||
"version": "0.3.10", | ||
"author": "Artem Zhukov <artzhuchka@gmail.com>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
58232
29
1562