Skip to content

Commit 6e6dbe2

Browse files
Add Firestore, initializeFirestore, getFirestore to Firestore lite (#3108)
1 parent 0d6f14e commit 6e6dbe2

File tree

8 files changed

+299
-2
lines changed

8 files changed

+299
-2
lines changed

packages/firestore/.eslintrc.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
118
module.exports = {
219
extends: '../../config/.eslintrc.js',
320
parserOptions: {
@@ -35,6 +52,13 @@ module.exports = {
3552
rules: {
3653
'@typescript-eslint/no-explicit-any': 'error'
3754
}
55+
},
56+
// TODO(firestorelite): Remove this exception when app-exp is published
57+
{
58+
files: ['lite/**/*.ts'],
59+
rules: {
60+
'import/no-extraneous-dependencies': 'off'
61+
}
3862
}
3963
]
4064
};

packages/firestore/lite/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp } from '@firebase/app-types';
18+
import { FirebaseApp } from '@firebase/app-types-exp';
1919

2020
/* eslint-disable @typescript-eslint/no-explicit-any */
2121

packages/firestore/lite/index.node.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
import { registerVersion, _registerComponent } from '@firebase/app-exp';
19+
import { Firestore } from './src/api/database';
20+
import { version } from '../package.json';
21+
import { Component, ComponentType } from '@firebase/component';
22+
23+
export {
24+
Firestore,
25+
initializeFirestore,
26+
getFirestore
27+
} from './src/api/database';
28+
29+
export function registerFirestore(): void {
30+
_registerComponent(
31+
new Component(
32+
'firestore/lite',
33+
container => {
34+
const app = container.getProvider('app-exp').getImmediate()!;
35+
return ((app, auth) => new Firestore(app, auth))(
36+
app,
37+
container.getProvider('auth-internal')
38+
);
39+
},
40+
ComponentType.PUBLIC
41+
)
42+
);
43+
registerVersion('firestore-lite', version, 'node');
44+
}
45+
46+
registerFirestore();

