@@ -11,42 +11,108 @@ public class FirewrapCollection {
1111 self . path = path
1212 }
1313
14+ public func document( ) -> FirewrapDocument {
15+ let db = Firestore . firestore ( )
16+ let newDocument = db. collection ( path) . document ( )
17+ return FirewrapDocument ( newDocument. path)
18+ }
19+
20+ public func document( _ path: String ) -> FirewrapDocument {
21+ return FirewrapDocument ( self . path + " / " + path)
22+ }
23+
1424 // MARK: - Getter
1525
16- func getDocuments( _ source: FirewrapSource = . default, completion: @escaping ( ( [ [ String : Any ] ] ? ) -> Void ) ) {
26+ public func getDocuments( _ source: FirewrapSource = . default, completion: @escaping ( [ [ String : Any ] ] ? ) -> Void ) {
1727 let db = Firestore . firestore ( )
1828 db. collection ( path) . getDocuments ( source: source. firebaseValue) { snapshot, error in
29+
1930 guard error == nil , let documents = snapshot? . documents else {
2031 completion ( nil )
2132 return
2233 }
23- completion ( documents. map ( { $0. data ( ) } ) )
34+
35+ let response = documents. map { $0. data ( ) }
36+ completion ( response)
37+ }
38+ }
39+
40+ public func getDocuments< T: Decodable > ( as type: T . Type , source: FirewrapSource = . default, completion: @escaping ( [ T ] ? , Error ? ) -> Void ) {
41+ let db = Firestore . firestore ( )
42+
43+ db. collection ( path) . getDocuments ( source: source. firebaseValue) { ( snapshot, error) in
44+
45+ guard error == nil , let snapshot else {
46+ completion ( nil , error)
47+ return
48+ }
49+
50+ let models = Self . decode ( as: T . self, from: snapshot)
51+ completion ( models, nil )
2452 }
2553 }
2654
27- func getDocument( _ source: FirewrapSource = . default, where field: String , equal: [ Any ] , completion: @escaping ( ( [ [ String : Any ] ] ? ) -> Void ) ) {
55+ public func getDocument( _ source: FirewrapSource = . default, where field: String , equal: [ Any ] , completion: @escaping ( ( [ [ String : Any ] ] ? ) -> Void ) ) {
2856 let db = Firestore . firestore ( )
2957 db. collection ( path) . whereField ( field, in: equal) . getDocuments ( source: source. firebaseValue) { snapshot, error in
58+
3059 guard error == nil , let documents = snapshot? . documents else {
3160 completion ( nil )
3261 return
3362 }
63+
3464 completion ( documents. map ( { $0. data ( ) } ) )
3565 }
3666 }
3767
3868 // MARK: - Observer
3969
40- func observe( _ handler: @escaping ( [ [ String : Any ] ] ? ) -> Void ) {
70+ public func observe< T : Decodable > ( as type : T . Type , handler: @escaping ( [ T ] ? ) -> Void ) {
4171 self . listener? . remove ( )
4272 let db = Firestore . firestore ( )
4373 self . listener = db. collection ( path) . addSnapshotListener { snapshot, error in
74+
75+ guard error == nil , let snapshot else {
76+ handler ( nil )
77+ return
78+ }
79+
80+ handler ( Self . decode ( as: T . self, from: snapshot) )
81+ }
82+ }
83+
84+ public func observe( _ handler: @escaping ( [ [ String : Any ] ] ? ) -> Void ) {
85+ self . listener? . remove ( )
86+ let db = Firestore . firestore ( )
87+ self . listener = db. collection ( path) . addSnapshotListener { snapshot, error in
88+
4489 guard error == nil , let documents = snapshot? . documents else {
4590 handler ( nil )
4691 return
4792 }
48- handler ( documents. map ( { $0. data ( ) } ) )
93+
94+ let response = documents. map { $0. data ( ) }
95+ handler ( response)
96+ }
97+ }
98+
99+ public func removeObserver( ) {
100+ self . listener? . remove ( )
101+ }
102+
103+ // MARK: - Private
104+
105+ private static func decode< T: Decodable > ( as type: T . Type , from snapshot: QuerySnapshot ) -> [ T ] ? {
106+ var models : [ T ] = [ ]
107+ for document in snapshot. documents {
108+ do {
109+ let model = try document. data ( as: T . self)
110+ models. append ( model)
111+ } catch {
112+ break
113+ }
49114 }
115+ return models
50116 }
51117}
52118#endif
0 commit comments