@react-native-firebase/database 24.1.1 → 25.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/dist/module/DatabaseDataSnapshot.js +144 -0
- package/dist/module/DatabaseDataSnapshot.js.map +1 -0
- package/dist/module/DatabaseOnDisconnect.js +84 -0
- package/dist/module/DatabaseOnDisconnect.js.map +1 -0
- package/dist/module/DatabaseQuery.js +298 -0
- package/dist/module/DatabaseQuery.js.map +1 -0
- package/{lib → dist/module}/DatabaseQueryModifiers.js +49 -98
- package/dist/module/DatabaseQueryModifiers.js.map +1 -0
- package/dist/module/DatabaseReference.js +169 -0
- package/dist/module/DatabaseReference.js.map +1 -0
- package/dist/module/DatabaseStatics.js +35 -0
- package/dist/module/DatabaseStatics.js.map +1 -0
- package/{lib → dist/module}/DatabaseSyncTree.js +74 -156
- package/dist/module/DatabaseSyncTree.js.map +1 -0
- package/{lib → dist/module}/DatabaseThenableReference.js +12 -12
- package/dist/module/DatabaseThenableReference.js.map +1 -0
- package/{lib → dist/module}/DatabaseTransaction.js +26 -89
- package/dist/module/DatabaseTransaction.js.map +1 -0
- package/dist/module/index.js +27 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/modular/query.js +138 -0
- package/dist/module/modular/query.js.map +1 -0
- package/dist/module/modular/transaction.js +24 -0
- package/dist/module/modular/transaction.js.map +1 -0
- package/dist/module/modular.js +76 -0
- package/dist/module/modular.js.map +1 -0
- package/dist/module/namespaced.js +147 -0
- package/dist/module/namespaced.js.map +1 -0
- package/dist/module/package.json +1 -0
- package/dist/module/types/database.js +21 -0
- package/dist/module/types/database.js.map +1 -0
- package/dist/module/types/internal.js +4 -0
- package/dist/module/types/internal.js.map +1 -0
- package/dist/module/types/namespaced.js +67 -0
- package/dist/module/types/namespaced.js.map +1 -0
- package/dist/module/version.js +5 -0
- package/dist/module/version.js.map +1 -0
- package/dist/module/web/RNFBDatabaseModule.android.js +6 -0
- package/dist/module/web/RNFBDatabaseModule.android.js.map +1 -0
- package/dist/module/web/RNFBDatabaseModule.ios.js +6 -0
- package/dist/module/web/RNFBDatabaseModule.ios.js.map +1 -0
- package/dist/module/web/RNFBDatabaseModule.js +375 -0
- package/dist/module/web/RNFBDatabaseModule.js.map +1 -0
- package/{lib → dist/module}/web/query.js +19 -24
- package/dist/module/web/query.js.map +1 -0
- package/dist/typescript/lib/DatabaseDataSnapshot.d.ts +20 -0
- package/dist/typescript/lib/DatabaseDataSnapshot.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseOnDisconnect.d.ts +19 -0
- package/dist/typescript/lib/DatabaseOnDisconnect.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseQuery.d.ts +32 -0
- package/dist/typescript/lib/DatabaseQuery.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseQueryModifiers.d.ts +55 -0
- package/dist/typescript/lib/DatabaseQueryModifiers.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseReference.d.ts +21 -0
- package/dist/typescript/lib/DatabaseReference.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseStatics.d.ts +10 -0
- package/dist/typescript/lib/DatabaseStatics.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseSyncTree.d.ts +72 -0
- package/dist/typescript/lib/DatabaseSyncTree.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseThenableReference.d.ts +13 -0
- package/dist/typescript/lib/DatabaseThenableReference.d.ts.map +1 -0
- package/dist/typescript/lib/DatabaseTransaction.d.ts +15 -0
- package/dist/typescript/lib/DatabaseTransaction.d.ts.map +1 -0
- package/dist/typescript/lib/index.d.ts +6 -0
- package/dist/typescript/lib/index.d.ts.map +1 -0
- package/dist/typescript/lib/modular/query.d.ts +41 -0
- package/dist/typescript/lib/modular/query.d.ts.map +1 -0
- package/dist/typescript/lib/modular/transaction.d.ts +3 -0
- package/dist/typescript/lib/modular/transaction.d.ts.map +1 -0
- package/dist/typescript/lib/modular.d.ts +24 -0
- package/dist/typescript/lib/modular.d.ts.map +1 -0
- package/dist/typescript/lib/namespaced.d.ts +13 -0
- package/dist/typescript/lib/namespaced.d.ts.map +1 -0
- package/dist/typescript/lib/types/database.d.ts +93 -0
- package/dist/typescript/lib/types/database.d.ts.map +1 -0
- package/dist/typescript/lib/types/internal.d.ts +189 -0
- package/dist/typescript/lib/types/internal.d.ts.map +1 -0
- package/dist/typescript/lib/types/namespaced.d.ts +1230 -0
- package/dist/typescript/lib/types/namespaced.d.ts.map +1 -0
- package/dist/typescript/lib/version.d.ts +2 -0
- package/dist/typescript/lib/version.d.ts.map +1 -0
- package/dist/typescript/lib/web/RNFBDatabaseModule.android.d.ts +3 -0
- package/dist/typescript/lib/web/RNFBDatabaseModule.android.d.ts.map +1 -0
- package/dist/typescript/lib/web/RNFBDatabaseModule.d.ts +3 -0
- package/dist/typescript/lib/web/RNFBDatabaseModule.d.ts.map +1 -0
- package/dist/typescript/lib/web/RNFBDatabaseModule.ios.d.ts +3 -0
- package/dist/typescript/lib/web/RNFBDatabaseModule.ios.d.ts.map +1 -0
- package/dist/typescript/lib/web/query.d.ts +13 -0
- package/dist/typescript/lib/web/query.d.ts.map +1 -0
- package/dist/typescript/package.json +1 -0
- package/lib/{DatabaseDataSnapshot.js → DatabaseDataSnapshot.ts} +62 -88
- package/lib/{DatabaseOnDisconnect.js → DatabaseOnDisconnect.ts} +25 -23
- package/lib/{DatabaseQuery.js → DatabaseQuery.ts} +130 -144
- package/lib/DatabaseQueryModifiers.ts +283 -0
- package/lib/{DatabaseReference.js → DatabaseReference.ts} +103 -85
- package/lib/{DatabaseStatics.js → DatabaseStatics.ts} +4 -2
- package/lib/DatabaseSyncTree.ts +390 -0
- package/lib/DatabaseThenableReference.ts +73 -0
- package/lib/DatabaseTransaction.ts +172 -0
- package/lib/index.ts +25 -0
- package/lib/modular/query.ts +410 -0
- package/lib/modular/transaction.ts +34 -0
- package/lib/modular.ts +134 -0
- package/lib/{index.js → namespaced.ts} +91 -90
- package/lib/types/database.ts +144 -0
- package/lib/types/internal.ts +317 -0
- package/lib/{index.d.ts → types/namespaced.ts} +48 -9
- package/lib/version.ts +2 -0
- package/lib/web/RNFBDatabaseModule.android.ts +4 -0
- package/lib/web/RNFBDatabaseModule.ios.ts +4 -0
- package/lib/web/RNFBDatabaseModule.ts +534 -0
- package/lib/web/query.ts +97 -0
- package/package.json +42 -7
- package/tsconfig.json +21 -0
- package/typedoc.json +3 -3
- package/lib/modular/index.d.ts +0 -246
- package/lib/modular/index.js +0 -134
- package/lib/modular/query.d.ts +0 -972
- package/lib/modular/query.js +0 -305
- package/lib/modular/transaction.d.ts +0 -54
- package/lib/modular/transaction.js +0 -24
- package/lib/version.js +0 -2
- package/lib/web/RNFBDatabaseModule.android.js +0 -2
- package/lib/web/RNFBDatabaseModule.ios.js +0 -2
- package/lib/web/RNFBDatabaseModule.js +0 -558
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import {
|
|
2
|
+
connectDatabaseEmulator,
|
|
3
|
+
enableLogging,
|
|
4
|
+
get,
|
|
5
|
+
getApp,
|
|
6
|
+
getDatabase,
|
|
7
|
+
goOffline,
|
|
8
|
+
goOnline,
|
|
9
|
+
onChildAdded,
|
|
10
|
+
onChildChanged,
|
|
11
|
+
onChildMoved,
|
|
12
|
+
onChildRemoved,
|
|
13
|
+
onDisconnect,
|
|
14
|
+
onValue,
|
|
15
|
+
ref,
|
|
16
|
+
remove,
|
|
17
|
+
runTransaction,
|
|
18
|
+
set,
|
|
19
|
+
setPriority,
|
|
20
|
+
setWithPriority,
|
|
21
|
+
update,
|
|
22
|
+
} from '@react-native-firebase/app/dist/module/internal/web/firebaseDatabase';
|
|
23
|
+
import {
|
|
24
|
+
emitEvent,
|
|
25
|
+
getWebError,
|
|
26
|
+
guard,
|
|
27
|
+
} from '@react-native-firebase/app/dist/module/internal/web/utils';
|
|
28
|
+
import { getQueryInstance } from './query';
|
|
29
|
+
import type { DatabaseQueryModifier } from '../DatabaseQueryModifiers';
|
|
30
|
+
import type {
|
|
31
|
+
DatabaseListenPropsInternal,
|
|
32
|
+
DatabaseSnapshotInternal,
|
|
33
|
+
DatabaseTransactionUpdatesInternal,
|
|
34
|
+
} from '../types/internal';
|
|
35
|
+
|
|
36
|
+
type WebApp = ReturnType<typeof getApp>;
|
|
37
|
+
type WebDatabase = ReturnType<typeof getDatabase>;
|
|
38
|
+
type WebDatabaseReference = ReturnType<typeof ref>;
|
|
39
|
+
type WebDataSnapshot = Awaited<ReturnType<typeof get>>;
|
|
40
|
+
type WebOnDisconnect = ReturnType<typeof onDisconnect>;
|
|
41
|
+
type WebUnsubscribe = ReturnType<typeof onValue>;
|
|
42
|
+
|
|
43
|
+
type DatabaseOperationPropsInternal =
|
|
44
|
+
| { value: unknown }
|
|
45
|
+
| { values: Record<string, unknown> }
|
|
46
|
+
| { value: unknown; priority: string | number | null }
|
|
47
|
+
| { priority: string | number | null };
|
|
48
|
+
|
|
49
|
+
function rejectWithCodeAndMessage(code: string, message: string): Promise<never> {
|
|
50
|
+
const error = new Error(message) as Error & { code?: string };
|
|
51
|
+
error.code = code;
|
|
52
|
+
return Promise.reject(getWebError(error));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function snapshotToObject(snapshot: WebDataSnapshot): DatabaseSnapshotInternal {
|
|
56
|
+
const childKeys: string[] = [];
|
|
57
|
+
|
|
58
|
+
if (snapshot.hasChildren()) {
|
|
59
|
+
snapshot.forEach((childSnapshot: WebDataSnapshot) => {
|
|
60
|
+
if (typeof childSnapshot.key === 'string') {
|
|
61
|
+
childKeys.push(childSnapshot.key);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
key: snapshot.key,
|
|
68
|
+
exists: snapshot.exists(),
|
|
69
|
+
hasChildren: snapshot.hasChildren(),
|
|
70
|
+
childrenCount: snapshot.size,
|
|
71
|
+
childKeys,
|
|
72
|
+
priority: snapshot.priority,
|
|
73
|
+
value: snapshot.val(),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getDatabaseWebError(error: unknown): unknown {
|
|
78
|
+
return getWebError(error as Error & { code?: string });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function snapshotWithPreviousChildToObject(
|
|
82
|
+
snapshot: WebDataSnapshot,
|
|
83
|
+
previousChildName: string | null,
|
|
84
|
+
): { snapshot: DatabaseSnapshotInternal; previousChildName: string | null } {
|
|
85
|
+
return {
|
|
86
|
+
snapshot: snapshotToObject(snapshot),
|
|
87
|
+
previousChildName,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type SnapshotWithPreviousChildInternal = {
|
|
92
|
+
snapshot: WebDataSnapshot;
|
|
93
|
+
previousChildName: string | null;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const appInstances: Record<string, WebApp> = {};
|
|
97
|
+
const databaseInstances: Record<string, WebDatabase> = {};
|
|
98
|
+
const onDisconnectRef: Record<string, WebOnDisconnect> = {};
|
|
99
|
+
const listeners: Record<string, WebUnsubscribe> = {};
|
|
100
|
+
const emulatorForApp: Record<string, { host: string; port: number; connected?: boolean }> = {};
|
|
101
|
+
|
|
102
|
+
function getCachedAppInstance(appName: string): WebApp {
|
|
103
|
+
return (appInstances[appName] ??= getApp(appName));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function getCachedDatabaseInstance(appName: string, dbURL: string): WebDatabase {
|
|
107
|
+
let instance = databaseInstances[`${appName}|${dbURL}`];
|
|
108
|
+
if (!instance) {
|
|
109
|
+
instance = getDatabase(getCachedAppInstance(appName), dbURL);
|
|
110
|
+
if (
|
|
111
|
+
emulatorForApp[appName] &&
|
|
112
|
+
!('_instanceStarted' in (instance as object)) &&
|
|
113
|
+
emulatorForApp[appName]
|
|
114
|
+
) {
|
|
115
|
+
const { host, port } = emulatorForApp[appName];
|
|
116
|
+
connectDatabaseEmulator(instance, host, port);
|
|
117
|
+
emulatorForApp[appName].connected = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return instance;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function getCachedOnDisconnectInstance(refValue: WebDatabaseReference): WebOnDisconnect {
|
|
124
|
+
const cacheKey = String(refValue.key);
|
|
125
|
+
return (onDisconnectRef[cacheKey] ??= onDisconnect(refValue));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const databaseFallbackModule: Record<string, unknown> = {
|
|
129
|
+
goOnline(appName: string, dbURL: string): Promise<void> {
|
|
130
|
+
return guard(async () => {
|
|
131
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
132
|
+
goOnline(db);
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
goOffline(appName: string, dbURL: string): Promise<void> {
|
|
137
|
+
return guard(async () => {
|
|
138
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
139
|
+
goOffline(db);
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
setPersistenceEnabled(): Promise<void> {
|
|
144
|
+
if (__DEV__) {
|
|
145
|
+
// eslint-disable-next-line no-console
|
|
146
|
+
console.warn(
|
|
147
|
+
'The Firebase Database `setPersistenceEnabled` method is not available in the this environment.',
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
return Promise.resolve();
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
setLoggingEnabled(_app: string, _dbURL: string, enabled: boolean): Promise<void> {
|
|
154
|
+
return guard(async () => {
|
|
155
|
+
enableLogging(enabled);
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
setPersistenceCacheSizeBytes(): Promise<void> {
|
|
160
|
+
return Promise.resolve();
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
useEmulator(appName: string, dbURL: string, host: string, port: number): Promise<void> {
|
|
164
|
+
return guard(async () => {
|
|
165
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
166
|
+
connectDatabaseEmulator(db, host, port);
|
|
167
|
+
emulatorForApp[appName] = { host, port };
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
set(
|
|
172
|
+
appName: string,
|
|
173
|
+
dbURL: string,
|
|
174
|
+
path: string,
|
|
175
|
+
props: DatabaseOperationPropsInternal,
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
return guard(async () => {
|
|
178
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
179
|
+
const dbRef = ref(db, path);
|
|
180
|
+
await set(dbRef, (props as { value: unknown }).value);
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
update(
|
|
185
|
+
appName: string,
|
|
186
|
+
dbURL: string,
|
|
187
|
+
path: string,
|
|
188
|
+
props: DatabaseOperationPropsInternal,
|
|
189
|
+
): Promise<void> {
|
|
190
|
+
return guard(async () => {
|
|
191
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
192
|
+
const dbRef = ref(db, path);
|
|
193
|
+
await update(dbRef, (props as { values: Record<string, unknown> }).values);
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
setWithPriority(
|
|
198
|
+
appName: string,
|
|
199
|
+
dbURL: string,
|
|
200
|
+
path: string,
|
|
201
|
+
props: DatabaseOperationPropsInternal,
|
|
202
|
+
): Promise<void> {
|
|
203
|
+
return guard(async () => {
|
|
204
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
205
|
+
const dbRef = ref(db, path);
|
|
206
|
+
const { value, priority } = props as { value: unknown; priority: string | number | null };
|
|
207
|
+
await setWithPriority(dbRef, value, priority);
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
remove(appName: string, dbURL: string, path: string): Promise<void> {
|
|
212
|
+
return guard(async () => {
|
|
213
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
214
|
+
const dbRef = ref(db, path);
|
|
215
|
+
await remove(dbRef);
|
|
216
|
+
});
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
setPriority(
|
|
220
|
+
appName: string,
|
|
221
|
+
dbURL: string,
|
|
222
|
+
path: string,
|
|
223
|
+
props: DatabaseOperationPropsInternal,
|
|
224
|
+
): Promise<void> {
|
|
225
|
+
return guard(async () => {
|
|
226
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
227
|
+
const dbRef = ref(db, path);
|
|
228
|
+
await setPriority(dbRef, (props as { priority: string | number | null }).priority);
|
|
229
|
+
});
|
|
230
|
+
},
|
|
231
|
+
|
|
232
|
+
once(
|
|
233
|
+
appName: string,
|
|
234
|
+
dbURL: string,
|
|
235
|
+
path: string,
|
|
236
|
+
modifiers: DatabaseQueryModifier[],
|
|
237
|
+
eventType: string,
|
|
238
|
+
): Promise<unknown> {
|
|
239
|
+
return guard(async () => {
|
|
240
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
241
|
+
const dbRef = ref(db, path);
|
|
242
|
+
const queryRef = getQueryInstance(dbRef, modifiers);
|
|
243
|
+
|
|
244
|
+
if (eventType === 'value') {
|
|
245
|
+
const snapshot = await new Promise<WebDataSnapshot>((resolve, reject) => {
|
|
246
|
+
onValue(queryRef, resolve, reject, { onlyOnce: true });
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
return snapshotToObject(snapshot);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (eventType === 'child_added') {
|
|
253
|
+
const { snapshot, previousChildName } =
|
|
254
|
+
await new Promise<SnapshotWithPreviousChildInternal>((resolve, reject) => {
|
|
255
|
+
onChildAdded(
|
|
256
|
+
queryRef,
|
|
257
|
+
(childSnapshot, childPreviousChildName) => {
|
|
258
|
+
resolve({
|
|
259
|
+
snapshot: childSnapshot,
|
|
260
|
+
previousChildName: childPreviousChildName,
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
reject,
|
|
264
|
+
{ onlyOnce: true },
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
return snapshotWithPreviousChildToObject(snapshot, previousChildName ?? null);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (eventType === 'child_changed') {
|
|
272
|
+
const { snapshot, previousChildName } =
|
|
273
|
+
await new Promise<SnapshotWithPreviousChildInternal>((resolve, reject) => {
|
|
274
|
+
onChildChanged(
|
|
275
|
+
queryRef,
|
|
276
|
+
(childSnapshot, childPreviousChildName) => {
|
|
277
|
+
resolve({
|
|
278
|
+
snapshot: childSnapshot,
|
|
279
|
+
previousChildName: childPreviousChildName,
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
reject,
|
|
283
|
+
{ onlyOnce: true },
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
return snapshotWithPreviousChildToObject(snapshot, previousChildName ?? null);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (eventType === 'child_removed') {
|
|
291
|
+
const snapshot = await new Promise<WebDataSnapshot>((resolve, reject) => {
|
|
292
|
+
onChildRemoved(queryRef, resolve, reject, { onlyOnce: true });
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
return snapshotWithPreviousChildToObject(snapshot, null);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (eventType === 'child_moved') {
|
|
299
|
+
const { snapshot, previousChildName } =
|
|
300
|
+
await new Promise<SnapshotWithPreviousChildInternal>((resolve, reject) => {
|
|
301
|
+
onChildMoved(
|
|
302
|
+
queryRef,
|
|
303
|
+
(childSnapshot, childPreviousChildName) => {
|
|
304
|
+
resolve({
|
|
305
|
+
snapshot: childSnapshot,
|
|
306
|
+
previousChildName: childPreviousChildName,
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
reject,
|
|
310
|
+
{ onlyOnce: true },
|
|
311
|
+
);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
return snapshotWithPreviousChildToObject(snapshot, previousChildName ?? null);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const snapshot = await get(dbRef);
|
|
318
|
+
return snapshotToObject(snapshot);
|
|
319
|
+
});
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
on(appName: string, dbURL: string, props: DatabaseListenPropsInternal): Promise<void> {
|
|
323
|
+
return guard(async () => {
|
|
324
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
325
|
+
const { key, modifiers, path, eventType, registration } = props;
|
|
326
|
+
const { eventRegistrationKey } = registration;
|
|
327
|
+
const dbRef = ref(db, path);
|
|
328
|
+
const queryRef = getQueryInstance(dbRef, modifiers);
|
|
329
|
+
|
|
330
|
+
function sendEvent(data: unknown): void {
|
|
331
|
+
emitEvent('database_sync_event', {
|
|
332
|
+
eventName: 'database_sync_event',
|
|
333
|
+
body: {
|
|
334
|
+
data,
|
|
335
|
+
key,
|
|
336
|
+
registration,
|
|
337
|
+
eventType,
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function sendError(error: unknown): void {
|
|
343
|
+
emitEvent('database_sync_event', {
|
|
344
|
+
eventName: 'database_sync_event',
|
|
345
|
+
body: {
|
|
346
|
+
key,
|
|
347
|
+
registration,
|
|
348
|
+
error: getDatabaseWebError(error),
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
let listener: WebUnsubscribe | null = null;
|
|
354
|
+
|
|
355
|
+
if (listeners[eventRegistrationKey]) {
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (eventType === 'value') {
|
|
360
|
+
listener = onValue(queryRef, snapshot => sendEvent(snapshotToObject(snapshot)), sendError);
|
|
361
|
+
} else if (eventType === 'child_added') {
|
|
362
|
+
listener = onChildAdded(
|
|
363
|
+
queryRef,
|
|
364
|
+
(snapshot, previousChildName) => {
|
|
365
|
+
sendEvent(snapshotWithPreviousChildToObject(snapshot, previousChildName ?? null));
|
|
366
|
+
},
|
|
367
|
+
sendError,
|
|
368
|
+
);
|
|
369
|
+
} else if (eventType === 'child_changed') {
|
|
370
|
+
listener = onChildChanged(
|
|
371
|
+
queryRef,
|
|
372
|
+
(snapshot, previousChildName) => {
|
|
373
|
+
sendEvent(snapshotWithPreviousChildToObject(snapshot, previousChildName ?? null));
|
|
374
|
+
},
|
|
375
|
+
sendError,
|
|
376
|
+
);
|
|
377
|
+
} else if (eventType === 'child_removed') {
|
|
378
|
+
listener = onChildRemoved(
|
|
379
|
+
queryRef,
|
|
380
|
+
snapshot => {
|
|
381
|
+
sendEvent(snapshotWithPreviousChildToObject(snapshot, null));
|
|
382
|
+
},
|
|
383
|
+
sendError,
|
|
384
|
+
);
|
|
385
|
+
} else if (eventType === 'child_moved') {
|
|
386
|
+
listener = onChildMoved(
|
|
387
|
+
queryRef,
|
|
388
|
+
(snapshot, previousChildName) => {
|
|
389
|
+
sendEvent(snapshotWithPreviousChildToObject(snapshot, previousChildName ?? null));
|
|
390
|
+
},
|
|
391
|
+
sendError,
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (listener) {
|
|
396
|
+
listeners[eventRegistrationKey] = listener;
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
},
|
|
400
|
+
|
|
401
|
+
off(_queryKey: string, eventRegistrationKey: string): void {
|
|
402
|
+
const listener = listeners[eventRegistrationKey];
|
|
403
|
+
if (listener) {
|
|
404
|
+
listener();
|
|
405
|
+
delete listeners[eventRegistrationKey];
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
|
|
409
|
+
keepSynced(): Promise<never> {
|
|
410
|
+
return rejectWithCodeAndMessage(
|
|
411
|
+
'unsupported',
|
|
412
|
+
'This operation is not supported on this environment.',
|
|
413
|
+
);
|
|
414
|
+
},
|
|
415
|
+
|
|
416
|
+
onDisconnectCancel(appName: string, dbURL: string, path: string): Promise<void> {
|
|
417
|
+
return guard(async () => {
|
|
418
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
419
|
+
const dbRef = ref(db, path);
|
|
420
|
+
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
421
|
+
await instance.cancel();
|
|
422
|
+
delete onDisconnectRef[String(dbRef.key)];
|
|
423
|
+
});
|
|
424
|
+
},
|
|
425
|
+
|
|
426
|
+
onDisconnectRemove(appName: string, dbURL: string, path: string): Promise<void> {
|
|
427
|
+
return guard(async () => {
|
|
428
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
429
|
+
const dbRef = ref(db, path);
|
|
430
|
+
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
431
|
+
await instance.remove();
|
|
432
|
+
});
|
|
433
|
+
},
|
|
434
|
+
|
|
435
|
+
onDisconnectSet(
|
|
436
|
+
appName: string,
|
|
437
|
+
dbURL: string,
|
|
438
|
+
path: string,
|
|
439
|
+
props: DatabaseOperationPropsInternal,
|
|
440
|
+
): Promise<void> {
|
|
441
|
+
return guard(async () => {
|
|
442
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
443
|
+
const dbRef = ref(db, path);
|
|
444
|
+
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
445
|
+
await instance.set((props as { value: unknown }).value);
|
|
446
|
+
});
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
onDisconnectSetWithPriority(
|
|
450
|
+
appName: string,
|
|
451
|
+
dbURL: string,
|
|
452
|
+
path: string,
|
|
453
|
+
props: DatabaseOperationPropsInternal,
|
|
454
|
+
): Promise<void> {
|
|
455
|
+
return guard(async () => {
|
|
456
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
457
|
+
const dbRef = ref(db, path);
|
|
458
|
+
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
459
|
+
const { value, priority } = props as { value: unknown; priority: string | number | null };
|
|
460
|
+
await instance.setWithPriority(value, priority);
|
|
461
|
+
});
|
|
462
|
+
},
|
|
463
|
+
|
|
464
|
+
onDisconnectUpdate(
|
|
465
|
+
appName: string,
|
|
466
|
+
dbURL: string,
|
|
467
|
+
path: string,
|
|
468
|
+
props: DatabaseOperationPropsInternal,
|
|
469
|
+
): Promise<void> {
|
|
470
|
+
return guard(async () => {
|
|
471
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
472
|
+
const dbRef = ref(db, path);
|
|
473
|
+
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
474
|
+
await instance.update((props as { values: Record<string, unknown> }).values);
|
|
475
|
+
});
|
|
476
|
+
},
|
|
477
|
+
|
|
478
|
+
transactionStart(
|
|
479
|
+
appName: string,
|
|
480
|
+
dbURL: string,
|
|
481
|
+
path: string,
|
|
482
|
+
transactionId: number,
|
|
483
|
+
applyLocally: boolean,
|
|
484
|
+
userExecutor?: (currentData: unknown) => unknown,
|
|
485
|
+
): Promise<void> {
|
|
486
|
+
return guard(async () => {
|
|
487
|
+
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
488
|
+
const dbRef = ref(db, path);
|
|
489
|
+
|
|
490
|
+
try {
|
|
491
|
+
const { committed, snapshot } = await runTransaction(
|
|
492
|
+
dbRef,
|
|
493
|
+
userExecutor ?? (data => data),
|
|
494
|
+
{
|
|
495
|
+
applyLocally,
|
|
496
|
+
},
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
emitEvent('database_transaction_event', {
|
|
500
|
+
body: {
|
|
501
|
+
committed,
|
|
502
|
+
type: 'complete',
|
|
503
|
+
snapshot: snapshotToObject(snapshot),
|
|
504
|
+
},
|
|
505
|
+
appName,
|
|
506
|
+
id: transactionId,
|
|
507
|
+
eventName: 'database_transaction_event',
|
|
508
|
+
});
|
|
509
|
+
} catch (e) {
|
|
510
|
+
emitEvent('database_transaction_event', {
|
|
511
|
+
body: {
|
|
512
|
+
committed: false,
|
|
513
|
+
type: 'error',
|
|
514
|
+
error: getDatabaseWebError(e),
|
|
515
|
+
},
|
|
516
|
+
appName,
|
|
517
|
+
id: transactionId,
|
|
518
|
+
eventName: 'database_transaction_event',
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
},
|
|
523
|
+
|
|
524
|
+
async transactionTryCommit(
|
|
525
|
+
_appName: string,
|
|
526
|
+
_dbURL: string,
|
|
527
|
+
_transactionId: string,
|
|
528
|
+
_updates: DatabaseTransactionUpdatesInternal,
|
|
529
|
+
): Promise<never> {
|
|
530
|
+
throw new Error('Not implemented');
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
export default databaseFallbackModule;
|
package/lib/web/query.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
endAt,
|
|
3
|
+
endBefore,
|
|
4
|
+
limitToFirst,
|
|
5
|
+
limitToLast,
|
|
6
|
+
orderByChild,
|
|
7
|
+
orderByKey,
|
|
8
|
+
orderByPriority,
|
|
9
|
+
orderByValue,
|
|
10
|
+
query,
|
|
11
|
+
startAfter,
|
|
12
|
+
startAt,
|
|
13
|
+
} from '@react-native-firebase/app/dist/module/internal/web/firebaseDatabase';
|
|
14
|
+
import type { DatabaseQueryModifier } from '../DatabaseQueryModifiers';
|
|
15
|
+
|
|
16
|
+
type WebDatabaseReference = Parameters<typeof query>[0];
|
|
17
|
+
type WebQueryConstraint = Parameters<typeof query>[1];
|
|
18
|
+
type QueryFilterValue = number | string | boolean | null;
|
|
19
|
+
type WebDatabaseQueryModifier =
|
|
20
|
+
| DatabaseQueryModifier
|
|
21
|
+
| {
|
|
22
|
+
type: 'filter';
|
|
23
|
+
name: 'endBefore' | 'startAfter';
|
|
24
|
+
value: QueryFilterValue;
|
|
25
|
+
key?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function isQueryFilterValue(value: unknown): value is QueryFilterValue {
|
|
29
|
+
return (
|
|
30
|
+
typeof value === 'number' ||
|
|
31
|
+
typeof value === 'string' ||
|
|
32
|
+
typeof value === 'boolean' ||
|
|
33
|
+
value === null
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function getQueryInstance(
|
|
38
|
+
dbRef: WebDatabaseReference,
|
|
39
|
+
modifiers: WebDatabaseQueryModifier[],
|
|
40
|
+
): ReturnType<typeof query> {
|
|
41
|
+
const constraints: WebQueryConstraint[] = [];
|
|
42
|
+
|
|
43
|
+
for (const modifier of modifiers) {
|
|
44
|
+
const { type, name } = modifier;
|
|
45
|
+
|
|
46
|
+
if (type === 'orderBy') {
|
|
47
|
+
switch (name) {
|
|
48
|
+
case 'orderByKey':
|
|
49
|
+
constraints.push(orderByKey());
|
|
50
|
+
break;
|
|
51
|
+
case 'orderByPriority':
|
|
52
|
+
constraints.push(orderByPriority());
|
|
53
|
+
break;
|
|
54
|
+
case 'orderByValue':
|
|
55
|
+
constraints.push(orderByValue());
|
|
56
|
+
break;
|
|
57
|
+
case 'orderByChild':
|
|
58
|
+
constraints.push(orderByChild(modifier.key ?? ''));
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (type === 'limit') {
|
|
64
|
+
const { value } = modifier;
|
|
65
|
+
|
|
66
|
+
switch (name) {
|
|
67
|
+
case 'limitToLast':
|
|
68
|
+
constraints.push(limitToLast(value));
|
|
69
|
+
break;
|
|
70
|
+
case 'limitToFirst':
|
|
71
|
+
constraints.push(limitToFirst(value));
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (type === 'filter' && isQueryFilterValue(modifier.value)) {
|
|
77
|
+
const { key, value } = modifier;
|
|
78
|
+
|
|
79
|
+
switch (name) {
|
|
80
|
+
case 'endAt':
|
|
81
|
+
constraints.push(endAt(value, key));
|
|
82
|
+
break;
|
|
83
|
+
case 'endBefore':
|
|
84
|
+
constraints.push(endBefore(value, key));
|
|
85
|
+
break;
|
|
86
|
+
case 'startAt':
|
|
87
|
+
constraints.push(startAt(value, key));
|
|
88
|
+
break;
|
|
89
|
+
case 'startAfter':
|
|
90
|
+
constraints.push(startAfter(value, key));
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return query(dbRef, ...constraints);
|
|
97
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-firebase/database",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "25.0.1",
|
|
4
4
|
"author": "Invertase <oss@invertase.io> (http://invertase.io)",
|
|
5
5
|
"description": "React Native Firebase - The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. React Native Firebase provides native integration with the Android & iOS Firebase SDKs, supporting both realtime data sync and offline capabilities.",
|
|
6
|
-
"main": "
|
|
7
|
-
"types": "lib/index.d.ts",
|
|
6
|
+
"main": "./dist/module/index.js",
|
|
7
|
+
"types": "./dist/typescript/lib/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build": "genversion --semi lib/version.
|
|
9
|
+
"build": "genversion --esm --semi lib/version.ts",
|
|
10
10
|
"build:clean": "rimraf android/build && rimraf ios/build",
|
|
11
|
-
"
|
|
11
|
+
"compile": "bob build",
|
|
12
|
+
"prepare": "yarn run build && yarn compile"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
@@ -25,11 +26,45 @@
|
|
|
25
26
|
"realtome database"
|
|
26
27
|
],
|
|
27
28
|
"peerDependencies": {
|
|
28
|
-
"@react-native-firebase/app": "
|
|
29
|
+
"@react-native-firebase/app": "25.0.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"react-native-builder-bob": "^0.41.0"
|
|
29
33
|
},
|
|
30
34
|
"publishConfig": {
|
|
31
35
|
"access": "public",
|
|
32
36
|
"provenance": true
|
|
33
37
|
},
|
|
34
|
-
"
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"source": "./lib/index.ts",
|
|
41
|
+
"types": "./dist/typescript/lib/index.d.ts",
|
|
42
|
+
"default": "./dist/module/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"react-native-builder-bob": {
|
|
47
|
+
"source": "lib",
|
|
48
|
+
"output": "dist",
|
|
49
|
+
"exclude": "**/*.d.ts",
|
|
50
|
+
"targets": [
|
|
51
|
+
[
|
|
52
|
+
"module",
|
|
53
|
+
{
|
|
54
|
+
"esm": true
|
|
55
|
+
}
|
|
56
|
+
],
|
|
57
|
+
[
|
|
58
|
+
"typescript",
|
|
59
|
+
{
|
|
60
|
+
"tsc": "../../node_modules/.bin/tsc"
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
"eslintIgnore": [
|
|
66
|
+
"node_modules/",
|
|
67
|
+
"dist/"
|
|
68
|
+
],
|
|
69
|
+
"gitHead": "8c1c22f54a052ea8151b66ce079caf06e7703d4a"
|
|
35
70
|
}
|