Skip to content

Commit

Permalink
Split database and database-compat (#5276)
Browse files Browse the repository at this point in the history
* compile database

* pass database tests

* compile and test database-compat

* pass all tests

* prettier

* cleanup

* fix lint

* address comments

* what is going on with ci

* use correct case in import path

* uppercase

* rename

* fix component name
  • Loading branch information
Feiyang1 committed Aug 24, 2021
1 parent 8b5d583 commit 1a3dd6f
Show file tree
Hide file tree
Showing 69 changed files with 2,001 additions and 2,309 deletions.
2 changes: 1 addition & 1 deletion packages-exp/app-exp/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { name as appCheckName } from '../../../packages-exp/app-check-exp/packag
import { name as authName } from '../../../packages-exp/auth-exp/package.json';
import { name as authCompatName } from '../../../packages-exp/auth-compat-exp/package.json';
import { name as databaseName } from '../../../packages/database/package.json';
import { name as databaseCompatName } from '../../../packages/database/compat/package.json';
import { name as databaseCompatName } from '../../../packages/database-compat/package.json';
import { name as functionsName } from '../../../packages-exp/functions-exp/package.json';
import { name as functionsCompatName } from '../../../packages-exp/functions-compat/package.json';
import { name as installationsName } from '../../../packages-exp/installations-exp/package.json';
Expand Down
64 changes: 64 additions & 0 deletions packages/database-compat/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license
* Copyright 2020 Google LLC
*
* 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.
*/

module.exports = {
extends: '../../config/.eslintrc.js',
parserOptions: {
project: 'tsconfig.json',
// to make vscode-eslint work with monorepo
// https://github.com/typescript-eslint/typescript-eslint/issues/251#issuecomment-463943250
tsconfigRootDir: __dirname
},
rules: {
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'no-restricted-properties': 'off',
'no-restricted-globals': 'off',
'no-throw-literal': 'off',
'id-blacklist': 'off',
'import/order': [
'error',
{
'groups': [
'builtin',
'external',
'internal',
'parent',
'sibling',
'index'
],
'newlines-between': 'always',
'alphabetize': { 'order': 'asc', 'caseInsensitive': true }
}
]
},
overrides: [
{
files: ['**/*.d.ts'],
rules: {
'@typescript-eslint/no-explicit-any': 'off'
}
},
{
files: ['scripts/*.ts'],
rules: {
'import/no-extraneous-dependencies': 'off'
}
}
]
};
5 changes: 5 additions & 0 deletions packages/database-compat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @firebase/database-compat

This is the compatibility layer for the Firebase Realtime Database component of the Firebase JS SDK.

**This package is not intended for direct usage, and should only be used via the officially supported [firebase](https://www.npmjs.com/package/firebase) package.**
34 changes: 34 additions & 0 deletions packages/database-compat/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright 2017 Google LLC
*
* 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.
*/

const karmaBase = require('../../config/karma.base');

const files = [`test/**/*.test.ts`];

module.exports = function (config) {
const karmaConfig = Object.assign({}, karmaBase, {
// files to load into karma
files: files,
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha']
});

config.set(karmaConfig);
};

module.exports.files = files;
37 changes: 37 additions & 0 deletions packages/database-compat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@firebase/database-compat",
"version": "0.0.900",
"description": "The Realtime Database component of the Firebase JS SDK.",
"author": "Firebase <firebase-support@google.com> (https://firebase.google.com/)",
"main": "dist/index.js",
"browser": "dist/index.esm2017.js",
"module": "dist/index.esm2017.js",
"esm5": "dist/index.esm5.js",
"license": "Apache-2.0",
"typings": "dist/database-compat/src/index.d.ts",
"scripts": {
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
"prettier": "prettier --write '*.js' '*.ts' '@(src|test)/**/*.ts'",
"build": "rollup -c rollup.config.js",
"build:release": "yarn build && yarn add-compat-overloads",
"build:deps": "lerna run --scope @firebase/database-compat --include-dependencies build",
"dev": "rollup -c -w",
"test": "run-p lint test:browser test:node",
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test",
"test:browser": "karma start --single-run",
"test:node": "TS_NODE_FILES=true TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --file src/index.node.ts --config ../../config/mocharc.node.js",
"add-compat-overloads": "ts-node-script ../../scripts/exp/create-overloads.ts -i ../database/dist/public.d.ts -o dist/database-compat/src/index.d.ts -a -r FirebaseDatabase:types.FirebaseDatabase -r Query:types.Query -r Reference:types.Reference -r FirebaseApp:FirebaseAppCompat --moduleToEnhance @firebase/database"
},
"peerDependencies": {
"@firebase/app-compat": "0.x"
},
"dependencies": {
"@firebase/database": "0.11.0",
"@firebase/database-types": "0.8.0",
"@firebase/logger": "0.2.6",
"@firebase/util": "1.3.0",
"@firebase/component": "0.5.6",
"tslib": "^2.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2018 Google LLC
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,16 +18,12 @@
import json from '@rollup/plugin-json';
import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';
import path from 'path';
import { importPathTransformer } from '../../scripts/exp/ts-transform-import-path';

import expPkg from './exp/package.json';
import pkg from './package.json';

const deps = [
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
'@firebase/app'
];
const deps = Object.keys(
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
);

function onWarn(warning, defaultWarn) {
if (warning.code === 'CIRCULAR_DEPENDENCY') {
Expand All @@ -42,8 +38,7 @@ function onWarn(warning, defaultWarn) {
const es5BuildPlugins = [
typescriptPlugin({
typescript,
abortOnError: false,
transformers: [importPathTransformer]
abortOnError: false
}),
json()
];
Expand All @@ -53,9 +48,13 @@ const es5Builds = [
* Node.js Build
*/
{
input: 'exp/index.node.ts',
input: 'src/index.node.ts',
output: [
{ file: path.resolve('exp', expPkg.main), format: 'cjs', sourcemap: true }
{
file: pkg.main,
format: 'cjs',
sourcemap: true
}
],
plugins: es5BuildPlugins,
treeshake: {
Expand All @@ -68,10 +67,10 @@ const es5Builds = [
* Browser Builds
*/
{
input: 'exp/index.ts',
input: 'src/index.ts',
output: [
{
file: path.resolve('exp', expPkg.esm5),
file: pkg.esm5,
format: 'es',
sourcemap: true
}
Expand All @@ -96,8 +95,7 @@ const es2017BuildPlugins = [
target: 'es2017'
}
},
abortOnError: false,
transformers: [importPathTransformer]
abortOnError: false
}),
json({ preferConst: true })
];
Expand All @@ -107,10 +105,10 @@ const es2017Builds = [
* Browser Build
*/
{
input: 'exp/index.ts',
input: 'src/index.ts',
output: [
{
file: path.resolve('exp', expPkg.browser),
file: pkg.browser,
format: 'es',
sourcemap: true
}
Expand Down
123 changes: 123 additions & 0 deletions packages/database-compat/src/api/Database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* @license
* Copyright 2017 Google LLC
*
* 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.
*/
// eslint-disable-next-line import/no-extraneous-dependencies

import { FirebaseApp } from '@firebase/app-types';
import { FirebaseService } from '@firebase/app-types/private';
import {
goOnline,
connectDatabaseEmulator,
goOffline,
ref,
refFromURL,
increment,
serverTimestamp,
Database as ModularDatabase
} from '@firebase/database';
import {
validateArgCount,
Compat,
EmulatorMockTokenOptions
} from '@firebase/util';


import { Reference } from './Reference';

/**
* Class representing a firebase database.
*/
export class Database implements FirebaseService, Compat<ModularDatabase> {
static readonly ServerValue = {
TIMESTAMP: serverTimestamp(),
increment: (delta: number) => increment(delta)
};

/**
* The constructor should not be called by users of our public API.
*/
constructor(readonly _delegate: ModularDatabase, readonly app: FirebaseApp) {}

INTERNAL = {
delete: () => this._delegate._delete()
};

/**
* Modify this instance to communicate with the Realtime Database emulator.
*
* <p>Note: This method must be called before performing any other operation.
*
* @param host - the emulator host (ex: localhost)
* @param port - the emulator port (ex: 8080)
* @param options.mockUserToken - the mock auth token to use for unit testing Security Rules
*/
useEmulator(
host: string,
port: number,
options: {
mockUserToken?: EmulatorMockTokenOptions;
} = {}
): void {
connectDatabaseEmulator(this._delegate, host, port, options);
}

/**
* Returns a reference to the root or to the path specified in the provided
* argument.
*
* @param path - The relative string path or an existing Reference to a database
* location.
* @throws If a Reference is provided, throws if it does not belong to the
* same project.
* @returns Firebase reference.
*/
ref(path?: string): Reference;
ref(path?: Reference): Reference;
ref(path?: string | Reference): Reference {
validateArgCount('database.ref', 0, 1, arguments.length);
if (path instanceof Reference) {
const childRef = refFromURL(this._delegate, path.toString());
return new Reference(this, childRef);
} else {
const childRef = ref(this._delegate, path);
return new Reference(this, childRef);
}
}

/**
* 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.
* @returns Firebase reference.
*/
refFromURL(url: string): Reference {
const apiName = 'database.refFromURL';
validateArgCount(apiName, 1, 1, arguments.length);
const childRef = refFromURL(this._delegate, url);
return new Reference(this, childRef);
}

// Make individual repo go offline.
goOffline(): void {
validateArgCount('database.goOffline', 0, 0, arguments.length);
return goOffline(this._delegate);
}

goOnline(): void {
validateArgCount('database.goOnline', 0, 0, arguments.length);
return goOnline(this._delegate);
}
}
Loading

0 comments on commit 1a3dd6f

Please sign in to comment.