Skip to content

Commit

Permalink
chore: use public API types internally
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed May 30, 2020
1 parent 54bd08c commit f41f905
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 348 deletions.
23 changes: 17 additions & 6 deletions dev/src/bulk-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
Precondition,
SetOptions,
DocumentReference as PublicDocumentReference,
FieldPath as PublicFieldPath,
UpdateData,
} from '@google-cloud/firestore';

import * as assert from 'assert';

import {FieldPath, Firestore} from '.';
import {delayExecution} from './backoff';
import {RateLimiter} from './rate-limiter';
import {DocumentReference} from './reference';
import {Timestamp} from './timestamp';
import {Precondition, SetOptions, UpdateData} from './types';
import {Deferred} from './util';
import {BatchWriteResult, WriteBatch, WriteResult} from './write-batch';

Expand Down Expand Up @@ -102,7 +110,10 @@ class BulkCommitBatch {
* Adds a `create` operation to the WriteBatch. Returns a promise that
* resolves with the result of the write.
*/
create<T>(documentRef: DocumentReference<T>, data: T): Promise<WriteResult> {
create<T>(
documentRef: PublicDocumentReference<T>,
data: T
): Promise<WriteResult> {
this.writeBatch.create(documentRef, data);
return this.processOperation(documentRef);
}
Expand All @@ -112,7 +123,7 @@ class BulkCommitBatch {
* resolves with the result of the delete.
*/
delete<T>(
documentRef: DocumentReference<T>,
documentRef: PublicDocumentReference<T>,
precondition?: Precondition
): Promise<WriteResult> {
this.writeBatch.delete(documentRef, precondition);
Expand Down Expand Up @@ -148,8 +159,8 @@ class BulkCommitBatch {
* resolves with the result of the write.
*/
update<T>(
documentRef: DocumentReference<T>,
dataOrField: UpdateData | string | FieldPath,
documentRef: PublicDocumentReference<T>,
dataOrField: UpdateData | string | PublicFieldPath,
...preconditionOrValues: Array<
{lastUpdateTime?: Timestamp} | unknown | string | FieldPath
>
Expand All @@ -163,7 +174,7 @@ class BulkCommitBatch {
* return the result.
*/
private processOperation<T>(
documentRef: DocumentReference<T>
documentRef: PublicDocumentReference<T>
): Promise<WriteResult> {
assert(
!this.docPaths.has(documentRef.path),
Expand Down
10 changes: 7 additions & 3 deletions dev/src/document-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/

import {
DocumentChange as PublicDocumentChange,
DocumentData,
} from '@google-cloud/firestore';

import {QueryDocumentSnapshot} from './document';
import {DocumentData} from './types';

export type DocumentChangeType = 'added' | 'removed' | 'modified';

Expand All @@ -25,7 +29,7 @@ export type DocumentChangeType = 'added' | 'removed' | 'modified';
*
* @class
*/
export class DocumentChange<T = DocumentData> {
export class DocumentChange<T = DocumentData> implements PublicDocumentChange {
private readonly _type: DocumentChangeType;
private readonly _document: QueryDocumentSnapshot<T>;
private readonly _oldIndex: number;
Expand Down Expand Up @@ -170,7 +174,7 @@ export class DocumentChange<T = DocumentData> {
* @param {*} other The value to compare against.
* @return true if this `DocumentChange` is equal to the provided value.
*/
isEqual(other: DocumentChange<T>): boolean {
isEqual(other: PublicDocumentChange<T>): boolean {
if (this === other) {
return true;
}
Expand Down
52 changes: 33 additions & 19 deletions dev/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
* limitations under the License.
*/

import {
Timestamp as PublicTimestamp,
FieldPath as PublicFieldPath,
DocumentSnapshot as PublicDocumentSnapshot,
QueryDocumentSnapshot as PublicQueryDocumentSnapshot,
DocumentReference as PublicDocumentReference,
DocumentData,
} from '@google-cloud/firestore';

import * as deepEqual from 'fast-deep-equal';

import * as assert from 'assert';
Expand All @@ -24,7 +33,7 @@ import {FieldPath, validateFieldPath} from './path';
import {DocumentReference} from './reference';
import {Serializer} from './serializer';
import {Timestamp} from './timestamp';
import {ApiMapValue, defaultConverter, DocumentData, UpdateMap} from './types';
import {ApiMapValue, defaultConverter, UpdateMap} from './types';
import {isEmpty, isObject, isPlainObject} from './util';

import api = google.firestore.v1;
Expand Down Expand Up @@ -94,7 +103,8 @@ export class DocumentSnapshotBuilder<T = DocumentData> {
*
* @class
*/
export class DocumentSnapshot<T = DocumentData> {
export class DocumentSnapshot<T = DocumentData>
implements PublicDocumentSnapshot<T> {
private _ref: DocumentReference<T>;
private _serializer: Serializer;
private _readTime: Timestamp | undefined;
Expand Down Expand Up @@ -137,11 +147,14 @@ export class DocumentSnapshot<T = DocumentData> {
* @return The created DocumentSnapshot.
*/
static fromObject<U>(
ref: DocumentReference<U>,
ref: PublicDocumentReference<U>,
obj: DocumentData
): DocumentSnapshot<U> {
const serializer = ref.firestore._serializer!;
return new DocumentSnapshot(ref, serializer.encodeFields(obj));
const serializer = (ref as DocumentReference<U>).firestore._serializer!;
return new DocumentSnapshot(
ref as DocumentReference<U>,
serializer.encodeFields(obj)
);
}
/**
* Creates a DocumentSnapshot from an UpdateMap.
Expand All @@ -155,10 +168,10 @@ export class DocumentSnapshot<T = DocumentData> {
* @return The created DocumentSnapshot.
*/
static fromUpdateMap<U>(
ref: DocumentReference<U>,
ref: PublicDocumentReference<U>,
data: UpdateMap
): DocumentSnapshot<U> {
const serializer = ref.firestore._serializer!;
const serializer = (ref as DocumentReference<U>).firestore._serializer!;

/**
* Merges 'value' at the field path specified by the path array into
Expand Down Expand Up @@ -228,7 +241,7 @@ export class DocumentSnapshot<T = DocumentData> {
merge(res, value, path, 0);
}

return new DocumentSnapshot(ref, res);
return new DocumentSnapshot(ref as DocumentReference<U>, res);
}

/**
Expand Down Expand Up @@ -504,13 +517,13 @@ export class DocumentSnapshot<T = DocumentData> {
* @return {boolean} true if this `DocumentSnapshot` is equal to the provided
* value.
*/
isEqual(other: DocumentSnapshot<T>): boolean {
isEqual(other: PublicDocumentSnapshot<T>): boolean {
// Since the read time is different on every document read, we explicitly
// ignore all document metadata in this comparison.
return (
this === other ||
(other instanceof DocumentSnapshot &&
this._ref.isEqual(other._ref) &&
this._ref.isEqual((other as DocumentSnapshot<T>)._ref) &&
deepEqual(this._fieldsProto, other._fieldsProto))
);
}
Expand All @@ -531,9 +544,8 @@ export class DocumentSnapshot<T = DocumentData> {
* @class
* @extends DocumentSnapshot
*/
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<T>
implements PublicQueryDocumentSnapshot<T> {
/**
* The time the document was created.
*
Expand Down Expand Up @@ -644,7 +656,9 @@ export class DocumentMask {
* @private
* @param fieldMask A list of field paths.
*/
static fromFieldMask(fieldMask: Array<string | FieldPath>): DocumentMask {
static fromFieldMask(
fieldMask: Array<string | PublicFieldPath>
): DocumentMask {
const fieldPaths: FieldPath[] = [];

for (const fieldPath of fieldMask) {
Expand Down Expand Up @@ -889,7 +903,7 @@ export class DocumentTransform<T = DocumentData> {
* @returns The Document Transform.
*/
static fromObject<T>(
ref: DocumentReference<T>,
ref: PublicDocumentReference<T>,
obj: DocumentData
): DocumentTransform<T> {
const updateMap = new Map<FieldPath, unknown>();
Expand All @@ -910,7 +924,7 @@ export class DocumentTransform<T = DocumentData> {
* @returns The Document Transform.
*/
static fromUpdateMap<T>(
ref: DocumentReference<T>,
ref: PublicDocumentReference<T>,
data: UpdateMap
): DocumentTransform<T> {
const transforms = new Map<FieldPath, FieldTransform>();
Expand Down Expand Up @@ -944,7 +958,7 @@ export class DocumentTransform<T = DocumentData> {
encode_(value, FieldPath.fromArgument(key), true);
});

return new DocumentTransform(ref, transforms);
return new DocumentTransform(ref as DocumentReference<T>, transforms);
}

/**
Expand Down Expand Up @@ -1007,10 +1021,10 @@ export class Precondition {
* document in Firestore.
* @param options
*/
constructor(options?: {exists?: boolean; lastUpdateTime?: Timestamp}) {
constructor(options?: {exists?: boolean; lastUpdateTime?: PublicTimestamp}) {
if (options !== undefined) {
this._exists = options.exists;
this._lastUpdateTime = options.lastUpdateTime;
this._lastUpdateTime = options.lastUpdateTime as Timestamp;
}
}

Expand Down
4 changes: 3 additions & 1 deletion dev/src/field-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import {FieldValue as PublicFieldValue} from '@google-cloud/firestore';

import * as deepEqual from 'fast-deep-equal';

import * as proto from '../protos/firestore_v1_proto_api';
Expand All @@ -34,7 +36,7 @@ import api = proto.google.firestore.v1;
*
* @class
*/
export class FieldValue {
export class FieldValue implements PublicFieldValue {
/**
* @hideconstructor
*/
Expand Down
6 changes: 4 additions & 2 deletions dev/src/geo-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import {GeoPoint as PublicGeoPoint} from '@google-cloud/firestore';

import {google} from '../protos/firestore_v1_proto_api';
import {Serializable} from './serializer';
import {validateNumber} from './validate';
Expand All @@ -26,7 +28,7 @@ import api = google.firestore.v1;
*
* @class
*/
export class GeoPoint implements Serializable {
export class GeoPoint implements Serializable, PublicGeoPoint {
private readonly _latitude: number;
private readonly _longitude: number;

Expand Down Expand Up @@ -82,7 +84,7 @@ export class GeoPoint implements Serializable {
* @param {*} other The value to compare against.
* @return {boolean} true if this `GeoPoint` is equal to the provided value.
*/
isEqual(other: GeoPoint): boolean {
isEqual(other: PublicGeoPoint): boolean {
return (
this === other ||
(other instanceof GeoPoint &&
Expand Down
40 changes: 22 additions & 18 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
* limitations under the License.
*/

import {
ReadOptions,
Settings,
Firestore as PublicFirestore,
DocumentReference as PublicDocumentReference,
FieldPath as PublicFieldPath,
} from '@google-cloud/firestore';

import {CallOptions, grpc} from 'google-gax';
import {Duplex, PassThrough, Transform} from 'stream';
import {URL} from 'url';
Expand Down Expand Up @@ -48,8 +56,6 @@ import {
FirestoreStreamingMethod,
FirestoreUnaryMethod,
GapicClient,
ReadOptions,
Settings,
UnaryMethod,
} from './types';
import {autoId, Deferred, isPermanentRpcError, requestTag} from './util';
Expand Down Expand Up @@ -84,15 +90,7 @@ export {DocumentChange} from './document-change';
export {FieldPath} from './path';
export {GeoPoint} from './geo-point';
export {setLogFunction} from './logger';
export {
BulkWriterOptions,
FirestoreDataConverter,
UpdateData,
DocumentData,
Settings,
Precondition,
SetOptions,
} from './types';
export {BulkWriterOptions} from './types';
export {Status as GrpcStatus} from 'google-gax';

const libVersion = require('../../package.json').version;
Expand Down Expand Up @@ -305,7 +303,7 @@ const MAX_CONCURRENT_REQUESTS_PER_CLIENT = 100;
* region_tag:firestore_quickstart
* Full quickstart example:
*/
export class Firestore {
export class Firestore implements PublicFirestore {
/**
* A client pool to distribute requests over multiple GAPIC clients in order
* to work around a connection limit of 100 concurrent requests per client.
Expand Down Expand Up @@ -970,7 +968,9 @@ export class Firestore {
* });
*/
getAll<T>(
...documentRefsOrReadOptions: Array<DocumentReference<T> | ReadOptions>
...documentRefsOrReadOptions: Array<
PublicDocumentReference<T> | ReadOptions
>
): Promise<Array<DocumentSnapshot<T>>> {
validateMinNumberOfArguments(
'Firestore.getAll',
Expand Down Expand Up @@ -999,16 +999,16 @@ export class Firestore {
* @returns A Promise that contains an array with the resulting documents.
*/
getAll_<T>(
docRefs: Array<DocumentReference<T>>,
fieldMask: FieldPath[] | null,
docRefs: Array<PublicDocumentReference<T>>,
fieldMask: PublicFieldPath[] | null,
requestTag: string,
transactionId?: Uint8Array
): Promise<Array<DocumentSnapshot<T>>> {
const requestedDocuments = new Set<string>();
const retrievedDocuments = new Map<string, DocumentSnapshot>();

for (const docRef of docRefs) {
requestedDocuments.add(docRef.formattedName);
requestedDocuments.add((docRef as DocumentReference<T>).formattedName);
}

const request: api.IBatchGetDocumentsRequest = {
Expand All @@ -1018,7 +1018,9 @@ export class Firestore {
};

if (fieldMask) {
const fieldPaths = fieldMask.map(fieldPath => fieldPath.formattedName);
const fieldPaths = fieldMask.map(
fieldPath => (fieldPath as FieldPath).formattedName
);
request.mask = {fieldPaths};
}

Expand Down Expand Up @@ -1089,7 +1091,9 @@ export class Firestore {
if (document !== undefined) {
// Recreate the DocumentSnapshot with the DocumentReference
// containing the original converter.
const finalDoc = new DocumentSnapshotBuilder(docRef);
const finalDoc = new DocumentSnapshotBuilder(
docRef as DocumentReference<T>
);
finalDoc.fieldsProto = document._fieldsProto;
finalDoc.readTime = document.readTime;
finalDoc.createTime = document.createTime;
Expand Down
Loading

0 comments on commit f41f905

Please sign in to comment.