Skip to content

Commit b92667f

Browse files
authored
feat: add explain MongoDB queries (#314)
* feat: add explain MongoDB queries * nits
1 parent e6c2db2 commit b92667f

File tree

9 files changed

+596
-86
lines changed

9 files changed

+596
-86
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
### main
44

5-
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/3.0.0...main)
5+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/3.1.0...main)
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

8+
### 3.1.0
9+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/3.0.0...3.1.0)
10+
11+
__New features__
12+
- Add the ability to explain MongoDB queries by setting isUsingMongoDB = true for the respective explain query ([#314](https://github.com/parse-community/Parse-Swift/pull/314)), thanks to [Corey Baker](https://github.com/cbaker6).
13+
814
### 3.0.0
915
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.5.1...3.0.0)
1016

Sources/ParseSwift/API/Responses.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ internal struct AnyResultsResponse<U: Decodable>: Decodable {
159159
let results: [U]
160160
}
161161

162+
internal struct AnyResultsMongoResponse<U: Decodable>: Decodable {
163+
let results: U
164+
}
165+
162166
// MARK: ConfigResponse
163167
internal struct ConfigFetchResponse<T>: Codable where T: ParseConfig {
164168
let params: T

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
enum ParseConstants {
1212
static let sdk = "swift"
13-
static let version = "3.0.0"
13+
static let version = "3.1.0"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

Sources/ParseSwift/Types/Query+async.swift

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@ public extension Query {
2828

2929
/**
3030
Query plan information for finding objects *asynchronously*.
31+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
3132
- parameter options: A set of header options sent to the server. Defaults to an empty set.
3233
- note: An explain query will have many different underlying types. Since Swift is a strongly
3334
typed language, a developer should specify the type expected to be decoded which will be
3435
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
3536
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
3637
- returns: An array of ParseObjects.
3738
- throws: An error of type `ParseError`.
39+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
40+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
41+
[here](https://github.com/parse-community/parse-server/pull/7440).
3842
*/
39-
func findExplain<U: Decodable>(options: API.Options = []) async throws -> [U] {
43+
func findExplain<U: Decodable>(isUsingMongoDB: Bool = false,
44+
options: API.Options = []) async throws -> [U] {
4045
try await withCheckedThrowingContinuation { continuation in
41-
self.findExplain(options: options,
46+
self.findExplain(isUsingMongoDB: isUsingMongoDB,
47+
options: options,
4248
completion: continuation.resume)
4349
}
4450
}
@@ -80,13 +86,19 @@ public extension Query {
8086
typed language, a developer should specify the type expected to be decoded which will be
8187
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
8288
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
89+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
8390
- parameter options: A set of header options sent to the server. Defaults to an empty set.
8491
- returns: An array of ParseObjects.
8592
- throws: An error of type `ParseError`.
93+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
94+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
95+
[here](https://github.com/parse-community/parse-server/pull/7440).
8696
*/
87-
func firstExplain<U: Decodable>(options: API.Options = []) async throws -> U {
97+
func firstExplain<U: Decodable>(isUsingMongoDB: Bool = false,
98+
options: API.Options = []) async throws -> U {
8899
try await withCheckedThrowingContinuation { continuation in
89-
self.firstExplain(options: options,
100+
self.firstExplain(isUsingMongoDB: isUsingMongoDB,
101+
options: options,
90102
completion: continuation.resume)
91103
}
92104
}
@@ -110,14 +122,19 @@ public extension Query {
110122
typed language, a developer should specify the type expected to be decoded which will be
111123
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
112124
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
113-
- parameter explain: Used to toggle the information on the query plan.
125+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
114126
- parameter options: A set of header options sent to the server. Defaults to an empty set.
115127
- returns: An array of ParseObjects.
116128
- throws: An error of type `ParseError`.
129+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
130+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
131+
[here](https://github.com/parse-community/parse-server/pull/7440).
117132
*/
118-
func countExplain<U: Decodable>(options: API.Options = []) async throws -> [U] {
133+
func countExplain<U: Decodable>(isUsingMongoDB: Bool = false,
134+
options: API.Options = []) async throws -> [U] {
119135
try await withCheckedThrowingContinuation { continuation in
120-
self.countExplain(options: options,
136+
self.countExplain(isUsingMongoDB: isUsingMongoDB,
137+
options: options,
121138
completion: continuation.resume)
122139
}
123140
}
@@ -142,14 +159,19 @@ public extension Query {
142159
typed language, a developer should specify the type expected to be decoded which will be
143160
different for mongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
144161
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
145-
- parameter explain: Used to toggle the information on the query plan.
162+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
146163
- parameter options: A set of header options sent to the server. Defaults to an empty set.
147164
- returns: An array of ParseObjects.
148165
- throws: An error of type `ParseError`.
166+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
167+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
168+
[here](https://github.com/parse-community/parse-server/pull/7440).
149169
*/
150-
func withCountExplain<U: Decodable>(options: API.Options = []) async throws -> [U] {
170+
func withCountExplain<U: Decodable>(isUsingMongoDB: Bool = false,
171+
options: API.Options = []) async throws -> [U] {
151172
try await withCheckedThrowingContinuation { continuation in
152-
self.withCountExplain(options: options,
173+
self.withCountExplain(isUsingMongoDB: isUsingMongoDB,
174+
options: options,
153175
completion: continuation.resume)
154176
}
155177
}
@@ -179,16 +201,22 @@ public extension Query {
179201
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
180202
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
181203
- parameter pipeline: A pipeline of stages to process query.
204+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
182205
- parameter options: A set of header options sent to the server. Defaults to an empty set.
183206
- returns: An array of ParseObjects.
184207
- throws: An error of type `ParseError`.
208+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
209+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
210+
[here](https://github.com/parse-community/parse-server/pull/7440).
185211
*/
186212
func aggregateExplain<U: Decodable>(_ pipeline: [[String: Encodable]],
213+
isUsingMongoDB: Bool = false,
187214
options: API.Options = []) async throws -> [U] {
188215
try await withCheckedThrowingContinuation { continuation in
189216
self.aggregateExplain(pipeline,
190-
options: options,
191-
completion: continuation.resume)
217+
isUsingMongoDB: isUsingMongoDB,
218+
options: options,
219+
completion: continuation.resume)
192220
}
193221
}
194222

@@ -217,14 +245,20 @@ public extension Query {
217245
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
218246
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
219247
- parameter key: A field to find distinct values.
248+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
220249
- parameter options: A set of header options sent to the server. Defaults to an empty set.
221250
- returns: An array of ParseObjects.
222251
- throws: An error of type `ParseError`.
252+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
253+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
254+
[here](https://github.com/parse-community/parse-server/pull/7440).
223255
*/
224256
func distinctExplain<U: Decodable>(_ key: String,
257+
isUsingMongoDB: Bool = false,
225258
options: API.Options = []) async throws -> [U] {
226259
try await withCheckedThrowingContinuation { continuation in
227260
self.distinctExplain(key,
261+
isUsingMongoDB: isUsingMongoDB,
228262
options: options,
229263
completion: continuation.resume)
230264
}

Sources/ParseSwift/Types/Query+combine.swift

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@ public extension Query {
2828

2929
/**
3030
Query plan information for finding objects *asynchronously* and publishes when complete.
31+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
3132
- parameter options: A set of header options sent to the server. Defaults to an empty set.
3233
- note: An explain query will have many different underlying types. Since Swift is a strongly
3334
typed language, a developer should specify the type expected to be decoded which will be
3435
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
3536
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
3637
- returns: A publisher that eventually produces a single value and then finishes or fails.
38+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
39+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
40+
[here](https://github.com/parse-community/parse-server/pull/7440).
3741
*/
38-
func findExplainPublisher<U: Decodable>(options: API.Options = []) -> Future<[U], ParseError> {
42+
func findExplainPublisher<U: Decodable>(isUsingMongoDB: Bool = false,
43+
options: API.Options = []) -> Future<[U], ParseError> {
3944
Future { promise in
40-
self.findExplain(options: options,
45+
self.findExplain(isUsingMongoDB: isUsingMongoDB,
46+
options: options,
4147
completion: promise)
4248
}
4349
}
@@ -78,12 +84,18 @@ public extension Query {
7884
typed language, a developer should specify the type expected to be decoded which will be
7985
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
8086
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
87+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
8188
- parameter options: A set of header options sent to the server. Defaults to an empty set.
8289
- returns: A publisher that eventually produces a single value and then finishes or fails.
90+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
91+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
92+
[here](https://github.com/parse-community/parse-server/pull/7440).
8393
*/
84-
func firstExplainPublisher<U: Decodable>(options: API.Options = []) -> Future<U, ParseError> {
94+
func firstExplainPublisher<U: Decodable>(isUsingMongoDB: Bool = false,
95+
options: API.Options = []) -> Future<U, ParseError> {
8596
Future { promise in
86-
self.firstExplain(options: options,
97+
self.firstExplain(isUsingMongoDB: isUsingMongoDB,
98+
options: options,
8799
completion: promise)
88100
}
89101
}
@@ -106,13 +118,18 @@ public extension Query {
106118
typed language, a developer should specify the type expected to be decoded which will be
107119
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
108120
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
109-
- parameter explain: Used to toggle the information on the query plan.
121+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
110122
- parameter options: A set of header options sent to the server. Defaults to an empty set.
111123
- returns: A publisher that eventually produces a single value and then finishes or fails.
124+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
125+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
126+
[here](https://github.com/parse-community/parse-server/pull/7440).
112127
*/
113-
func countExplainPublisher<U: Decodable>(options: API.Options = []) -> Future<[U], ParseError> {
128+
func countExplainPublisher<U: Decodable>(isUsingMongoDB: Bool = false,
129+
options: API.Options = []) -> Future<[U], ParseError> {
114130
Future { promise in
115-
self.countExplain(options: options,
131+
self.countExplain(isUsingMongoDB: isUsingMongoDB,
132+
options: options,
116133
completion: promise)
117134
}
118135
}
@@ -137,13 +154,18 @@ public extension Query {
137154
typed language, a developer should specify the type expected to be decoded which will be
138155
different for mongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
139156
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
140-
- parameter explain: Used to toggle the information on the query plan.
157+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
141158
- parameter options: A set of header options sent to the server. Defaults to an empty set.
142159
- returns: A publisher that eventually produces a single value and then finishes or fails.
160+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
161+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
162+
[here](https://github.com/parse-community/parse-server/pull/7440).
143163
*/
144-
func withCountExplainPublisher<U: Decodable>(options: API.Options = []) -> Future<[U], ParseError> {
164+
func withCountExplainPublisher<U: Decodable>(isUsingMongoDB: Bool = false,
165+
options: API.Options = []) -> Future<[U], ParseError> {
145166
Future { promise in
146-
self.withCountExplain(options: options,
167+
self.withCountExplain(isUsingMongoDB: isUsingMongoDB,
168+
options: options,
147169
completion: promise)
148170
}
149171
}
@@ -172,15 +194,21 @@ public extension Query {
172194
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
173195
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
174196
- parameter pipeline: A pipeline of stages to process query.
197+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
175198
- parameter options: A set of header options sent to the server. Defaults to an empty set.
176199
- returns: A publisher that eventually produces a single value and then finishes or fails.
200+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
201+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
202+
[here](https://github.com/parse-community/parse-server/pull/7440).
177203
*/
178204
func aggregateExplainPublisher<U: Decodable>(_ pipeline: [[String: Encodable]],
205+
isUsingMongoDB: Bool = false,
179206
options: API.Options = []) -> Future<[U], ParseError> {
180207
Future { promise in
181208
self.aggregateExplain(pipeline,
182-
options: options,
183-
completion: promise)
209+
isUsingMongoDB: isUsingMongoDB,
210+
options: options,
211+
completion: promise)
184212
}
185213
}
186214

@@ -208,13 +236,19 @@ public extension Query {
208236
different for MongoDB and PostgreSQL. One way around this is to use a type-erased wrapper
209237
such as the [AnyCodable](https://github.com/Flight-School/AnyCodable) package.
210238
- parameter key: A field to find distinct values.
239+
- parameter isUsingMongoDB: Set to **true** if your Parse Server uses MongoDB. Defaults to **false**.
211240
- parameter options: A set of header options sent to the server. Defaults to an empty set.
212241
- returns: A publisher that eventually produces a single value and then finishes or fails.
242+
- warning: MongoDB's **explain** does not conform to the traditional Parse Server response, so the
243+
`isUsingMongoDB` flag needs to be set for MongoDB users. See more
244+
[here](https://github.com/parse-community/parse-server/pull/7440).
213245
*/
214246
func distinctExplainPublisher<U: Decodable>(_ key: String,
247+
isUsingMongoDB: Bool = false,
215248
options: API.Options = []) -> Future<[U], ParseError> {
216249
Future { promise in
217250
self.distinctExplain(key,
251+
isUsingMongoDB: isUsingMongoDB,
218252
options: options,
219253
completion: promise)
220254
}

0 commit comments

Comments
 (0)