forked from GitLiveApp/firebase-kotlin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.kt
164 lines (139 loc) · 7.93 KB
/
database.kt
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.gitlive.firebase.database
import dev.gitlive.firebase.DecodeSettings
import dev.gitlive.firebase.EncodeDecodeSettingsBuilder
import dev.gitlive.firebase.EncodeSettings
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseApp
import dev.gitlive.firebase.database.ChildEvent.Type.ADDED
import dev.gitlive.firebase.database.ChildEvent.Type.CHANGED
import dev.gitlive.firebase.database.ChildEvent.Type.MOVED
import dev.gitlive.firebase.database.ChildEvent.Type.REMOVED
import dev.gitlive.firebase.encode
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
/** Returns the [FirebaseDatabase] instance of the default [FirebaseApp]. */
expect val Firebase.database: FirebaseDatabase
/** Returns the [FirebaseDatabase] instance for the specified [url]. */
expect fun Firebase.database(url: String): FirebaseDatabase
/** Returns the [FirebaseDatabase] instance of the given [FirebaseApp]. */
expect fun Firebase.database(app: FirebaseApp): FirebaseDatabase
/** Returns the [FirebaseDatabase] instance of the given [FirebaseApp] and [url]. */
expect fun Firebase.database(app: FirebaseApp, url: String): FirebaseDatabase
expect class FirebaseDatabase {
fun reference(path: String): DatabaseReference
fun reference(): DatabaseReference
fun setPersistenceEnabled(enabled: Boolean)
fun setLoggingEnabled(enabled: Boolean)
fun useEmulator(host: String, port: Int)
}
data class ChildEvent internal constructor(
val snapshot: DataSnapshot,
val type: Type,
val previousChildName: String?
) {
enum class Type {
ADDED,
CHANGED,
MOVED,
REMOVED
}
}
internal expect open class NativeQuery
expect open class Query internal constructor(nativeQuery: NativeQuery) {
val valueEvents: Flow<DataSnapshot>
fun childEvents(vararg types: ChildEvent.Type = arrayOf(ADDED, CHANGED, MOVED, REMOVED)): Flow<ChildEvent>
fun orderByKey(): Query
fun orderByValue(): Query
fun orderByChild(path: String): Query
fun startAt(value: String, key: String? = null): Query
fun startAt(value: Double, key: String? = null): Query
fun startAt(value: Boolean, key: String? = null): Query
fun endAt(value: String, key: String? = null): Query
fun endAt(value: Double, key: String? = null): Query
fun endAt(value: Boolean, key: String? = null): Query
fun limitToFirst(limit: Int): Query
fun limitToLast(limit: Int): Query
fun equalTo(value: String, key: String? = null): Query
fun equalTo(value: Double, key: String? = null): Query
fun equalTo(value: Boolean, key: String? = null): Query
}
@PublishedApi
internal expect class NativeDatabaseReference : NativeQuery {
val key: String?
fun push(): NativeDatabaseReference
suspend fun setValueEncoded(encodedValue: Any?)
suspend fun updateEncodedChildren(encodedUpdate: Any?)
fun child(path: String): NativeDatabaseReference
fun onDisconnect(): NativeOnDisconnect
suspend fun removeValue()
suspend fun <T> runTransaction(strategy: KSerializer<T>, buildSettings: EncodeDecodeSettingsBuilder.() -> Unit = {}, transactionUpdate: (currentData: T) -> T): DataSnapshot
}
class DatabaseReference internal constructor(@PublishedApi internal val nativeReference: NativeDatabaseReference) : Query(nativeReference) {
val key: String? = nativeReference.key
fun push(): DatabaseReference = DatabaseReference(nativeReference.push())
fun child(path: String): DatabaseReference = DatabaseReference(nativeReference.child(path))
fun onDisconnect(): OnDisconnect = OnDisconnect(nativeReference.onDisconnect())
@Deprecated("Deprecated. Use builder instead", replaceWith = ReplaceWith("setValue(value) { this.encodeDefaults = encodeDefaults }"))
suspend inline fun <reified T> setValue(value: T?, encodeDefaults: Boolean) =
setValue(value) {
this.encodeDefaults = encodeDefaults
}
suspend inline fun <reified T> setValue(value: T?, buildSettings: EncodeSettings.Builder.() -> Unit = {}) =
nativeReference.setValueEncoded(encode(value, buildSettings))
@Deprecated("Deprecated. Use builder instead", replaceWith = ReplaceWith("setValue(strategy, value) { this.encodeDefaults = encodeDefaults }"))
suspend fun <T> setValue(strategy: SerializationStrategy<T>, value: T, encodeDefaults: Boolean) =
setValue(strategy, value) {
this.encodeDefaults = encodeDefaults
}
suspend inline fun <T> setValue(strategy: SerializationStrategy<T>, value: T, buildSettings: EncodeSettings.Builder.() -> Unit = {}) = nativeReference.setValueEncoded(encode(strategy, value, buildSettings))
@Deprecated("Deprecated. Use builder instead", replaceWith = ReplaceWith("updateChildren(update) { this.encodeDefaults = encodeDefaults }"))
suspend fun updateChildren(update: Map<String, Any?>, encodeDefaults: Boolean) = updateChildren(update) {
this.encodeDefaults = encodeDefaults
}
suspend inline fun updateChildren(update: Map<String, Any?>, buildSettings: EncodeSettings.Builder.() -> Unit = {}) = nativeReference.updateEncodedChildren(
encode(update, buildSettings))
suspend fun removeValue() = nativeReference.removeValue()
suspend fun <T> runTransaction(strategy: KSerializer<T>, buildSettings: EncodeDecodeSettingsBuilder.() -> Unit = {}, transactionUpdate: (currentData: T) -> T): DataSnapshot = nativeReference.runTransaction(strategy, buildSettings, transactionUpdate)
}
expect class DataSnapshot {
val exists: Boolean
val key: String?
val ref: DatabaseReference
val value: Any?
inline fun <reified T> value(): T
inline fun <T> value(strategy: DeserializationStrategy<T>, buildSettings: DecodeSettings.Builder.() -> Unit = {}): T
fun child(path: String): DataSnapshot
val hasChildren: Boolean
val children: Iterable<DataSnapshot>
}
expect class DatabaseException(message: String?, cause: Throwable?) : RuntimeException
@PublishedApi
internal expect class NativeOnDisconnect {
suspend fun removeValue()
suspend fun cancel()
suspend fun setValue(encodedValue: Any?)
suspend fun updateEncodedChildren(encodedUpdate: Map<String, Any?>)
}
class OnDisconnect internal constructor(@PublishedApi internal val native: NativeOnDisconnect) {
suspend fun removeValue() = native.removeValue()
suspend fun cancel() = native.cancel()
@Deprecated("Deprecated. Use builder instead", replaceWith = ReplaceWith("setValue(value) { this.encodeDefaults = encodeDefaults }"))
suspend inline fun <reified T> setValue(value: T?, encodeDefaults: Boolean) =
setValue(value) { this.encodeDefaults = encodeDefaults }
suspend inline fun <reified T> setValue(value: T?, buildSettings: EncodeSettings.Builder.() -> Unit = {}) =
native.setValue(encode(value, buildSettings))
@Deprecated("Deprecated. Use builder instead", replaceWith = ReplaceWith("setValue(strategy, value) { this.encodeDefaults = encodeDefaults }"))
suspend fun <T> setValue(strategy: SerializationStrategy<T>, value: T, encodeDefaults: Boolean) =
setValue(strategy, value) { this.encodeDefaults = encodeDefaults }
suspend inline fun <T> setValue(strategy: SerializationStrategy<T>, value: T, buildSettings: EncodeSettings.Builder.() -> Unit = {}) = setValue(encode(strategy, value, buildSettings))
suspend inline fun updateChildren(update: Map<String, Any?>, buildSettings: EncodeSettings.Builder.() -> Unit = {}) = native.updateEncodedChildren(update.mapValues { (_, it) -> encode(it, buildSettings) })
@Deprecated("Deprecated. Use builder instead", replaceWith = ReplaceWith("updateChildren(update) { this.encodeDefaults = encodeDefaults }"))
suspend fun updateChildren(update: Map<String, Any?>, encodeDefaults: Boolean) = updateChildren(update) {
this.encodeDefaults = encodeDefaults
}
}