Skip to content

Commit 568459d

Browse files
authored
Split database and database-compat (#5276)
* 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
1 parent d82d6f2 commit 568459d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2001
-2309
lines changed

packages-exp/app-exp/src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { name as appCheckName } from '../../../packages-exp/app-check-exp/packag
2424
import { name as authName } from '../../../packages-exp/auth-exp/package.json';
2525
import { name as authCompatName } from '../../../packages-exp/auth-compat-exp/package.json';
2626
import { name as databaseName } from '../../../packages/database/package.json';
27-
import { name as databaseCompatName } from '../../../packages/database/compat/package.json';
27+
import { name as databaseCompatName } from '../../../packages/database-compat/package.json';
2828
import { name as functionsName } from '../../../packages-exp/functions-exp/package.json';
2929
import { name as functionsCompatName } from '../../../packages-exp/functions-compat/package.json';
3030
import { name as installationsName } from '../../../packages-exp/installations-exp/package.json';

packages/database-compat/.eslintrc.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license
3+
* Copyright 2020 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+
18+
module.exports = {
19+
extends: '../../config/.eslintrc.js',
20+
parserOptions: {
21+
project: 'tsconfig.json',
22+
// to make vscode-eslint work with monorepo
23+
// https://github.com/typescript-eslint/typescript-eslint/issues/251#issuecomment-463943250
24+
tsconfigRootDir: __dirname
25+
},
26+
rules: {
27+
'@typescript-eslint/no-unused-vars': 'off',
28+
'@typescript-eslint/no-floating-promises': 'off',
29+
'@typescript-eslint/explicit-function-return-type': 'off',
30+
'no-restricted-properties': 'off',
31+
'no-restricted-globals': 'off',
32+
'no-throw-literal': 'off',
33+
'id-blacklist': 'off',
34+
'import/order': [
35+
'error',
36+
{
37+
'groups': [
38+
'builtin',
39+
'external',
40+
'internal',
41+
'parent',
42+
'sibling',
43+
'index'
44+
],
45+
'newlines-between': 'always',
46+
'alphabetize': { 'order': 'asc', 'caseInsensitive': true }
47+
}
48+
]
49+
},
50+
overrides: [
51+
{
52+
files: ['**/*.d.ts'],
53+
rules: {
54+
'@typescript-eslint/no-explicit-any': 'off'
55+
}
56+
},
57+
{
58+
files: ['scripts/*.ts'],
59+
rules: {
60+
'import/no-extraneous-dependencies': 'off'
61+
}
62+
}
63+
]
64+
};

packages/database-compat/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @firebase/database-compat
2+
3+
This is the compatibility layer for the Firebase Realtime Database component of the Firebase JS SDK.
4+
5+
**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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
18+
const karmaBase = require('../../config/karma.base');
19+
20+
const files = [`test/**/*.test.ts`];
21+
22+
module.exports = function (config) {
23+
const karmaConfig = Object.assign({}, karmaBase, {
24+
// files to load into karma
25+
files: files,
26+
// frameworks to use
27+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
28+
frameworks: ['mocha']
29+
});
30+
31+
config.set(karmaConfig);
32+
};
33+
34+
module.exports.files = files;

packages/database-compat/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@firebase/database-compat",
3+
"version": "0.0.900",
4+
"description": "The Realtime Database component of the Firebase JS SDK.",
5+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
6+
"main": "dist/index.js",
7+
"browser": "dist/index.esm2017.js",
8+
"module": "dist/index.esm2017.js",
9+
"esm5": "dist/index.esm5.js",
10+
"license": "Apache-2.0",
11+
"typings": "dist/database-compat/src/index.d.ts",
12+
"scripts": {
13+
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
14+
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
15+
"prettier": "prettier --write '*.js' '*.ts' '@(src|test)/**/*.ts'",
16+
"build": "rollup -c rollup.config.js",
17+
"build:release": "yarn build && yarn add-compat-overloads",
18+
"build:deps": "lerna run --scope @firebase/database-compat --include-dependencies build",
19+
"dev": "rollup -c -w",
20+
"test": "run-p lint test:browser test:node",
21+
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test",
22+
"test:browser": "karma start --single-run",
23+
"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",
24+
"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"
25+
},
26+
"peerDependencies": {
27+
"@firebase/app-compat": "0.x"
28+
},
29+
"dependencies": {
30+
"@firebase/database": "0.11.0",
31+
"@firebase/database-types": "0.8.0",
32+
"@firebase/logger": "0.2.6",
33+
"@firebase/util": "1.3.0",
34+
"@firebase/component": "0.5.6",
35+
"tslib": "^2.1.0"
36+
}
37+
}

packages/database/rollup.config.exp.js renamed to packages/database-compat/rollup.config.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2018 Google LLC
3+
* Copyright 2021 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,16 +18,12 @@
1818
import json from '@rollup/plugin-json';
1919
import typescriptPlugin from 'rollup-plugin-typescript2';
2020
import typescript from 'typescript';
21-
import path from 'path';
22-
import { importPathTransformer } from '../../scripts/exp/ts-transform-import-path';
2321

24-
import expPkg from './exp/package.json';
2522
import pkg from './package.json';
2623

27-
const deps = [
28-
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
29-
'@firebase/app'
30-
];
24+
const deps = Object.keys(
25+
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
26+
);
3127

3228
function onWarn(warning, defaultWarn) {
3329
if (warning.code === 'CIRCULAR_DEPENDENCY') {
@@ -42,8 +38,7 @@ function onWarn(warning, defaultWarn) {
4238
const es5BuildPlugins = [
4339
typescriptPlugin({
4440
typescript,
45-
abortOnError: false,
46-
transformers: [importPathTransformer]
41+
abortOnError: false
4742
}),
4843
json()
4944
];
@@ -53,9 +48,13 @@ const es5Builds = [
5348
* Node.js Build
5449
*/
5550
{
56-
input: 'exp/index.node.ts',
51+
input: 'src/index.node.ts',
5752
output: [
58-
{ file: path.resolve('exp', expPkg.main), format: 'cjs', sourcemap: true }
53+
{
54+
file: pkg.main,
55+
format: 'cjs',
56+
sourcemap: true
57+
}
5958
],
6059
plugins: es5BuildPlugins,
6160
treeshake: {
@@ -68,10 +67,10 @@ const es5Builds = [
6867
* Browser Builds
6968
*/
7069
{
71-
input: 'exp/index.ts',
70+
input: 'src/index.ts',
7271
output: [
7372
{
74-
file: path.resolve('exp', expPkg.esm5),
73+
file: pkg.esm5,
7574
format: 'es',
7675
sourcemap: true
7776
}
@@ -96,8 +95,7 @@ const es2017BuildPlugins = [
9695
target: 'es2017'
9796
}
9897
},
99-
abortOnError: false,
100-
transformers: [importPathTransformer]
98+
abortOnError: false
10199
}),
102100
json({ preferConst: true })
103101
];
@@ -107,10 +105,10 @@ const es2017Builds = [
107105
* Browser Build
108106
*/
109107
{
110-
input: 'exp/index.ts',
108+
input: 'src/index.ts',
111109
output: [
112110
{
113-
file: path.resolve('exp', expPkg.browser),
111+
file: pkg.browser,
114112
format: 'es',
115113
sourcemap: true
116114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)