Skip to content

Commit 4112d7f

Browse files
committed
feat: allow to pass an array to filter
1 parent 4fd0bb5 commit 4112d7f

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

src/property.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Property extends BaseProperty {
8080

8181
availableValues(): Array<string> | null {
8282
return this.sequelizePath.values && this.sequelizePath.values.length
83-
? this.sequelizePath.values
83+
? this.sequelizePath.values as Array<string>
8484
: null
8585
}
8686

src/utils/convert-filter.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,64 @@ const convertFilter = (filter) => {
88
return {}
99
}
1010
return filter.reduce((memo, filterProperty) => {
11-
const { property, value } = filterProperty
11+
const { property, value, path: filterPath } = filterProperty
12+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13+
const [_, index] = filterPath.split('.')
14+
const isArray = typeof index !== 'undefined' && !Number.isNaN(Number(index))
15+
const previousValue = memo[property.name()] || {}
1216
switch (property.type()) {
13-
case 'string':
17+
case 'string': {
1418
if (property.sequelizePath.values) {
1519
return {
1620
[property.name()]: { [Op.eq]: `${escape(value)}` },
1721
...memo,
1822
}
1923
}
24+
if (isArray) {
25+
return {
26+
...memo,
27+
[property.name()]: {
28+
[Op.in]: [
29+
...(previousValue[Op.in] || []),
30+
escape(value),
31+
],
32+
},
33+
}
34+
}
2035
return {
2136
[Op.and]: [
2237
...(memo[Op.and] || []),
2338
{
24-
[`${property.name()}`]: {
39+
[property.name()]: {
2540
[Op.iLike as unknown as string]: `%${escape(value)}%`,
2641
},
2742
},
2843
],
2944
...memo,
3045
}
31-
case 'number':
46+
}
47+
case 'number': {
3248
if (!Number.isNaN(Number(value))) {
49+
if (isArray) {
50+
return {
51+
...memo,
52+
[property.name()]: {
53+
[Op.in]: [
54+
...(previousValue[Op.in] || []),
55+
Number(value),
56+
],
57+
},
58+
}
59+
}
3360
return {
3461
[property.name()]: Number(value),
3562
...memo,
3663
}
3764
}
3865
return memo
66+
}
3967
case 'date':
40-
case 'datetime':
68+
case 'datetime': {
4169
if (value.from || value.to) {
4270
return {
4371
[property.name()]: {
@@ -48,9 +76,21 @@ const convertFilter = (filter) => {
4876
}
4977
}
5078
break
79+
}
5180
default:
5281
break
5382
}
83+
if (isArray) {
84+
return {
85+
...memo,
86+
[property.name()]: {
87+
[Op.in]: [
88+
...(previousValue[Op.in] || []),
89+
value,
90+
],
91+
},
92+
}
93+
}
5494
return {
5595
[property.name()]: value,
5696
...memo,

yarn.lock

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,7 +2323,7 @@ babel-polyfill@6.26.0:
23232323
core-js "^2.5.0"
23242324
regenerator-runtime "^0.10.5"
23252325

2326-
babel-runtime@^6.11.6, babel-runtime@^6.23.0, babel-runtime@^6.26.0:
2326+
babel-runtime@^6.23.0, babel-runtime@^6.26.0:
23272327
version "6.26.0"
23282328
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
23292329
dependencies:
@@ -2680,10 +2680,6 @@ chalk@^4.0.0:
26802680
ansi-styles "^4.1.0"
26812681
supports-color "^7.1.0"
26822682

2683-
chance@^1.0.4:
2684-
version "1.0.18"
2685-
resolved "https://registry.yarnpkg.com/chance/-/chance-1.0.18.tgz#79788fe6fca4c338bf404321c347eecc80f969ee"
2686-
26872683
character-entities-html4@^1.0.0:
26882684
version "1.1.4"
26892685
resolved "https://registry.yarnpkg.com/character-entities-html4/-/character-entities-html4-1.1.4.tgz#0e64b0a3753ddbf1fdc044c5fd01d0199a02e125"
@@ -4074,13 +4070,6 @@ extsprintf@^1.2.0:
40744070
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
40754071
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=
40764072

4077-
factory-girl@^5.0.4:
4078-
version "5.0.4"
4079-
resolved "https://registry.yarnpkg.com/factory-girl/-/factory-girl-5.0.4.tgz#378caabe03aac7b327d47d9e28b4f02ced0c3c0b"
4080-
dependencies:
4081-
babel-runtime "^6.11.6"
4082-
chance "^1.0.4"
4083-
40844073
fast-deep-equal@^3.1.1:
40854074
version "3.1.1"
40864075
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz#545145077c501491e33b15ec408c294376e94ae4"
@@ -5997,7 +5986,7 @@ lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.4, lodash@^4.17.5:
59975986
version "4.17.11"
59985987
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
59995988

6000-
lodash@^4.17.14:
5989+
lodash@^4.17.14, lodash@^4.17.20:
60015990
version "4.17.20"
60025991
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
60035992
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
@@ -8654,15 +8643,15 @@ sequelize-pool@^6.0.0:
86548643
resolved "https://registry.yarnpkg.com/sequelize-pool/-/sequelize-pool-6.1.0.tgz#caaa0c1e324d3c2c3a399fed2c7998970925d668"
86558644
integrity sha512-4YwEw3ZgK/tY/so+GfnSgXkdwIJJ1I32uZJztIEgZeAO6HMgj64OzySbWLgxj+tXhZCJnzRfkY9gINw8Ft8ZMg==
86568645

8657-
sequelize@^6.3.5:
8658-
version "6.3.5"
8659-
resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-6.3.5.tgz#80e3db7ac8b76d98c45ca93334197eb6e2335158"
8660-
integrity sha512-MiwiPkYSA8NWttRKAXdU9h0TxP6HAc1fl7qZmMO/VQqQOND83G4nZLXd0kWILtAoT9cxtZgFqeb/MPYgEeXwsw==
8646+
sequelize@>4.42.0:
8647+
version "6.4.0"
8648+
resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-6.4.0.tgz#02c7e06aa7371488c23f110cdd7e4c2666d92142"
8649+
integrity sha512-XiSAaYMidgLHgOFz0d0rMlSXP07YoL3GwuG0KTtXR6moR+lfdAA93vhLaN9K6f1ElLMutNTx2f7bNK6mACYfIA==
86618650
dependencies:
86628651
debug "^4.1.1"
86638652
dottie "^2.0.0"
86648653
inflection "1.12.0"
8665-
lodash "^4.17.15"
8654+
lodash "^4.17.20"
86668655
moment "^2.26.0"
86678656
moment-timezone "^0.5.31"
86688657
retry-as-promised "^3.2.0"

0 commit comments

Comments
 (0)