Skip to content

Conversation

anivar
Copy link

@anivar anivar commented Sep 4, 2025

Implements support for 'in' and 'notIn' operators in DataStore predicates, enabling cleaner multi-value filtering without complex OR chains.

Resolves #14543

Description

Instead of writing multiple OR conditions, developers can now use array-based filtering. The implementation adds these operators to the comparison set, implements matching logic to check array membership, validates that operands are arrays, and includes TypeScript type definitions for IDE support.

Implementation Details

Core Predicates (datastore package)

  • Added 'in' and 'notIn' operators to comparison operators
  • Implemented array membership checking logic
  • Added validation and TypeScript definitions

SQLite Adapter (datastore-storage-adapter package)

  • Implemented SQL generation for IN/NOT IN operators in whereConditionFromPredicateObject
  • Empty arrays return logical SQL conditions (1=0 for 'in', 1=1 for 'notIn')
  • Uses parameterized queries to prevent SQL injection
  • Handles complex types through prepareValueForDML (JSON.stringify for objects)
  • Fixed TypeScript issue in CommonSQLiteAdapter with optional chaining

Testing

  • Added comprehensive test coverage for both predicates and SQL generation
  • 10 unit tests for whereConditionFromPredicateObject
  • 3 integration tests for compound predicates (AND/OR/nested)
  • Edge cases covered: empty arrays, non-arrays, null values, complex objects
  • All tests pass, linting clean, build successful

Example Usage

// Filter posts by multiple statuses
await DataStore.query(Post, p => 
  p.status.in(['draft', 'review', 'pending'])
);

// Exclude archived items
await DataStore.query(Post, p => 
  p.status.notIn(['archived', 'deleted'])
);

Compatibility

The existing GraphQL filter generation and storage adapters handle these operators without modification since they process operators generically. This is a purely additive feature with no breaking changes.

Signed-off-by: Anivar Aravind [email protected]

@anivar anivar requested a review from a team as a code owner September 4, 2025 20:30
Anivar A Aravind added 2 commits September 6, 2025 20:42
…ring

Implements support for 'in' and 'notIn' operators in DataStore predicates,
enabling cleaner multi-value filtering without complex OR chains.

Changes:
- Add 'in' and 'notIn' to comparisonKeys Set
- Implement array membership checking logic in matches()
- Add proper validation for array operands
- Include negation operator mapping
- Add TypeScript type definitions for full IDE support

Usage examples:
  c.status.in(['ACTIVE', 'PENDING', 'DRAFT'])
  c.priority.notIn(['ARCHIVED', 'DELETED'])
  c.tenantId.in(allowedTenantIds)

Resolves aws-amplify#14543
…redicates

- Add 'in' and 'notIn' case handlers in validatePredicateField function
- Update ValuePredicate type to support array operands for in/notIn operators
- Ensure proper type safety and validation for multi-value filtering
@anivar anivar force-pushed the feat/add-in-operator-datastore branch from e5b70fe to 518d678 Compare September 6, 2025 15:15
anivar and others added 2 commits September 12, 2025 00:48
…rators

Implements SQL generation for 'in' and 'notIn' operators in SQLite adapter
to support DataStore predicates with array-based filtering.

Changes:
- Add in/notIn SQL generation in whereConditionFromPredicateObject
- Handle empty arrays with logical conditions (1=0 for false, 1=1 for true)
- Use parameterized queries to prevent SQL injection
- Add prepareValueForDML for complex type handling
- Fix TypeScript issue in CommonSQLiteAdapter with optional chaining

Testing:
- Add 10 unit tests for whereConditionFromPredicateObject
- Add 3 integration tests for compound predicates
- Cover edge cases including empty arrays and complex objects
- All tests pass with 100% success rate

Fixes aws-amplify#14544

Signed-off-by: Anivar Aravind <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add 'in' and 'notIn' operators to DataStore predicates
1 participant