Skip to content

Commit dc583be

Browse files
committed
transaction.ts: fix precondition for deleted documents
1 parent 06d859e commit dc583be

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

packages/firestore/src/core/transaction.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { SnapshotVersion } from './snapshot_version';
4040
*/
4141
export class Transaction {
4242
// The version of each document that was read during this transaction.
43-
private readVersions = new Map</* path */ string, SnapshotVersion>();
43+
private readVersions = new Map</* path */ string, SnapshotVersion | null>();
4444
private mutations: Mutation[] = [];
4545
private committed = false;
4646

@@ -115,20 +115,24 @@ export class Transaction {
115115
}
116116

117117
private recordVersion(doc: Document): void {
118-
let docVersion: SnapshotVersion;
118+
let docVersion: SnapshotVersion | null;
119119

120120
if (doc.isFoundDocument()) {
121121
docVersion = doc.version;
122122
} else if (doc.isNoDocument()) {
123-
// For deleted docs, we must use baseVersion 0 when we overwrite them.
124-
docVersion = SnapshotVersion.min();
123+
// For deleted docs, we must use {exists: false} when we overwrite them.
124+
docVersion = null;
125125
} else {
126126
throw fail('Document in a transaction was a ' + doc.constructor.name);
127127
}
128128

129129
const existingVersion = this.readVersions.get(doc.key.toString());
130-
if (existingVersion) {
131-
if (!docVersion.isEqual(existingVersion)) {
130+
if (existingVersion !== undefined && existingVersion !== docVersion) {
131+
if (
132+
docVersion == null ||
133+
existingVersion == null ||
134+
!docVersion.isEqual(existingVersion)
135+
) {
132136
// This transaction will fail no matter what.
133137
throw new FirestoreError(
134138
Code.ABORTED,
@@ -146,8 +150,12 @@ export class Transaction {
146150
*/
147151
private precondition(key: DocumentKey): Precondition {
148152
const version = this.readVersions.get(key.toString());
149-
if (!this.writtenDocs.has(key.toString()) && version) {
150-
return Precondition.updateTime(version);
153+
if (!this.writtenDocs.has(key.toString()) && version !== undefined) {
154+
if (version == null) {
155+
return Precondition.exists(false);
156+
} else {
157+
return Precondition.updateTime(version);
158+
}
151159
} else {
152160
return Precondition.none();
153161
}
@@ -160,8 +168,8 @@ export class Transaction {
160168
const version = this.readVersions.get(key.toString());
161169
// The first time a document is written, we want to take into account the
162170
// read time and existence
163-
if (!this.writtenDocs.has(key.toString()) && version) {
164-
if (version.isEqual(SnapshotVersion.min())) {
171+
if (!this.writtenDocs.has(key.toString()) && version !== undefined) {
172+
if (version === null) {
165173
// The document doesn't exist, so fail the transaction.
166174

167175
// This has to be validated locally because you can't send a

0 commit comments

Comments
 (0)