不要怂,就是干,撸起袖子干!

Commit d0c3a16f by Mick Hansen

feat(where): explicit casting with attribute::integer for json nested querying

1 parent 05059632
...@@ -1759,7 +1759,17 @@ module.exports = (function() { ...@@ -1759,7 +1759,17 @@ module.exports = (function() {
traverse = function (prop, item, path) { traverse = function (prop, item, path) {
var $where = {} var $where = {}
, $key , $key
, $baseKey = self.quoteIdentifier(key)+'#>>\'{'+path.join(', ')+'}\''; , $cast
, $baseKey
, $tmp;
if (path[path.length - 1].indexOf('::') > -1) {
$tmp = path[path.length - 1].split('::');
$cast = $tmp[1];
path[path.length - 1] = $tmp[0];
}
$baseKey = self.quoteIdentifier(key)+'#>>\'{'+path.join(', ')+'}\'';
if (options.prefix) { if (options.prefix) {
if (options.prefix instanceof Utils.literal) { if (options.prefix instanceof Utils.literal) {
...@@ -1777,12 +1787,18 @@ module.exports = (function() { ...@@ -1777,12 +1787,18 @@ module.exports = (function() {
$where[$prop] = $item; $where[$prop] = $item;
$key = $baseKey; $key = $baseKey;
if (!$cast) {
if (typeof $item === 'number') { if (typeof $item === 'number') {
$key += '::double precision'; $cast = 'double precision';
} }
if ($item instanceof Date) { if ($item instanceof Date) {
$key += '::timestamptz'; $cast = 'timestamptz';
}
}
if ($cast) {
$key += '::'+$cast;
} }
$items.push(self.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/)); $items.push(self.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/));
......
...@@ -550,6 +550,20 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -550,6 +550,20 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
testsql('data', { testsql('data', {
nested: { nested: {
"attribute::integer": {
$gt: 2
}
}
}, {
field: {
type: new DataTypes.JSONB()
}
}, {
default: "([data]#>>'{nested, attribute}')::integer > 2"
});
testsql('data', {
nested: {
attribute: { attribute: {
$gt: new Date() $gt: new Date()
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!