Skip to content

Commit 9947b62

Browse files
committed
Add ModelStorage Codable extensions
1 parent 35e1a69 commit 9947b62

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

Sources/CoreModel/Decodable.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import Foundation
99

10+
// MARK: - ModelData Decoding
11+
1012
public extension ModelData {
1113

1214
func decode<T, K>(_ type: T.Type, forKey key: K) throws -> T where T: AttributeDecodable, K: CodingKey {
@@ -67,11 +69,15 @@ public extension ModelData {
6769
}
6870
}
6971

72+
// MARK: - Default Codable Implementation
73+
7074
extension Entity where Self: Decodable, Self.ID: Decodable {
7175

7276
// TODO: Default implementation for Decodable
7377
}
7478

79+
// MARK: - AttributeDecodable
80+
7581
public protocol AttributeDecodable {
7682

7783
init?(attributeValue: AttributeValue)

Sources/CoreModel/Encodable.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import Foundation
99

10+
// MARK: - ModelData Encoding
11+
1012
public extension ModelData {
1113

1214
mutating func encode<T, K>(_ value: T, forKey key: K) where T: AttributeEncodable, K: CodingKey {
@@ -30,19 +32,14 @@ public extension ModelData {
3032
}
3133
}
3234

35+
// MARK: - Default Codable Implementation
36+
3337
extension Entity where Self: Encodable, Self.ID: Encodable {
3438

3539
// TODO: Default implementation for Encodable
3640
}
3741

38-
public extension ModelStorage {
39-
40-
/// Create or edit a managed object.
41-
func insert<T>(_ value: T) async throws where T: Entity, T: Encodable {
42-
let model = value.encode()
43-
try await insert(model)
44-
}
45-
}
42+
// MARK: - AttributeEncodable
4643

4744
public protocol AttributeEncodable {
4845

Sources/CoreModel/Store.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// Copyright © 2015 PureSwift. All rights reserved.
77
//
88

9+
import Predicate
10+
911
/// CoreModel Store Protocol
1012
public protocol ModelStorage: AnyObject {
1113

@@ -32,6 +34,8 @@ public extension ModelStorage {
3234
}
3335
}
3436

37+
// MARK: - ModelData
38+
3539
/// CoreModel Object Instance
3640
public struct ModelData: Equatable, Hashable, Identifiable, Codable {
3741

@@ -55,3 +59,63 @@ public struct ModelData: Equatable, Hashable, Identifiable, Codable {
5559
self.relationships = relationships
5660
}
5761
}
62+
63+
// MARK: - ModelStorage Codable Extensions
64+
65+
public extension ModelStorage {
66+
67+
/// Fetch managed object.
68+
func fetch<T>(_ entity: T.Type, for id: ObjectID) async throws -> T? where T: Entity {
69+
guard let model = try await fetch(T.entityName, for: id) else {
70+
return nil
71+
}
72+
return try T.init(from: model)
73+
}
74+
75+
/// Fetch managed objects.
76+
func fetch<T>(
77+
_ entity: T.Type,
78+
sortDescriptors: [FetchRequest.SortDescriptor] = [],
79+
predicate: Predicate? = nil,
80+
fetchLimit: Int = 0,
81+
fetchOffset: Int = 0
82+
) async throws -> [ModelData] where T: Entity {
83+
let fetchRequest = FetchRequest(
84+
entity: T.entityName,
85+
sortDescriptors: sortDescriptors,
86+
predicate: predicate,
87+
fetchLimit: fetchLimit,
88+
fetchOffset: fetchOffset
89+
)
90+
return try await fetch(fetchRequest)
91+
}
92+
93+
/// Fetch and return result count.
94+
func count<T>(
95+
_ entity: T.Type,
96+
sortDescriptors: [FetchRequest.SortDescriptor] = [],
97+
predicate: Predicate? = nil,
98+
fetchLimit: Int = 0,
99+
fetchOffset: Int = 0
100+
) async throws -> UInt where T: Entity {
101+
let fetchRequest = FetchRequest(
102+
entity: T.entityName,
103+
sortDescriptors: sortDescriptors,
104+
predicate: predicate,
105+
fetchLimit: fetchLimit,
106+
fetchOffset: fetchOffset
107+
)
108+
return try await count(fetchRequest)
109+
}
110+
111+
/// Create or edit a managed object.
112+
func insert<T>(_ value: T) async throws where T: Entity, T: Encodable {
113+
let model = value.encode()
114+
try await insert(model)
115+
}
116+
117+
/// Delete the specified managed object.
118+
func delete<T>(_ entity: T.Type, for id: ObjectID) async throws where T: Entity {
119+
try await delete(T.entityName, for: id)
120+
}
121+
}

0 commit comments

Comments
 (0)