@altangent/graphql-filter-mysql
Advanced tools
Comparing version 1.3.0 to 1.4.0
{ | ||
"name": "@altangent/graphql-filter-mysql", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Graphql filtering exection to generate mysql query syntax", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,4 +5,21 @@ let mysqlClient = require('mysql'); | ||
buildWhere, | ||
properties, | ||
}; | ||
function properties(rootFilter) { | ||
let keys = Object.keys(rootFilter); | ||
let props = {}; | ||
for (let key of keys) { | ||
if (key === '_and' || key === '_or') { | ||
for (let subquery of rootFilter[key]) { | ||
props = { ...props, ...properties(subquery) }; | ||
} | ||
} | ||
else { | ||
props[key] = true; | ||
} | ||
} | ||
return props; | ||
} | ||
function buildWhere(rootFilter, columnMap) { | ||
@@ -9,0 +26,0 @@ let result = ' and '; |
const { expect } = require('chai'); | ||
const { buildWhere } = require('./filter-map'); | ||
const { buildWhere, properties } = require('./filter-map'); | ||
@@ -123,1 +123,68 @@ describe('.buildQuery', () => { | ||
}); | ||
describe('.properties', () => { | ||
let assess = [ | ||
{ | ||
desc: 'should return single property', | ||
input: { currencyName: { _eq: 'bitcoin' } }, | ||
expected: { currencyName: true }, | ||
}, | ||
{ | ||
desc: 'should return multiply properties', | ||
input: { currencyName: { _eq: 'bitcoin' }, currencySymbol: { _like: '*bit*' } }, | ||
expected: { currencyName: true, currencySymbol: true }, | ||
}, | ||
{ | ||
desc: 'should return anded object at root', | ||
input: { | ||
_and: [{ currencyName: { _eq: 'bitcoin' } }, { currencySymbol: { _like: '*bit*' } }], | ||
}, | ||
expected: { currencyName: true, currencySymbol: true }, | ||
}, | ||
{ | ||
desc: 'should return ored object at root', | ||
input: { | ||
_or: [{ currencyName: { _eq: 'bitcoin' } }, { currencySymbol: { _like: '*bit*' } }], | ||
}, | ||
expected: { currencyName: true, currencySymbol: true }, | ||
}, | ||
{ | ||
desc: 'should return nested anded object', | ||
input: { | ||
currencySupply: { _eq: 1 }, | ||
_and: [{ currencyName: { _eq: 'bitcoin' } }, { currencySymbol: { _like: '*bit*' } }], | ||
}, | ||
expected: { currencySupply: true, currencyName: true, currencySymbol: true }, | ||
}, | ||
{ | ||
desc: 'should return nested ored object', | ||
input: { | ||
currencySupply: { _eq: 1 }, | ||
_or: [{ currencyName: { _eq: 'bitcoin' } }, { currencySymbol: { _like: '*bit*' } }], | ||
}, | ||
expected: { currencySupply: true, currencyName: true, currencySymbol: true }, | ||
}, | ||
{ | ||
desc: 'should return combined anded and ored', | ||
input: { | ||
_and: [{ currencyName: { _eq: 'bitcoin' } }, { currencySymbol: { _like: '*bit*' } }], | ||
_or: [{ currencyName: { _eq: 'bitcoin' } }, { currencySupply: { _eq: 1 } }], | ||
}, | ||
expected: { currencySupply: true, currencyName: true, currencySymbol: true }, | ||
}, | ||
{ | ||
desc: 'should have single entry for dupes', | ||
input: { | ||
_and: [{ currencyName: { _eq: 'bitcoin' } }, { currencySymbol: { _like: '*bit*' } }], | ||
_or: [{ currencyName: { _eq: 'bitcoin' } }, { currencyName: { _eq: 'litecoin' } }], | ||
}, | ||
expected: { currencyName: true, currencySymbol: true }, | ||
}, | ||
]; | ||
for (let ass of assess) { | ||
it(ass.desc, () => { | ||
expect(properties(ass.input)).to.deep.equal(ass.expected); | ||
}); | ||
} | ||
}); |
13470
387