Skip to content
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

Add DocumentReference #3123

Merged
merged 2 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 2 additions & 8 deletions packages/firestore/exp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ 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): Transaction;
set<T>(
documentRef: DocumentReference<T>,
data: Partial<T>,
Expand All @@ -193,10 +190,7 @@ export class Transaction {
export class WriteBatch {
private constructor();

set<T>(
documentRef: DocumentReference<T>,
data: T,
): WriteBatch;
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
set<T>(
documentRef: DocumentReference<T>,
data: Partial<T>,
Expand Down
2 changes: 2 additions & 0 deletions packages/firestore/lite/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export {
getFirestore
} from './src/api/database';

export { DocumentReference } from './src/api/reference';

// TOOD(firestorelite): Add tests when setDoc() is available
export {
FieldValue,
Expand Down
51 changes: 51 additions & 0 deletions packages/firestore/lite/src/api/reference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @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 { DocumentKey } from '../../../src/model/document_key';
import { Firestore } from './database';
import { DocumentKeyReference } from '../../../src/api/user_data_reader';

/**
* A reference to a particular document in a collection in the database.
*/
export class DocumentReference<T = firestore.DocumentData>
extends DocumentKeyReference<T>
implements firestore.DocumentReference<T> {
constructor(
readonly firestore: Firestore,
key: DocumentKey,
readonly _converter?: firestore.FirestoreDataConverter<T>
) {
super(firestore._databaseId, key, _converter);
}

get id(): string {
return this._key.path.lastSegment();
}

get path(): string {
return this._key.path.canonicalString();
}

withConverter<U>(
converter: firestore.FirestoreDataConverter<U>
): firestore.DocumentReference<U> {
return new DocumentReference<U>(this.firestore, this._key, converter);
}
}
11 changes: 10 additions & 1 deletion packages/firestore/src/api/user_data_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ import { PlatformSupport } from '../platform/platform';

const RESERVED_FIELD_REGEX = /^__.*__$/;

/**
* An untyped Firestore Data Converter interface that is shared between the
* lite, full and legacy SDK.
*/
export interface UntypedFirestoreDataConverter<T> {
toFirestore(modelObject: T): firestore.DocumentData;
fromFirestore(snapshot: unknown, options?: unknown): T;
}

/**
* A reference to a document in a Firebase project.
*
Expand All @@ -59,7 +68,7 @@ export class DocumentKeyReference<T> {
constructor(
public readonly _databaseId: DatabaseId,
public readonly _key: DocumentKey,
public readonly _converter?: firestore.FirestoreDataConverter<T>
public readonly _converter?: UntypedFirestoreDataConverter<T>
) {}
}

Expand Down