@firebase/database-compat 0.1.1-2021823172527 → 0.1.2-canary.0b3ca78eb

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +19 -8
  2. package/dist/database-compat/src/index.d.ts +0 -9
  3. package/dist/database-compat/src/index.node.d.ts +0 -53
  4. package/dist/database-compat/src/index.standalone.d.ts +52 -0
  5. package/dist/index.esm2017.js +5 -6
  6. package/dist/index.esm2017.js.map +1 -1
  7. package/dist/index.esm5.js +2 -3
  8. package/dist/index.esm5.js.map +1 -1
  9. package/dist/index.js +10 -77
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.standalone.js +15150 -0
  12. package/dist/index.standalone.js.map +1 -0
  13. package/dist/node-esm/database-compat/src/api/Database.d.ts +72 -0
  14. package/dist/node-esm/database-compat/src/api/Reference.d.ts +201 -0
  15. package/{karma.conf.js → dist/node-esm/database-compat/src/api/TransactionResult.d.ts} +26 -34
  16. package/dist/node-esm/database-compat/src/api/internal.d.ts +39 -0
  17. package/dist/node-esm/database-compat/src/api/onDisconnect.d.ts +27 -0
  18. package/dist/node-esm/database-compat/src/index.d.ts +32 -0
  19. package/dist/node-esm/database-compat/src/index.node.d.ts +14 -0
  20. package/dist/node-esm/database-compat/src/index.standalone.d.ts +52 -0
  21. package/{src/util/util.ts → dist/node-esm/database-compat/src/util/util.d.ts} +17 -25
  22. package/dist/node-esm/database-compat/src/util/validation.d.ts +18 -0
  23. package/{src/api/TransactionResult.ts → dist/node-esm/database-compat/test/browser/crawler_support.test.d.ts} +17 -34
  24. package/dist/node-esm/database-compat/test/database.test.d.ts +17 -0
  25. package/dist/node-esm/database-compat/test/datasnapshot.test.d.ts +17 -0
  26. package/dist/node-esm/database-compat/test/helpers/events.d.ts +34 -0
  27. package/dist/node-esm/database-compat/test/helpers/util.d.ts +42 -0
  28. package/dist/node-esm/database-compat/test/info.test.d.ts +17 -0
  29. package/dist/node-esm/database-compat/test/order.test.d.ts +17 -0
  30. package/dist/node-esm/database-compat/test/order_by.test.d.ts +17 -0
  31. package/dist/node-esm/database-compat/test/promise.test.d.ts +17 -0
  32. package/dist/node-esm/database-compat/test/query.test.d.ts +17 -0
  33. package/dist/node-esm/database-compat/test/servervalues.test.d.ts +17 -0
  34. package/dist/node-esm/database-compat/test/transaction.test.d.ts +17 -0
  35. package/dist/node-esm/index.js +868 -0
  36. package/dist/node-esm/index.js.map +1 -0
  37. package/dist/node-esm/package.json +1 -0
  38. package/package.json +34 -9
  39. package/standalone/package.json +7 -0
  40. package/.eslintrc.js +0 -64
  41. package/rollup.config.js +0 -125
  42. package/src/api/Database.ts +0 -123
  43. package/src/api/Reference.ts +0 -790
  44. package/src/api/internal.ts +0 -91
  45. package/src/api/onDisconnect.ts +0 -112
  46. package/src/index.node.ts +0 -142
  47. package/src/index.ts +0 -88
  48. package/src/util/validation.ts +0 -59
  49. package/test/browser/crawler_support.test.ts +0 -206
  50. package/test/database.test.ts +0 -306
  51. package/test/datasnapshot.test.ts +0 -249
  52. package/test/helpers/events.ts +0 -258
  53. package/test/helpers/util.ts +0 -177
  54. package/test/info.test.ts +0 -222
  55. package/test/order.test.ts +0 -592
  56. package/test/order_by.test.ts +0 -530
  57. package/test/promise.test.ts +0 -261
  58. package/test/query.test.ts +0 -4670
  59. package/test/servervalues.test.ts +0 -152
  60. package/test/transaction.test.ts +0 -1534
  61. package/tsconfig.json +0 -11
