-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMultiObjectStore.swift
104 lines (88 loc) · 4.21 KB
/
MultiObjectStore.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/// An API for a key-value store that offers a convenient and type-safe way to store and retrieve a collection of
/// `Codable` and `Identifiable` objects.
public protocol MultiObjectStore {
// Storable object.
associatedtype Object: Codable & Identifiable
/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any error that might occur during the save operation.
func save(_ object: Object) throws
/// Saves an optional object to store —if not nil—.
/// **This method has a default implementation.**
/// - Parameter object: optional object to be saved.
/// - Throws error: any error that might occur during the save operation.
func save(_ object: Object?) throws
/// Saves an array of objects to store.
/// **This method has a default implementation.**
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any error that might occur during the save operation.
func save(_ objects: [Object]) throws
/// The number of all objects stored in store.
var objectsCount: Int { get }
/// Whether the store contains a saved object with the given id.
/// - Parameter id: object id.
/// - Returns: true if store contains an object with the given id.
func containsObject(withId id: Object.ID) -> Bool
/// Returns an object for the given id, or `nil` if no object is found.
/// - Parameter id: object id.
/// - Returns: object with the given id, or `nil` if no object with the given id is found.
func object(withId id: Object.ID) -> Object?
/// Returns objects for given ids, and ignores any id that does not represent an object in the store.
/// **This method has a default implementation.**
/// - Parameter ids: object ids.
/// - Returns: array of objects with the given ids.
func objects(withIds ids: [Object.ID]) -> [Object]
/// Returns all objects in the store.
/// - Returns: collection containing all objects stored in the store without a given order.
func allObjects() -> [Object]
/// Removes object with the given id —if found—.
/// - Parameter id: id for the object to be removed.
/// - Throws error: any error that might occur during the removal operation.
func remove(withId id: Object.ID) throws
/// Removes objects with given ids —if found—, and ignore any ids that does not represent objects stored
/// in the store.
/// **This method has a default implementation.**
/// - Parameter ids: ids for the objects to be deleted.
/// - Throws error: any error that might occur during the removal operation.
func remove(withIds ids: [Object.ID]) throws
/// Removes all objects in store.
/// - Throws error: any errors that might occur during the removal operation.
func removeAll() throws
}
public extension MultiObjectStore {
/// Saves an optional object to store —if not nil—.
/// - Parameter object: optional object to be saved.
/// - Throws error: any error that might occur during the save operation.
func save(_ object: Object?) throws {
if let object = object {
try save(object)
}
}
/// Saves an array of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any error that might occur during the save operation.
func save(_ objects: [Object]) throws {
try objects.forEach(save)
}
/// Returns objects for given ids, and ignores any ids that does not represent an object in the store.
/// - Parameter ids: object ids.
/// - Returns: array of objects with the given ids.
func objects(withIds ids: [Object.ID]) -> [Object] {
return ids.compactMap(object(withId:))
}
/// Removes objects with given ids —if found—, and ignore any ids that does not represent objects stored
/// in the store.
/// - Parameter ids: ids for the objects to be deleted.
/// - Throws error: any error that might occur during the removal operation.
func remove(withIds ids: [Object.ID]) throws {
try ids.forEach(remove(withId:))
}
}
public extension MultiObjectStore where Object: Hashable {
/// Saves a set of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any error that might occur during the save operation.
func save(_ objects: Set<Object>) throws {
try save(Array(objects))
}
}