packages/firestore/lite/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@firebase/firestore/lite",
3+
"description": "A lite version of the Firestore SDK",
4+
"main": "../dist/lite/index.node.cjs.js",
5+
"private": true
6+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
import * as firestore from '../../';
19+
20+
import { _getProvider } from '@firebase/app-exp';
21+
import { FirebaseApp } from '@firebase/app-types-exp';
22+
import { Provider } from '@firebase/component';
23+
24+
import { Code, FirestoreError } from '../../../src/util/error';
25+
import { DatabaseId } from '../../../src/core/database_info';
26+
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
27+
import {
28+
CredentialsProvider,
29+
FirebaseCredentialsProvider
30+
} from '../../../src/api/credentials';
31+
32+
// TODO(firestorelite): Depend on FirebaseService once #3112 is merged
33+
34+
/**
35+
* The root reference to the Firestore Lite database.
36+
*/
37+
export class Firestore implements firestore.FirebaseFirestore {
38+
readonly _databaseId: DatabaseId;
39+
private readonly _firebaseApp: FirebaseApp;
40+
private readonly _credentials: CredentialsProvider;
41+
private _settings?: firestore.Settings;
42+
43+
constructor(
44+
app: FirebaseApp,
45+
authProvider: Provider<FirebaseAuthInternalName>
46+
) {
47+
this._firebaseApp = app;
48+
this._databaseId = Firestore.databaseIdFromApp(app);
49+
this._credentials = new FirebaseCredentialsProvider(authProvider);
50+
}
51+
52+
get app(): FirebaseApp {
53+
return this._firebaseApp;
54+
}
55+
56+
_configureClient(settings: firestore.Settings): void {
57+
if (this._settings) {
58+
throw new FirestoreError(
59+
Code.FAILED_PRECONDITION,
60+
'Firestore has already been started and its settings can no longer ' +
61+
'be changed. initializeFirestore() cannot be called after calling ' +
62+
'getFirestore().'
63+
);
64+
}
65+
this._settings = settings;
66+
}
67+
68+
_ensureClientConfigured(): void {
69+
if (!this._settings) {
70+
this._settings = {};
71+
}
72+
}
73+
74+
private static databaseIdFromApp(app: FirebaseApp): DatabaseId {
75+
if (!Object.prototype.hasOwnProperty.apply(app.options, ['projectId'])) {
76+
throw new FirestoreError(
77+
Code.INVALID_ARGUMENT,
78+
'"projectId" not provided in firebase.initializeApp.'
79+
);
80+
}
81+
82+
return new DatabaseId(app.options.projectId!);
83+
}
84+
}
85+
86+
export function initializeFirestore(
87+
app: FirebaseApp,
88+
settings: firestore.Settings
89+
): Firestore {
90+
const firestore = _getProvider(
91+
app,
92+
'firestore/lite'
93+
).getImmediate() as Firestore;
94+
firestore._configureClient(settings);
95+
return firestore;
96+
}
97+
98+
export function getFirestore(app: FirebaseApp): Firestore {
99+
return _getProvider(app, 'firestore/lite').getImmediate() as Firestore;
100+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import { expect } from 'chai';
19+
20+
import { initializeApp } from '@firebase/app-exp';
21+
import {
22+
Firestore,
23+
getFirestore,
24+
initializeFirestore
25+
} from '../src/api/database';
26+
27+
describe('Firestore', () => {
28+
it('can provide setting', () => {
29+
const app = initializeApp(
30+
{ apiKey: 'fake-api-key', projectId: 'test-project' },
31+
'test-app-initializeFirestore'
32+
);
33+
const fs1 = initializeFirestore(app, { host: 'localhost', ssl: false });
34+
expect(fs1).to.be.an.instanceOf(Firestore);
35+
});
36+
37+
it('returns same instance', () => {
38+
const app = initializeApp(
39+
{ apiKey: 'fake-api-key', projectId: 'test-project' },
40+
'test-app-getFirestore'
41+
);
42+
const fs1 = getFirestore(app);
43+
const fs2 = getFirestore(app);
44+
expect(fs1 === fs2).to.be.true;
45+
});
46+
47+
it('cannot call initializeFirestore() twice', () => {
48+
const app = initializeApp(
49+
{ apiKey: 'fake-api-key', projectId: 'test-project' },
50+
'test-app-initializeFirestore-twice'
51+
);
52+
initializeFirestore(app, { host: 'localhost', ssl: false });
53+
expect(() => {
54+
initializeFirestore(app, { host: 'localhost', ssl: false });
55+
}).to.throw(
56+
'Firestore has already been started and its settings can no longer be changed.'
57+
);
58+
});
59+
});

packages/firestore/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
99
"scripts": {
1010
"prebuild": "tsc -m es2015 --moduleResolution node scripts/*.ts ",
11-
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js",
11+
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js && yarn build:lite",
1212
"build:deps": "lerna run --scope @firebase/'{app,firestore}' --include-dependencies build",
1313
"build:console": "node tools/console.build.js",
1414
"build:exp": "rollup -c rollup.config.exp.js",
15+
"build:lite": "rollup -c rollup.config.lite.js",
1516
"predev": "yarn prebuild",
1617
"dev": "rollup -c -w",
1718
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
@@ -32,6 +33,8 @@
3233
"test:node:persistence:prod": "USE_MOCK_PERSISTENCE=YES TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --require ts-node/register --require index.node.ts --require test/util/node_persistence.ts --config ../../config/mocharc.node.js",
3334
"test:travis": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/firestore-test-runner.ts",
3435
"test:minified": "(cd ../../integration/firestore ; yarn test)",
36+
"pretest:lite": "yarn build:lite",
37+
"test:lite": "TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'lite/test/**/*.test.ts' --file lite/index.node.ts --config ../../config/mocharc.node.js",
3538
"prepare": "yarn build"
3639
},
3740
"main": "dist/index.node.cjs.js",
@@ -60,6 +63,8 @@
6063
"@firebase/app-types": "0.x"
6164
},
6265
"devDependencies": {
66+
"@firebase/app-exp": "0.x",
67+
"@firebase/app-types-exp": "0.x",
6368
"@types/json-stable-stringify": "1.0.32",
6469
"json-stable-stringify": "1.0.1",
6570
"protobufjs": "6.9.0",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import json from 'rollup-plugin-json';
19+
import typescriptPlugin from 'rollup-plugin-typescript2';
20+
import typescript from 'typescript';
21+
22+
import { resolveNodeExterns } from './rollup.shared';
23+
24+
import pkg from './lite/package.json';
25+
import path from 'path';
26+
27+
const defaultPlugins = [
28+
typescriptPlugin({
29+
typescript,
30+
tsconfigOverride: {
31+
compilerOptions: {
32+
target: 'es2017'
33+
}
34+
},
35+
clean: true
36+
}),
37+
json({ preferConst: true })
38+
];
39+
40+
const nodeBuilds = [
41+
{
42+
input: 'lite/index.node.ts',
43+
output: {
44+
file: path.resolve('./lite', pkg.main),
45+
format: 'es'
46+
},
47+
plugins: defaultPlugins,
48+
external: resolveNodeExterns,
49+
treeshake: {
50+
tryCatchDeoptimization: false
51+
}
52+
}
53+
];
54+
55+
// TODO(firestorelite): Add browser builds
56+
57+
export default [...nodeBuilds];

0 commit comments

Comments
 (0)