-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
DetaQuery
class to create query in search
- Loading branch information
Showing
2 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
part of 'deta.dart'; | ||
|
||
/// {@template deta_query} | ||
/// Create a query for search elements in the databese. | ||
/// {@endtemplate} | ||
class DetaQuery { | ||
///{@macro deta_query} | ||
DetaQuery(this.key) : query = <String, dynamic>{}; | ||
|
||
DetaQuery._withPreviousQuery(this.key, this.query); | ||
|
||
/// Key for indicate the search value. | ||
final String key; | ||
|
||
/// Query status. | ||
final Map<String, dynamic> query; | ||
|
||
/// Join the old query with a new one. | ||
DetaQuery and(String key) { | ||
return DetaQuery._withPreviousQuery(key, query); | ||
} | ||
|
||
/// Check the content of [key] is equal to [value]. | ||
DetaQuery equalTo(Object value) { | ||
return _createQuery(value, ''); | ||
} | ||
|
||
/// Check the content of [key] is not equal to [value]. | ||
DetaQuery notEqualTo(Object value) { | ||
return _createQuery(value, '?ne'); | ||
} | ||
|
||
/// Check the content of [key] is less than [value]. | ||
DetaQuery lessThan(num value) { | ||
return _createQuery(value, '?lt'); | ||
} | ||
|
||
/// Check the content of [key] is greater than [value]. | ||
DetaQuery greaterThan(num value) { | ||
return _createQuery(value, '?gt'); | ||
} | ||
|
||
/// Check the content of [key] isgreater than or equal to [value]. | ||
DetaQuery greaterThanOrEqualTo(num value) { | ||
return _createQuery(value, '?gte'); | ||
} | ||
|
||
/// Check the content of [key] is less than or equal to [value]. | ||
DetaQuery lessThanOrEqualTo(num value) { | ||
return _createQuery(value, '?lte'); | ||
} | ||
|
||
/// Check the content of [key] is a prefix equal to [value]. | ||
DetaQuery prefix(String value) { | ||
return _createQuery(value, '?pfx'); | ||
} | ||
|
||
/// Check the content of [key] in the range [start] and [end]. | ||
DetaQuery range(num start, num end) { | ||
return _createQuery([start, end], '?r'); | ||
} | ||
|
||
/// Check the content of [key] constains [value]. | ||
/// | ||
/// NOTE: Only works for a list of strings, not apply to list | ||
/// of other data types. | ||
DetaQuery contains(String value) { | ||
return _createQuery(value, '?contains'); | ||
} | ||
|
||
/// Check the content of [key] does not constain [value]. | ||
/// | ||
/// NOTE: Only works for a list of strings, not apply to list | ||
/// of other data types. | ||
DetaQuery notContains(String value) { | ||
return _createQuery(value, '?not_contains'); | ||
} | ||
|
||
DetaQuery _createQuery(Object value, String operator) { | ||
query['$key$operator'] = value; | ||
return DetaQuery._withPreviousQuery(key, query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:deta/deta.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
late DetaQuery query; | ||
const key = 'name'; | ||
|
||
setUp(() { | ||
query = DetaQuery(key); | ||
}); | ||
|
||
group('DetaQuery', () { | ||
test('equalTo', () { | ||
expect( | ||
query.equalTo('Jhon').query, | ||
equals(<String, dynamic>{key: 'Jhon'}), | ||
); | ||
}); | ||
|
||
test('notEqualTo', () { | ||
expect( | ||
query.notEqualTo('Jhon').query, | ||
equals(<String, dynamic>{'$key?ne': 'Jhon'}), | ||
); | ||
}); | ||
|
||
test('lessThan', () { | ||
expect( | ||
query.lessThan(16).query, | ||
equals(<String, dynamic>{'$key?lt': 16}), | ||
); | ||
}); | ||
|
||
test('greaterThan', () { | ||
expect( | ||
query.greaterThan(18).query, | ||
equals(<String, dynamic>{'$key?gt': 18}), | ||
); | ||
}); | ||
test('lessThanOrEqualTo', () { | ||
expect( | ||
query.lessThanOrEqualTo(22).query, | ||
equals(<String, dynamic>{'$key?lte': 22}), | ||
); | ||
}); | ||
test('greaterThanOrEqualTo', () { | ||
expect( | ||
query.greaterThanOrEqualTo(28).query, | ||
equals(<String, dynamic>{'$key?gte': 28}), | ||
); | ||
}); | ||
test('prefix', () { | ||
expect( | ||
query.prefix('coin').query, | ||
equals(<String, dynamic>{'$key?pfx': 'coin'}), | ||
); | ||
}); | ||
|
||
test('range', () { | ||
expect( | ||
query.range(16, 28).query, | ||
equals(<String, dynamic>{ | ||
'$key?r': [16, 28] | ||
}), | ||
); | ||
}); | ||
test('contains', () { | ||
expect( | ||
query.contains('@deta.sh').query, | ||
equals(<String, dynamic>{'$key?contains': '@deta.sh'}), | ||
); | ||
}); | ||
|
||
test('notContains', () { | ||
expect( | ||
query.notContains('@deta.sh').query, | ||
equals(<String, dynamic>{'$key?not_contains': '@deta.sh'}), | ||
); | ||
}); | ||
|
||
test('and', () { | ||
expect( | ||
DetaQuery('email') | ||
.contains('@deta.sh') | ||
.and('name') | ||
.equalTo('Jhon') | ||
.query, | ||
equals(<String, dynamic>{'email?contains': '@deta.sh', 'name': 'Jhon'}), | ||
); | ||
}); | ||
}); | ||
} |