Skip to content

Commit

Permalink
Merge pull request #3 from seratch/issue-2
Browse files Browse the repository at this point in the history
Fix #2 by adding countAll and countBy methods
  • Loading branch information
seratch authored Mar 21, 2024
2 parents 93a3f14 + 06b5d62 commit c297347
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 7 deletions.
51 changes: 51 additions & 0 deletions data_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
Operator,
SimpleExpression,
} from "./mod.ts";
import { CountResponse } from "./types.ts";
import {
AndConditions,
DataMapperExpressionCountArgs,
DataMapperExpressionQueryArgs,
DataMapperFindFirstExpressionQueryArgs,
DataMapperIdQueryArgs,
Expand Down Expand Up @@ -149,6 +151,17 @@ export class DataMapper<Def extends Definition> {
});
}

async countAll(): Promise<CountResponse> {
const datastore = this.#defaultDatastore;
if (!datastore) {
throw new ConfigurationError(this.#datastoreMissingError);
}
return await this.countBy({
datastore,
expression: { expression: "", values: {}, attributes: {} },
});
}

async findAllBy(
args:
| DataMapperExpressionQueryArgs<Def>
Expand Down Expand Up @@ -197,6 +210,44 @@ export class DataMapper<Def extends Definition> {
});
}

async countBy(
args:
| DataMapperExpressionCountArgs<Def>
| RawExpression
| SimpleExpression<Def>,
): Promise<CountResponse> {
const datastore = this.#defaultDatastore;
if (!datastore) {
throw new ConfigurationError(this.#datastoreMissingError);
}
let expression:
| RawExpression
| SimpleExpression<Def>
| undefined = undefined;
if (Object.keys(args).includes("expression")) {
const expressionProperty = (args as
| DataMapperExpressionCountArgs<Def>
| RawExpression).expression;
if (typeof expressionProperty === "string") {
expression = args as RawExpression;
} else {
expression = expressionProperty as
| SimpleExpression<Def>
| RawExpression;
}
} else if (Object.keys(args).includes("where")) {
expression = args as SimpleExpression<Def>;
} else {
throw new ConfigurationError(`An unknown argument is passed: ${args}`);
}
return await func.countBy<Def>({
client: this.#client,
datastore,
expression: compileExpression<Def>(expression),
logger: this.#logger,
});
}

async deleteById(
args: DataMapperIdQueryArgs | string,
): Promise<DeleteResponse> {
Expand Down
2 changes: 1 addition & 1 deletion dependencies/deno_slack_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/deno_slack_api@2.1.2/mod.ts";
export * from "https://deno.land/x/deno_slack_api@2.3.2/mod.ts";
2 changes: 1 addition & 1 deletion dependencies/deno_slack_api_typed_method_types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/deno_slack_api@2.1.2/typed-method-types/apps.ts";
export * from "https://deno.land/x/deno_slack_api@2.3.2/typed-method-types/apps.ts";
2 changes: 1 addition & 1 deletion dependencies/deno_slack_api_types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/deno_slack_api@2.1.2/types.ts";
export * from "https://deno.land/x/deno_slack_api@2.3.2/types.ts";
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/deno_slack_sdk@2.5.0/mod.ts";
export * from "https://deno.land/x/deno_slack_sdk@2.8.0/mod.ts";
4 changes: 2 additions & 2 deletions dependencies/logger.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { ConsoleHandler } from "https://deno.land/std@0.212.0/log/handlers.ts";
export * from "https://deno.land/std@0.212.0/log/mod.ts";
export { ConsoleHandler } from "https://deno.land/std@0.220.1/log/console_handler.ts";
export * from "https://deno.land/std@0.220.1/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.212.0/assert/mod.ts";
export * from "https://deno.land/std@0.220.1/assert/mod.ts";
47 changes: 47 additions & 0 deletions functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "./dependencies/deno_slack_api_typed_method_types.ts";
import { DatastoreError } from "./errors.ts";
import {
CountResponse,
Definition,
DeleteResponse,
FindFirstRawExpressionQueryArgs,
Expand All @@ -16,6 +17,8 @@ import {
RawExpressionQueryArgs,
SaveArgs,
} from "./types.ts";
import { RawExpressionCountArgs } from "./types.ts";
import { DatastoreCountArgs } from "./dependencies/deno_slack_api_typed_method_types.ts";

export const defaultLogger = log.getLogger();
const logName = "[deno_slack_data_mapper]";
Expand Down Expand Up @@ -208,6 +211,50 @@ export async function findAllBy<Def extends Definition>({
return results as QueryResponse<Def>;
}

export async function countBy<Def extends Definition>({
client,
datastore,
expression,
logger,
}: RawExpressionCountArgs): Promise<CountResponse> {
const _logger = logger ?? defaultLogger;
if (_logger.level === log.LogLevels.DEBUG) {
if (expression.expression) {
const data = JSON.stringify(expression);
_logger.debug(
`${logName} Counting records: (datastore: ${datastore}, expression: ${data})`,
);
} else {
_logger.debug(
`${logName} Counting all records (datastore: ${datastore})`,
);
}
}
let queryArgs: DatastoreCountArgs<Def> = {
datastore,
};
if (expression.expression && expression.expression !== "") {
queryArgs = {
datastore,
expression: expression.expression,
expression_attributes: expression.attributes,
expression_values: expression.values,
};
}
const result = await client.apps.datastore.count(queryArgs);
if (_logger.level === log.LogLevels.DEBUG) {
const response = JSON.stringify(result);
_logger.debug(
`${logName} Found: (datastore: ${datastore}, response: ${response})`,
);
}
if (result.error) {
const error = `Failed to count rows due to ${result.error}`;
throw new DatastoreError(error, result);
}
return result;
}

export async function deleteById({
client,
datastore,
Expand Down
Loading

0 comments on commit c297347

Please sign in to comment.