| 171 | } |
| 172 | |
| 173 | export class Transaction implements ITransaction { |
| 174 | _native: FIRTransaction; |
| 175 | |
| 176 | static fromNative(transaction: FIRTransaction) { |
| 177 | if (transaction instanceof FIRTransaction) { |
| 178 | const tran = new Transaction(); |
| 179 | tran._native = transaction; |
| 180 | return tran; |
| 181 | } |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | delete<T extends DocumentData = DocumentData>(documentRef: DocumentReference): Transaction { |
| 186 | return Transaction.fromNative(this._native.deleteDocument(documentRef.native)); |
| 187 | } |
| 188 | |
| 189 | get<T extends DocumentData = DocumentData>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> { |
| 190 | // TODO check error returned |
| 191 | return new Promise((resolve, reject) => { |
| 192 | try { |
| 193 | resolve(DocumentSnapshot.fromNative(this._native.getDocumentError(documentRef.native))); |
| 194 | } catch (e) { |
| 195 | reject(e); |
| 196 | } |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | update<T extends DocumentData = DocumentData>(documentRef: DocumentReference<T>, data: Partial<{ [K in keyof T]: T[K] | FieldValue }>): Transaction; |
| 201 | update<T extends DocumentData = DocumentData, K extends keyof T = string>(documentRef: DocumentReference<T>, field: K | FieldPath, value: T[K], moreFieldsAndValues: any[]): Transaction; |
| 202 | update(documentRef: any, field: any, value?: any, moreFieldsAndValues?: any): Transaction { |
| 203 | const data = createDictionary(field, value, moreFieldsAndValues); |
| 204 | return Transaction.fromNative(this._native.updateDataForDocument(data as any, documentRef?.native)); |
| 205 | } |
| 206 | |
| 207 | set<T extends DocumentData = DocumentData>(documentRef: DocumentReference<T>, data: T, options?: SetOptions): Transaction { |
| 208 | let transaction; |
| 209 | if (options) { |
| 210 | if (typeof options?.merge === 'boolean') { |
| 211 | transaction = this.native.setDataForDocumentMerge(serializeItems(data), documentRef.native, options.merge); |
| 212 | } else if (options.mergeFields) { |
| 213 | if (Array.isArray(options.mergeFields)) { |
| 214 | if (typeof options.mergeFields[0] === 'string') { |
| 215 | transaction = this.native.setDataForDocumentMergeFields(serializeItems(data), documentRef.native, options.mergeFields); |
| 216 | } else { |
| 217 | transaction = this.native.setDataForDocumentMergeFields( |
| 218 | serializeItems(data), |
| 219 | documentRef.native, |
| 220 | options.mergeFields.map((field) => field.native) |
| 221 | ); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } else { |
| 226 | transaction = this.native.setDataForDocument(serializeItems(data), documentRef.native); |
| 227 | } |
| 228 | return Transaction.fromNative(transaction); |
| 229 | } |
| 230 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…