Skip to content

Commit cc66323

Browse files
authored
Adding code comments and @hidden tags for src/providers/database.ts (#531)
* Adding code comments to src/providers/database.ts file * Adding an @hidden tag to properties and methods of database.DataSnapshot with an underscore before them
1 parent 3491a6d commit cc66323

File tree

1 file changed

+191
-21
lines changed

1 file changed

+191
-21
lines changed

src/providers/database.ts

Lines changed: 191 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,26 @@ export const service = 'firebaseio.com';
4444
const databaseURLRegex = new RegExp('https://([^.]+).firebaseio.com');
4545

4646
/**
47-
* Selects a database instance that will trigger the function.
48-
* If omitted, will pick the default database for your project.
49-
* @param instance The Realtime Database instance to use.
47+
* Registers a function that triggers on events from a specific
48+
* Firebase Realtime Database instance.
49+
*
50+
* Use this method together with `ref` to specify the instance on which to
51+
* watch for database events. For example: `firebase.database.instance('my-app-db-2').ref('/foo/bar')`
52+
*
53+
* Note that `functions.database.ref` used without `instance` watches the
54+
* *default* instance for events.
55+
*
56+
* @param instance The instance name of the database instance
57+
* to watch for write events.
58+
* @return Firebase Realtime Database instance builder interface.
5059
*/
5160
export function instance(instance: string) {
5261
return _instanceWithOptions(instance, {});
5362
}
5463

5564
/**
56-
* Select Firebase Realtime Database Reference to listen to.
65+
* Registers a function that triggers on Firebase Realtime Database write
66+
* events.
5767
*
5868
* This method behaves very similarly to the method of the same name in the
5969
* client and Admin Firebase SDKs. Any change to the Database that affects the
@@ -65,15 +75,19 @@ export function instance(instance: string) {
6575
* 1. Cloud Functions allows wildcards in the `path` name. Any `path` component
6676
* in curly brackets (`{}`) is a wildcard that matches all strings. The value
6777
* that matched a certain invocation of a Cloud Function is returned as part
68-
* of the `context.params` object. For example, `ref("messages/{messageId}")`
69-
* matches changes at `/messages/message1` or `/messages/message2`, resulting
70-
* in `context.params.messageId` being set to `"message1"` or `"message2"`,
78+
* of the [`event.params`](functions.EventContext#params) object. For
79+
* example, `ref("messages/{messageId}")` matches changes at
80+
* `/messages/message1` or `/messages/message2`, resulting in
81+
* `event.params.messageId` being set to `"message1"` or `"message2"`,
7182
* respectively.
7283
* 2. Cloud Functions do not fire an event for data that already existed before
7384
* the Cloud Function was deployed.
74-
* 3. Cloud Function events have access to more information, including information
75-
* about the user who triggered the Cloud Function.
76-
* @param ref Path of the database to listen to.
85+
* 3. Cloud Function events have access to more information, including a
86+
* snapshot of the previous event data and information about the user who
87+
* triggered the Cloud Function.
88+
*
89+
* @param path The path within the Database to watch for write events.
90+
* @return Firebase Realtime Database builder interface.
7791
*/
7892
export function ref(path: string) {
7993
return _refWithOptions(path, {});
@@ -87,10 +101,18 @@ export function _instanceWithOptions(
87101
return new InstanceBuilder(instance, options);
88102
}
89103

104+
/**
105+
* The Firebase Realtime Database instance builder interface.
106+
*
107+
* Access via [`functions.database.instance()`](functions.database#.instance).
108+
*/
90109
export class InstanceBuilder {
91110
/** @hidden */
92111
constructor(private instance: string, private options: DeploymentOptions) {}
93112

113+
/**
114+
* @return Firebase Realtime Database reference builder interface.
115+
*/
94116
ref(path: string): RefBuilder {
95117
const normalized = normalizePath(path);
96118
return new RefBuilder(
@@ -130,7 +152,11 @@ export function _refWithOptions(
130152
return new RefBuilder(apps(), resourceGetter, options);
131153
}
132154

133-
/** Builder used to create Cloud Functions for Firebase Realtime Database References. */
155+
/**
156+
* The Firebase Realtime Database reference builder interface.
157+
*
158+
* Access via [`functions.database.ref()`](functions.database#.ref).
159+
*/
134160
export class RefBuilder {
135161
/** @hidden */
136162
constructor(
@@ -139,7 +165,14 @@ export class RefBuilder {
139165
private options: DeploymentOptions
140166
) {}
141167

142-
/** Respond to any write that affects a ref. */
168+
/**
169+
* Event handler that fires every time a Firebase Realtime Database write
170+
* of any kind (creation, update, or delete) occurs.
171+
*
172+
* @param handler Event handler that runs every time a Firebase Realtime Database
173+
* write occurs.
174+
* @return A Cloud Function that you can export and deploy.
175+
*/
143176
onWrite(
144177
handler: (
145178
change: Change<DataSnapshot>,
@@ -149,7 +182,15 @@ export class RefBuilder {
149182
return this.onOperation(handler, 'ref.write', this.changeConstructor);
150183
}
151184

152-
/** Respond to update on a ref. */
185+
/**
186+
* Event handler that fires every time data is updated in
187+
* Firebase Realtime Database.
188+
*
189+
* @param handler Event handler which is run every time a Firebase Realtime Database
190+
* write occurs.
191+
* @return A Cloud
192+
* Function which you can export and deploy.
193+
*/
153194
onUpdate(
154195
handler: (
155196
change: Change<DataSnapshot>,
@@ -159,7 +200,14 @@ export class RefBuilder {
159200
return this.onOperation(handler, 'ref.update', this.changeConstructor);
160201
}
161202

162-
/** Respond to new data on a ref. */
203+
/**
204+
* Event handler that fires every time new data is created in
205+
* Firebase Realtime Database.
206+
*
207+
* @param handler Event handler that runs every time new data is created in
208+
* Firebase Realtime Database.
209+
* @return A Cloud Function that you can export and deploy.
210+
*/
163211
onCreate(
164212
handler: (
165213
snapshot: DataSnapshot,
@@ -180,7 +228,14 @@ export class RefBuilder {
180228
return this.onOperation(handler, 'ref.create', dataConstructor);
181229
}
182230

183-
/** Respond to all data being deleted from a ref. */
231+
/**
232+
* Event handler that fires every time data is deleted from
233+
* Firebase Realtime Database.
234+
*
235+
* @param handler Event handler that runs every time data is deleted from
236+
* Firebase Realtime Database.
237+
* @return A Cloud Function that you can export and deploy.
238+
*/
184239
onDelete(
185240
handler: (
186241
snapshot: DataSnapshot,
@@ -259,11 +314,22 @@ export function resourceToInstanceAndPath(resource: string) {
259314
return [dbInstance, path];
260315
}
261316

317+
/**
318+
* Interface representing a Firebase Realtime Database data snapshot.
319+
*/
262320
export class DataSnapshot {
263321
public instance: string;
322+
323+
/** @hidden */
264324
private _ref: firebase.database.Reference;
325+
326+
/** @hidden */
265327
private _path: string;
328+
329+
/** @hidden */
266330
private _data: any;
331+
332+
/** @hidden */
267333
private _childPath: string;
268334

269335
constructor(
@@ -286,7 +352,11 @@ export class DataSnapshot {
286352
this._data = data;
287353
}
288354

289-
/** Ref returns a reference to the database with full admin access. */
355+
/**
356+
* Returns a [`Reference`](/docs/reference/admin/node/admin.database.Reference)
357+
* to the Database location where the triggering write occurred. Has
358+
* full read and write access.
359+
*/
290360
get ref(): firebase.database.Reference {
291361
if (!this.app) {
292362
// may be unpopulated in user's unit tests
@@ -301,11 +371,30 @@ export class DataSnapshot {
301371
return this._ref;
302372
}
303373

374+
/**
375+
* The key (last part of the path) of the location of this `DataSnapshot`.
376+
*
377+
* The last token in a Database location is considered its key. For example,
378+
* "ada" is the key for the `/users/ada/` node. Accessing the key on any
379+
* `DataSnapshot` will return the key for the location that generated it.
380+
* However, accessing the key on the root URL of a Database will return `null`.
381+
*/
304382
get key(): string {
305383
const last = _.last(pathParts(this._fullPath()));
306384
return !last || last === '' ? null : last;
307385
}
308386

387+
/**
388+
* Extracts a JavaScript value from a `DataSnapshot`.
389+
*
390+
* Depending on the data in a `DataSnapshot`, the `val()` method may return a
391+
* scalar type (string, number, or boolean), an array, or an object. It may also
392+
* return `null`, indicating that the `DataSnapshot` is empty (contains no
393+
* data).
394+
*
395+
* @return The DataSnapshot's contents as a JavaScript value (Object,
396+
* Array, string, number, boolean, or `null`).
397+
*/
309398
val(): any {
310399
const parts = pathParts(this._childPath);
311400
const source = this._data;
@@ -315,27 +404,78 @@ export class DataSnapshot {
315404
return this._checkAndConvertToArray(node);
316405
}
317406

318-
// TODO(inlined): figure out what to do here
407+
/**
408+
* Exports the entire contents of the `DataSnapshot` as a JavaScript object.
409+
*
410+
* The `exportVal()` method is similar to `val()`, except priority information
411+
* is included (if available), making it suitable for backing up your data.
412+
*
413+
* @return The contents of the `DataSnapshot` as a JavaScript value
414+
* (Object, Array, string, number, boolean, or `null`).
415+
*/
319416
exportVal(): any {
320417
return this.val();
321418
}
322419

323-
// TODO(inlined): figure out what to do here
420+
/**
421+
* Gets the priority value of the data in this `DataSnapshot`.
422+
*
423+
* As an alternative to using priority, applications can order collections by
424+
* ordinary properties. See [Sorting and filtering
425+
* data](/docs/database/web/lists-of-data#sorting_and_filtering_data).
426+
*
427+
* @return The priority value of the data.
428+
*/
324429
getPriority(): string | number | null {
325430
return 0;
326431
}
327432

433+
/**
434+
* Returns `true` if this `DataSnapshot` contains any data. It is slightly more
435+
* efficient than using `snapshot.val() !== null`.
436+
*
437+
* @return `true` if this `DataSnapshot` contains any data; otherwise, `false`.
438+
*/
328439
exists(): boolean {
329440
return !_.isNull(this.val());
330441
}
331442

443+
/**
444+
* Gets a `DataSnapshot` for the location at the specified relative path.
445+
*
446+
* The relative path can either be a simple child name (for example, "ada") or
447+
* a deeper slash-separated path (for example, "ada/name/first").
448+
*
449+
* @param path A relative path from this location to the desired child
450+
* location.
451+
* @return The specified child location.
452+
*/
332453
child(childPath: string): DataSnapshot {
333454
if (!childPath) {
334455
return this;
335456
}
336457
return this._dup(childPath);
337458
}
338459

460+
/**
461+
* Enumerates the `DataSnapshot`s of the children items.
462+
*
463+
* Because of the way JavaScript objects work, the ordering of data in the
464+
* JavaScript object returned by `val()` is not guaranteed to match the ordering
465+
* on the server nor the ordering of `child_added` events. That is where
466+
* `forEach()` comes in handy. It guarantees the children of a `DataSnapshot`
467+
* will be iterated in their query order.
468+
*
469+
* If no explicit `orderBy*()` method is used, results are returned
470+
* ordered by key (unless priorities are used, in which case, results are
471+
* returned by priority).
472+
*
473+
* @param action A function that will be called for each child `DataSnapshot`.
474+
* The callback can return `true` to cancel further enumeration.
475+
*
476+
* @return `true` if enumeration was canceled due to your callback
477+
* returning `true`.
478+
*/
339479
forEach(action: (a: DataSnapshot) => boolean): boolean {
340480
const val = this.val();
341481
if (_.isPlainObject(val)) {
@@ -347,29 +487,57 @@ export class DataSnapshot {
347487
return false;
348488
}
349489

490+
/**
491+
* Returns `true` if the specified child path has (non-`null`) data.
492+
*
493+
* @param path A relative path to the location of a potential child.
494+
* @return `true` if data exists at the specified child path; otherwise,
495+
* `false`.
496+
*/
350497
hasChild(childPath: string): boolean {
351498
return this.child(childPath).exists();
352499
}
353500

501+
/**
502+
* Returns whether or not the `DataSnapshot` has any non-`null` child
503+
* properties.
504+
*
505+
* You can use `hasChildren()` to determine if a `DataSnapshot` has any
506+
* children. If it does, you can enumerate them using `forEach()`. If it
507+
* doesn't, then either this snapshot contains a primitive value (which can be
508+
* retrieved with `val()`) or it is empty (in which case, `val()` will return
509+
* `null`).
510+
*
511+
* @return `true` if this snapshot has any children; else `false`.
512+
*/
354513
hasChildren(): boolean {
355514
const val = this.val();
356515
return _.isPlainObject(val) && _.keys(val).length > 0;
357516
}
358517

518+
/**
519+
* Returns the number of child properties of this `DataSnapshot`.
520+
*
521+
* @return Number of child properties of this `DataSnapshot`.
522+
*/
359523
numChildren(): number {
360524
const val = this.val();
361525
return _.isPlainObject(val) ? Object.keys(val).length : 0;
362526
}
363527

364528
/**
365-
* Prints the value of the snapshot; use '.previous.toJSON()' and '.current.toJSON()' to explicitly see
366-
* the previous and current values of the snapshot.
529+
* Returns a JSON-serializable representation of this object.
530+
*
531+
* @return A JSON-serializable representation of this object.
367532
*/
368533
toJSON(): Object {
369534
return this.val();
370535
}
371536

372-
/* Recursive function to check if keys are numeric & convert node object to array if they are */
537+
/** Recursive function to check if keys are numeric & convert node object to array if they are
538+
*
539+
* @hidden
540+
*/
373541
private _checkAndConvertToArray(node: any): any {
374542
if (node === null || typeof node === 'undefined') {
375543
return null;
@@ -408,6 +576,7 @@ export class DataSnapshot {
408576
return obj;
409577
}
410578

579+
/** @hidden */
411580
private _dup(childPath?: string): DataSnapshot {
412581
const dup = new DataSnapshot(
413582
this._data,
@@ -424,6 +593,7 @@ export class DataSnapshot {
424593
return dup;
425594
}
426595

596+
/** @hidden */
427597
private _fullPath(): string {
428598
const out = (this._path || '') + '/' + (this._childPath || '');
429599
return out;

0 commit comments

Comments
 (0)