@@ -40,7 +40,7 @@ import { SnapshotVersion } from './snapshot_version';
40
40
*/
41
41
export class Transaction {
42
42
// 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 > ( ) ;
44
44
private mutations : Mutation [ ] = [ ] ;
45
45
private committed = false ;
46
46
@@ -115,20 +115,24 @@ export class Transaction {
115
115
}
116
116
117
117
private recordVersion ( doc : Document ) : void {
118
- let docVersion : SnapshotVersion ;
118
+ let docVersion : SnapshotVersion | null ;
119
119
120
120
if ( doc . isFoundDocument ( ) ) {
121
121
docVersion = doc . version ;
122
122
} 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 ;
125
125
} else {
126
126
throw fail ( 'Document in a transaction was a ' + doc . constructor . name ) ;
127
127
}
128
128
129
129
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
+ ) {
132
136
// This transaction will fail no matter what.
133
137
throw new FirestoreError (
134
138
Code . ABORTED ,
@@ -146,8 +150,12 @@ export class Transaction {
146
150
*/
147
151
private precondition ( key : DocumentKey ) : Precondition {
148
152
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
+ }
151
159
} else {
152
160
return Precondition . none ( ) ;
153
161
}
@@ -160,8 +168,8 @@ export class Transaction {
160
168
const version = this . readVersions . get ( key . toString ( ) ) ;
161
169
// The first time a document is written, we want to take into account the
162
170
// 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 ) {
165
173
// The document doesn't exist, so fail the transaction.
166
174
167
175
// This has to be validated locally because you can't send a
0 commit comments