Skip to content

Commit

Permalink
feat: add Precondition.exists to delete() (#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Chen committed May 19, 2021
1 parent c2c1606 commit 28d645b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dev/src/index.ts
Expand Up @@ -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.
Expand All @@ -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
*/

Expand Down
2 changes: 2 additions & 0 deletions dev/src/reference.ts
Expand Up @@ -371,6 +371,8 @@ export class DocumentReference<T = firestore.DocumentData>
* @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.<WriteResult>} A Promise that resolves with the
* delete time.
*
Expand Down
2 changes: 2 additions & 0 deletions dev/src/transaction.ts
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions dev/src/write-batch.ts
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions dev/system-test/firestore.ts
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions types/firestore.d.ts
Expand Up @@ -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}.
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 28d645b

Please sign in to comment.