Skip to content

Commit 2e2994e

Browse files
wip - adding blacklist and custom handlers
1 parent 78162be commit 2e2994e

File tree

1 file changed

+67
-35
lines changed

1 file changed

+67
-35
lines changed

index.js

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class SequelizeQS {
2323
constructor(options) {
2424
this.opt = options || {}
2525
this.ops = this.opt.ops || Object.keys(this.#opMaps);
26+
this.alias = this.opt.alias || {};
27+
this.blacklist = this.opt.blacklist || [];
28+
this.customHandlers = this.opt.custom || {};
2629
this.defaultPaginationLimit = this.opt.defaultPaginationLimit || 25;
2730
this.maximumPageSize = this.opt.maximumPageSize || 100;
2831

@@ -109,50 +112,79 @@ class SequelizeQS {
109112

110113
#parseWhereProperties(properties) {
111114
let where = {};
112-
Object.keys(properties).forEach((k) => {
113-
let key = k;
115+
let columns = Object.keys(properties);
116+
117+
for (const column of columns) {
118+
let key = this.alias[column] || column; // handles an alias
119+
console.log('column', column);
114120
let value = properties[key];
115121

116-
// check we have an actual value set here
117-
if (!value
118-
|| value.length === 0
119-
|| (
120-
typeof (value) === 'string'
121-
&& value.toLowerCase() === 'null'
122-
)) {
123-
// checks the value to see if it is null
122+
// check the blacklist
123+
if (this.blacklist.some(el => el === key)) {
124+
break;
125+
}
126+
127+
// TODO: do we need to check for whitelist & let more dangerous query run?
128+
129+
// handles a custom function when we want to override the base functionality
130+
if (this.customHandlers[key] && typeof(this.customHandlers[key]) === 'function') {
131+
// call the custom handler function and get the operation to apply
132+
let customOperation = this.customHandlers[key](key, value, this.opt);
133+
where = {
134+
...where,
135+
[key]: customOperation
136+
};
137+
138+
break;
139+
}
140+
141+
// handle when no value is supplied (e.g. `?maxAge=`)
142+
if (!value || value.length === 0 || (typeof(value) === 'string' && value.toLowerCase() === 'null')) {
124143
let { operation } = handleBasicComparator('is', key, null, this.opt);
125-
value = operation;
144+
where = {
145+
...where,
146+
[key]: operation
147+
};
148+
149+
break;
126150
}
127-
else if (typeof (value) === 'string') {
128-
if (value === '!' || value.toLowerCase() === '!null') {
129-
// checks to see if the field IS NOT NULL
130-
let { operation } = handleBasicComparator('is not', key, null, this.opt);
131-
value = operation;
132-
}
133-
else if (this.ops.some(op => value.startsWith(op))) {
134-
let op = this.ops.find(op => value.startsWith(op));
135-
let fn = this.#opMaps[op];
136-
137-
if (!fn || !typeof (fn) === 'function') {
138-
throw 'Operation is not a function';
139-
}
140-
141-
const { operation } = fn(op, key, value, this.opt);
142-
value = operation;
143-
}
144-
else {
145-
// this is just a straight up value/equals key-value pair
146-
let { operation } = handleBasicComparator('=', key, value, this.opt);
147-
value = operation;
151+
152+
// handle when a NOT value is supplied (so when not null sort of a thing) (e.g. `?maxAge=!` or `?maxAge=!null`)
153+
if (typeof(value) === 'string' && (value === '!' || value.toLowerCase() === '!null')) {
154+
let { operation } = handleBasicComparator('is not', key, null, this.opt);
155+
where = {
156+
...where,
157+
[key]: operation
158+
};
159+
160+
break;
161+
}
162+
163+
// check if we have an operator to handle a special type (not a basic `=` equals basically)
164+
if (typeof(value) === 'string' && this.ops.some(op => value.startsWith(op))) {
165+
let op = this.ops.find(op => value.startsWith(op));
166+
let fn = this.#opMaps[op];
167+
168+
if (!fn || !typeof (fn) === 'function') {
169+
throw 'Operation is not a function';
148170
}
171+
172+
let { operation } = fn(op, key, value, this.opt);
173+
where = {
174+
...where,
175+
[key]: operation
176+
};
177+
178+
break;
149179
}
150-
180+
181+
// finally, we will just default to the basic equals operation
182+
let { operation } = handleBasicComparator('=', key, value, this.opt);
151183
where = {
152184
...where,
153-
[key]: value
185+
[key]: operation
154186
};
155-
});
187+
}
156188

157189
return where;
158190
}

0 commit comments

Comments
 (0)