Skip to content

Commit 07ef867

Browse files
authored
Merge pull request #766 from appwrite/feat-json-queries
Feat json queries
2 parents 4bece60 + 677a61c commit 07ef867

File tree

37 files changed

+1295
-675
lines changed

37 files changed

+1295
-675
lines changed

src/SDK/Language/JS.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ public function getTypeName(array $parameter, array $spec = []): string
131131
if (!empty($parameter['enumValues'])) {
132132
return \ucfirst($parameter['name']);
133133
}
134-
135134
switch ($parameter['type']) {
136135
case self::TYPE_INTEGER:
137136
case self::TYPE_NUMBER:
Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
11
package {{ sdk.namespace | caseDot }}
22

3-
class Query {
4-
companion object {
5-
fun equal(attribute: String, value: Any) = addQuery(attribute, "equal", value)
3+
import {{ sdk.namespace | caseDot }}.extensions.toJson
4+
import {{ sdk.namespace | caseDot }}.extensions.fromJson
65

7-
fun notEqual(attribute: String, value: Any) = Query.addQuery(attribute, "notEqual", value)
6+
class Query(
7+
val method: String,
8+
val attribute: String? = null,
9+
val values: List<Any>? = null,
10+
) {
11+
override fun toString() = this.toJson()
812

9-
fun lessThan(attribute: String, value: Any) = Query.addQuery(attribute, "lessThan", value)
13+
companion object {
14+
fun equal(attribute: String, value: Any) = Query("equal", attribute, parseValue(value)).toJson()
1015

11-
fun lessThanEqual(attribute: String, value: Any) = Query.addQuery(attribute, "lessThanEqual", value)
16+
fun notEqual(attribute: String, value: Any) = Query("notEqual", attribute, parseValue(value)).toJson()
1217

13-
fun greaterThan(attribute: String, value: Any) = Query.addQuery(attribute, "greaterThan", value)
18+
fun lessThan(attribute: String, value: Any) = Query("lessThan", attribute, parseValue(value)).toJson()
1419

15-
fun greaterThanEqual(attribute: String, value: Any) = Query.addQuery(attribute, "greaterThanEqual", value)
16-
17-
fun search(attribute: String, value: String) = Query.addQuery(attribute, "search", value)
20+
fun lessThanEqual(attribute: String, value: Any) = Query("lessThanEqual", attribute, parseValue(value)).toJson()
1821

19-
fun isNull(attribute: String) = "isNull(\"${attribute}\")"
22+
fun greaterThan(attribute: String, value: Any) = Query("greaterThan", attribute, parseValue(value)).toJson()
2023

21-
fun isNotNull(attribute: String) = "isNotNull(\"${attribute}\")"
24+
fun greaterThanEqual(attribute: String, value: Any) = Query("greaterThanEqual", attribute, parseValue(value)).toJson()
2225

23-
fun between(attribute: String, start: Int, end: Int) = "between(\"${attribute}\", ${start}, ${end})"
26+
fun search(attribute: String, value: String) = Query("search", attribute, listOf(value)).toJson()
2427

25-
fun between(attribute: String, start: Double, end: Double) = "between(\"${attribute}\", ${start}, ${end})"
28+
fun isNull(attribute: String) = Query("isNull", attribute).toJson()
2629

27-
fun between(attribute: String, start: String, end: String) = "between(\"${attribute}\", \"${start}\", \"${end}\")"
30+
fun isNotNull(attribute: String) = Query("isNotNull", attribute).toJson()
2831

29-
fun startsWith(attribute: String, value: String) = Query.addQuery(attribute, "startsWith", value)
32+
fun between(attribute: String, start: Any, end: Any) = Query("between", attribute, listOf(start, end)).toJson()
3033

31-
fun endsWith(attribute: String, value: String) = Query.addQuery(attribute, "endsWith", value)
34+
fun startsWith(attribute: String, value: String) = Query("startsWith", attribute, listOf(value)).toJson()
3235

33-
fun select(attributes: List<String>) = "select([${attributes.joinToString(",") { "\"$it\"" }}])"
36+
fun endsWith(attribute: String, value: String) = Query("endsWith", attribute, listOf(value)).toJson()
3437

35-
fun orderAsc(attribute: String) = "orderAsc(\"${attribute}\")"
38+
fun select(attributes: List<String>) = Query("select", null, attributes).toJson()
3639

37-
fun orderDesc(attribute: String) = "orderDesc(\"${attribute}\")"
40+
fun orderAsc(attribute: String) = Query("orderAsc", attribute).toJson()
3841

39-
fun cursorBefore(documentId: String) = "cursorBefore(\"${documentId}\")"
42+
fun orderDesc(attribute: String) = Query("orderDesc", attribute).toJson()
4043

41-
fun cursorAfter(documentId: String) = "cursorAfter(\"${documentId}\")"
44+
fun cursorBefore(documentId: String) = Query("cursorBefore", null, listOf(documentId)).toJson()
4245

43-
fun limit(limit: Int) = "limit(${limit})"
46+
fun cursorAfter(documentId: String) = Query("cursorAfter", null, listOf(documentId)).toJson()
4447

45-
fun offset(offset: Int) = "offset(${offset})"
48+
fun limit(limit: Int) = Query("limit", null, listOf(limit)).toJson()
4649

47-
private fun addQuery(attribute: String, method: String, value: Any): String {
48-
return when (value) {
49-
is List<*> -> "${method}(\"${attribute}\", [${value.map{it -> parseValues(it!!)}.joinToString(",")}])"
50-
else -> "${method}(\"${attribute}\", [${Query.parseValues(value)}])"
51-
}
52-
}
53-
private fun parseValues(value: Any): String {
54-
return when (value) {
55-
is String -> "\"${value}\""
56-
else -> "${value}"
57-
}
50+
fun offset(offset: Int) = Query("offset", null, listOf(offset)).toJson()
51+
52+
fun contains(attribute: String, value: Any) = Query("contains", attribute, parseValue(value)).toJson()
53+
54+
fun or(queries: List<String>) = Query("or", null, queries.map { it.fromJson<Query>() }).toJson()
55+
56+
fun and(queries: List<String>) = Query("and", null, queries.map { it.fromJson<Query>() }).toJson()
57+
58+
private fun parseValue(value: Any): List<Any> {
59+
return when (value) {
60+
is List<*> -> value as List<Any>
61+
else -> listOf(value)
62+
}
63+
}
5864
}
59-
}
60-
}
65+
}

