Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloads to TableTransaction.updateEntity #23132

Merged
6 commits merged into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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

Expand Down
3 changes: 2 additions & 1 deletion sdk/tables/data-tables/review/data-tables.api.md
Expand Up @@ -416,7 +416,8 @@ export class TableTransaction {
actions: TransactionAction[];
createEntity<T extends object = Record<string, unknown>>(entity: TableEntity<T>): void;
deleteEntity(partitionKey: string, rowKey: string): void;
updateEntity<T extends object = Record<string, unknown>>(entity: TableEntity<T>, updateMode?: UpdateMode, updateOptions?: UpdateTableEntityOptions): void;
updateEntity<T extends object = Record<string, unknown>>(entity: TableEntity<T>, updateOptions?: UpdateTableEntityOptions): void;
updateEntity<T extends object = Record<string, unknown>>(entity: TableEntity<T>, updateMode: UpdateMode, updateOptions?: UpdateTableEntityOptions): void;
upsertEntity<T extends object = Record<string, unknown>>(entity: TableEntity<T>, updateMode?: UpdateMode): void;
}

Expand Down
33 changes: 30 additions & 3 deletions sdk/tables/data-tables/src/TableTransaction.ts
Expand Up @@ -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<T extends object = Record<string, unknown>>(
entity: TableEntity<T>,
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<T extends object = Record<string, unknown>>(
entity: TableEntity<T>,
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<T extends object = Record<string, unknown>>(
entity: TableEntity<T>,
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 ?? {}]);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions sdk/tables/data-tables/test/internal/tableTransaction.spec.ts
Expand Up @@ -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: "" }],
]);
});
});
});