Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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