templates/dart/lib/package.dart.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ library {{ language.params.packageName }};
77

88
import 'dart:async';
99
import 'dart:typed_data';
10+
import 'dart:convert';
1011

1112
import 'src/enums.dart';
1213
import 'src/service.dart';

templates/dart/lib/query.dart.twig

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,123 @@
11
part of {{ language.params.packageName }};
22

3+
34
/// Helper class to generate query strings.
45
class Query {
5-
Query._();
6+
final String method;
7+
final String? attribute;
8+
final dynamic values;
9+
10+
Query._(this.method, [this.attribute = null, this.values = null]);
11+
12+
Map<String, dynamic> toJson() {
13+
final map = <String, dynamic>{
14+
'method': method,
15+
};
16+
17+
if(attribute != null) {
18+
map['attribute'] = attribute;
19+
}
20+
21+
if(values != null) {
22+
map['values'] = values is List ? values : [values];
23+
}
24+
25+
return map;
26+
}
27+
28+
@override
29+
String toString() => jsonEncode(toJson());
630

731
/// Filter resources where [attribute] is equal to [value].
832
///
933
/// [value] can be a single value or a list. If a list is used
1034
/// the query will return resources where [attribute] is equal
1135
/// to any of the values in the list.
1236
static String equal(String attribute, dynamic value) =>
13-
_addQuery(attribute, 'equal', value);
37+
Query._('equal', attribute, value).toString();
1438

1539
/// Filter resources where [attribute] is not equal to [value].
16-
///
17-
/// [value] can be a single value or a list. If a list is used
18-
/// the query will return resources where [attribute] is equal
19-
/// to any of the values in the list.
2040
static String notEqual(String attribute, dynamic value) =>
21-
_addQuery(attribute, 'notEqual', value);
41+
Query._('notEqual', attribute, [value]).toString();
2242

2343
/// Filter resources where [attribute] is less than [value].
2444
static String lessThan(String attribute, dynamic value) =>
25-
_addQuery(attribute, 'lessThan', value);
45+
Query._('lessThan', attribute, value).toString();
2646

2747
/// Filter resources where [attribute] is less than or equal to [value].
2848
static String lessThanEqual(String attribute, dynamic value) =>
29-
_addQuery(attribute, 'lessThanEqual', value);
49+
Query._('lessThanEqual', attribute, value).toString();
3050

3151
/// Filter resources where [attribute] is greater than [value].
3252
static String greaterThan(String attribute, dynamic value) =>
33-
_addQuery(attribute, 'greaterThan', value);
53+
Query._('greaterThan', attribute, value).toString();
3454

3555
/// Filter resources where [attribute] is greater than or equal to [value].
3656
static String greaterThanEqual(String attribute, dynamic value) =>
37-
_addQuery(attribute, 'greaterThanEqual', value);
57+
Query._('greaterThanEqual', attribute, value).toString();
3858

3959
/// Filter resources where by searching [attribute] for [value].
4060
static String search(String attribute, String value) =>
41-
_addQuery(attribute, 'search', value);
61+
Query._('search', attribute, value).toString();
4262

4363
/// Filter resources where [attribute] is null.
44-
static String isNull(String attribute) => 'isNull("$attribute")';
64+
static String isNull(String attribute) => Query._('isNull', attribute).toString();
4565

4666
/// Filter resources where [attribute] is not null.
47-
static String isNotNull(String attribute) => 'isNotNull("$attribute")';
67+
static String isNotNull(String attribute) => Query._('isNotNull', attribute).toString();
4868

4969
/// Filter resources where [attribute] is between [start] and [end] (inclusive).
5070
static String between(String attribute, dynamic start, dynamic end) =>
51-
'between("$attribute", ${_parseValues(start)}, ${_parseValues(end)})';
71+
Query._('between', attribute, [start, end]).toString();
5272

5373
/// Filter resources where [attribute] starts with [value].
5474
static String startsWith(String attribute, String value) =>
55-
_addQuery(attribute, 'startsWith', value);
75+
Query._('startsWith', attribute, value).toString();
5676

5777
/// Filter resources where [attribute] ends with [value].
5878
static String endsWith(String attribute, String value) =>
59-
_addQuery(attribute, 'endsWith', value);
79+
Query._('endsWith', attribute, value).toString();
80+
81+
/// Filter resources where [attribute] contains [value]
82+
/// [value] can be a single value or a list.
83+
static String contains(String attribute, dynamic value) =>
84+
Query._('contains', attribute, value).toString();
85+
86+
static String or(List<String> queries) =>
87+
Query._('and', null, queries.map((query) => jsonDecode(query)).toList()).toString();
88+
89+
static String and(List<String> queries) =>
90+
Query._('and', null, queries.map((query) => jsonDecode(query)).toList()).toString();
6091

6192
/// Specify which attributes should be returned by the API call.
6293
static String select(List<String> attributes) =>
63-
'select([${attributes.map((attr) => "\"$attr\"").join(",")}])';
94+
Query._('select', null, attributes).toString();
6495

6596
/// Sort results by [attribute] ascending.
66-
static String orderAsc(String attribute) => 'orderAsc("$attribute")';
97+
static String orderAsc(String attribute) => Query._('orderAsc', attribute).toString();
6798

6899
/// Sort results by [attribute] descending.
69-
static String orderDesc(String attribute) => 'orderDesc("$attribute")';
100+
static String orderDesc(String attribute) => Query._('orderDesc', attribute).toString();
70101

71102
/// Return results before [id].
72103
///
73104
/// Refer to the [Cursor Based Pagination]({{sdk.url}}/docs/pagination#cursor-pagination)
74105
/// docs for more information.
75-
static String cursorBefore(String id) => 'cursorBefore("$id")';
106+
static String cursorBefore(String id) => Query._('cursorBefore', null, id).toString();
76107

77108
/// Return results after [id].
78109
///
79110
/// Refer to the [Cursor Based Pagination]({{sdk.url}}/docs/pagination#cursor-pagination)
80111
/// docs for more information.
81-
static String cursorAfter(String id) => 'cursorAfter("$id")';
112+
static String cursorAfter(String id) => Query._('cursorAfter', null, id).toString();
82113

83114
/// Return only [limit] results.
84-
static String limit(int limit) => 'limit($limit)';
115+
static String limit(int limit) => Query._('limit', null, limit).toString();
85116

86117
/// Return results from [offset].
87118
///
88119
/// Refer to the [Offset Pagination]({{sdk.url}}/docs/pagination#offset-pagination)
89120
/// docs for more information.
90-
static String offset(int offset) => 'offset($offset)';
91-
92-
static String _addQuery(String attribute, String method, dynamic value) => (value
93-
is List)
94-
? '$method("$attribute", [${value.map((item) => _parseValues(item)).join(",")}])'
95-
: '$method("$attribute", [${_parseValues(value)}])';
121+
static String offset(int offset) => Query._('offset', null, offset).toString();
96122

97-
static String _parseValues(dynamic value) =>
98-
(value is String) ? '"$value"' : '$value';
99123
}

0 commit comments

Comments
 (0)