From 78244ef15991805ff9ee1a5beeda854a7158949a Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Thu, 9 Nov 2023 13:27:33 +0900 Subject: [PATCH] Enhance deleteById to be consistent with findById --- data_mapper.ts | 15 ++++++++++++--- data_mapper_test.ts | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/data_mapper.ts b/data_mapper.ts index a73c57f..1d96ae5 100644 --- a/data_mapper.ts +++ b/data_mapper.ts @@ -197,15 +197,24 @@ export class DataMapper { }); } - async deleteById(args: DataMapperIdQueryArgs): Promise { - const datastore = args.datastore ?? this.#defaultDatastore; + async deleteById( + args: DataMapperIdQueryArgs | string, + ): Promise { + let datastore = this.#defaultDatastore; + let id = undefined; + if (typeof args === "string") { + id = args; + } else { + id = args.id; + datastore = args.datastore ?? this.#defaultDatastore; + } if (!datastore) { throw new ConfigurationError(this.#datastoreMissingError); } return await func.deleteById({ client: this.#client, datastore, - id: args.id, + id, logger: this.#logger, }); } diff --git a/data_mapper_test.ts b/data_mapper_test.ts index c9c6258..6481923 100644 --- a/data_mapper_test.ts +++ b/data_mapper_test.ts @@ -69,6 +69,13 @@ mf.mock("POST@/api/apps.datastore.get", () => { ); }); +mf.mock("POST@/api/apps.datastore.delete", () => { + return new Response( + JSON.stringify({ "ok": true }), + { status: 200 }, + ); +}); + export const Surveys = DefineDatastore( { name: "surveys", @@ -146,6 +153,9 @@ Deno.test("Run a query", async () => { attributes: { "#title": "title" }, values: { ":title": "Off-site event ideas" }, }); + + await dataMapper.deleteById("123"); + await dataMapper.deleteById({ id: "123" }); }); Deno.test("Run a query with simple expressions", async () => {