Skip to content

Add Transaction #3153

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 95 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
da82b5d
Add Firestore, initializeFirestore, getFirestore to Firestore lite
schmidt-sebastian May 26, 2020
30bfc3f
Review
schmidt-sebastian May 27, 2020
f49096f
add one more tests
schmidt-sebastian May 27, 2020
da3403f
Fix lint
schmidt-sebastian May 27, 2020
bd3596e
Add lite to build
schmidt-sebastian May 27, 2020
3aac6f4
Add private
schmidt-sebastian May 27, 2020
6f05e2f
Fix Path
schmidt-sebastian May 27, 2020
634145a
Remove unused import
schmidt-sebastian May 27, 2020
8187cb7
Update package.json
schmidt-sebastian May 27, 2020
5760b02
Simplify
schmidt-sebastian May 27, 2020
4ba0f4e
Merge branch 'mrschmidt/initializefirestore' of github.com:firebase/f…
schmidt-sebastian May 27, 2020
e40faf4
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian May 27, 2020
f243a6c
Add DocumentReference
schmidt-sebastian May 27, 2020
d48405b
Feedback
schmidt-sebastian May 28, 2020
804fe94
Typo
schmidt-sebastian May 28, 2020
d244bcd
Lint fix
schmidt-sebastian May 28, 2020
bc342a3
Update integration.test.ts
schmidt-sebastian May 28, 2020
5297d47
Update rollup.config.lite.js
schmidt-sebastian May 28, 2020
867d9c0
One more test
schmidt-sebastian May 28, 2020
084fdcf
Update reference.ts
schmidt-sebastian May 28, 2020
ca44064
Add CollectionReference
schmidt-sebastian May 28, 2020
920c0e2
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian May 28, 2020
d5745f0
Merge
schmidt-sebastian May 28, 2020
b636d27
Merge
schmidt-sebastian May 28, 2020
31ba3ba
Add DocumentSnapshot
schmidt-sebastian May 28, 2020
505792f
Add exports
schmidt-sebastian May 28, 2020
3750a00
Add getDoc()
schmidt-sebastian May 28, 2020
5d3e2aa
Fix build
schmidt-sebastian May 28, 2020
58a84d1
More integration test fixes
schmidt-sebastian May 29, 2020
6f91171
Fix Integration tests
schmidt-sebastian May 29, 2020
c63977f
Add DocumentReference
schmidt-sebastian May 29, 2020
6352026
Add deleteDoc()
schmidt-sebastian May 29, 2020
be02305
Cleanup
schmidt-sebastian May 29, 2020
518631d
Merge
schmidt-sebastian May 29, 2020
6318fa4
Update datastore.ts
schmidt-sebastian May 29, 2020
a4ff5fd
Add setDoc()
schmidt-sebastian May 29, 2020
72b0b8b
Add setDoc()
schmidt-sebastian May 29, 2020
f3a4316
Add addDoc()
schmidt-sebastian May 30, 2020
efa58c4
Add converter
schmidt-sebastian May 30, 2020
7649b67
Throw when user data parsing fails
schmidt-sebastian May 30, 2020
3aa5d2f
Merge
schmidt-sebastian May 30, 2020
66d72d0
Throw when user data parsing fails
schmidt-sebastian May 30, 2020
e5b5d8f
Add test
schmidt-sebastian May 30, 2020
ca9ab39
Update test names
schmidt-sebastian May 30, 2020
97cf094
Merge branch 'mrschmidt/setdoc' into mrschmidt/adddoc
schmidt-sebastian May 30, 2020
1a81864
Simplify
schmidt-sebastian May 30, 2020
068d5a5
Simplify
schmidt-sebastian May 30, 2020
94e22df
Merge branch 'mrschmidt/newgetdocument' into mrschmidt/deletedoc
schmidt-sebastian May 30, 2020
555a16c
Merge branch 'mrschmidt/deletedoc' of github.com:firebase/firebase-js…
schmidt-sebastian May 30, 2020
61b7c4c
Merge branch 'mrschmidt/deletedoc' into mrschmidt/setdoc
schmidt-sebastian May 30, 2020
d8abac1
Simplify
schmidt-sebastian May 30, 2020
ad1a038
Merge branch 'mrschmidt/setdoc' into mrschmidt/adddoc
schmidt-sebastian May 30, 2020
b2fd485
Simplify
schmidt-sebastian May 30, 2020
df8186b
addDoc
schmidt-sebastian May 30, 2020
8a7fc93
Add documentID() (#3137)
schmidt-sebastian Jun 1, 2020
8bb9829
Add updateDoc()
schmidt-sebastian Jun 2, 2020
3242bc0
Add WriteBatch
schmidt-sebastian Jun 2, 2020
f3bfcb2
Use validateReference
schmidt-sebastian Jun 2, 2020
637b4bd
Update types
schmidt-sebastian Jun 2, 2020
1da4a6f
Add Transaction
schmidt-sebastian Jun 2, 2020
7736e3d
Lint
schmidt-sebastian Jun 2, 2020
5dc9471
Merge
schmidt-sebastian Jun 2, 2020
0230550
Typo
schmidt-sebastian Jun 2, 2020
c3e3375
Merge
schmidt-sebastian Jun 3, 2020
835ad93
Merge
schmidt-sebastian Jun 3, 2020
bd5a998
Merge
schmidt-sebastian Jun 3, 2020
f6553cb
Merge branch 'mrschmidt/snapshot' of github.com:firebase/firebase-js-…
schmidt-sebastian Jun 3, 2020
25f01e8
Merge
schmidt-sebastian Jun 3, 2020
0174149
Add terminate()
schmidt-sebastian Jun 3, 2020
7c4e78a
Lint
schmidt-sebastian Jun 3, 2020
5e62710
lint
schmidt-sebastian Jun 3, 2020
9aa0263
Add test
schmidt-sebastian Jun 3, 2020
2685912
Simplify
schmidt-sebastian Jun 4, 2020
6a34bbb
Merge
schmidt-sebastian Jun 4, 2020
3361a63
Feedback
schmidt-sebastian Jun 4, 2020
27bed90
Merge
schmidt-sebastian Jun 4, 2020
6f8521a
Merge branch 'master' into mrschmidt/newgetdocument
schmidt-sebastian Jun 4, 2020
4142278
Merge
schmidt-sebastian Jun 4, 2020
a005ed5
Merge
schmidt-sebastian Jun 4, 2020
d7200bd
Merge
schmidt-sebastian Jun 5, 2020
6af39e9
Merge
schmidt-sebastian Jun 5, 2020
b327858
Merge
schmidt-sebastian Jun 5, 2020
403fe89
Merge
schmidt-sebastian Jun 5, 2020
93d89cd
Lint
schmidt-sebastian Jun 5, 2020
7b6e899
Merge
schmidt-sebastian Jun 5, 2020
03f8223
Merge
schmidt-sebastian Jun 5, 2020
2216e4a
Merge
schmidt-sebastian Jun 5, 2020
29b9fa9
Merge branch 'master' into mrschmidt/terminate
schmidt-sebastian Jun 5, 2020
07005ec
fix build
schmidt-sebastian Jun 5, 2020
cba92df
Merge
schmidt-sebastian Jun 5, 2020
863a6d8
Fix
schmidt-sebastian Jun 5, 2020
8d14d84
Merge
schmidt-sebastian Jun 5, 2020
581a42c
Merge
schmidt-sebastian Jun 5, 2020
f77c7a9
Merge
schmidt-sebastian Jun 5, 2020
3d6bb9c
Feedback
schmidt-sebastian Jun 5, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion integration/firestore/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* 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.
Expand Down Expand Up @@ -51,6 +51,7 @@ function copyTests() {
testBase + '/integration/api/*.ts',
testBase + '/integration/util/events_accumulator.ts',
testBase + '/integration/util/helpers.ts',
testBase + '/integration/util/settings.ts',
testBase + '/util/equality_matcher.ts',
testBase + '/util/promise.ts'
],
Expand Down
24 changes: 24 additions & 0 deletions packages/firestore/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* @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: {
Expand Down Expand Up @@ -35,6 +52,13 @@ module.exports = {
rules: {
'@typescript-eslint/no-explicit-any': 'error'
}
},
// TODO(firestorelite): Remove this exception when app-exp is published
{
files: ['lite/**/*.ts'],
rules: {
'import/no-extraneous-dependencies': 'off'
}
}
]
};
19 changes: 11 additions & 8 deletions packages/firestore/lite/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

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

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

