Predicate

Comprehensive guide for querying data using the Predicate

Predicates are used to build query conditions for filtering data. They support various comparison operators, string operations, range checks, collection operations, array operations, date operations, and numeric operations.

Basic Usage

// Basic comparison
final equalsPredicate = Predicate.equals('age', 25);
final greaterThanPredicate = Predicate.greaterThan('price', 100.0);

// String operations
final containsPredicate = Predicate.contains('name', 'John');
final startsWithPredicate = Predicate.startsWith('email', 'john');

// Range operations
final betweenPredicate = Predicate.between('price', 10, 100);

// Collection operations
final inPredicate = Predicate.in_('status', ['active', 'pending']);

// Array operations
final arrayContainsPredicate = Predicate.arrayContains('tags', 'urgent');

// Date operations
final datePredicate = Predicate.dateBefore('createdAt', DateTime.now());

// Numeric operations
final evenPredicate = Predicate.isEven('count');

Fields

  • field: The name of the field to apply the predicate on.
  • operator: The type of operation to perform.
  • value: The primary value to compare against.
  • secondValue: The secondary value (used in operations like between).
  • table: Optional table name for join operations.

Categories of Operations

Basic Comparison Operators

  • equals: Checks if field equals value.
  • notEquals: Checks if field does not equal value.
  • greaterThan: Checks if field is greater than value.
  • lessThan: Checks if field is less than value.
  • greaterThanOrEquals: Checks if field is greater than or equal to value.
  • lessThanOrEquals: Checks if field is less than or equal to value.

Null Checks

  • isNull: Checks if field is null.
  • isNotNull: Checks if field is not null.

String Operations

  • contains: Checks if field contains value.
  • notContains: Checks if field does not contain value.
  • startsWith: Checks if field starts with value.
  • endsWith: Checks if field ends with value.
  • matches: Checks if field matches regular expression pattern.

Case-Insensitive String Operations

  • iEquals: Case-insensitive equality check.
  • iContains: Case-insensitive contains check.
  • iStartsWith: Case-insensitive starts with check.
  • iEndsWith: Case-insensitive ends with check.

Range Operations

  • between: Checks if field is between start and end values.
  • notBetween: Checks if field is not between start and end values.

Collection Operations

  • in_: Checks if field value is in list of values.
  • notIn: Checks if field value is not in list of values.

Array Operations

  • arrayContains: Checks if array field contains single value.
  • arrayContainsAny: Checks if array field contains any of the values.
  • arrayContainsAll: Checks if array field contains all values.
  • arrayIsEmpty: Checks if array field is empty.
  • arrayIsNotEmpty: Checks if array field is not empty.

Date Operations

  • dateEquals: Checks if date field equals value.
  • dateBefore: Checks if date field is before value.
  • dateAfter: Checks if date field is after value.
  • dateOnOrBefore: Checks if date field is on or before value.
  • dateOnOrAfter: Checks if date field is on or after value.
  • dateBetween: Checks if date field is between start and end dates.

Numeric Operations

  • isEven: Checks if numeric field is even.
  • isOdd: Checks if numeric field is odd.
  • isDivisibleBy: Checks if numeric field is divisible by value.
  • fullTextMatch: Performs full-text search on field.