Skip to content

Commit da82b5d

Browse files
Add Firestore, initializeFirestore, getFirestore to Firestore lite
1 parent 5ffa43b commit da82b5d

File tree

5 files changed

+262
-1
lines changed

5 files changed

+262
-1
lines changed

packages/firestore/lite/index.node.ts

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

packages/firestore/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js",
1212
"build:deps": "lerna run --scope @firebase/'{app,firestore}' --include-dependencies build",
1313
"build:console": "node tools/console.build.js",
14-
"build:exp": "rollup -c rollup.config.exp.js",
14+
"build:exp": "rollup -c rollup.config.exp.js",
15+
"build:lite": "lerna run --scope @firebase/app-exp --include-dependencies build && 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,8 +33,11 @@
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
},
40+
"lite": "dist/lite/index.node.cjs.js",
3741
"main": "dist/index.node.cjs.js",
3842
"main-esm2017": "dist/index.node.esm2017.js",
3943
"browser": "dist/index.cjs.js",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license
3+
* Copyright 2018 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 './package.json';
25+
26+
const defaultPlugins = [
27+
typescriptPlugin({
28+
typescript,
29+
tsconfigOverride: {
30+
compilerOptions: {
31+
target: 'es2017'
32+
}
33+
},
34+
clean: true
35+
}),
36+
json({ preferConst: true })
37+
];
38+
39+
const nodeBuilds = [
40+
{
41+
input: 'lite/index.node.ts',
42+
output: {
43+
file: pkg.lite,
44+
format: 'es'
45+
},
46+
plugins: defaultPlugins,
47+
external: resolveNodeExterns,
48+
treeshake: {
49+
tryCatchDeoptimization: false
50+
}
51+
}
52+
];
53+
54+
export default [...nodeBuilds];

0 commit comments

Comments
 (0)