From 2f9c8330d9522cd55ed110641163efe3ab81b906 Mon Sep 17 00:00:00 2001 From: Thomas Rademaker Date: Tue, 6 Sep 2022 23:55:13 -0400 Subject: [PATCH 1/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c6071f9..0b0cff6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This can all be easily hosted on [Swift Cloud](https://swift.cloud) Install MongoManager ```swift -.package(url: "https://github.com/SparrowTek/MongoManager", from: "1.0.3") +.package(url: "https://github.com/SparrowTek/MongoManager", from: "1.0.4") ``` Add it as a target dependency From 9e7121a59e6acacc3d90ec8b7288bef3c6438b1e Mon Sep 17 00:00:00 2001 From: Thomas Rademaker Date: Sun, 11 Sep 2022 17:00:53 -0400 Subject: [PATCH 2/2] add support for unwrapping mongo document array --- README.md | 2 +- Sources/MongoManager/MongoManager.swift | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b0cff6..5393850 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This can all be easily hosted on [Swift Cloud](https://swift.cloud) Install MongoManager ```swift -.package(url: "https://github.com/SparrowTek/MongoManager", from: "1.0.4") +.package(url: "https://github.com/SparrowTek/MongoManager", from: "1.0.5") ``` Add it as a target dependency diff --git a/Sources/MongoManager/MongoManager.swift b/Sources/MongoManager/MongoManager.swift index e859326..1d96468 100644 --- a/Sources/MongoManager/MongoManager.swift +++ b/Sources/MongoManager/MongoManager.swift @@ -42,6 +42,18 @@ public struct MongoManager { unwrapDocument(try await res.body.decode(MongoDocument.self)) } + /// unwrap your `Codable` array from the MongoDB Document object + public static func unwrapDocuments(_ documents: MongoDocuments) -> C { + documents.documents + } + + /// decode a `MongoDocuments` object from the given FetchResponse + /// - Parameters res: A `FetchResponse` object to decode + /// - Returns an array of objects conforming to `Codable` + public static func decodeDocuments(from res: FetchResponse) async throws -> C { + unwrapDocuments(try await res.body.decode(MongoDocuments.self)) + } + private static func headers(for data: MongoData) -> [String : String] { [ "Content-Type" : data.contentType, @@ -322,6 +334,10 @@ public struct MongoManager { public let document: C } + public struct MongoDocuments: Codable { + public let documents: C + } + struct InsertOneRequest: Codable { let collection: String let database: String