MCPcopy Index your code
hub / github.com/firebase/firebase-js-sdk / push

Function push

packages/database/src/api/Reference_impl.ts:599–631  ·  view source on GitHub ↗
(
  parent: DatabaseReference,
  value?: unknown
)

Source from the content-addressed store, hash-verified

597 * but can be used immediately as the `Reference` to the child location.
598 */
599export function push(
600 parent: DatabaseReference,
601 value?: unknown
602): ThenableReference {
603 parent = getModularInstance(parent);
604 validateWritablePath('push', parent._path);
605 validateFirebaseDataArg('push', value, parent._path, true);
606 const now = repoServerTime(parent._repo);
607 const name = nextPushId(now);
608
609 // push() returns a ThennableReference whose promise is fulfilled with a
610 // regular Reference. We use child() to create handles to two different
611 // references. The first is turned into a ThennableReference below by adding
612 // then() and catch() methods and is used as the return value of push(). The
613 // second remains a regular Reference and is used as the fulfilled value of
614 // the first ThennableReference.
615 const thenablePushRef: Partial<ThenableReferenceImpl> = child(
616 parent,
617 name
618 ) as ReferenceImpl;
619 const pushRef = child(parent, name) as ReferenceImpl;
620
621 let promise: Promise<ReferenceImpl>;
622 if (value != null) {
623 promise = set(pushRef, value).then(() => pushRef);
624 } else {
625 promise = Promise.resolve(pushRef);
626 }
627
628 thenablePushRef.then = promise.then.bind(promise);
629 thenablePushRef.catch = promise.then.bind(promise, undefined);
630 return thenablePushRef as ThenableReferenceImpl;
631}
632
633/**
634 * Removes the data at this Database location.

Callers 2

pushMethod · 0.90

Calls 8

getModularInstanceFunction · 0.90
validateWritablePathFunction · 0.90
validateFirebaseDataArgFunction · 0.90
repoServerTimeFunction · 0.90
resolveMethod · 0.80
childFunction · 0.70
setFunction · 0.70
thenMethod · 0.65

Tested by

no test coverage detected