@@ -101,12 +101,15 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
101
101
*
102
102
* @returns The entity.
103
103
*/
104
- public async get ( id : string ) : Promise < T | null > {
105
- const snapshot = await this . _native . doc ( id ) . get ( ) ;
106
- if ( snapshot . exists ) {
107
- return FirestoreSerializer . deserialize ( snapshot , this . _Entity , this ) ;
108
- }
109
- return null ;
104
+ public get ( id : string ) : Promise < T | null > {
105
+ return new Promise ( ( resolve ) : void => {
106
+ this . _native . doc ( id ) . get ( ) . then ( ( snapshot ) : void => {
107
+ if ( snapshot . exists ) {
108
+ resolve ( FirestoreSerializer . deserialize ( snapshot , this . _Entity , this ) ) ;
109
+ }
110
+ return resolve ( null ) ;
111
+ } ) ;
112
+ } ) ;
110
113
}
111
114
112
115
/**
@@ -115,13 +118,18 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
115
118
*
116
119
* @returns The updated entity.
117
120
*/
118
- public async update ( entity : T ) : Promise < T | null > {
119
- if ( ! entity . id ) {
120
- throw new Error ( `An ID must be provided when updating ${ entity . constructor . name } ` ) ;
121
- }
122
- const { id, ...data } = FirestoreSerializer . serialize ( entity , WriteTypes . Update ) ;
123
- await this . _native . doc ( id ) . update ( data ) ;
124
- return this . get ( entity . id ) ;
121
+ public update ( entity : T ) : Promise < T | null > {
122
+ return new Promise ( ( resolve ) : void => {
123
+ if ( ! entity . id ) {
124
+ throw new Error ( `An ID must be provided when updating ${ entity . constructor . name } ` ) ;
125
+ }
126
+ const { id, ...data } = FirestoreSerializer . serialize ( entity , WriteTypes . Update ) ;
127
+ this . _native . doc ( id ) . update ( data ) . then ( ( ) : void => {
128
+ this . get ( entity . id ) . then ( ( updatedEntity ) : void => {
129
+ resolve ( updatedEntity ) ;
130
+ } ) ;
131
+ } ) ;
132
+ } ) ;
125
133
} ;
126
134
127
135
/**
@@ -130,15 +138,23 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
130
138
*
131
139
* @returns The created entity.
132
140
*/
133
- public async create ( entity : T ) : Promise < T | null > {
134
- const { id, ...data } = FirestoreSerializer . serialize ( entity , WriteTypes . Create ) ;
135
- if ( id ) {
136
- await this . _native . doc ( id ) . set ( data ) ;
137
- return this . get ( id ) ;
138
- } else {
139
- const result = await this . _native . add ( data ) ;
140
- return this . get ( result . id ) ;
141
- }
141
+ public create ( entity : T ) : Promise < T | null > {
142
+ return new Promise ( ( resolve ) : void => {
143
+ const { id, ...data } = FirestoreSerializer . serialize ( entity , WriteTypes . Create ) ;
144
+ if ( id ) {
145
+ this . _native . doc ( id ) . set ( data ) . then ( ( ) : void => {
146
+ this . get ( id ) . then ( ( snapshot ) : void => {
147
+ resolve ( snapshot ) ;
148
+ } ) ;
149
+ } ) ;
150
+ } else {
151
+ this . _native . add ( data ) . then ( ( result ) : void => {
152
+ this . get ( result . id ) . then ( ( snapshot ) : void => {
153
+ resolve ( snapshot ) ;
154
+ } ) ;
155
+ } ) ;
156
+ }
157
+ } ) ;
142
158
} ;
143
159
144
160
/**
@@ -147,25 +163,33 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
147
163
*
148
164
* @returns A list of entities matching the criteria.
149
165
*/
150
- public async find ( query ? : ICollectionQuery < T > ) : Promise < T [ ] > {
151
- let querySnapshot : firestore . QuerySnapshot ;
152
- if ( query ) {
153
- const fields = getRepository ( this . _Entity . prototype . constructor . name ) . fields ;
154
- querySnapshot = await QueryBuilder . query ( this , fields , query ) . get ( ) ;
155
- } else {
156
- querySnapshot = await this . _native . get ( ) ;
157
- }
158
- return querySnapshot . docs . map ( ( snapshot ) : T => {
159
- return FirestoreSerializer . deserialize ( snapshot , this . _Entity , this ) as T ;
166
+ public find ( query ? : ICollectionQuery < T > ) : Promise < T [ ] > {
167
+ return new Promise ( ( resolve ) : void => {
168
+ let querySnapshotPromise : Promise < firestore . QuerySnapshot > ;
169
+ if ( query ) {
170
+ const fields = getRepository ( this . _Entity . prototype . constructor . name ) . fields ;
171
+ querySnapshotPromise = QueryBuilder . query ( this , fields , query ) . get ( ) ;
172
+ } else {
173
+ querySnapshotPromise = this . _native . get ( ) ;
174
+ }
175
+ querySnapshotPromise . then ( ( querySnapshot ) : void => {
176
+ resolve ( querySnapshot . docs . map ( ( snapshot ) : T => {
177
+ return FirestoreSerializer . deserialize ( snapshot , this . _Entity , this ) as T ;
178
+ } ) ) ;
179
+ } ) ;
160
180
} ) ;
161
181
}
162
182
163
183
/**
164
184
* Removes a document from the collection.
165
185
* @param id The document ID to remove.
166
186
*/
167
- public async remove ( id : string ) : Promise < void > {
168
- await this . _native . doc ( id ) . delete ( ) ;
187
+ public remove ( id : string ) : Promise < void > {
188
+ return new Promise ( ( resolve ) : void => {
189
+ this . _native . doc ( id ) . delete ( ) . then ( ( ) : void => {
190
+ resolve ( ) ;
191
+ } ) ;
192
+ } ) ;
169
193
}
170
194
}
171
195
0 commit comments