Expand Down Expand Up @@ -134,10 +134,11 @@ export class Transaction {

get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;

set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: SetOptions
data: Partial<T>,
options: SetOptions
): Transaction;

update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
Expand All @@ -154,10 +155,11 @@ export class Transaction {
export class WriteBatch {
private constructor();

set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: SetOptions
data: Partial<T>,
options: SetOptions
): WriteBatch;

update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
Expand Down Expand Up @@ -263,7 +265,7 @@ export function getQuery<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
export function addDoc<T>(
reference: CollectionReference<T>,
data: T
): Promise<DocumentSnapshot<T>>;
): Promise<DocumentReference<T>>;
export function setDoc<T>(
reference: DocumentReference<T>,
data: T
Expand All @@ -274,15 +276,16 @@ export function setDoc<T>(
options: SetOptions
): Promise<void>;
export function updateDoc(
reference: DocumentReference,
reference: DocumentReference<unknown>,
data: UpdateData
): Promise<void>;
export function updateDoc(
reference: DocumentReference<unknown>,
field: string | FieldPath,
value: any,
...moreFieldsAndValues: any[]
): Promise<void>;
export function deleteDoc(reference: DocumentReference): Promise<void>;
export function deleteDoc(reference: DocumentReference<unknown>): Promise<void>;

export class FieldValue {
private constructor();
Expand Down
70 changes: 70 additions & 0 deletions packages/firestore/lite/index.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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';

import '../src/platform_node/node_init';

export {
Firestore,
initializeFirestore,
getFirestore
} from './src/api/database';

export {
DocumentReference,
Query,
CollectionReference,
collection,
doc,
parent,
getDoc,
deleteDoc,
setDoc,
updateDoc,
addDoc
} from './src/api/reference';

export { FieldPath } from './src/api/field_path';

export { DocumentSnapshot, QueryDocumentSnapshot } from './src/api/snapshot';

export { WriteBatch, writeBatch } from './src/api/write_batch';

export { Transaction, runTransaction } from './src/api/transaction';

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();
6 changes: 6 additions & 0 deletions packages/firestore/lite/package.json
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
}
139 changes: 139 additions & 0 deletions packages/firestore/lite/src/api/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @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, DatabaseInfo } from '../../../src/core/database_info';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import {
CredentialsProvider,
FirebaseCredentialsProvider
} from '../../../src/api/credentials';
import { Datastore, newDatastore } from '../../../src/remote/datastore';
import { PlatformSupport } from '../../../src/platform/platform';
import { Deferred } from '../../../src/util/promise';

