All files class.ts

87.5% Statements 70/80
73.17% Branches 30/41
100% Functions 12/12
87.88% Lines 58/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                          1x                       1x         1x 1x 2x   1x 1x     1x 24x     1x 28x 10x     1x 62x     1x 3x 3x     1x 1x     3x     1x     6x         1x 13x 8x 7x 4x 3x 2x       6x         3x     7x                   1x 1x     6x 6x       5x 5x         1x 1x                       6x   2x     24x       6x         12x               13x 13x 13x       1x 1x           5x   8x     1x         3x       3x       3x     6x   1x  
import { ArgType, Values, Keywords } from './types'
 
const defaultKeywords: Keywords = {
	_OR: '$or',
	_AND: '$and',
	_NOR: '$nor',
	_ALL: '$all',
	_IN: '$in',
	_NIN: '$nin',
	_EQ: '$eq',
	_NE: '$ne',
	_LT: '$lt',
	_LTE: '$lte',
	_GT: '$gt',
	_GTE: '$gte',
	// geo queries operators:
	_GEO_INTERSECTS: '$geoIntersects',
	_GEO_WITHIN: '$geoWithin',
	_NEAR: '$near',
	_NEAR_SPHERE: '$nearSphere',
	// geo shapes operators:
	_GEOMETRY: '$geometry',
	_BOX: '$box',
	_POLYGON: '$polygon',
	_CENTER: '$center',
	_CENTER_SPHERE: '$centerSphere',
	_MAX_DISTANCE: '$maxDistance',
	_MIN_DISTANCE: '$minDistance'
}
const defaultValues: Values = {}
 
const primitives = [
	'string',
	'number',
	'boolean',
	'bigint',
	'undefined',
	'null',
	'symbol'
]
 
export default class GQLMongoQuery {
	keywords: Keywords
	values: Values
 
	constructor(
		Ikeywords: Keywords = defaultKeywords,
		Ivalues: Values = defaultValues,
		Emerge: boolean = true
	) {
		this.keywords = merge ? { ...defaultKeywords, ...keywords } : keywords
		this.values = merge ? { ...defaultValues, ...values } : values
	}
 
	private isOperator(key): boolean {
		return Object.keys(this.keywords).includes(key)
	}
 
	private isPrimitive(val): boolean {
		if (primitives.includes(typeof val)) return true
		else return false
	}
 
	private isComputable(key): boolean {
		return Object.keys(this.values).includes(key)
	}
 
	private isNested(obj): boolean {
		Iif (typeof obj !== 'object') return false
		let isNested = false
		for (const k in obj) {
			if (!this.isOperator(k) && !this.isPrimitive(obj[k]) && !this.isComputable(k)) {
				isNested = true
				break
			}
		}
		return isNested
	}
 
	private computedValue(args) {
		for (const valueKey in this.values) {
			if (args[valueKey] !== undefined) {
				return this.values[valueKey](args)
			}
		}
	}
 
	private argType(key?, val?): ArgType {
		if (this.isOperator(key)) return 'OPERATOR'
		else if (this.isComputable(key)) return 'COMPUTED'
		else if (this.isPrimitive(val)) return 'PRIMITIVE'
		else if ( Array.isArray(val) ) return 'ARRAY'
		else if (this.isNested(val)) return 'NESTED'
		else Eif (typeof val === 'object') return 'FLAT'
		else return null
	}
 
	private parseNested(key, val, ElastResult = {}) {
 
		if (this.isComputable(key)) {
			return this.buildFilters(val, key)
		}
		let result = lastResult
 
		for (const k in val) {
			let isFinal = false
 
			// COMPUTABLE VALUE
			if (this.isComputable(k)) {
				result = { ...result, ...this.buildFilters(val[k], k) }
				return result
			}
 
			// OPERATOR
			if (this.isOperator(k)) {
				result = { ...result, [key]: this.buildFilters(val, key) }
				return result
			}
 
			const subkey = key + '.' + k
			const subval = val[k]
 
			// subval is COMPUTABLE VALUE
			if (this.isComputable(subkey)) {
				result = { ...result, ...this.buildFilters(subval, subkey) }
				isFinal = true
			}
 
			// subval is a PRIMITIVE
			else if (this.isPrimitive(subval)) {
				result[subkey] = this.buildFilters(subval)
				isFinal = true
			}
 
			// subval is NESTED
			else for (const sk in subval) {
				const t = this.argType(sk, subval)
				if (t !== 'NESTED' && t !== 'FLAT') {
					result[subkey] = this.buildFilters(subval)
					isFinal = true
					break
				}
			}
			Iif (!isFinal) this.parseNested(subkey, subval, result)
		}
		return result
	}
 
	buildFilters(args, parentKey?) {
 
		// PARENT IS A COMPUTABLE VALUE
		if (this.isComputable(parentKey)) {
			return this.computedValue({ [parentKey]: args })
		}
 
		// NO PARENT AND ARGS IS A DIRECT VALUE
		if (!parentKey && this.isPrimitive(args)) {
			return args
		}
 
		// ELSE, ARGS MUST BE ON OBJECT. LET'S ITERATE:
		let filters
 
		for (const key in args) {
 
			if (!filters) filters = {}
			const val = args[key]
			const t = this.argType(key, val)
 
			// COMPUTED VALUE
			if (this.isComputable(key)) {
				const computed = this.buildFilters(val, key)
				filters = {
					...filters, ...computed
				}
			}
			// OPERATOR
			else if (t === 'OPERATOR') {
				const keyword = this.keywords[key]
				if (Array.isArray(val)) {
					filters[keyword] = val.map(v => this.buildFilters(v))
				}
				else {
					filters[keyword] = this.buildFilters(val)
				}
			}
			// NESTED QUERY
			else if (t === 'NESTED' || t === 'FLAT') {
				filters = { ...filters, ...this.parseNested(key, val) }
			}
			// ARRAY
			else if (t === 'ARRAY') {
				filters[key] = val.map(v => this.buildFilters(v))
			}
			// ELSE
			else {
				filters[key] = this.buildFilters(val)
			}
		}
		return filters
	}
}