Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into swift_5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
radmakr committed Sep 13, 2022
2 parents aeb4062 + 9e7121a commit 0565f3c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.5")
```

Add it as a target dependency
Expand Down
16 changes: 16 additions & 0 deletions Sources/MongoManager/MongoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public struct MongoManager {
try await res.body.decode(MongoDocument<C>.self).document
}

/// unwrap your `Codable` array from the MongoDB Document object
public static func unwrapDocuments<C: Codable>(_ documents: MongoDocuments<C>) -> 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<C: Codable>(from res: FetchResponse) async throws -> C {
unwrapDocuments(try await res.body.decode(MongoDocuments<C>.self))
}

private static func headers(for data: MongoData) -> [String : String] {
[
"Content-Type" : data.contentType,
Expand Down Expand Up @@ -322,6 +334,10 @@ public struct MongoManager {
public let document: C
}

public struct MongoDocuments<C: Codable>: Codable {
public let documents: C
}

struct InsertOneRequest<C: Codable>: Codable {
let collection: String
let database: String
Expand Down

0 comments on commit 0565f3c

Please sign in to comment.