Skip to content

Commit 185b183

Browse files
committed
Implemented ModelData.init(bson:entity:)
1 parent 42b7b1e commit 185b183

4 files changed

Lines changed: 80 additions & 10 deletions

File tree

Sources/MongoDBModel/ModelData.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,26 @@ import MongoSwift
1111

1212
public extension ModelData {
1313

14-
init(bson: BSONDocument) {
15-
fatalError()
14+
init(bson: BSONDocument, model: EntityDescription) throws {
15+
let id = try bson.modelObjectID
16+
self.init(entity: model.id, id: id)
17+
// decode attributes
18+
for attribute in model.attributes {
19+
let value = bson[attribute.id.rawValue]
20+
.map { AttributeValue(bson: $0) } ?? .null
21+
self.attributes[attribute.id] = value
22+
}
23+
// decode relationships
24+
for relationship in model.relationships {
25+
let relationshipBSON = bson[relationship.id.rawValue] ?? .null
26+
guard let value = RelationshipValue(
27+
bson: relationshipBSON,
28+
type: relationship.type
29+
) else {
30+
throw DecodingError.typeMismatch(ModelData.self, DecodingError.Context(codingPath: [], debugDescription: ""))
31+
}
32+
self.relationships[relationship.id] = value
33+
}
1634
}
1735
}
1836

Sources/MongoDBModel/MongoDatabase.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@ import Foundation
22
@_exported import CoreModel
33
@_exported import MongoSwift
44

5-
extension MongoDatabase: ModelStorage {
5+
extension MongoDatabase {
66

77
/// Fetch managed object.
88
public func fetch(
9-
_ entity: EntityName,
9+
_ entity: EntityDescription,
1010
for id: ObjectID
1111
) async throws -> ModelData? {
12-
guard let document = try await find(entity, for: id) else {
12+
guard let document = try await find(entity.id, for: id) else {
1313
return nil
1414
}
15-
return ModelData(bson: document)
15+
return try ModelData(bson: document, model: entity)
1616
}
1717

1818
/// Fetch managed objects.
19-
public func fetch(_ fetchRequest: FetchRequest) async throws -> [ModelData] {
19+
public func fetch(
20+
_ fetchRequest: FetchRequest,
21+
entity: EntityDescription
22+
) async throws -> [ModelData] {
2023
try await fetchDocuments(fetchRequest)
21-
.map { ModelData(bson: $0) }
24+
.map { try ModelData(bson: $0, model: entity) }
2225
}
2326

2427
/// Fetch and return result count.

Sources/MongoDBModel/ObjectID.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,29 @@ extension BSONObjectID: ObjectIDConvertible {
1515
try? self.init(objectID.rawValue)
1616
}
1717
}
18+
19+
internal extension BSONDocument {
20+
21+
var modelObjectID: ObjectID {
22+
get throws {
23+
guard let id = self[BSONDocument.BuiltInProperty.id.rawValue]?.stringValue else {
24+
throw CocoaError(.coderValueNotFound)
25+
}
26+
return ObjectID(rawValue: id)
27+
}
28+
}
29+
}
30+
31+
public extension ObjectID {
32+
33+
init?(bson: BSON) {
34+
switch bson {
35+
case let .string(string):
36+
self.init(rawValue: string)
37+
case let .objectID(objectID):
38+
self.init(rawValue: objectID.description)
39+
default:
40+
return nil
41+
}
42+
}
43+
}

Sources/MongoDBModel/RelationshipValue.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,31 @@ import MongoSwift
1111

1212
public extension RelationshipValue {
1313

14-
init?(bson: BSON) {
15-
fatalError()
14+
init?(bson: BSON, type: RelationshipType) {
15+
guard bson != .null else {
16+
self = .null
17+
return
18+
}
19+
switch type {
20+
case .toOne:
21+
guard let objectID = ObjectID(bson: bson) else {
22+
return nil
23+
}
24+
self = .toOne(objectID)
25+
case .toMany:
26+
guard case let .array(array) = bson else {
27+
return nil
28+
}
29+
var objectIDs = [ObjectID]()
30+
objectIDs.reserveCapacity(array.count)
31+
for item in array {
32+
guard let objectID = ObjectID(bson: item) else {
33+
return nil
34+
}
35+
objectIDs.append(objectID)
36+
}
37+
self = .toMany(objectIDs)
38+
}
1639
}
1740
}
1841

0 commit comments

Comments
 (0)