|
| 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 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 18 | + |
| 19 | +import { FirebaseApp } from '@firebase/app-types'; |
| 20 | +import { FirebaseService } from '@firebase/app-types/private'; |
| 21 | +import { |
| 22 | + goOnline, |
| 23 | + connectDatabaseEmulator, |
| 24 | + goOffline, |
| 25 | + ref, |
| 26 | + refFromURL, |
| 27 | + increment, |
| 28 | + serverTimestamp, |
| 29 | + Database as ModularDatabase |
| 30 | +} from '@firebase/database'; |
| 31 | +import { |
| 32 | + validateArgCount, |
| 33 | + Compat, |
| 34 | + EmulatorMockTokenOptions |
| 35 | +} from '@firebase/util'; |
| 36 | + |
| 37 | + |
| 38 | +import { Reference } from './Reference'; |
| 39 | + |
| 40 | +/** |
| 41 | + * Class representing a firebase database. |
| 42 | + */ |
| 43 | +export class Database implements FirebaseService, Compat<ModularDatabase> { |
| 44 | + static readonly ServerValue = { |
| 45 | + TIMESTAMP: serverTimestamp(), |
| 46 | + increment: (delta: number) => increment(delta) |
| 47 | + }; |
| 48 | + |
| 49 | + /** |
| 50 | + * The constructor should not be called by users of our public API. |
| 51 | + */ |
| 52 | + constructor(readonly _delegate: ModularDatabase, readonly app: FirebaseApp) {} |
| 53 | + |
| 54 | + INTERNAL = { |
| 55 | + delete: () => this._delegate._delete() |
| 56 | + }; |
| 57 | + |
| 58 | + /** |
| 59 | + * Modify this instance to communicate with the Realtime Database emulator. |
| 60 | + * |
| 61 | + * <p>Note: This method must be called before performing any other operation. |
| 62 | + * |
| 63 | + * @param host - the emulator host (ex: localhost) |
| 64 | + * @param port - the emulator port (ex: 8080) |
| 65 | + * @param options.mockUserToken - the mock auth token to use for unit testing Security Rules |
| 66 | + */ |
| 67 | + useEmulator( |
| 68 | + host: string, |
| 69 | + port: number, |
| 70 | + options: { |
| 71 | + mockUserToken?: EmulatorMockTokenOptions; |
| 72 | + } = {} |
| 73 | + ): void { |
| 74 | + connectDatabaseEmulator(this._delegate, host, port, options); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns a reference to the root or to the path specified in the provided |
| 79 | + * argument. |
| 80 | + * |
| 81 | + * @param path - The relative string path or an existing Reference to a database |
| 82 | + * location. |
| 83 | + * @throws If a Reference is provided, throws if it does not belong to the |
| 84 | + * same project. |
| 85 | + * @returns Firebase reference. |
| 86 | + */ |
| 87 | + ref(path?: string): Reference; |
| 88 | + ref(path?: Reference): Reference; |
| 89 | + ref(path?: string | Reference): Reference { |
| 90 | + validateArgCount('database.ref', 0, 1, arguments.length); |
| 91 | + if (path instanceof Reference) { |
| 92 | + const childRef = refFromURL(this._delegate, path.toString()); |
| 93 | + return new Reference(this, childRef); |
| 94 | + } else { |
| 95 | + const childRef = ref(this._delegate, path); |
| 96 | + return new Reference(this, childRef); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns a reference to the root or the path specified in url. |
| 102 | + * We throw a exception if the url is not in the same domain as the |
| 103 | + * current repo. |
| 104 | + * @returns Firebase reference. |
| 105 | + */ |
| 106 | + refFromURL(url: string): Reference { |
| 107 | + const apiName = 'database.refFromURL'; |
| 108 | + validateArgCount(apiName, 1, 1, arguments.length); |
| 109 | + const childRef = refFromURL(this._delegate, url); |
| 110 | + return new Reference(this, childRef); |
| 111 | + } |
| 112 | + |
| 113 | + // Make individual repo go offline. |
| 114 | + goOffline(): void { |
| 115 | + validateArgCount('database.goOffline', 0, 0, arguments.length); |
| 116 | + return goOffline(this._delegate); |
| 117 | + } |
| 118 | + |
| 119 | + goOnline(): void { |
| 120 | + validateArgCount('database.goOnline', 0, 0, arguments.length); |
| 121 | + return goOnline(this._delegate); |
| 122 | + } |
| 123 | +} |
0 commit comments