diff --git a/sdk/tables/data-tables/CHANGELOG.md b/sdk/tables/data-tables/CHANGELOG.md index 19e0e2355cb1..bc3b30e153df 100644 --- a/sdk/tables/data-tables/CHANGELOG.md +++ b/sdk/tables/data-tables/CHANGELOG.md @@ -1,10 +1,11 @@ # Release History -## 13.2.0 (2022-09-06) +## 13.2.0 (2022-09-07) ### Features Added - Add option to `transaction.updateEntity()` [#22562](https://github.com/Azure/azure-sdk-for-js/pull/22562). (A community contribution, courtesy of _[tmcgannon](https://github.com/tmcgannon))_ + - Add overloads to `TableTransaction.updateEntity` so `undefined` doesn't need to be passed explicitly for an optional argument. [#23132](https://github.com/Azure/azure-sdk-for-js/pull/23132) ### Bugs Fixed diff --git a/sdk/tables/data-tables/review/data-tables.api.md b/sdk/tables/data-tables/review/data-tables.api.md index 2e413c9eea8b..3276f7ffe268 100644 --- a/sdk/tables/data-tables/review/data-tables.api.md +++ b/sdk/tables/data-tables/review/data-tables.api.md @@ -416,7 +416,8 @@ export class TableTransaction { actions: TransactionAction[]; createEntity>(entity: TableEntity): void; deleteEntity(partitionKey: string, rowKey: string): void; - updateEntity>(entity: TableEntity, updateMode?: UpdateMode, updateOptions?: UpdateTableEntityOptions): void; + updateEntity>(entity: TableEntity, updateOptions?: UpdateTableEntityOptions): void; + updateEntity>(entity: TableEntity, updateMode: UpdateMode, updateOptions?: UpdateTableEntityOptions): void; upsertEntity>(entity: TableEntity, updateMode?: UpdateMode): void; } diff --git a/sdk/tables/data-tables/src/TableTransaction.ts b/sdk/tables/data-tables/src/TableTransaction.ts index d80c6b1d1ee6..c338b2390ca4 100644 --- a/sdk/tables/data-tables/src/TableTransaction.ts +++ b/sdk/tables/data-tables/src/TableTransaction.ts @@ -73,18 +73,45 @@ export class TableTransaction { this.actions.push(["delete", { partitionKey, rowKey }]); } + /** + * Adds an update action to the transaction + * @param entity - entity to update + * @param updateOptions - options for the update operation + */ + updateEntity>( + entity: TableEntity, + updateOptions?: UpdateTableEntityOptions + ): void; + /** * Adds an update action to the transaction * @param entity - entity to update * @param updateMode - update mode - * @param options - options for the update operation + * @param updateOptions - options for the update operation + */ + updateEntity>( + entity: TableEntity, + updateMode: UpdateMode, + updateOptions?: UpdateTableEntityOptions + ): void; + + /** + * Adds an update action to the transaction + * @param entity - entity to update + * @param updateModeOrOptions - update mode or update options + * @param updateOptions - options for the update operation */ updateEntity>( entity: TableEntity, - updateMode: UpdateMode = "Merge", + updateModeOrOptions: UpdateMode | UpdateTableEntityOptions | undefined, updateOptions?: UpdateTableEntityOptions ): void { - this.actions.push(["update", entity, updateMode, updateOptions]); + // UpdateMode is a string union + const realUpdateMode: UpdateMode | undefined = + typeof updateModeOrOptions === "string" ? updateModeOrOptions : undefined; + const realUpdateOptions: UpdateTableEntityOptions | undefined = + typeof updateModeOrOptions === "object" ? updateModeOrOptions : updateOptions; + this.actions.push(["update", entity, realUpdateMode ?? "Merge", realUpdateOptions ?? {}]); } /** diff --git a/sdk/tables/data-tables/test/internal/tableTransaction.spec.ts b/sdk/tables/data-tables/test/internal/tableTransaction.spec.ts index a17fbf733106..26e40e3e4c19 100644 --- a/sdk/tables/data-tables/test/internal/tableTransaction.spec.ts +++ b/sdk/tables/data-tables/test/internal/tableTransaction.spec.ts @@ -69,4 +69,21 @@ describe("TableTransaction", () => { assert.isTrue(isProxy); }); }); + + describe("updateEntity", () => { + it("should have ergonomic overloads", () => { + const transaction = new TableTransaction(); + const entity = { partitionKey: "1", rowKey: "1" }; + transaction.updateEntity(entity); + transaction.updateEntity(entity, "Replace"); + transaction.updateEntity(entity, { etag: "" }); + transaction.updateEntity(entity, "Merge", { etag: "" }); + assert.deepEqual(transaction.actions, [ + ["update", { partitionKey: "1", rowKey: "1" }, "Merge", {}], + ["update", { partitionKey: "1", rowKey: "1" }, "Replace", {}], + ["update", { partitionKey: "1", rowKey: "1" }, "Merge", { etag: "" }], + ["update", { partitionKey: "1", rowKey: "1" }, "Merge", { etag: "" }], + ]); + }); + }); });