ran-boilerplate
Version:
React . Apollo (GraphQL) . Next.js Toolkit
132 lines (130 loc) • 4.65 kB
JavaScript
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { fatal } from '../core/util/util';
import { parseRepoInfo } from '../core/util/libs/parser';
import { Path } from '../core/util/Path';
import { Reference } from './Reference';
import { Repo } from '../core/Repo';
import { RepoManager } from '../core/RepoManager';
import { validateArgCount } from '@firebase/util';
import { validateUrl } from '../core/util/validation';
/**
* Class representing a firebase database.
* @implements {FirebaseService}
*/
var Database = /** @class */ (function () {
/**
* The constructor should not be called by users of our public API.
* @param {!Repo} repo_
*/
function Database(repo_) {
this.repo_ = repo_;
if (!(repo_ instanceof Repo)) {
fatal("Don't call new Database() directly - please use firebase.database().");
}
/** @type {Reference} */
this.root_ = new Reference(repo_, Path.Empty);
this.INTERNAL = new DatabaseInternals(this);
}
Object.defineProperty(Database.prototype, "app", {
get: function () {
return this.repo_.app;
},
enumerable: true,
configurable: true
});
/**
* Returns a reference to the root or the path specified in opt_pathString.
* @param {string=} pathString
* @return {!Reference} Firebase reference.
*/
Database.prototype.ref = function (pathString) {
this.checkDeleted_('ref');
validateArgCount('database.ref', 0, 1, arguments.length);
return pathString !== undefined ? this.root_.child(pathString) : this.root_;
};
/**
* Returns a reference to the root or the path specified in url.
* We throw a exception if the url is not in the same domain as the
* current repo.
* @param {string} url
* @return {!Reference} Firebase reference.
*/
Database.prototype.refFromURL = function (url) {
/** @const {string} */
var apiName = 'database.refFromURL';
this.checkDeleted_(apiName);
validateArgCount(apiName, 1, 1, arguments.length);
var parsedURL = parseRepoInfo(url);
validateUrl(apiName, 1, parsedURL);
var repoInfo = parsedURL.repoInfo;
if (repoInfo.host !== this.repo_.repoInfo_.host) {
fatal(apiName +
': Host name does not match the current database: ' +
'(found ' +
repoInfo.host +
' but expected ' +
this.repo_.repoInfo_.host +
')');
}
return this.ref(parsedURL.path.toString());
};
/**
* @param {string} apiName
*/
Database.prototype.checkDeleted_ = function (apiName) {
if (this.repo_ === null) {
fatal('Cannot call ' + apiName + ' on a deleted database.');
}
};
// Make individual repo go offline.
Database.prototype.goOffline = function () {
validateArgCount('database.goOffline', 0, 0, arguments.length);
this.checkDeleted_('goOffline');
this.repo_.interrupt();
};
Database.prototype.goOnline = function () {
validateArgCount('database.goOnline', 0, 0, arguments.length);
this.checkDeleted_('goOnline');
this.repo_.resume();
};
Database.ServerValue = {
TIMESTAMP: {
'.sv': 'timestamp'
}
};
return Database;
}());
export { Database };
var DatabaseInternals = /** @class */ (function () {
/** @param {!Database} database */
function DatabaseInternals(database) {
this.database = database;
}
/** @return {Promise<void>} */
DatabaseInternals.prototype.delete = function () {
this.database.checkDeleted_('delete');
RepoManager.getInstance().deleteRepo(this.database.repo_);
this.database.repo_ = null;
this.database.root_ = null;
this.database.INTERNAL = null;
this.database = null;
return Promise.resolve();
};
return DatabaseInternals;
}());
export { DatabaseInternals };
//# sourceMappingURL=Database.js.map