Skip to content

Commit 28d645b

Browse files
author
Brian Chen
authored
feat: add Precondition.exists to delete() (#1505)
1 parent c2c1606 commit 28d645b

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

dev/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const MAX_CONCURRENT_REQUESTS_PER_CLIENT = 100;
225225
* [update()]{@link DocumentReference#update} and
226226
* [delete()]{@link DocumentReference#delete} calls in
227227
* [DocumentReference]{@link DocumentReference},
228-
* [WriteBatch]{@link WriteBatch}, and
228+
* [WriteBatch]{@link WriteBatch}, [BulkWriter]({@link BulkWriter}, and
229229
* [Transaction]{@link Transaction}. Using Preconditions, these calls
230230
* can be restricted to only apply to documents that match the specified
231231
* conditions.
@@ -243,6 +243,8 @@ const MAX_CONCURRENT_REQUESTS_PER_CLIENT = 100;
243243
* @property {Timestamp} lastUpdateTime The update time to enforce. If set,
244244
* enforces that the document was last updated at lastUpdateTime. Fails the
245245
* operation if the document was last updated at a different time.
246+
* @property {boolean} exists If set, enforces that the target document must
247+
* or must not exist.
246248
* @typedef {Object} Precondition
247249
*/
248250

dev/src/reference.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ export class DocumentReference<T = firestore.DocumentData>
371371
* @param {Timestamp=} precondition.lastUpdateTime If set, enforces that the
372372
* document was last updated at lastUpdateTime. Fails the delete if the
373373
* document was last updated at a different time.
374+
* @param {boolean=} precondition.exists If set, enforces that the target
375+
* document must or must not exist.
374376
* @returns {Promise.<WriteResult>} A Promise that resolves with the
375377
* delete time.
376378
*

dev/src/transaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ export class Transaction implements firestore.Transaction {
330330
* @param {Timestamp=} precondition.lastUpdateTime If set, enforces that the
331331
* document was last updated at lastUpdateTime. Fails the transaction if the
332332
* document doesn't exist or was last updated at a different time.
333+
* @param {boolean=} precondition.exists If set, enforces that the target
334+
* document must or must not exist.
333335
* @returns {Transaction} This Transaction instance. Used for
334336
* chaining method calls.
335337
*

dev/src/write-batch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ export class WriteBatch implements firestore.WriteBatch {
222222
* @param {Timestamp=} precondition.lastUpdateTime If set, enforces that the
223223
* document was last updated at lastUpdateTime. Fails the batch if the
224224
* document doesn't exist or was last updated at a different time.
225+
* @param {boolean= } precondition.exists If set to true, enforces that the target
226+
* document must or must not exist.
225227
* @returns {WriteBatch} This WriteBatch instance. Used for chaining
226228
* method calls.
227229
*

dev/system-test/firestore.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,16 @@ describe('DocumentReference class', () => {
735735
return ref.delete();
736736
});
737737

738+
it('will fail to delete document with exists: true if doc does not exist', () => {
739+
const ref = randomCol.doc();
740+
return ref
741+
.delete({exists: true})
742+
.then(() => Promise.reject('Delete should have failed'))
743+
.catch((err: Error) => {
744+
expect(err.message).to.contain('NOT_FOUND: No document to update');
745+
});
746+
});
747+
738748
it('supports non-alphanumeric field names', () => {
739749
const ref = randomCol.doc('doc');
740750
return ref

types/firestore.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ declare namespace FirebaseFirestore {
551551
* @param precondition.lastUpdateTime If set, enforces that the
552552
* document was last updated at lastUpdateTime. Fails the batch if the
553553
* document doesn't exist or was last updated at a different time.
554+
* @param precondition.exists If set, enforces that the target document
555+
* must or must not exist.
554556
* @returns A promise that resolves with the result of the delete. If the
555557
* delete fails, the promise is rejected with a
556558
* [BulkWriterError]{@link BulkWriterError}.
@@ -878,6 +880,11 @@ declare namespace FirebaseFirestore {
878880
* If set, the last update time to enforce.
879881
*/
880882
readonly lastUpdateTime?: Timestamp;
883+
884+
/**
885+
* If set, enforces that the target document must or must not exist.
886+
*/
887+
readonly exists?: boolean;
881888
}
882889

883890
/**

0 commit comments

Comments
 (0)