|
| 1 | +import 'package:codelessly_json_annotation/codelessly_json_annotation.dart'; |
| 2 | +import 'package:equatable/equatable.dart'; |
| 3 | + |
| 4 | +import 'query_filter.dart'; |
| 5 | + |
| 6 | +part 'where_query_filter.g.dart'; |
| 7 | + |
| 8 | +/// Enum representing the Firestore where operation. |
| 9 | +/// |
| 10 | +/// Reference: https://firebase.google.com/docs/firestore/query-data/queries?hl=en |
| 11 | +/// |
| 12 | +/// Cloud Firestore supports the following comparison operators: |
| 13 | +/// < less than |
| 14 | +/// <= less than or equal to |
| 15 | +/// == equal to |
| 16 | +/// > greater than |
| 17 | +/// >= greater than or equal to |
| 18 | +/// != not equal to |
| 19 | +/// array-contains |
| 20 | +/// array-contains-any |
| 21 | +/// in |
| 22 | +/// not-in |
| 23 | +enum WhereQueryOperator { |
| 24 | + /// Compares two values and returns true if they are equal. |
| 25 | + equal('Equal To', '=='), |
| 26 | + |
| 27 | + /// Checks if two values are not equal. |
| 28 | + notEqual('Not Equal To', '!='), |
| 29 | + |
| 30 | + /// Compares two values and returns true if the first is less than the second. |
| 31 | + lessThan('Less Than', '<'), |
| 32 | + |
| 33 | + /// Compares two values and returns true if the first is less than or equal |
| 34 | + /// to the second. |
| 35 | + lessThanOrEqual('Less Than Or Equal To', '<='), |
| 36 | + |
| 37 | + /// Compares two values and returns true if the first is greater than the |
| 38 | + /// second. |
| 39 | + greaterThan('Greater Than', '>'), |
| 40 | + |
| 41 | + /// Compares two values and returns true if the first is greater than or equal |
| 42 | + /// to the second. |
| 43 | + greaterThanOrEqual('Greater Than Or Equal To', '>='), |
| 44 | + |
| 45 | + /// Checks if an array contains a value. |
| 46 | + arrayContains('Array Contains', 'array-contains'), |
| 47 | + |
| 48 | + /// Checks if an array contains any of the values in a given array. |
| 49 | + arrayContainsAny('Array Contains Any', 'array-contains-any'), |
| 50 | + |
| 51 | + /// Checks if a value is in a given array. |
| 52 | + inArray('In Array', 'in'), |
| 53 | + |
| 54 | + /// Checks if a value is not in a given array. |
| 55 | + notInArray('Not In Array', 'not-in'); |
| 56 | + |
| 57 | + /// The human-readable name of the operation. |
| 58 | + final String label; |
| 59 | + |
| 60 | + /// The operator to use in the query. |
| 61 | + final String operator; |
| 62 | + |
| 63 | + const WhereQueryOperator(this.label, this.operator); |
| 64 | + |
| 65 | + /// Returns true if this operation is a range operation. |
| 66 | + bool get isRangeOperator => |
| 67 | + this == WhereQueryOperator.greaterThan || |
| 68 | + this == WhereQueryOperator.greaterThanOrEqual || |
| 69 | + this == WhereQueryOperator.lessThan || |
| 70 | + this == WhereQueryOperator.lessThanOrEqual; |
| 71 | + |
| 72 | + /// Returns true if this operation is an inequality operation. |
| 73 | + bool get isInequalityOperator => |
| 74 | + isRangeOperator || |
| 75 | + this == WhereQueryOperator.notEqual || |
| 76 | + this == WhereQueryOperator.notInArray; |
| 77 | + |
| 78 | + /// Returns true if this operation is an equality operation. |
| 79 | + bool get isEqualityOperator => |
| 80 | + this == WhereQueryOperator.equal || |
| 81 | + this == WhereQueryOperator.arrayContains || |
| 82 | + this == WhereQueryOperator.inArray; |
| 83 | + |
| 84 | + /// Returns a set of all range operators. |
| 85 | + static Set<WhereQueryOperator> get rangeOperators => { |
| 86 | + WhereQueryOperator.greaterThan, |
| 87 | + WhereQueryOperator.greaterThanOrEqual, |
| 88 | + WhereQueryOperator.lessThan, |
| 89 | + WhereQueryOperator.lessThanOrEqual, |
| 90 | + }; |
| 91 | + |
| 92 | + /// Returns a set of all inequality operators. |
| 93 | + static Set<WhereQueryOperator> get inequalityOperators => { |
| 94 | + ...rangeOperators, |
| 95 | + WhereQueryOperator.notEqual, |
| 96 | + WhereQueryOperator.notInArray, |
| 97 | + }; |
| 98 | + |
| 99 | + /// Returns a set of all equality operators. |
| 100 | + static Set<WhereQueryOperator> get equalityOperators => { |
| 101 | + WhereQueryOperator.equal, |
| 102 | + WhereQueryOperator.arrayContains, |
| 103 | + WhereQueryOperator.inArray, |
| 104 | + }; |
| 105 | + |
| 106 | + /// Returns true if the given [operators] iterable contains any equality |
| 107 | + /// operator. |
| 108 | + static bool hasEqualityOperator(Iterable<WhereQueryOperator> operators) { |
| 109 | + return operators.any((operator) => operator.isEqualityOperator); |
| 110 | + } |
| 111 | + |
| 112 | + /// Returns true if the given [operators] iterable contains any inequality |
| 113 | + /// operator. |
| 114 | + static bool hasInequalityOperator(Iterable<WhereQueryOperator> operators) { |
| 115 | + return operators.any((operator) => operator.isInequalityOperator); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/// Represents the where operation in Codelessly Cloud Database. |
| 120 | +/// |
| 121 | +/// Takes three parameters: a field to filter on, a comparison operator, and a value. |
| 122 | +/// |
| 123 | +/// Reference: https://firebase.google.com/docs/firestore/query-data/queries?hl=en |
| 124 | +@JsonSerializable() |
| 125 | +class WhereQueryFilter extends QueryFilter with EquatableMixin { |
| 126 | + /// The where operation to perform. |
| 127 | + final WhereQueryOperator operator; |
| 128 | + |
| 129 | + /// The value to compare against. |
| 130 | + final dynamic value; |
| 131 | + |
| 132 | + /// Creates a new [WhereQueryFilter] instance, given a [field], an [operator], |
| 133 | + /// and a [value]. |
| 134 | + const WhereQueryFilter({ |
| 135 | + required super.field, |
| 136 | + required this.operator, |
| 137 | + required this.value, |
| 138 | + }); |
| 139 | + |
| 140 | + /// Creates a copy of this [WhereQueryFilter] instance, given a [field], an |
| 141 | + /// [operator], and a [value]. |
| 142 | + WhereQueryFilter copyWith({ |
| 143 | + String? field, |
| 144 | + WhereQueryOperator? operator, |
| 145 | + dynamic value, |
| 146 | + }) => |
| 147 | + WhereQueryFilter( |
| 148 | + field: field ?? this.field, |
| 149 | + operator: operator ?? this.operator, |
| 150 | + value: value ?? this.value, |
| 151 | + ); |
| 152 | + |
| 153 | + /// Converts a JSON object to a [WhereQueryFilter] instance. |
| 154 | + factory WhereQueryFilter.fromJson(Map<String, dynamic> json) => |
| 155 | + _$WhereQueryFilterFromJson(json); |
| 156 | + |
| 157 | + /// Converts this [WhereQueryFilter] instance to a JSON object. |
| 158 | + Map<String, dynamic> toJson() => _$WhereQueryFilterToJson(this); |
| 159 | + |
| 160 | + @override |
| 161 | + List<Object?> get props => [field, operator, value]; |
| 162 | +} |
0 commit comments