@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
|
@@ -1,558 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getApp,
|
|
3
|
-
getDatabase,
|
|
4
|
-
connectDatabaseEmulator,
|
|
5
|
-
enableLogging,
|
|
6
|
-
goOnline,
|
|
7
|
-
goOffline,
|
|
8
|
-
ref,
|
|
9
|
-
set,
|
|
10
|
-
update,
|
|
11
|
-
setWithPriority,
|
|
12
|
-
remove,
|
|
13
|
-
setPriority,
|
|
14
|
-
onDisconnect,
|
|
15
|
-
onValue,
|
|
16
|
-
onChildAdded,
|
|
17
|
-
onChildChanged,
|
|
18
|
-
onChildMoved,
|
|
19
|
-
onChildRemoved,
|
|
20
|
-
runTransaction,
|
|
21
|
-
} from '@react-native-firebase/app/dist/module/internal/web/firebaseDatabase';
|
|
22
|
-
import {
|
|
23
|
-
guard,
|
|
24
|
-
getWebError,
|
|
25
|
-
emitEvent,
|
|
26
|
-
} from '@react-native-firebase/app/dist/module/internal/web/utils';
|
|
27
|
-
import { getQueryInstance } from './query';
|
|
28
|
-
|
|
29
|
-
function rejectWithCodeAndMessage(code, message) {
|
|
30
|
-
return Promise.reject(
|
|
31
|
-
getWebError({
|
|
32
|
-
code,
|
|
33
|
-
message,
|
|
34
|
-
}),
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Converts a DataSnapshot to an object.
|
|
39
|
-
function snapshotToObject(snapshot) {
|
|
40
|
-
const childKeys = [];
|
|
41
|
-
|
|
42
|
-
if (snapshot.hasChildren()) {
|
|
43
|
-
snapshot.forEach(childSnapshot => {
|
|
44
|
-
childKeys.push(childSnapshot.key);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
key: snapshot.key,
|
|
50
|
-
exists: snapshot.exists(),
|
|
51
|
-
hasChildren: snapshot.hasChildren(),
|
|
52
|
-
childrenCount: snapshot.size,
|
|
53
|
-
childKeys,
|
|
54
|
-
priority: snapshot.priority,
|
|
55
|
-
value: snapshot.val(),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function getDatabaseWebError(error) {
|
|
60
|
-
// Possible to override messages/codes here if necessary.
|
|
61
|
-
return getWebError(error);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Converts a DataSnapshot and previous child name to an object.
|
|
65
|
-
function snapshotWithPreviousChildToObject(snapshot, previousChildName) {
|
|
66
|
-
return {
|
|
67
|
-
snapshot: snapshotToObject(snapshot),
|
|
68
|
-
previousChildName,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const appInstances = {};
|
|
73
|
-
const databaseInstances = {};
|
|
74
|
-
const onDisconnectRef = {};
|
|
75
|
-
const listeners = {};
|
|
76
|
-
const emulatorForApp = {};
|
|
77
|
-
|
|
78
|
-
function getCachedAppInstance(appName) {
|
|
79
|
-
return (appInstances[appName] ??= getApp(appName));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Returns a cached Database instance.
|
|
83
|
-
function getCachedDatabaseInstance(appName, dbURL) {
|
|
84
|
-
let instance = databaseInstances[`${appName}|${dbURL}`];
|
|
85
|
-
if (!instance) {
|
|
86
|
-
instance = getDatabase(getCachedAppInstance(appName), dbURL);
|
|
87
|
-
// Relying on internals here so need to be careful between SDK versions.
|
|
88
|
-
if (emulatorForApp[appName] && !instance._instanceStarted) {
|
|
89
|
-
const { host, port } = emulatorForApp[appName];
|
|
90
|
-
connectDatabaseEmulator(instance, host, port);
|
|
91
|
-
emulatorForApp[appName].connected = true;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return instance;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Returns a cached onDisconnect instance.
|
|
98
|
-
function getCachedOnDisconnectInstance(ref) {
|
|
99
|
-
return (onDisconnectRef[ref.key] ??= onDisconnect(ref));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export default {
|
|
103
|
-
/**
|
|
104
|
-
* Reconnects to the server.
|
|
105
|
-
* @param {string} appName - The app name.
|
|
106
|
-
* @param {string} dbURL - The database URL.
|
|
107
|
-
* @returns {Promise<void>}
|
|
108
|
-
*/
|
|
109
|
-
goOnline(appName, dbURL) {
|
|
110
|
-
return guard(async () => {
|
|
111
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
112
|
-
goOnline(db);
|
|
113
|
-
});
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Disconnects from the server.
|
|
118
|
-
* @param {string} appName - The app name.
|
|
119
|
-
* @param {string} dbURL - The database URL.
|
|
120
|
-
* @returns {Promise<void>}
|
|
121
|
-
*/
|
|
122
|
-
goOffline(appName, dbURL) {
|
|
123
|
-
return guard(async () => {
|
|
124
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
125
|
-
goOffline(db);
|
|
126
|
-
});
|
|
127
|
-
},
|
|
128
|
-
|
|
129
|
-
setPersistenceEnabled() {
|
|
130
|
-
if (__DEV__) {
|
|
131
|
-
// eslint-disable-next-line no-console
|
|
132
|
-
console.warn(
|
|
133
|
-
'The Firebase Database `setPersistenceEnabled` method is not available in the this environment.',
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
return Promise.resolve();
|
|
137
|
-
},
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Sets the logging enabled state.
|
|
141
|
-
* @param {string} appName - The app name, not used.
|
|
142
|
-
* @param {string} dbURL - The database URL, not used.
|
|
143
|
-
* @param {boolean} enabled - The logging enabled state.
|
|
144
|
-
*/
|
|
145
|
-
setLoggingEnabled(_app, _dbURL, enabled) {
|
|
146
|
-
return guard(async () => {
|
|
147
|
-
enableLogging(enabled);
|
|
148
|
-
});
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
setPersistenceCacheSizeBytes() {
|
|
152
|
-
// no-op on other platforms
|
|
153
|
-
return Promise.resolve();
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Connects to the Firebase database emulator.
|
|
158
|
-
* @param {string} appName - The app name.
|
|
159
|
-
* @param {string} dbURL - The database URL.
|
|
160
|
-
* @param {string} host - The emulator host.
|
|
161
|
-
* @param {number} port - The emulator
|
|
162
|
-
* @returns {Promise<void>}
|
|
163
|
-
*/
|
|
164
|
-
useEmulator(appName, dbURL, host, port) {
|
|
165
|
-
return guard(async () => {
|
|
166
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
167
|
-
connectDatabaseEmulator(db, host, port);
|
|
168
|
-
emulatorForApp[appName] = { host, port };
|
|
169
|
-
});
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Reference
|
|
174
|
-
*/
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Sets a value at the specified path.
|
|
178
|
-
* @param {string} appName - The app name.
|
|
179
|
-
* @param {string} dbURL - The database URL.
|
|
180
|
-
* @param {string} path - The path.
|
|
181
|
-
* @param {object} props - The properties
|
|
182
|
-
* @returns {Promise<void>}
|
|
183
|
-
*/
|
|
184
|
-
set(appName, dbURL, path, props) {
|
|
185
|
-
return guard(async () => {
|
|
186
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
187
|
-
const dbRef = ref(db, path);
|
|
188
|
-
const value = props.value;
|
|
189
|
-
await set(dbRef, value);
|
|
190
|
-
});
|
|
191
|
-
},
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Updates the specified path with the provided values.
|
|
195
|
-
* @param {string} appName - The app name.
|
|
196
|
-
* @param {string} dbURL - The database URL.
|
|
197
|
-
* @param {string} path - The path.
|
|
198
|
-
* @param {object} props - The properties
|
|
199
|
-
* @returns {Promise<void>}
|
|
200
|
-
*/
|
|
201
|
-
update(appName, dbURL, path, props) {
|
|
202
|
-
return guard(async () => {
|
|
203
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
204
|
-
const dbRef = ref(db, path);
|
|
205
|
-
const values = props.values;
|
|
206
|
-
await update(dbRef, values);
|
|
207
|
-
});
|
|
208
|
-
},
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Sets a value at the specified path with a priority.
|
|
212
|
-
* @param {string} appName - The app name.
|
|
213
|
-
* @param {string} dbURL - The database URL.
|
|
214
|
-
* @param {string} path - The path.
|
|
215
|
-
* @param {object} props - The properties, including value and priority.
|
|
216
|
-
* @returns {Promise<void>}
|
|
217
|
-
*/
|
|
218
|
-
setWithPriority(appName, dbURL, path, props) {
|
|
219
|
-
return guard(async () => {
|
|
220
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
221
|
-
const dbRef = ref(db, path);
|
|
222
|
-
const value = props.value;
|
|
223
|
-
const priority = props.priority;
|
|
224
|
-
await setWithPriority(dbRef, value, priority);
|
|
225
|
-
});
|
|
226
|
-
},
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Removes the nodd at the specified path.
|
|
230
|
-
* @param {string} appName - The app name.
|
|
231
|
-
* @param {string} dbURL - The database URL.
|
|
232
|
-
* @param {string} path - The path.
|
|
233
|
-
* @returns {Promise<void>}
|
|
234
|
-
*/
|
|
235
|
-
remove(appName, dbURL, path) {
|
|
236
|
-
return guard(async () => {
|
|
237
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
238
|
-
const dbRef = ref(db, path);
|
|
239
|
-
await remove(dbRef);
|
|
240
|
-
});
|
|
241
|
-
},
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Sets the priority of the node at the specified path.
|
|
245
|
-
* @param {string} appName - The app name.
|
|
246
|
-
* @param {string} dbURL - The database URL.
|
|
247
|
-
* @param {string} path - The path.
|
|
248
|
-
* @param {object} props - The properties, including priority.
|
|
249
|
-
* @returns {Promise<void>}
|
|
250
|
-
*/
|
|
251
|
-
setPriority(appName, dbURL, path, props) {
|
|
252
|
-
return guard(async () => {
|
|
253
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
254
|
-
const dbRef = ref(db, path);
|
|
255
|
-
const priority = props.priority;
|
|
256
|
-
await setPriority(dbRef, priority);
|
|
257
|
-
});
|
|
258
|
-
},
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Query
|
|
262
|
-
*/
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Listens for data changes at the specified path once.
|
|
266
|
-
* @param {string} appName - The app name.
|
|
267
|
-
* @param {string} dbURL - The database URL.
|
|
268
|
-
* @param {string} path - The path.
|
|
269
|
-
* @param {object} modifiers - The modifiers.
|
|
270
|
-
* @param {string} eventType - The event type.
|
|
271
|
-
* @returns {Promise<object>}
|
|
272
|
-
*/
|
|
273
|
-
once(appName, dbURL, path, modifiers, eventType) {
|
|
274
|
-
return guard(async () => {
|
|
275
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
276
|
-
const dbRef = ref(db, path);
|
|
277
|
-
const queryRef = getQueryInstance(dbRef, modifiers);
|
|
278
|
-
|
|
279
|
-
if (eventType === 'value') {
|
|
280
|
-
const snapshot = await new Promise((resolve, reject) => {
|
|
281
|
-
onValue(queryRef, resolve, reject, { onlyOnce: true });
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
return snapshotToObject(snapshot);
|
|
285
|
-
} else {
|
|
286
|
-
let fn = null;
|
|
287
|
-
|
|
288
|
-
if (eventType === 'child_added') {
|
|
289
|
-
fn = onChildAdded;
|
|
290
|
-
} else if (eventType === 'child_changed') {
|
|
291
|
-
fn = onChildChanged;
|
|
292
|
-
} else if (eventType === 'child_removed') {
|
|
293
|
-
fn = onChildRemoved;
|
|
294
|
-
} else if (eventType === 'child_moved') {
|
|
295
|
-
fn = onChildMoved;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (fn) {
|
|
299
|
-
const { snapshot, previousChildName } = await new Promise((resolve, reject) => {
|
|
300
|
-
fn(
|
|
301
|
-
queryRef,
|
|
302
|
-
(snapshot, previousChildName) => {
|
|
303
|
-
resolve({ snapshot, previousChildName });
|
|
304
|
-
},
|
|
305
|
-
reject,
|
|
306
|
-
{ onlyOnce: true },
|
|
307
|
-
);
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
return snapshotWithPreviousChildToObject(snapshot, previousChildName);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const snapshot = await get(dbRef, modifiers);
|
|
315
|
-
return snapshot;
|
|
316
|
-
});
|
|
317
|
-
},
|
|
318
|
-
|
|
319
|
-
on(appName, dbURL, props) {
|
|
320
|
-
return guard(async () => {
|
|
321
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
322
|
-
const { key, modifiers, path, eventType, registration } = props;
|
|
323
|
-
const { eventRegistrationKey } = registration;
|
|
324
|
-
const dbRef = ref(db, path);
|
|
325
|
-
|
|
326
|
-
const queryRef = getQueryInstance(dbRef, modifiers);
|
|
327
|
-
|
|
328
|
-
function sendEvent(data) {
|
|
329
|
-
const event = {
|
|
330
|
-
eventName: 'database_sync_event',
|
|
331
|
-
body: {
|
|
332
|
-
data,
|
|
333
|
-
key,
|
|
334
|
-
registration,
|
|
335
|
-
eventType,
|
|
336
|
-
},
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
emitEvent('database_sync_event', event);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
function sendError(error) {
|
|
343
|
-
const event = {
|
|
344
|
-
eventName: 'database_sync_event',
|
|
345
|
-
body: {
|
|
346
|
-
key,
|
|
347
|
-
registration,
|
|
348
|
-
error: getDatabaseWebError(error),
|
|
349
|
-
},
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
emitEvent('database_sync_event', event);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
let listener = null;
|
|
356
|
-
|
|
357
|
-
// Ignore if the listener already exists.
|
|
358
|
-
if (listeners[eventRegistrationKey]) {
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if (eventType === 'value') {
|
|
363
|
-
listener = onValue(queryRef, snapshot => sendEvent(snapshotToObject(snapshot)), sendError);
|
|
364
|
-
} else {
|
|
365
|
-
let fn = null;
|
|
366
|
-
|
|
367
|
-
if (eventType === 'child_added') {
|
|
368
|
-
fn = onChildAdded;
|
|
369
|
-
} else if (eventType === 'child_changed') {
|
|
370
|
-
fn = onChildChanged;
|
|
371
|
-
} else if (eventType === 'child_removed') {
|
|
372
|
-
fn = onChildRemoved;
|
|
373
|
-
} else if (eventType === 'child_moved') {
|
|
374
|
-
fn = onChildMoved;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (fn) {
|
|
378
|
-
listener = fn(
|
|
379
|
-
queryRef,
|
|
380
|
-
(snapshot, previousChildName) => {
|
|
381
|
-
sendEvent(snapshotWithPreviousChildToObject(snapshot, previousChildName));
|
|
382
|
-
},
|
|
383
|
-
sendError,
|
|
384
|
-
);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
listeners[eventRegistrationKey] = listener;
|
|
389
|
-
});
|
|
390
|
-
},
|
|
391
|
-
|
|
392
|
-
off(_queryKey, eventRegistrationKey) {
|
|
393
|
-
const listener = listeners[eventRegistrationKey];
|
|
394
|
-
if (listener) {
|
|
395
|
-
listener();
|
|
396
|
-
delete listeners[eventRegistrationKey];
|
|
397
|
-
}
|
|
398
|
-
},
|
|
399
|
-
|
|
400
|
-
keepSynced() {
|
|
401
|
-
return rejectWithCodeAndMessage(
|
|
402
|
-
'unsupported',
|
|
403
|
-
'This operation is not supported on this environment.',
|
|
404
|
-
);
|
|
405
|
-
},
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* OnDisconnect
|
|
409
|
-
*/
|
|
410
|
-
|
|
411
|
-
/**
|
|
412
|
-
* Cancels the onDisconnect instance at the specified path.
|
|
413
|
-
* @param {string} appName - The app name.
|
|
414
|
-
* @param {string} dbURL - The database URL.
|
|
415
|
-
* @param {string} path - The path.
|
|
416
|
-
* @returns {Promise<void>}
|
|
417
|
-
*/
|
|
418
|
-
onDisconnectCancel(appName, dbURL, path) {
|
|
419
|
-
return guard(async () => {
|
|
420
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
421
|
-
const dbRef = ref(db, path);
|
|
422
|
-
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
423
|
-
await instance.cancel();
|
|
424
|
-
|
|
425
|
-
// Delete the onDisconnect instance from the cache.
|
|
426
|
-
delete onDisconnectRef[dbRef.key];
|
|
427
|
-
});
|
|
428
|
-
},
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* Sets a value to be written to the database on disconnect.
|
|
432
|
-
* @param {string} appName - The app name.
|
|
433
|
-
* @param {string} dbURL - The database URL.
|
|
434
|
-
* @param {string} path - The path.
|
|
435
|
-
* @returns {Promise<void>}
|
|
436
|
-
*/
|
|
437
|
-
onDisconnectRemove(appName, dbURL, path) {
|
|
438
|
-
return guard(async () => {
|
|
439
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
440
|
-
const dbRef = ref(db, path);
|
|
441
|
-
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
442
|
-
await instance.remove();
|
|
443
|
-
});
|
|
444
|
-
},
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Sets a value to be written to the database on disconnect.
|
|
448
|
-
* @param {string} appName - The app name.
|
|
449
|
-
* @param {string} dbURL - The database URL.
|
|
450
|
-
* @param {string} path - The path.
|
|
451
|
-
* @param {object} props - The properties, including value.
|
|
452
|
-
* @returns {Promise<void>}
|
|
453
|
-
*/
|
|
454
|
-
onDisconnectSet(appName, dbURL, path, props) {
|
|
455
|
-
return guard(async () => {
|
|
456
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
457
|
-
const dbRef = ref(db, path);
|
|
458
|
-
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
459
|
-
const value = props.value;
|
|
460
|
-
await instance.set(value);
|
|
461
|
-
});
|
|
462
|
-
},
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* Sets a value to be written to the database on disconnect with a priority.
|
|
466
|
-
* @param {string} appName - The app name.
|
|
467
|
-
* @param {string} dbURL - The database URL.
|
|
468
|
-
* @param {string} path - The path.
|
|
469
|
-
* @param {object} props - The properties, including value and priority.
|
|
470
|
-
* @returns {Promise<void>}
|
|
471
|
-
*/
|
|
472
|
-
onDisconnectSetWithPriority(appName, dbURL, path, props) {
|
|
473
|
-
return guard(async () => {
|
|
474
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
475
|
-
const dbRef = ref(db, path);
|
|
476
|
-
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
477
|
-
const value = props.value;
|
|
478
|
-
const priority = props.priority;
|
|
479
|
-
await instance.setWithPriority(value, priority);
|
|
480
|
-
});
|
|
481
|
-
},
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Updates the specified path with the provided values on disconnect.
|
|
485
|
-
* @param {string} appName - The app name.
|
|
486
|
-
* @param {string} dbURL - The database URL.
|
|
487
|
-
* @param {string} path - The path.
|
|
488
|
-
* @param {object} props - The properties, including values.
|
|
489
|
-
* @returns {Promise<void>}
|
|
490
|
-
*/
|
|
491
|
-
onDisconnectUpdate(appName, dbURL, path, props) {
|
|
492
|
-
return guard(async () => {
|
|
493
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
494
|
-
const dbRef = ref(db, path);
|
|
495
|
-
const instance = getCachedOnDisconnectInstance(dbRef);
|
|
496
|
-
const values = props.values;
|
|
497
|
-
await instance.update(values);
|
|
498
|
-
});
|
|
499
|
-
},
|
|
500
|
-
|
|
501
|
-
/**
|
|
502
|
-
* Transaction
|
|
503
|
-
*/
|
|
504
|
-
|
|
505
|
-
transactionStart(appName, dbURL, path, transactionId, applyLocally, userExecutor) {
|
|
506
|
-
return guard(async () => {
|
|
507
|
-
const db = getCachedDatabaseInstance(appName, dbURL);
|
|
508
|
-
const dbRef = ref(db, path);
|
|
509
|
-
|
|
510
|
-
try {
|
|
511
|
-
const { committed, snapshot } = await runTransaction(dbRef, userExecutor, {
|
|
512
|
-
applyLocally,
|
|
513
|
-
});
|
|
514
|
-
|
|
515
|
-
const event = {
|
|
516
|
-
body: {
|
|
517
|
-
committed,
|
|
518
|
-
type: 'complete',
|
|
519
|
-
snapshot: snapshotToObject(snapshot),
|
|
520
|
-
},
|
|
521
|
-
appName,
|
|
522
|
-
id: transactionId,
|
|
523
|
-
eventName: 'database_transaction_event',
|
|
524
|
-
};
|
|
525
|
-
|
|
526
|
-
emitEvent('database_transaction_event', event);
|
|
527
|
-
} catch (e) {
|
|
528
|
-
const event = {
|
|
529
|
-
body: {
|
|
530
|
-
committed: false,
|
|
531
|
-
type: 'error',
|
|
532
|
-
error: getDatabaseWebError(e),
|
|
533
|
-
},
|
|
534
|
-
appName,
|
|
535
|
-
id: transactionId,
|
|
536
|
-
eventName: 'database_transaction_event',
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
emitEvent('database_transaction_event', event);
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
},
|
|
543
|
-
|
|
544
|
-
/**
|
|
545
|
-
* Commits the transaction with the specified updates.
|
|
546
|
-
* @param {string} appName - The app name.
|
|
547
|
-
* @param {string} dbURL - The database URL.
|
|
548
|
-
* @param {string} transactionId - The transaction ID.
|
|
549
|
-
* @param {object} updates - The updates.
|
|
550
|
-
* @returns {Promise<void>}
|
|
551
|
-
*/
|
|
552
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
553
|
-
async transactionTryCommit(appName, dbURL, transactionId, updates) {
|
|
554
|
-
// We don't need to implement this as for 'Other' platforms
|
|
555
|
-
// we pass the users transaction function to the Firebase JS SDK directly.
|
|
556
|
-
throw new Error('Not implemented');
|
|
557
|
-
},
|
|
558
|
-
};
|