chore: escape underscore char in include query (#3681)
This commit is contained in:
parent
94cbbf93b8
commit
37b97c6e52
@ -24,6 +24,22 @@ describe('string operator', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should escape underscore in inlcude operator', async () => {
|
||||||
|
const u1 = await db.getRepository('users').create({
|
||||||
|
values: {
|
||||||
|
name: 'names of u1',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const u1Res = await db.getRepository('users').findOne({
|
||||||
|
filter: {
|
||||||
|
'name.$includes': '_',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(u1Res).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
it('should query with include operator', async () => {
|
it('should query with include operator', async () => {
|
||||||
const u1 = await db.getRepository('users').create({
|
const u1 = await db.getRepository('users').create({
|
||||||
values: {
|
values: {
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
import { Op } from 'sequelize';
|
import { Op } from 'sequelize';
|
||||||
import { isPg } from './utils';
|
import { isPg } from './utils';
|
||||||
|
|
||||||
|
function escapeLike(value: string) {
|
||||||
|
return value.replace(/[_%]/g, '\\$&');
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
$includes(value, ctx) {
|
$includes(value, ctx) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const conditions = value.map((item) => ({
|
const conditions = value.map((item) => ({
|
||||||
[isPg(ctx) ? Op.iLike : Op.like]: `%${item}%`,
|
[isPg(ctx) ? Op.iLike : Op.like]: `%${escapeLike(item)}%`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -14,14 +18,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[isPg(ctx) ? Op.iLike : Op.like]: `%${value}%`,
|
[isPg(ctx) ? Op.iLike : Op.like]: `%${escapeLike(value)}%`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
$notIncludes(value, ctx) {
|
$notIncludes(value, ctx) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const conditions = value.map((item) => ({
|
const conditions = value.map((item) => ({
|
||||||
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${item}%`,
|
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${escapeLike(item)}%`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -30,14 +34,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}%`,
|
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${escapeLike(value)}%`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
$startsWith(value, ctx) {
|
$startsWith(value, ctx) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const conditions = value.map((item) => ({
|
const conditions = value.map((item) => ({
|
||||||
[isPg(ctx) ? Op.iLike : Op.like]: `${item}%`,
|
[isPg(ctx) ? Op.iLike : Op.like]: `${escapeLike(item)}%`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -46,14 +50,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[isPg(ctx) ? Op.iLike : Op.like]: `${value}%`,
|
[isPg(ctx) ? Op.iLike : Op.like]: `${escapeLike(value)}%`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
$notStartsWith(value, ctx) {
|
$notStartsWith(value, ctx) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const conditions = value.map((item) => ({
|
const conditions = value.map((item) => ({
|
||||||
[isPg(ctx) ? Op.notILike : Op.notLike]: `${item}%`,
|
[isPg(ctx) ? Op.notILike : Op.notLike]: `${escapeLike(item)}%`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -62,14 +66,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[isPg(ctx) ? Op.notILike : Op.notLike]: `${value}%`,
|
[isPg(ctx) ? Op.notILike : Op.notLike]: `${escapeLike(value)}%`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
$endWith(value, ctx) {
|
$endWith(value, ctx) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const conditions = value.map((item) => ({
|
const conditions = value.map((item) => ({
|
||||||
[isPg(ctx) ? Op.iLike : Op.like]: `%${item}`,
|
[isPg(ctx) ? Op.iLike : Op.like]: `%${escapeLike(item)}`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -78,14 +82,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[isPg(ctx) ? Op.iLike : Op.like]: `%${value}`,
|
[isPg(ctx) ? Op.iLike : Op.like]: `%${escapeLike(value)}`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
$notEndWith(value, ctx) {
|
$notEndWith(value, ctx) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const conditions = value.map((item) => ({
|
const conditions = value.map((item) => ({
|
||||||
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${item}`,
|
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${escapeLike(item)}`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -94,7 +98,7 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}`,
|
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${escapeLike(value)}`,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
} as Record<string, any>;
|
} as Record<string, any>;
|
||||||
|
Loading…
Reference in New Issue
Block a user