DataFn
@datafn/core

Filter Utilities

Filter evaluation and operator utilities.

evaluateFilter

Evaluates a DFQL filter against a single record. Returns true if the record matches all filter conditions.

function evaluateFilter(
  record: Record<string, unknown>,
  filters: Record<string, unknown>,
  opts?: FilterEvalOptions,
): boolean;

FilterEvalOptions

interface FilterEvalOptions {
  resolveRelation?: (resource: string, recordId: string, relationName: string) => Record<string, unknown>[];
  resource?: string;
}

The resolveRelation callback is used for dot-path filters that traverse relations (e.g., "author.name": { $eq: "Alice" }).

Supported Operators

All operators use the $-prefix form internally.

OperatorDescription
$eqEqual.
$neNot equal.
$gtGreater than.
$gteGreater than or equal.
$ltLess than.
$lteLess than or equal.
$inValue in array.
$ninValue not in array.
$containsString contains substring, or array contains element.
$startsWithString starts with prefix.
$endsWithString ends with suffix.
$likeSQL LIKE pattern (% = any, _ = single char). Case-sensitive.
$ilikeCase-insensitive LIKE.
$not_likeNegated LIKE.
$not_ilikeNegated case-insensitive LIKE.
$is_nulltrue if value is null/undefined, false if not.
$is_not_nullInverse of $is_null.
$is_emptytrue if null, undefined, empty string, or empty array.
$is_not_emptyInverse of $is_empty.
$beforeLess than (for dates/timestamps).
$afterGreater than (for dates/timestamps).
$betweenValue in [low, high] range (inclusive).
$not_betweenValue outside [low, high] range.

Logical Combinators

CombinatorDescription
$andArray of filter objects. All must match.
$orArray of filter objects. At least one must match.

Maximum nesting depth is 10 levels.

OP_REMAP

Maps non-$-prefixed operator names to their canonical $-prefixed form.

const OP_REMAP: Record<string, string> = {
  eq: "$eq", ne: "$ne", gt: "$gt", gte: "$gte", lt: "$lt", lte: "$lte",
  in: "$in", not_in: "$nin",
  like: "$like", ilike: "$ilike", not_like: "$not_like", not_ilike: "$not_ilike",
  is_null: "$is_null", is_not_null: "$is_not_null",
  is_empty: "$is_empty", is_not_empty: "$is_not_empty",
  before: "$before", after: "$after", between: "$between", not_between: "$not_between",
};

normalizeFilterOps

Recursively remaps non-$-prefixed operator names to their canonical form.

function normalizeFilterOps(filters: Record<string, unknown>): Record<string, unknown>;

likeToRegExp

Converts a SQL LIKE pattern to a JavaScript RegExp.

function likeToRegExp(pattern: string, caseInsensitive: boolean): RegExp;

% maps to .*, _ maps to .. Special regex characters are escaped.