-
Notifications
You must be signed in to change notification settings - Fork 946
Add Firestore, initializeFirestore, getFirestore to Firestore lite #3108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
da82b5d
Add Firestore, initializeFirestore, getFirestore to Firestore lite
schmidt-sebastian 30bfc3f
Review
schmidt-sebastian f49096f
add one more tests
schmidt-sebastian da3403f
Fix lint
schmidt-sebastian bd3596e
Add lite to build
schmidt-sebastian 3aac6f4
Add private
schmidt-sebastian 6f05e2f
Fix Path
schmidt-sebastian 634145a
Remove unused import
schmidt-sebastian 8187cb7
Update package.json
schmidt-sebastian 5760b02
Simplify
schmidt-sebastian 4ba0f4e
Merge branch 'mrschmidt/initializefirestore' of github.com:firebase/f…
schmidt-sebastian e40faf4
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian d48405b
Feedback
schmidt-sebastian 804fe94
Typo
schmidt-sebastian d244bcd
Lint fix
schmidt-sebastian bc342a3
Update integration.test.ts
schmidt-sebastian 5297d47
Update rollup.config.lite.js
schmidt-sebastian 867d9c0
One more test
schmidt-sebastian 920c0e2
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @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. | ||
*/ | ||
|
||
import { registerVersion, _registerComponent } from '@firebase/app-exp'; | ||
import { Firestore } from './src/api/database'; | ||
import { version } from '../package.json'; | ||
import { Component, ComponentType } from '@firebase/component'; | ||
|
||
export { | ||
Firestore, | ||
initializeFirestore, | ||
getFirestore | ||
} from './src/api/database'; | ||
|
||
export function registerFirestore(): void { | ||
_registerComponent( | ||
new Component( | ||
'firestore/lite', | ||
container => { | ||
const app = container.getProvider('app-exp').getImmediate()!; | ||
return ((app, auth) => new Firestore(app, auth))( | ||
app, | ||
container.getProvider('auth-internal') | ||
); | ||
}, | ||
ComponentType.PUBLIC | ||
) | ||
); | ||
registerVersion('firestore-lite', version, 'node'); | ||
} | ||
|
||
registerFirestore(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@firebase/firestore/lite", | ||
"description": "A lite version of the Firestore SDK", | ||
"main": "../dist/lite/index.node.cjs.js", | ||
"private": true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* @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. | ||
*/ | ||
|
||
import * as firestore from '../../'; | ||
|
||
import { _getProvider } from '@firebase/app-exp'; | ||
import { FirebaseApp } from '@firebase/app-types-exp'; | ||
import { Provider } from '@firebase/component'; | ||
|
||
import { Code, FirestoreError } from '../../../src/util/error'; | ||
import { DatabaseId } from '../../../src/core/database_info'; | ||
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types'; | ||
import { | ||
CredentialsProvider, | ||
FirebaseCredentialsProvider | ||
} from '../../../src/api/credentials'; | ||
|
||
// TODO(firestorelite): Depend on FirebaseService once #3112 is merged | ||
|
||
/** | ||
* The root reference to the Firestore Lite database. | ||
*/ | ||
export class Firestore implements firestore.FirebaseFirestore { | ||
readonly _databaseId: DatabaseId; | ||
private readonly _firebaseApp: FirebaseApp; | ||
private readonly _credentials: CredentialsProvider; | ||
private _settings?: firestore.Settings; | ||
|
||
constructor( | ||
app: FirebaseApp, | ||
authProvider: Provider<FirebaseAuthInternalName> | ||
) { | ||
this._firebaseApp = app; | ||
this._databaseId = Firestore.databaseIdFromApp(app); | ||
this._credentials = new FirebaseCredentialsProvider(authProvider); | ||
} | ||
|
||
get app(): FirebaseApp { | ||
return this._firebaseApp; | ||
} | ||
|
||
_configureClient(settings: firestore.Settings): void { | ||
if (this._settings) { | ||
throw new FirestoreError( | ||
Code.FAILED_PRECONDITION, | ||
'Firestore has already been started and its settings can no longer ' + | ||
'be changed. initializeFirestore() cannot be called after calling ' + | ||
'getFirestore().' | ||
); | ||
} | ||
this._settings = settings; | ||
} | ||
|
||
_ensureClientConfigured(): void { | ||
if (!this._settings) { | ||
this._settings = {}; | ||
} | ||
} | ||
|
||
private static databaseIdFromApp(app: FirebaseApp): DatabaseId { | ||
if (!Object.prototype.hasOwnProperty.apply(app.options, ['projectId'])) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'"projectId" not provided in firebase.initializeApp.' | ||
); | ||
} | ||
|
||
return new DatabaseId(app.options.projectId!); | ||
} | ||
} | ||
|
||
export function initializeFirestore( | ||
app: FirebaseApp, | ||
settings: firestore.Settings | ||
): Firestore { | ||
const firestore = _getProvider( | ||
app, | ||
'firestore/lite' | ||
).getImmediate() as Firestore; | ||
firestore._configureClient(settings); | ||
return firestore; | ||
} | ||
|
||
export function getFirestore(app: FirebaseApp): Firestore { | ||
return _getProvider(app, 'firestore/lite').getImmediate() as Firestore; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @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. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { initializeApp } from '@firebase/app-exp'; | ||
import { | ||
Firestore, | ||
getFirestore, | ||
initializeFirestore | ||
} from '../src/api/database'; | ||
|
||
describe('Firestore', () => { | ||
it('can provide setting', () => { | ||
const app = initializeApp( | ||
{ apiKey: 'fake-api-key', projectId: 'test-project' }, | ||
'test-app-initializeFirestore' | ||
); | ||
const fs1 = initializeFirestore(app, { host: 'localhost', ssl: false }); | ||
expect(fs1).to.be.an.instanceOf(Firestore); | ||
}); | ||
|
||
it('returns same instance', () => { | ||
const app = initializeApp( | ||
{ apiKey: 'fake-api-key', projectId: 'test-project' }, | ||
'test-app-getFirestore' | ||
); | ||
const fs1 = getFirestore(app); | ||
const fs2 = getFirestore(app); | ||
expect(fs1 === fs2).to.be.true; | ||
}); | ||
|
||
it('cannot call initializeFirestore() twice', () => { | ||
const app = initializeApp( | ||
{ apiKey: 'fake-api-key', projectId: 'test-project' }, | ||
'test-app-initializeFirestore-twice' | ||
); | ||
initializeFirestore(app, { host: 'localhost', ssl: false }); | ||
expect(() => { | ||
initializeFirestore(app, { host: 'localhost', ssl: false }); | ||
}).to.throw( | ||
'Firestore has already been started and its settings can no longer be changed.' | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,11 @@ | |
"author": "Firebase <[email protected]> (https://firebase.google.com/)", | ||
"scripts": { | ||
"prebuild": "tsc -m es2015 --moduleResolution node scripts/*.ts ", | ||
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js", | ||
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js && yarn build:lite", | ||
"build:deps": "lerna run --scope @firebase/'{app,firestore}' --include-dependencies build", | ||
"build:console": "node tools/console.build.js", | ||
"build:exp": "rollup -c rollup.config.exp.js", | ||
"build:lite": "rollup -c rollup.config.lite.js", | ||
"predev": "yarn prebuild", | ||
"dev": "rollup -c -w", | ||
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'", | ||
|
@@ -32,6 +33,8 @@ | |
"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", | ||
"test:travis": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/firestore-test-runner.ts", | ||
"test:minified": "(cd ../../integration/firestore ; yarn test)", | ||
"pretest:lite": "yarn build:lite", | ||
"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", | ||
"prepare": "yarn build" | ||
}, | ||
"main": "dist/index.node.cjs.js", | ||
|
@@ -60,6 +63,8 @@ | |
"@firebase/app-types": "0.x" | ||
}, | ||
"devDependencies": { | ||
"@firebase/app-exp": "0.x", | ||
"@firebase/app-types-exp": "0.x", | ||
"@types/json-stable-stringify": "1.0.32", | ||
"json-stable-stringify": "1.0.1", | ||
"protobufjs": "6.9.0", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @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. | ||
*/ | ||
|
||
import json from 'rollup-plugin-json'; | ||
import typescriptPlugin from 'rollup-plugin-typescript2'; | ||
import typescript from 'typescript'; | ||
|
||
import { resolveNodeExterns } from './rollup.shared'; | ||
|
||
import pkg from './lite/package.json'; | ||
import path from 'path'; | ||
|
||
const defaultPlugins = [ | ||
typescriptPlugin({ | ||
typescript, | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
target: 'es2017' | ||
} | ||
}, | ||
clean: true | ||
}), | ||
json({ preferConst: true }) | ||
]; | ||
|
||
const nodeBuilds = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you will add browser builds eventually? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This slows down development for now. Added TODO. |
||
{ | ||
input: 'lite/index.node.ts', | ||
output: { | ||
file: path.resolve('./lite', pkg.main), | ||
format: 'es' | ||
}, | ||
plugins: defaultPlugins, | ||
external: resolveNodeExterns, | ||
treeshake: { | ||
tryCatchDeoptimization: false | ||
} | ||
} | ||
]; | ||
|
||
// TODO(firestorelite): Add browser builds | ||
|
||
export default [...nodeBuilds]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.