Skip to content

Commit

Permalink
Add findAll method to DataMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Oct 27, 2023
1 parent 19288b9 commit 4d015d9
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 14 deletions.
44 changes: 41 additions & 3 deletions data_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ export class DataMapper<Def extends Definition> {
});
}

async findAll(args: PaginationArgs = {}): Promise<QueryResponse<Def>> {
const datastore = this.#defaultDatastore;
if (!datastore) {
throw new ConfigurationError(this.#datastoreMissingError);
}
let autoPagination = Object.keys(args).includes("autoPagination")
// deno-lint-ignore no-explicit-any
? (args as any).autoPagination
: true;
if (autoPagination === undefined || autoPagination === null) {
autoPagination = true;
}
return await this.findAllBy({
datastore,
expression: { expression: "", values: {}, attributes: {} },
cursor: args.cursor,
limit: args.limit,
autoPagination,
});
}

async findAllBy(
args:
| DataMapperExpressionQueryArgs<Def>
Expand Down Expand Up @@ -242,7 +263,7 @@ function isConditions<Def extends Definition>(
value: Condition<Def> | Conditions<Def>,
): boolean {
const keys = Object.keys(value);
return keys.includes("and") || keys.includes("or");
return keys.length > 1 || keys.includes("and") || keys.includes("or");
}

function parseCondition<Def extends Definition>(
Expand All @@ -259,7 +280,12 @@ function parseCondition<Def extends Definition>(
let expression = "";
// deno-lint-ignore no-explicit-any
const attributeValue = (condition as Record<string, any>)[attributeName];
if (typeof attributeValue === "string") {
console.log(attributeValue);
if (
typeof attributeValue === "string" ||
typeof attributeValue === "number" ||
typeof attributeValue === "boolean"
) {
values[`:${randomName}`] = attributeValue;
expression = `#${randomName} = :${randomName}`;
} else {
Expand Down Expand Up @@ -302,7 +328,19 @@ function parseConditions<Def extends Definition>(
if (currentExpression && typeof currentExpression !== "string") {
expression = currentExpression;
}
const _conditions = conditions as Conditions<Def>;
let _conditions: Conditions<Def> = conditions as Conditions<Def>;
if (conditions && Object.keys(conditions).length > 1) {
const andConditions: (Condition<Def> | Conditions<Def>)[] = [];
for (const [k, v] of Object.entries(conditions as Condition<Def>)) {
if (k && v !== undefined) {
// deno-lint-ignore no-explicit-any
const c: any = {};
c[k] = v;
andConditions.push(c as Condition<Def> | Conditions<Def>);
}
}
_conditions = { and: andConditions };
}
if (Object.keys(_conditions).includes("and")) {
const andConditions = _conditions as AndConditions<Def>;
if (!expression || !Object.keys(expression).includes("and")) {
Expand Down
21 changes: 21 additions & 0 deletions data_mapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Deno.test("Run a query", async () => {
logLevel: "DEBUG",
});

await dataMapper.findAll();

await dataMapper.findAllBy({
expression: {
expression: "#title = :title",
Expand Down Expand Up @@ -264,3 +266,22 @@ Deno.test("Construct complex conditions", () => {
"((ATTR = VALUE) and (ATTR = VALUE)) or (ATTR = VALUE) or (ATTR = VALUE)",
);
});

Deno.test("Two conditions in a single condition object", () => {
const result = compileExpression<typeof Surveys.definition>({
where: { title: "New project ideas", closed: false },
});
assertEquals(Object.keys(result.attributes).length, 2);
assertEquals(Object.keys(result.values).length, 2);
let expression = result.expression;
for (const name of Object.keys(result.attributes)) {
expression = expression.replaceAll(name, "ATTR");
}
for (const name of Object.keys(result.values)) {
expression = expression.replaceAll(name, "VALUE");
}
assertEquals(
expression,
"(ATTR = VALUE) and (ATTR = VALUE)",
);
});
113 changes: 113 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dependencies/deno_slack_sdk.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/[email protected].0/mod.ts";
export * from "https://deno.land/x/[email protected].1/mod.ts";
2 changes: 1 addition & 1 deletion dependencies/logger.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/std@0.203.0/log/mod.ts";
export * from "https://deno.land/std@0.204.0/log/mod.ts";
2 changes: 1 addition & 1 deletion dependencies/testing_asserts.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/std@0.203.0/assert/mod.ts";
export * from "https://deno.land/std@0.204.0/assert/mod.ts";
29 changes: 21 additions & 8 deletions functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as log from "./dependencies/logger.ts";
import {
DatastoreItem,
DatastoreQueryArgs,
DatastoreSchema,
} from "./dependencies/deno_slack_api_typed_method_types.ts";
import { DatastoreError } from "./errors.ts";
Expand Down Expand Up @@ -115,17 +116,29 @@ export async function findAllBy<Def extends Definition>({
const _autoPagination = autoPagination === undefined || autoPagination;
const _logger = logger ?? defaultLogger;
const _limit = limit ?? 1000;
_logger.debug(
`Finding records by an expression: ${JSON.stringify(expression)}`,
);
const results = await client.apps.datastore.query({
if (expression.expression) {
_logger.debug(
`Finding records by an expression: ${JSON.stringify(expression)}`,
);
} else {
_logger.debug("Finding all records");
}
let queryArgs: DatastoreQueryArgs<Def> = {
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
cursor,
limit: _autoPagination ? 1000 : _limit,
});
};
if (expression.expression && expression.expression !== "") {
queryArgs = {
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
cursor,
limit: _autoPagination ? 1000 : _limit,
};
}
const results = await client.apps.datastore.query(queryArgs);
_logger.debug(`Found: ${JSON.stringify(results)}`);
if (results.error) {
const error = `Failed to fetch rows due to ${results.error}`;
Expand Down
10 changes: 10 additions & 0 deletions test-app/deno.lock

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

0 comments on commit 4d015d9

Please sign in to comment.