11/**
2- * Copyright 2017 Google Inc.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
16-
17- import { fatal } from "../core/util/util" ;
18- import { parseRepoInfo } from "../core/util/libs/parser" ;
19- import { Path } from "../core/util/Path" ;
20- import { PromiseImpl } from "../../utils/promise" ;
21- import { Reference } from "./Reference" ;
22- import { Repo } from "../core/Repo" ;
23- import { RepoManager } from "../core/RepoManager" ;
24- import { validateArgCount } from "../../utils/validation" ;
25- import { FirebaseApp } from "../../app/firebase_app" ;
26- import { validateUrl } from "../core/util/validation" ;
2+ * Copyright 2017 Google Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import { fatal } from '../core/util/util' ;
18+ import { parseRepoInfo } from '../core/util/libs/parser' ;
19+ import { Path } from '../core/util/Path' ;
20+ import { PromiseImpl } from '../../utils/promise' ;
21+ import { Reference } from './Reference' ;
22+ import { Repo } from '../core/Repo' ;
23+ import { RepoManager } from '../core/RepoManager' ;
24+ import { validateArgCount } from '../../utils/validation' ;
25+ import { validateUrl } from '../core/util/validation' ;
26+ import { FirebaseApp , FirebaseService } from '../../app/firebase_app' ;
27+ import { RepoInfo } from '../core/RepoInfo' ;
2728
2829/**
2930 * Class representing a firebase database.
30- * @implements {firebase.Service }
31+ * @implements {FirebaseService }
3132 */
32- export class Database {
33- repo_ : Repo ;
34- root_ : Reference ;
35- INTERNAL ;
36-
37- static ServerValue = {
33+ export class Database implements FirebaseService {
34+ INTERNAL : DatabaseInternals ;
35+ private root_ : Reference ;
36+
37+ static readonly ServerValue = {
3838 'TIMESTAMP' : {
39- '.sv' : 'timestamp'
39+ '.sv' : 'timestamp'
4040 }
41- }
41+ } ;
4242
4343 /**
4444 * The constructor should not be called by users of our public API.
45- * @param {!Repo } repo
45+ * @param {!Repo } repo_
4646 */
47- constructor ( repo ) {
48- if ( ! ( repo instanceof Repo ) ) {
49- fatal ( " Don't call new Database() directly - please use firebase.database()." ) ;
47+ constructor ( private repo_ : Repo ) {
48+ if ( ! ( repo_ instanceof Repo ) ) {
49+ fatal ( ' Don\ 't call new Database() directly - please use firebase.database().' ) ;
5050 }
5151
52- /** @type {Repo } */
53- this . repo_ = repo ;
54-
55- /** @type {Firebase } */
56- this . root_ = new Reference ( repo , Path . Empty ) ;
52+ /** @type {Reference } */
53+ this . root_ = new Reference ( repo_ , Path . Empty ) ;
5754
5855 this . INTERNAL = new DatabaseInternals ( this ) ;
5956 }
@@ -65,7 +62,7 @@ export class Database {
6562 /**
6663 * Returns a reference to the root or the path specified in opt_pathString.
6764 * @param {string= } pathString
68- * @return {!Firebase } Firebase reference.
65+ * @return {!Reference } Firebase reference.
6966 */
7067 ref ( pathString ?: string ) : Reference {
7168 this . checkDeleted_ ( 'ref' ) ;
@@ -79,20 +76,21 @@ export class Database {
7976 * We throw a exception if the url is not in the same domain as the
8077 * current repo.
8178 * @param {string } url
82- * @return {!Firebase } Firebase reference.
79+ * @return {!Reference } Firebase reference.
8380 */
84- refFromURL ( url ) {
81+ refFromURL ( url : string ) : Reference {
8582 /** @const {string} */
86- var apiName = 'database.refFromURL' ;
83+ const apiName = 'database.refFromURL' ;
8784 this . checkDeleted_ ( apiName ) ;
8885 validateArgCount ( apiName , 1 , 1 , arguments . length ) ;
89- var parsedURL = parseRepoInfo ( url ) ;
86+ const parsedURL = parseRepoInfo ( url ) ;
9087 validateUrl ( apiName , 1 , parsedURL ) ;
9188
92- var repoInfo = parsedURL . repoInfo ;
93- if ( repoInfo . host !== this . repo_ . repoInfo_ . host ) {
94- fatal ( apiName + ": Host name does not match the current database: " +
95- "(found " + repoInfo . host + " but expected " + this . repo_ . repoInfo_ . host + ")" ) ;
89+ const repoInfo = parsedURL . repoInfo ;
90+ if ( repoInfo . host !== ( ( this . repo_ as any ) . repoInfo_ as RepoInfo ) . host ) {
91+ fatal ( apiName + ': Host name does not match the current database: ' +
92+ '(found ' + repoInfo . host + ' but expected ' +
93+ ( ( this . repo_ as any ) . repoInfo_ as RepoInfo ) . host + ')' ) ;
9694 }
9795
9896 return this . ref ( parsedURL . path . toString ( ) ) ;
@@ -101,9 +99,9 @@ export class Database {
10199 /**
102100 * @param {string } apiName
103101 */
104- private checkDeleted_ ( apiName ) {
102+ private checkDeleted_ ( apiName : string ) {
105103 if ( this . repo_ === null ) {
106- fatal ( " Cannot call " + apiName + " on a deleted database." ) ;
104+ fatal ( ' Cannot call ' + apiName + ' on a deleted database.' ) ;
107105 }
108106 }
109107
@@ -114,36 +112,28 @@ export class Database {
114112 this . repo_ . interrupt ( ) ;
115113 }
116114
117- goOnline ( ) {
115+ goOnline ( ) {
118116 validateArgCount ( 'database.goOnline' , 0 , 0 , arguments . length ) ;
119117 this . checkDeleted_ ( 'goOnline' ) ;
120118 this . repo_ . resume ( ) ;
121119 }
122- } ;
123-
124- Object . defineProperty ( Repo . prototype , 'database' , {
125- get ( ) {
126- return this . __database || ( this . __database = new Database ( this ) ) ;
127- }
128- } ) ;
120+ }
129121
130- class DatabaseInternals {
131- database
122+ export class DatabaseInternals {
132123 /** @param {!Database } database */
133- constructor ( database ) {
134- this . database = database ;
124+ constructor ( public database : Database ) {
135125 }
136126
137- /** @return {firebase. Promise<void> } */
138- delete ( ) {
139- this . database . checkDeleted_ ( 'delete' ) ;
140- RepoManager . getInstance ( ) . deleteRepo ( /** @type { !Repo } */ ( this . database . repo_ ) ) ;
127+ /** @return {Promise<void> } */
128+ delete ( ) : Promise < void > {
129+ ( this . database as any ) . checkDeleted_ ( 'delete' ) ;
130+ RepoManager . getInstance ( ) . deleteRepo ( ( this . database as any ) . repo_ as Repo ) ;
141131
142- this . database . repo_ = null ;
143- this . database . root_ = null ;
132+ ( this . database as any ) . repo_ = null ;
133+ ( this . database as any ) . root_ = null ;
144134 this . database . INTERNAL = null ;
145135 this . database = null ;
146136 return PromiseImpl . resolve ( ) ;
147137 }
148- } ;
138+ }
149139
0 commit comments