@@ -1,790 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2017 Google LLC
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
-
18
- import {
19
- OnDisconnect as ModularOnDisconnect,
20
- off,
21
- onChildAdded,
22
- onChildChanged,
23
- onChildMoved,
24
- onChildRemoved,
25
- onValue,
26
- EventType,
27
- limitToFirst,
28
- query,
29
- limitToLast,
30
- orderByChild,
31
- orderByKey,
32
- orderByValue,
33
- orderByPriority,
34
- startAt,
35
- startAfter,
36
- endAt,
37
- endBefore,
38
- equalTo,
39
- get,
40
- set,
41
- update,
42
- setWithPriority,
43
- remove,
44
- setPriority,
45
- push,
46
- runTransaction,
47
- child,
48
- DataSnapshot as ModularDataSnapshot,
49
- Query as ExpQuery,
50
- DatabaseReference as ModularReference,
51
- _QueryImpl,
52
- _ReferenceImpl,
53
- _validatePathString,
54
- _validateWritablePath,
55
- _UserCallback,
56
- _QueryParams
57
- } from '@firebase/database';
58
- import {
59
- Compat,
60
- Deferred,
61
- errorPrefix,
62
- validateArgCount,
63
- validateCallback,
64
- validateContextObject
65
- } from '@firebase/util';
66
-
67
- import { warn } from '../util/util';
68
- import { validateBoolean, validateEventType } from '../util/validation';
69
-
70
- import { Database } from './Database';
71
- import { OnDisconnect } from './onDisconnect';
72
- import { TransactionResult } from './TransactionResult';
73
-
74
- /**
75
- * Class representing a firebase data snapshot. It wraps a SnapshotNode and
76
- * surfaces the public methods (val, forEach, etc.) we want to expose.
77
- */
78
- export class DataSnapshot implements Compat<ModularDataSnapshot> {
79
- constructor(
80
- readonly _database: Database,
81
- readonly _delegate: ModularDataSnapshot
82
- ) {}
83
-
84
- /**
85
- * Retrieves the snapshot contents as JSON. Returns null if the snapshot is
86
- * empty.
87
- *
88
- * @returns JSON representation of the DataSnapshot contents, or null if empty.
89
- */
90
- val(): unknown {
91
- validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
92
- return this._delegate.val();
93
- }
94
-
95
- /**
96
- * Returns the snapshot contents as JSON, including priorities of node. Suitable for exporting
97
- * the entire node contents.
98
- * @returns JSON representation of the DataSnapshot contents, or null if empty.
99
- */
100
- exportVal(): unknown {
101
- validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
102
- return this._delegate.exportVal();
103
- }
104
-
105
- // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
106
- // for end-users
107
- toJSON(): unknown {
108
- // Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
109
- validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
110
- return this._delegate.toJSON();
111
- }
112
-
113
- /**
114
- * Returns whether the snapshot contains a non-null value.
115
- *
116
- * @returns Whether the snapshot contains a non-null value, or is empty.
117
- */
118
- exists(): boolean {
119
- validateArgCount('DataSnapshot.exists', 0, 0, arguments.length);
120
- return this._delegate.exists();
121
- }
122
-
123
- /**
124
- * Returns a DataSnapshot of the specified child node's contents.
125
- *
126
- * @param path - Path to a child.
127
- * @returns DataSnapshot for child node.
128
- */
129
- child(path: string): DataSnapshot {
130
- validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
131
- // Ensure the childPath is a string (can be a number)
132
- path = String(path);
133
- _validatePathString('DataSnapshot.child', 'path', path, false);
134
- return new DataSnapshot(this._database, this._delegate.child(path));
135
- }
136
-
137
- /**
138
- * Returns whether the snapshot contains a child at the specified path.
139
- *
140
- * @param path - Path to a child.
141
- * @returns Whether the child exists.
142
- */
143
- hasChild(path: string): boolean {
144
- validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
145
- _validatePathString('DataSnapshot.hasChild', 'path', path, false);
146
- return this._delegate.hasChild(path);
147
- }
148
-
149
- /**
150
- * Returns the priority of the object, or null if no priority was set.
151
- *
152
- * @returns The priority.
153
- */
154
- getPriority(): string | number | null {
155
- validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
156
- return this._delegate.priority;
157
- }
158
-
159
- /**
160
- * Iterates through child nodes and calls the specified action for each one.
161
- *
162
- * @param action - Callback function to be called
163
- * for each child.
164
- * @returns True if forEach was canceled by action returning true for
165
- * one of the child nodes.
166
- */
167
- forEach(action: (snapshot: DataSnapshot) => boolean | void): boolean {
168
- validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
169
- validateCallback('DataSnapshot.forEach', 'action', action, false);
170
- return this._delegate.forEach(expDataSnapshot =>
171
- action(new DataSnapshot(this._database, expDataSnapshot))
172
- );
173
- }
174
-
175
- /**
176
- * Returns whether this DataSnapshot has children.
177
- * @returns True if the DataSnapshot contains 1 or more child nodes.
178
- */
179
- hasChildren(): boolean {
180
- validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
181
- return this._delegate.hasChildren();
182
- }
183
-
184
- get key() {
185
- return this._delegate.key;
186
- }
187
-
188
- /**
189
- * Returns the number of children for this DataSnapshot.
190
- * @returns The number of children that this DataSnapshot contains.
191
- */
192
- numChildren(): number {
193
- validateArgCount('DataSnapshot.numChildren', 0, 0, arguments.length);
194
- return this._delegate.size;
195
- }
196
-
197
- /**
198
- * @returns The Firebase reference for the location this snapshot's data came
199
- * from.
200
- */
201
- getRef(): Reference {
202
- validateArgCount('DataSnapshot.ref', 0, 0, arguments.length);
203
- return new Reference(this._database, this._delegate.ref);
204
- }
205
-
206
- get ref(): Reference {
207
- return this.getRef();
208
- }
209
- }
210
-
211
- export interface SnapshotCallback {
212
- (dataSnapshot: DataSnapshot, previousChildName?: string | null): unknown;
213
- }
214
-
215
- /**
216
- * A Query represents a filter to be applied to a firebase location. This object purely represents the
217
- * query expression (and exposes our public API to build the query). The actual query logic is in ViewBase.js.
218
- *
219
- * Since every Firebase reference is a query, Firebase inherits from this object.
220
- */
221
- export class Query implements Compat<ExpQuery> {
222
- constructor(readonly database: Database, readonly _delegate: ExpQuery) {}
223
-
224
- on(
225
- eventType: string,
226
- callback: SnapshotCallback,
227
- cancelCallbackOrContext?: ((a: Error) => unknown) | object | null,
228
- context?: object | null
229
- ): SnapshotCallback {
230
- validateArgCount('Query.on', 2, 4, arguments.length);
231
- validateCallback('Query.on', 'callback', callback, false);
232
-
233
- const ret = Query.getCancelAndContextArgs_(
234
- 'Query.on',
235
- cancelCallbackOrContext,
236
- context
237
- );
238
- const valueCallback = (expSnapshot, previousChildName?) => {
239
- callback.call(
240
- ret.context,
241
- new DataSnapshot(this.database, expSnapshot),
242
- previousChildName
243
- );
244
- };
245
- valueCallback.userCallback = callback;
246
- valueCallback.context = ret.context;
247
- const cancelCallback = ret.cancel?.bind(ret.context);
248
-
249
- switch (eventType) {
250
- case 'value':
251
- onValue(this._delegate, valueCallback, cancelCallback);
252
- return callback;
253
- case 'child_added':
254
- onChildAdded(this._delegate, valueCallback, cancelCallback);
255
- return callback;
256
- case 'child_removed':
257
- onChildRemoved(this._delegate, valueCallback, cancelCallback);
258
- return callback;
259
- case 'child_changed':
260
- onChildChanged(this._delegate, valueCallback, cancelCallback);
261
- return callback;
262
- case 'child_moved':
263
- onChildMoved(this._delegate, valueCallback, cancelCallback);
264
- return callback;
265
- default:
266
- throw new Error(
267
- errorPrefix('Query.on', 'eventType') +
268
- 'must be a valid event type = "value", "child_added", "child_removed", ' +
269
- '"child_changed", or "child_moved".'
270
- );
271
- }
272
- }
273
-
274
- off(
275
- eventType?: string,
276
- callback?: SnapshotCallback,
277
- context?: object | null
278
- ): void {
279
- validateArgCount('Query.off', 0, 3, arguments.length);
280
- validateEventType('Query.off', eventType, true);
281
- validateCallback('Query.off', 'callback', callback, true);
282
- validateContextObject('Query.off', 'context', context, true);
283
- if (callback) {
284
- const valueCallback: _UserCallback = () => {};
285
- valueCallback.userCallback = callback;
286
- valueCallback.context = context;
287
- off(this._delegate, eventType as EventType, valueCallback);
288
- } else {
289
- off(this._delegate, eventType as EventType | undefined);
290
- }
291
- }
292
-
293
- /**
294
- * Get the server-value for this query, or return a cached value if not connected.
295
- */
296
- get(): Promise<DataSnapshot> {
297
- return get(this._delegate).then(expSnapshot => {
298
- return new DataSnapshot(this.database, expSnapshot);
299
- });
300
- }
301
-
302
- /**
303
- * Attaches a listener, waits for the first event, and then removes the listener
304
- */
305
- once(
306
- eventType: string,
307
- callback?: SnapshotCallback,
308
- failureCallbackOrContext?: ((a: Error) => void) | object | null,
309
- context?: object | null
310
- ): Promise<DataSnapshot> {
311
- validateArgCount('Query.once', 1, 4, arguments.length);
312
- validateCallback('Query.once', 'callback', callback, true);
313
-
314
- const ret = Query.getCancelAndContextArgs_(
315
- 'Query.once',
316
- failureCallbackOrContext,
317
- context
318
- );
319
- const deferred = new Deferred<DataSnapshot>();
320
- const valueCallback: _UserCallback = (expSnapshot, previousChildName?) => {
321
- const result = new DataSnapshot(this.database, expSnapshot);
322
- if (callback) {
323
- callback.call(ret.context, result, previousChildName);
324
- }
325
- deferred.resolve(result);
326
- };
327
- valueCallback.userCallback = callback;
328
- valueCallback.context = ret.context;
329
- const cancelCallback = (error: Error) => {
330
- if (ret.cancel) {
331
- ret.cancel.call(ret.context, error);
332
- }
333
- deferred.reject(error);
334
- };
335
-
336
- switch (eventType) {
337
- case 'value':
338
- onValue(this._delegate, valueCallback, cancelCallback, {
339
- onlyOnce: true
340
- });
341
- break;
342
- case 'child_added':
343
- onChildAdded(this._delegate, valueCallback, cancelCallback, {
344
- onlyOnce: true
345
- });
346
- break;
347
- case 'child_removed':
348
- onChildRemoved(this._delegate, valueCallback, cancelCallback, {
349
- onlyOnce: true
350
- });
351
- break;
352
- case 'child_changed':
353
- onChildChanged(this._delegate, valueCallback, cancelCallback, {
354
- onlyOnce: true
355
- });
356
- break;
357
- case 'child_moved':
358
- onChildMoved(this._delegate, valueCallback, cancelCallback, {
359
- onlyOnce: true
360
- });
361
- break;
362
- default:
363
- throw new Error(
364
- errorPrefix('Query.once', 'eventType') +
365
- 'must be a valid event type = "value", "child_added", "child_removed", ' +
366
- '"child_changed", or "child_moved".'
367
- );
368
- }
369
-
370
- return deferred.promise;
371
- }
372
-
373
- /**
374
- * Set a limit and anchor it to the start of the window.
375
- */
376
- limitToFirst(limit: number): Query {
377
- validateArgCount('Query.limitToFirst', 1, 1, arguments.length);
378
- return new Query(this.database, query(this._delegate, limitToFirst(limit)));
379
- }
380
-
381
- /**
382
- * Set a limit and anchor it to the end of the window.
383
- */
384
- limitToLast(limit: number): Query {
385
- validateArgCount('Query.limitToLast', 1, 1, arguments.length);
386
- return new Query(this.database, query(this._delegate, limitToLast(limit)));
387
- }
388
-
389
- /**
390
- * Given a child path, return a new query ordered by the specified grandchild path.
391
- */
392
- orderByChild(path: string): Query {
393
- validateArgCount('Query.orderByChild', 1, 1, arguments.length);
394
- return new Query(this.database, query(this._delegate, orderByChild(path)));
395
- }
396
-
397
- /**
398
- * Return a new query ordered by the KeyIndex
399
- */
400
- orderByKey(): Query {
401
- validateArgCount('Query.orderByKey', 0, 0, arguments.length);
402
- return new Query(this.database, query(this._delegate, orderByKey()));
403
- }
404
-
405
- /**
406
- * Return a new query ordered by the PriorityIndex
407
- */
408
- orderByPriority(): Query {
409
- validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
410
- return new Query(this.database, query(this._delegate, orderByPriority()));
411
- }
412
-
413
- /**
414
- * Return a new query ordered by the ValueIndex
415
- */
416
- orderByValue(): Query {
417
- validateArgCount('Query.orderByValue', 0, 0, arguments.length);
418
- return new Query(this.database, query(this._delegate, orderByValue()));
419
- }
420
-
421
- startAt(
422
- value: number | string | boolean | null = null,
423
- name?: string | null
424
- ): Query {
425
- validateArgCount('Query.startAt', 0, 2, arguments.length);
426
- return new Query(
427
- this.database,
428
- query(this._delegate, startAt(value, name))
429
- );
430
- }
431
-
432
- startAfter(
433
- value: number | string | boolean | null = null,
434
- name?: string | null
435
- ): Query {
436
- validateArgCount('Query.startAfter', 0, 2, arguments.length);
437
- return new Query(
438
- this.database,
439
- query(this._delegate, startAfter(value, name))
440
- );
441
- }
442
-
443
- endAt(
444
- value: number | string | boolean | null = null,
445
- name?: string | null
446
- ): Query {
447
- validateArgCount('Query.endAt', 0, 2, arguments.length);
448
- return new Query(this.database, query(this._delegate, endAt(value, name)));
449
- }
450
-
451
- endBefore(
452
- value: number | string | boolean | null = null,
453
- name?: string | null
454
- ): Query {
455
- validateArgCount('Query.endBefore', 0, 2, arguments.length);
456
- return new Query(
457
- this.database,
458
- query(this._delegate, endBefore(value, name))
459
- );
460
- }
461
-
462
- /**
463
- * Load the selection of children with exactly the specified value, and, optionally,
464
- * the specified name.
465
- */
466
- equalTo(value: number | string | boolean | null, name?: string) {
467
- validateArgCount('Query.equalTo', 1, 2, arguments.length);
468
- return new Query(
469
- this.database,
470
- query(this._delegate, equalTo(value, name))
471
- );
472
- }
473
-
474
- /**
475
- * @returns URL for this location.
476
- */
477
- toString(): string {
478
- validateArgCount('Query.toString', 0, 0, arguments.length);
479
- return this._delegate.toString();
480
- }
481
-
482
- // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
483
- // for end-users.
484
- toJSON() {
485
- // An optional spacer argument is unnecessary for a string.
486
- validateArgCount('Query.toJSON', 0, 1, arguments.length);
487
- return this._delegate.toJSON();
488
- }
489
-
490
- /**
491
- * Return true if this query and the provided query are equivalent; otherwise, return false.
492
- */
493
- isEqual(other: Query): boolean {
494
- validateArgCount('Query.isEqual', 1, 1, arguments.length);
495
- if (!(other instanceof Query)) {
496
- const error =
497
- 'Query.isEqual failed: First argument must be an instance of firebase.database.Query.';
498
- throw new Error(error);
499
- }
500
- return this._delegate.isEqual(other._delegate);
501
- }
502
-
503
- /**
504
- * Helper used by .on and .once to extract the context and or cancel arguments.
505
- * @param fnName - The function name (on or once)
506
- *
507
- */
508
- private static getCancelAndContextArgs_(
509
- fnName: string,
510
- cancelOrContext?: ((a: Error) => void) | object | null,
511
- context?: object | null
512
- ): { cancel: ((a: Error) => void) | undefined; context: object | undefined } {
513
- const ret: {
514
- cancel: ((a: Error) => void) | null;
515
- context: object | null;
516
- } = { cancel: undefined, context: undefined };
517
- if (cancelOrContext && context) {
518
- ret.cancel = cancelOrContext as (a: Error) => void;
519
- validateCallback(fnName, 'cancel', ret.cancel, true);
520
-
521
- ret.context = context;
522
- validateContextObject(fnName, 'context', ret.context, true);
523
- } else if (cancelOrContext) {
524
- // we have either a cancel callback or a context.
525
- if (typeof cancelOrContext === 'object' && cancelOrContext !== null) {
526
- // it's a context!
527
- ret.context = cancelOrContext;
528
- } else if (typeof cancelOrContext === 'function') {
529
- ret.cancel = cancelOrContext as (a: Error) => void;
530
- } else {
531
- throw new Error(
532
- errorPrefix(fnName, 'cancelOrContext') +
533
- ' must either be a cancel callback or a context object.'
534
- );
535
- }
536
- }
537
- return ret;
538
- }
539
-
540
- get ref(): Reference {
541
- return new Reference(
542
- this.database,
543
- new _ReferenceImpl(this._delegate._repo, this._delegate._path)
544
- );
545
- }
546
- }
547
-
548
- export class Reference extends Query implements Compat<ModularReference> {
549
- then: Promise<Reference>['then'];
550
- catch: Promise<Reference>['catch'];
551
-
552
- /**
553
- * Call options:
554
- * new Reference(Repo, Path) or
555
- * new Reference(url: string, string|RepoManager)
556
- *
557
- * Externally - this is the firebase.database.Reference type.
558
- */
559
- constructor(
560
- readonly database: Database,
561
- readonly _delegate: ModularReference
562
- ) {
563
- super(
564
- database,
565
- new _QueryImpl(
566
- _delegate._repo,
567
- _delegate._path,
568
- new _QueryParams(),
569
- false
570
- )
571
- );
572
- }
573
-
574
- /** @returns {?string} */
575
- getKey(): string | null {
576
- validateArgCount('Reference.key', 0, 0, arguments.length);
577
- return this._delegate.key;
578
- }
579
-
580
- child(pathString: string): Reference {
581
- validateArgCount('Reference.child', 1, 1, arguments.length);
582
- if (typeof pathString === 'number') {
583
- pathString = String(pathString);
584
- }
585
- return new Reference(this.database, child(this._delegate, pathString));
586
- }
587
-
588
- /** @returns {?Reference} */
589
- getParent(): Reference | null {
590
- validateArgCount('Reference.parent', 0, 0, arguments.length);
591
- const parent = this._delegate.parent;
592
- return parent ? new Reference(this.database, parent) : null;
593
- }
594
-
595
- /** @returns {!Reference} */
596
- getRoot(): Reference {
597
- validateArgCount('Reference.root', 0, 0, arguments.length);
598
- return new Reference(this.database, this._delegate.root);
599
- }
600
-
601
- set(
602
- newVal: unknown,
603
- onComplete?: (error: Error | null) => void
604
- ): Promise<unknown> {
605
- validateArgCount('Reference.set', 1, 2, arguments.length);
606
- validateCallback('Reference.set', 'onComplete', onComplete, true);
607
- const result = set(this._delegate, newVal);
608
- if (onComplete) {
609
- result.then(
610
- () => onComplete(null),
611
- error => onComplete(error)
612
- );
613
- }
614
- return result;
615
- }
616
-
617
- update(
618
- values: object,
619
- onComplete?: (a: Error | null) => void
620
- ): Promise<unknown> {
621
- validateArgCount('Reference.update', 1, 2, arguments.length);
622
-
623
- if (Array.isArray(values)) {
624
- const newObjectToMerge: { [k: string]: unknown } = {};
625
- for (let i = 0; i < values.length; ++i) {
626
- newObjectToMerge['' + i] = values[i];
627
- }
628
- values = newObjectToMerge;
629
- warn(
630
- 'Passing an Array to Firebase.update() is deprecated. ' +
631
- 'Use set() if you want to overwrite the existing data, or ' +
632
- 'an Object with integer keys if you really do want to ' +
633
- 'only update some of the children.'
634
- );
635
- }
636
- _validateWritablePath('Reference.update', this._delegate._path);
637
- validateCallback('Reference.update', 'onComplete', onComplete, true);
638
-
639
- const result = update(this._delegate, values);
640
- if (onComplete) {
641
- result.then(
642
- () => onComplete(null),
643
- error => onComplete(error)
644
- );
645
- }
646
- return result;
647
- }
648
-
649
- setWithPriority(
650
- newVal: unknown,
651
- newPriority: string | number | null,
652
- onComplete?: (a: Error | null) => void
653
- ): Promise<unknown> {
654
- validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
655
- validateCallback(
656
- 'Reference.setWithPriority',
657
- 'onComplete',
658
- onComplete,
659
- true
660
- );
661
-
662
- const result = setWithPriority(this._delegate, newVal, newPriority);
663
- if (onComplete) {
664
- result.then(
665
- () => onComplete(null),
666
- error => onComplete(error)
667
- );
668
- }
669
- return result;
670
- }
671
-
672
- remove(onComplete?: (a: Error | null) => void): Promise<unknown> {
673
- validateArgCount('Reference.remove', 0, 1, arguments.length);
674
- validateCallback('Reference.remove', 'onComplete', onComplete, true);
675
-
676
- const result = remove(this._delegate);
677
- if (onComplete) {
678
- result.then(
679
- () => onComplete(null),
680
- error => onComplete(error)
681
- );
682
- }
683
- return result;
684
- }
685
-
686
- transaction(
687
- transactionUpdate: (currentData: unknown) => unknown,
688
- onComplete?: (
689
- error: Error | null,
690
- committed: boolean,
691
- dataSnapshot: DataSnapshot | null
692
- ) => void,
693
- applyLocally?: boolean
694
- ): Promise<TransactionResult> {
695
- validateArgCount('Reference.transaction', 1, 3, arguments.length);
696
- validateCallback(
697
- 'Reference.transaction',
698
- 'transactionUpdate',
699
- transactionUpdate,
700
- false
701
- );
702
- validateCallback('Reference.transaction', 'onComplete', onComplete, true);
703
- validateBoolean(
704
- 'Reference.transaction',
705
- 'applyLocally',
706
- applyLocally,
707
- true
708
- );
709
-
710
- const result = runTransaction(this._delegate, transactionUpdate, {
711
- applyLocally
712
- }).then(
713
- transactionResult =>
714
- new TransactionResult(
715
- transactionResult.committed,
716
- new DataSnapshot(this.database, transactionResult.snapshot)
717
- )
718
- );
719
- if (onComplete) {
720
- result.then(
721
- transactionResult =>
722
- onComplete(
723
- null,
724
- transactionResult.committed,
725
- transactionResult.snapshot
726
- ),
727
- error => onComplete(error, false, null)
728
- );
729
- }
730
- return result;
731
- }
732
-
733
- setPriority(
734
- priority: string | number | null,
735
- onComplete?: (a: Error | null) => void
736
- ): Promise<unknown> {
737
- validateArgCount('Reference.setPriority', 1, 2, arguments.length);
738
- validateCallback('Reference.setPriority', 'onComplete', onComplete, true);
739
-
740
- const result = setPriority(this._delegate, priority);
741
- if (onComplete) {
742
- result.then(
743
- () => onComplete(null),
744
- error => onComplete(error)
745
- );
746
- }
747
- return result;
748
- }
749
-
750
- push(value?: unknown, onComplete?: (a: Error | null) => void): Reference {
751
- validateArgCount('Reference.push', 0, 2, arguments.length);
752
- validateCallback('Reference.push', 'onComplete', onComplete, true);
753
-
754
- const expPromise = push(this._delegate, value);
755
- const promise = expPromise.then(
756
- expRef => new Reference(this.database, expRef)
757
- );
758
-
759
- if (onComplete) {
760
- promise.then(
761
- () => onComplete(null),
762
- error => onComplete(error)
763
- );
764
- }
765
-
766
- const result = new Reference(this.database, expPromise);
767
- result.then = promise.then.bind(promise);
768
- result.catch = promise.catch.bind(promise, undefined);
769
- return result;
770
- }
771
-
772
- onDisconnect(): OnDisconnect {
773
- _validateWritablePath('Reference.onDisconnect', this._delegate._path);
774
- return new OnDisconnect(
775
- new ModularOnDisconnect(this._delegate._repo, this._delegate._path)
776
- );
777
- }
778
-
779
- get key(): string | null {
780
- return this.getKey();
781
- }
782
-
783
- get parent(): Reference | null {
784
- return this.getParent();
785
- }
786
-
787
- get root(): Reference {
788
- return this.getRoot();
789
- }
790
- }