filter buildout

This commit is contained in:
Tom Moor committed 2025-12-14 21:31:11 -05:00
1 parent 14cdfb6917
commit ca2fce9b7a
3 files changed
+708

No files matched your search

+560
View File
@@ -0,0 +1,560 @@
import { Op } from "sequelize";
import { buildWhere } from "./Filters";
describe("buildWhere", () => {
describe("single conditions", () => {
it("should handle eq operator", () => {
const filter = {
field: "title",
operator: "eq",
value: "Test Document",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.eq]: "Test Document" },
});
});
it("should handle neq operator", () => {
const filter = {
field: "status",
operator: "neq",
value: "archived",
};
const result = buildWhere(filter);
expect(result).toEqual({
status: { [Op.ne]: "archived" },
});
});
it("should handle lt operator", () => {
const filter = {
field: "views",
operator: "lt",
value: 100,
};
const result = buildWhere(filter);
expect(result).toEqual({
views: { [Op.lt]: 100 },
});
});
it("should handle lte operator", () => {
const filter = {
field: "views",
operator: "lte",
value: 100,
};
const result = buildWhere(filter);
expect(result).toEqual({
views: { [Op.lte]: 100 },
});
});
it("should handle gt operator", () => {
const filter = {
field: "views",
operator: "gt",
value: 100,
};
const result = buildWhere(filter);
expect(result).toEqual({
views: { [Op.gt]: 100 },
});
});
it("should handle gte operator", () => {
const filter = {
field: "views",
operator: "gte",
value: 100,
};
const result = buildWhere(filter);
expect(result).toEqual({
views: { [Op.gte]: 100 },
});
});
it("should handle contains operator", () => {
const filter = {
field: "title",
operator: "contains",
value: "test",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.like]: "%test%" },
});
});
it("should handle containsCaseInsensitive operator", () => {
const filter = {
field: "title",
operator: "containsCaseInsensitive",
value: "test",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.iLike]: "%test%" },
});
});
it("should handle startsWith operator", () => {
const filter = {
field: "title",
operator: "startsWith",
value: "test",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.like]: "test%" },
});
});
it("should handle startsWithCaseInsensitive operator", () => {
const filter = {
field: "title",
operator: "startsWithCaseInsensitive",
value: "test",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.iLike]: "test%" },
});
});
it("should handle endsWith operator", () => {
const filter = {
field: "title",
operator: "endsWith",
value: "test",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.like]: "%test" },
});
});
it("should handle endsWithCaseInsensitive operator", () => {
const filter = {
field: "title",
operator: "endsWithCaseInsensitive",
value: "test",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.iLike]: "%test" },
});
});
it("should handle in operator", () => {
const filter = {
field: "status",
operator: "in",
value: ["draft", "published"],
};
const result = buildWhere(filter);
expect(result).toEqual({
status: { [Op.in]: ["draft", "published"] },
});
});
it("should handle notIn operator", () => {
const filter = {
field: "status",
operator: "notIn",
value: ["archived", "deleted"],
};
const result = buildWhere(filter);
expect(result).toEqual({
status: { [Op.notIn]: ["archived", "deleted"] },
});
});
it("should handle isNull operator", () => {
const filter = {
field: "deletedAt",
operator: "isNull",
};
const result = buildWhere(filter);
expect(result).toEqual({
deletedAt: { [Op.is]: null },
});
});
it("should handle isNotNull operator", () => {
const filter = {
field: "publishedAt",
operator: "isNotNull",
};
const result = buildWhere(filter);
expect(result).toEqual({
publishedAt: { [Op.not]: null },
});
});
it("should throw error for unsupported operator", () => {
const filter = {
field: "title",
operator: "unsupported",
value: "test",
};
expect(() => buildWhere(filter)).toThrow(
"Unsupported operator: unsupported"
);
});
});
describe("filter groups", () => {
it("should handle AND group with multiple conditions", () => {
const filter = {
operator: "AND" as const,
filters: [
{
field: "status",
operator: "eq",
value: "published",
},
{
field: "views",
operator: "gt",
value: 100,
},
],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.and]: [
{ status: { [Op.eq]: "published" } },
{ views: { [Op.gt]: 100 } },
],
});
});
it("should handle OR group with multiple conditions", () => {
const filter = {
operator: "OR" as const,
filters: [
{
field: "title",
operator: "contains",
value: "test",
},
{
field: "content",
operator: "contains",
value: "test",
},
],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.or]: [
{ title: { [Op.like]: "%test%" } },
{ content: { [Op.like]: "%test%" } },
],
});
});
it("should handle nested filter groups", () => {
const filter = {
operator: "AND" as const,
filters: [
{
field: "status",
operator: "eq",
value: "published",
},
{
operator: "OR" as const,
filters: [
{
field: "title",
operator: "contains",
value: "important",
},
{
field: "tags",
operator: "contains",
value: "urgent",
},
],
},
],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.and]: [
{ status: { [Op.eq]: "published" } },
{
[Op.or]: [
{ title: { [Op.like]: "%important%" } },
{ tags: { [Op.like]: "%urgent%" } },
],
},
],
});
});
it("should handle deeply nested filter groups", () => {
const filter = {
operator: "OR" as const,
filters: [
{
operator: "AND" as const,
filters: [
{
field: "status",
operator: "eq",
value: "published",
},
{
field: "views",
operator: "gte",
value: 1000,
},
],
},
{
operator: "AND" as const,
filters: [
{
field: "featured",
operator: "eq",
value: true,
},
{
operator: "OR" as const,
filters: [
{
field: "createdAt",
operator: "gt",
value: new Date("2024-01-01"),
},
{
field: "updatedAt",
operator: "gt",
value: new Date("2024-01-01"),
},
],
},
],
},
],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.or]: [
{
[Op.and]: [
{ status: { [Op.eq]: "published" } },
{ views: { [Op.gte]: 1000 } },
],
},
{
[Op.and]: [
{ featured: { [Op.eq]: true } },
{
[Op.or]: [
{ createdAt: { [Op.gt]: new Date("2024-01-01") } },
{ updatedAt: { [Op.gt]: new Date("2024-01-01") } },
],
},
],
},
],
});
});
it("should handle single condition in a group", () => {
const filter = {
operator: "AND" as const,
filters: [
{
field: "status",
operator: "eq",
value: "published",
},
],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.and]: [{ status: { [Op.eq]: "published" } }],
});
});
it("should handle empty filter groups", () => {
const filter = {
operator: "AND" as const,
filters: [],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.and]: [],
});
});
it("should handle mixed numeric and string values", () => {
const filter = {
operator: "AND" as const,
filters: [
{
field: "price",
operator: "gte",
value: 99.99,
},
{
field: "category",
operator: "eq",
value: "electronics",
},
{
field: "inStock",
operator: "eq",
value: true,
},
],
};
const result = buildWhere(filter);
expect(result).toEqual({
[Op.and]: [
{ price: { [Op.gte]: 99.99 } },
{ category: { [Op.eq]: "electronics" } },
{ inStock: { [Op.eq]: true } },
],
});
});
});
describe("edge cases", () => {
it("should handle special characters in like operators", () => {
const filter = {
field: "title",
operator: "contains",
value: "test%_special",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.like]: "%test%_special%" },
});
});
it("should handle empty strings in values", () => {
const filter = {
field: "title",
operator: "eq",
value: "",
};
const result = buildWhere(filter);
expect(result).toEqual({
title: { [Op.eq]: "" },
});
});
it("should handle null values in eq/neq operators", () => {
const filterEq = {
field: "parentId",
operator: "eq",
value: null,
};
const resultEq = buildWhere(filterEq);
expect(resultEq).toEqual({
parentId: { [Op.eq]: null },
});
const filterNeq = {
field: "parentId",
operator: "neq",
value: null,
};
const resultNeq = buildWhere(filterNeq);
expect(resultNeq).toEqual({
parentId: { [Op.ne]: null },
});
});
it("should handle boolean values", () => {
const filter = {
field: "isPublished",
operator: "eq",
value: false,
};
const result = buildWhere(filter);
expect(result).toEqual({
isPublished: { [Op.eq]: false },
});
});
it("should handle date values", () => {
const date = new Date("2024-01-01T00:00:00Z");
const filter = {
field: "createdAt",
operator: "gte",
value: date,
};
const result = buildWhere(filter);
expect(result).toEqual({
createdAt: { [Op.gte]: date },
});
});
});
});
+80
View File
@@ -0,0 +1,80 @@
import { Op, WhereOptions } from "sequelize";
const operatorMap: Record<string, symbol> = {
eq: Op.eq,
neq: Op.ne,
lt: Op.lt,
lte: Op.lte,
gt: Op.gt,
gte: Op.gte,
contains: Op.like,
containsCaseInsensitive: Op.iLike,
startsWith: Op.like,
startsWithCaseInsensitive: Op.iLike,
endsWith: Op.like,
endsWithCaseInsensitive: Op.iLike,
in: Op.in,
notIn: Op.notIn,
isNull: Op.is,
isNotNull: Op.not,
};
function buildCondition(condition: {
field: string;
operator: string;
value?: unknown;
}): WhereOptions {
const { field, operator, value } = condition;
const sequelizeOp = operatorMap[operator];
if (!sequelizeOp) {
throw new Error(`Unsupported operator: ${operator}`);
}
switch (operator) {
case "contains":
case "containsCaseInsensitive":
return { [field]: { [sequelizeOp]: `%${value}%` } };
case "startsWith":
case "startsWithCaseInsensitive":
return { [field]: { [sequelizeOp]: `${value}%` } };
case "endsWith":
case "endsWithCaseInsensitive":
return { [field]: { [sequelizeOp]: `%${value}` } };
case "isNull":
return { [field]: { [Op.is]: null } };
case "isNotNull":
return { [field]: { [Op.not]: null } };
default:
return { [field]: { [sequelizeOp]: value } };
}
}
type FilterCondition = {
field: string;
operator: string;
value?: unknown;
};
type FilterGroup = {
operator: "AND" | "OR";
filters: Array<FilterCondition | FilterGroup>;
};
export function buildWhere(
filter: FilterCondition | FilterGroup
): WhereOptions {
if ("field" in filter) {
return buildCondition(filter);
}
const subWheres = filter.filters.map(buildWhere);
return {
[filter.operator === "AND" ? Op.and : Op.or]: subWheres,
};
}
+68
View File
@@ -0,0 +1,68 @@
import { z } from "zod";
/**
* Factory to create a strongly-typed filter schema for a given entity.
*
* Example:
* const UserFields = z.enum(["id", "createdAt", "email", "status"]);
* const UserFilterSchema = createFilterSchema(UserFields);
*/
export function builderFilterSchema<Fields extends [string, ...string[]]>(
fieldsEnum: z.ZodEnum<Fields>
) {
const ComparisonOperator = z.enum([
"eq",
"neq",
"lt",
"lte",
"gt",
"gte",
"contains",
"containsCaseInsensitive",
"startsWith",
"startsWithCaseInsensitive",
"endsWith",
"endsWithCaseInsensitive",
"in",
"notIn",
"isNull",
"isNotNull",
]);
const LogicalOperator = z.enum(["AND", "OR"]);
const FilterValue = z.union([
z.string(),
z.number(),
z.boolean(),
z.date(),
z.array(z.string()),
z.array(z.number()),
]);
const FilterCondition = z.object({
field: fieldsEnum,
operator: ComparisonOperator,
value: FilterValue.optional(), // omitted for isNull / isNotNull
});
const FilterGroup: z.ZodType<{
operator: z.infer<typeof LogicalOperator>;
filters: Array<z.infer<typeof FilterCondition> | any>;
}> = z.lazy(() =>
z.object({
operator: LogicalOperator,
filters: z.array(z.union([FilterCondition, FilterGroup])).min(1),
})
);
const FilterSchema = z.union([FilterCondition, FilterGroup]);
return {
FilterSchema,
FilterCondition,
FilterGroup,
ComparisonOperator,
LogicalOperator,
};
}