Skip to content

Commit

Permalink
Update findById method
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Oct 28, 2023
1 parent 32061da commit 60b167c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
20 changes: 15 additions & 5 deletions data_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,26 @@ export class DataMapper<Def extends Definition> {
});
}

async findById(args: DataMapperIdQueryArgs): Promise<GetResponse<Def>> {
const datastore = args.datastore ?? this.#defaultDatastore;
async findById(
args: DataMapperIdQueryArgs | string,
): Promise<GetResponse<Def>> {
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);
}
const [client, logger] = [this.#client, this.#logger];
return await func.findById<Def>({
client: this.#client,
client,
datastore,
id: args.id,
logger: this.#logger,
logger,
id,
});
}

Expand Down
23 changes: 23 additions & 0 deletions data_mapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ mf.mock("POST@/api/apps.datastore.query", () => {
);
});

mf.mock("POST@/api/apps.datastore.get", () => {
return new Response(
JSON.stringify({
"ok": true,
"datastore": "suveys",
"item": {
"id": "123",
"title": "Off-site event ideas",
"questions": [
"Can you share a fun idea for our off-site event in December?",
],
"closed": false,
},
}),
{
status: 200,
},
);
});

export const Surveys = DefineDatastore(
{
name: "surveys",
Expand Down Expand Up @@ -108,6 +128,9 @@ Deno.test("Run a query", async () => {
logLevel: "DEBUG",
});

await dataMapper.findById("123");
await dataMapper.findById({ id: "123" });

await dataMapper.findAll();

await dataMapper.findAllBy({
Expand Down
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 60b167c

Please sign in to comment.