Skip to content

Commit 8b903fe

Browse files
Checking for UnknownDocument (#1174)
* Checking for UnknownDocument * Remove unused import * Adding an assert in Transaction
1 parent 92ae0fc commit 8b903fe

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

packages/firestore/src/api/database.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,11 @@ export class Transaction implements firestore.Transaction {
597597
} else if (doc instanceof Document) {
598598
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
599599
} else {
600-
throw fail('MaybeDocument is neither Document nor NoDocument');
600+
throw fail(
601+
`BatchGetDocumentsRequest returned unexpected document type: ${
602+
doc.constructor.name
603+
}`
604+
);
601605
}
602606
});
603607
}

packages/firestore/src/core/transaction.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
import { ParsedSetData, ParsedUpdateData } from '../api/user_data_converter';
1818
import { SnapshotVersion } from './snapshot_version';
1919
import { documentVersionMap } from '../model/collections';
20-
import { NoDocument } from '../model/document';
20+
import { NoDocument, Document } from '../model/document';
2121
import { MaybeDocument } from '../model/document';
2222
import { DocumentKey } from '../model/document_key';
2323
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
2424
import { Datastore } from '../remote/datastore';
2525
import { Code, FirestoreError } from '../util/error';
26+
import { fail } from '../util/assert';
2627

2728
/**
2829
* Internal transaction object responsible for accumulating the mutations to
@@ -36,7 +37,7 @@ export class Transaction {
3637

3738
constructor(private datastore: Datastore) {}
3839

39-
private recordVersion(doc: MaybeDocument): void {
40+
private recordVersion(doc: NoDocument | Document): void {
4041
let docVersion = doc.version;
4142
if (doc instanceof NoDocument) {
4243
// For deleted docs, we must use baseVersion 0 when we overwrite them.
@@ -68,7 +69,13 @@ export class Transaction {
6869
);
6970
}
7071
return this.datastore.lookup(keys).then(docs => {
71-
docs.forEach(doc => this.recordVersion(doc));
72+
docs.forEach(doc => {
73+
if (doc instanceof NoDocument || doc instanceof Document) {
74+
this.recordVersion(doc);
75+
} else {
76+
fail('Document in a transaction was a ' + doc.constructor.name);
77+
}
78+
});
7279
return docs;
7380
});
7481
}

packages/firestore/src/local/local_documents_view.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { Document, MaybeDocument, NoDocument } from '../model/document';
2727
import { DocumentKey } from '../model/document_key';
2828
import { MutationBatch } from '../model/mutation_batch';
2929
import { ResourcePath } from '../model/path';
30-
import { fail } from '../util/assert';
3130

3231
import { MutationQueue } from './mutation_queue';
3332
import { PersistenceTransaction } from './persistence';
@@ -168,12 +167,10 @@ export class LocalDocumentsView {
168167
baseDoc,
169168
batch.localWriteTime
170169
);
171-
if (!mutatedDoc || mutatedDoc instanceof NoDocument) {
172-
results = results.remove(key);
173-
} else if (mutatedDoc instanceof Document) {
170+
if (mutatedDoc instanceof Document) {
174171
results = results.insert(key, mutatedDoc);
175172
} else {
176-
fail('Unknown MaybeDocument: ' + mutatedDoc);
173+
results = results.remove(key);
177174
}
178175
}
179176
}

packages/firestore/src/model/mutation.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ export class Precondition {
156156
maybeDoc.version.isEqual(this.updateTime)
157157
);
158158
} else if (this.exists !== undefined) {
159-
if (this.exists) {
160-
return maybeDoc instanceof Document;
161-
} else {
162-
return maybeDoc === null || maybeDoc instanceof NoDocument;
163-
}
159+
return this.exists === maybeDoc instanceof Document;
164160
} else {
165161
assert(this.isNone, 'Precondition should be empty');
166162
return true;

0 commit comments

Comments
 (0)