Skip to content

Commit de8974b

Browse files
authored
Add Firestore Metadata (#106)
1 parent a3ecd94 commit de8974b

File tree

5 files changed

+39
-2
lines changed
  • firebase-common/src/jsMain/kotlin/dev/gitlive/firebase
  • firebase-firestore/src

5 files changed

+39
-2
lines changed

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,23 @@ external object firebase {
366366
open class QuerySnapshot {
367367
val docs: Array<DocumentSnapshot>
368368
val empty: Boolean
369+
val metadata: SnapshotMetadata
369370
}
370371

371372
open class DocumentSnapshot {
372373
val id: String
373374
val ref: DocumentReference
374375
val exists: Boolean
376+
val metadata: SnapshotMetadata
375377
fun data(options: Any? = definedExternally): Any?
376378
fun get(fieldPath: Any, options: Any? = definedExternally): Any?
377379
}
378380

381+
open class SnapshotMetadata {
382+
val hasPendingWrites: Boolean
383+
val fromCache: Boolean
384+
}
385+
379386
open class DocumentReference {
380387
val id: String
381388
val path: String

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ actual typealias FirestoreExceptionCode = com.google.firebase.firestore.Firebase
335335
actual class QuerySnapshot(val android: com.google.firebase.firestore.QuerySnapshot) {
336336
actual val documents
337337
get() = android.documents.map { DocumentSnapshot(it) }
338+
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(android.metadata)
338339
}
339340

340341
@Suppress("UNCHECKED_CAST")
@@ -355,6 +356,13 @@ actual class DocumentSnapshot(val android: com.google.firebase.firestore.Documen
355356
actual fun contains(field: String) = android.contains(field)
356357

357358
actual val exists get() = android.exists()
359+
360+
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(android.metadata)
361+
}
362+
363+
actual class SnapshotMetadata(val android: com.google.firebase.firestore.SnapshotMetadata) {
364+
actual val hasPendingWrites: Boolean get() = android.hasPendingWrites()
365+
actual val isFromCache: Boolean get() = android.isFromCache()
358366
}
359367

360368
actual typealias FieldPath = com.google.firebase.firestore.FieldPath

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ expect enum class FirestoreExceptionCode {
147147

148148
expect class QuerySnapshot {
149149
val documents: List<DocumentSnapshot>
150+
val metadata: SnapshotMetadata
150151
}
151152

152153
expect class DocumentSnapshot {
@@ -162,6 +163,12 @@ expect class DocumentSnapshot {
162163
val exists: Boolean
163164
val id: String
164165
val reference: DocumentReference
166+
val metadata: SnapshotMetadata
167+
}
168+
169+
expect class SnapshotMetadata {
170+
val hasPendingWrites: Boolean
171+
val isFromCache: Boolean
165172
}
166173

167174
expect class FieldPath

firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ fun NSError.toException() = when(domain) {
284284
actual class QuerySnapshot(val ios: FIRQuerySnapshot) {
285285
actual val documents
286286
get() = ios.documents.map { DocumentSnapshot(it as FIRDocumentSnapshot) }
287+
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(ios.metadata)
287288
}
288289

289290
@Suppress("UNCHECKED_CAST")
@@ -305,6 +306,13 @@ actual class DocumentSnapshot(val ios: FIRDocumentSnapshot) {
305306
actual fun contains(field: String) = ios.valueForField(field) != null
306307

307308
actual val exists get() = ios.exists
309+
310+
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(ios.metadata)
311+
}
312+
313+
actual class SnapshotMetadata(val ios: FIRSnapshotMetadata) {
314+
actual val hasPendingWrites: Boolean get() = ios.pendingWrites
315+
actual val isFromCache: Boolean get() = ios.fromCache
308316
}
309317

310318
actual typealias FieldPath = FIRFieldPath

firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,15 @@ actual val FirebaseFirestoreException.code: FirestoreExceptionCode get() = code
317317
actual class QuerySnapshot(val js: firebase.firestore.QuerySnapshot) {
318318
actual val documents
319319
get() = js.docs.map { DocumentSnapshot(it) }
320+
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(js.metadata)
320321
}
321322

322323
actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {
323324

324325
actual val id get() = rethrow { js.id }
325326
actual val reference get() = rethrow { DocumentReference(js.ref) }
326327

327-
actual inline fun <reified T: Any> data(): T =
328+
actual inline fun <reified T : Any> data(): T =
328329
rethrow { decode<T>(value = js.data()) }
329330

330331
actual fun <T> data(strategy: DeserializationStrategy<T>): T =
@@ -337,7 +338,13 @@ actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {
337338
rethrow { decode(strategy, js.get(field)) }
338339

339340
actual fun contains(field: String) = rethrow { js.get(field) != undefined }
340-
actual val exists get() = rethrow { js.exists }
341+
actual val exists get() = rethrow { js.exists }
342+
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(js.metadata)
343+
}
344+
345+
actual class SnapshotMetadata(val js: firebase.firestore.SnapshotMetadata) {
346+
actual val hasPendingWrites: Boolean get() = js.hasPendingWrites
347+
actual val isFromCache: Boolean get() = js.fromCache
341348
}
342349

343350
actual typealias FieldPath = Any

0 commit comments

Comments
 (0)