diff --git a/dev/src/index.ts b/dev/src/index.ts index d8127fcc7..d6ab41765 100644 --- a/dev/src/index.ts +++ b/dev/src/index.ts @@ -225,7 +225,7 @@ const MAX_CONCURRENT_REQUESTS_PER_CLIENT = 100; * [update()]{@link DocumentReference#update} and * [delete()]{@link DocumentReference#delete} calls in * [DocumentReference]{@link DocumentReference}, - * [WriteBatch]{@link WriteBatch}, and + * [WriteBatch]{@link WriteBatch}, [BulkWriter]({@link BulkWriter}, and * [Transaction]{@link Transaction}. Using Preconditions, these calls * can be restricted to only apply to documents that match the specified * conditions. @@ -243,6 +243,8 @@ const MAX_CONCURRENT_REQUESTS_PER_CLIENT = 100; * @property {Timestamp} lastUpdateTime The update time to enforce. If set, * enforces that the document was last updated at lastUpdateTime. Fails the * operation if the document was last updated at a different time. + * @property {boolean} exists If set, enforces that the target document must + * or must not exist. * @typedef {Object} Precondition */ diff --git a/dev/src/reference.ts b/dev/src/reference.ts index 6ce5920a4..2c62dde71 100644 --- a/dev/src/reference.ts +++ b/dev/src/reference.ts @@ -371,6 +371,8 @@ export class DocumentReference * @param {Timestamp=} precondition.lastUpdateTime If set, enforces that the * document was last updated at lastUpdateTime. Fails the delete if the * document was last updated at a different time. + * @param {boolean=} precondition.exists If set, enforces that the target + * document must or must not exist. * @returns {Promise.} A Promise that resolves with the * delete time. * diff --git a/dev/src/transaction.ts b/dev/src/transaction.ts index 3ad7a56eb..ccd50588f 100644 --- a/dev/src/transaction.ts +++ b/dev/src/transaction.ts @@ -330,6 +330,8 @@ export class Transaction implements firestore.Transaction { * @param {Timestamp=} precondition.lastUpdateTime If set, enforces that the * document was last updated at lastUpdateTime. Fails the transaction if the * document doesn't exist or was last updated at a different time. + * @param {boolean=} precondition.exists If set, enforces that the target + * document must or must not exist. * @returns {Transaction} This Transaction instance. Used for * chaining method calls. * diff --git a/dev/src/write-batch.ts b/dev/src/write-batch.ts index 60e721772..d4adf0e51 100644 --- a/dev/src/write-batch.ts +++ b/dev/src/write-batch.ts @@ -222,6 +222,8 @@ export class WriteBatch implements firestore.WriteBatch { * @param {Timestamp=} precondition.lastUpdateTime If set, enforces that the * document was last updated at lastUpdateTime. Fails the batch if the * document doesn't exist or was last updated at a different time. + * @param {boolean= } precondition.exists If set to true, enforces that the target + * document must or must not exist. * @returns {WriteBatch} This WriteBatch instance. Used for chaining * method calls. * diff --git a/dev/system-test/firestore.ts b/dev/system-test/firestore.ts index 70e5a9517..10379e830 100644 --- a/dev/system-test/firestore.ts +++ b/dev/system-test/firestore.ts @@ -735,6 +735,16 @@ describe('DocumentReference class', () => { return ref.delete(); }); + it('will fail to delete document with exists: true if doc does not exist', () => { + const ref = randomCol.doc(); + return ref + .delete({exists: true}) + .then(() => Promise.reject('Delete should have failed')) + .catch((err: Error) => { + expect(err.message).to.contain('NOT_FOUND: No document to update'); + }); + }); + it('supports non-alphanumeric field names', () => { const ref = randomCol.doc('doc'); return ref diff --git a/types/firestore.d.ts b/types/firestore.d.ts index 8763562b2..464e65ceb 100644 --- a/types/firestore.d.ts +++ b/types/firestore.d.ts @@ -551,6 +551,8 @@ declare namespace FirebaseFirestore { * @param precondition.lastUpdateTime If set, enforces that the * document was last updated at lastUpdateTime. Fails the batch if the * document doesn't exist or was last updated at a different time. + * @param precondition.exists If set, enforces that the target document + * must or must not exist. * @returns A promise that resolves with the result of the delete. If the * delete fails, the promise is rejected with a * [BulkWriterError]{@link BulkWriterError}. @@ -878,6 +880,11 @@ declare namespace FirebaseFirestore { * If set, the last update time to enforce. */ readonly lastUpdateTime?: Timestamp; + + /** + * If set, enforces that the target document must or must not exist. + */ + readonly exists?: boolean; } /**