// settings() defaults:
const DEFAULT_HOST = 'firestore.googleapis.com';
const DEFAULT_SSL = true;

// 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;

// Assigned via _configureClient()/_ensureClientConfigured()
_settings?: firestore.Settings;
private readonly _datastoreDeferred = new Deferred<Datastore>();

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;

const databaseInfo = this._makeDatabaseInfo(settings);

// Kick off initializing the datastore but don't actually wait for it.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
PlatformSupport.getPlatform()
.loadConnection(databaseInfo)
.then(connection => {
const serializer = PlatformSupport.getPlatform().newSerializer(
databaseInfo.databaseId
);
const datastore = newDatastore(
connection,
this._credentials,
serializer
);
this._datastoreDeferred.resolve(datastore);
});
}

_ensureClientConfigured(): Promise<Datastore> {
if (!this._settings) {
this._settings = {};
}
return this._datastoreDeferred.promise;
}

private _makeDatabaseInfo(settings: firestore.Settings): DatabaseInfo {
return new DatabaseInfo(
this._databaseId,
/* persistenceKey= */ 'unsupported',
settings.host ?? DEFAULT_HOST,
settings.ssl ?? DEFAULT_SSL,
/* forceLongPolling= */ false
);
}

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;
}
49 changes: 49 additions & 0 deletions packages/firestore/lite/src/api/field_path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @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 '../../index';

import { tryCast } from './util';
import { BaseFieldPath } from '../../../src/api/field_path';

/**
* A FieldPath refers to a field in a document. The path may consist of a single
* field name (referring to a top-level field in the document), or a list of
* field names (referring to a nested field in the document).
*/
export class FieldPath extends BaseFieldPath implements firestore.FieldPath {
// Note: This class is stripped down a copy of the FieldPath class in the
// legacy SDK. The changes are:
// - The `documentId()` static method has been removed
// - Input validation is limited to errors that cannot be caught by the
// TypeScript transpiler.

/**
* Creates a FieldPath from the provided field names. If more than one field
* name is provided, the path will point to a nested field in a document.
*
* @param fieldNames A list of field names.
*/
constructor(...fieldNames: string[]) {
super(fieldNames);
}

isEqual(other: firestore.FieldPath): boolean {
const path = tryCast(other, FieldPath);
return this._internalPath.isEqual(path._internalPath);
}
}
Loading