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

feat: Add datastore count API #93

Merged
merged 3 commits into from
Mar 13, 2024
Merged

feat: Add datastore count API #93

merged 3 commits into from
Mar 13, 2024

Conversation

cyungslack
Copy link
Contributor

@cyungslack cyungslack commented Mar 4, 2024

Summary

Add support for Count API commands for datastores.

testing

Created an app with a datastore, and used the new API to count the number of items in the datastore.

// Example function
export const CountFunctionDefinition = DefineFunction({
  callback_id: "count_function",
  title: "count function",
  description: "Count all rows in datastore",
  source_file: "functions/count_function.ts",
  input_parameters: {
    properties: {
      datastore: {
        type: Schema.types.string,
        description: "Name of datastore to count",
      },
    },
    required: ["datastore"],
  }
});

export default SlackFunction(
  CountFunctionDefinition,
  async ({ client, inputs }) => {
    const res = await client.apps.datastore.count({
      datastore: inputs.datastore,
    });
    if (!res.ok) {
      const errorMsg = `(Error detail: ${res.error})`;
      console.log(errorMsg);
      return { error: errorMsg };
    }
    return { outputs: { count: res.count.toString() } };
  },
);

Special notes

The API is behind a toggle which is only enabled for dev right now. The plan is to dial up the toggle with the next SDK/CLI release.
PR for CLI changes: https://github.com/slackapi/slack-cli/pull/1262

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've ran deno task test after making the changes.

@cyungslack cyungslack requested a review from a team as a code owner March 4, 2024 18:15
Copy link

codecov bot commented Mar 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (fcb40ae) to head (3ea0b53).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #93   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           36        36           
  Lines         1091      1091           
  Branches        16        16           
=========================================
  Hits          1091      1091           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@WilliamBergamin WilliamBergamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me 💯 left one comment

* @description The name of the datastore
*/
datastore: Schema["name"];
expression?: string;
Copy link
Contributor

@WilliamBergamin WilliamBergamin Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the descriptions of these fields be included in the documentation?
Should we add descriptions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are descriptions in the documentation.
Here's the dev API page https://api.dev.slack.com/methods/apps.datastore.count which has short descriptions of the optional fields.
There's a much more in-depth explanation of the expression fields here (which should be re-used for the count explanation as the expression fields work the same way as query): https://api.slack.com/automation/datastores-retrieve#query

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do as much as we can to provide at least a basic description to specific fields. If there are relevant links for devs to read up on for more info, let's link them to it.

See for example how we do document fields and parameters and link to more information in our Node SDK: https://github.com/slackapi/node-slack-sdk/blob/main/packages/web-api/src/methods.ts#L1229-L1232

@WilliamBergamin WilliamBergamin added enhancement New feature or request semver:minor requires a minor version number bump labels Mar 11, 2024
Copy link
Contributor

@WilliamBergamin WilliamBergamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the descriptions 💯

Copy link
Contributor

@filmaj filmaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a couple of formatting suggestions and also let's DRY up the shared dynamo query expression fields so that each of the API method arguments can extend from a single type.

expression?: string;
/**
* @description A map of attributes referenced in expression
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @description A map of attributes referenced in expression
* @description A map of attributes referenced in `expression`

(most IDEs will render markdown-like formatting)

"expression_attributes"?: Record<string, string>;
/**
* @description A map of values referenced in expression
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @description A map of values referenced in expression
* @description A map of values referenced in `expression`

Schema extends DatastoreSchema,
> =
& BaseMethodArgs
& {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the arguments to datastore.query and datastore.count share a lot of fields, yes? Like datastore, expression, attributes and values.

I recommend factoring out these shared fields into its own dedicated type (doesn't have to be exported, can just exist in this file), maybe something like a type DynamoQueryArgs, and have each of DatastoreQueryArgs and DatastoreCountArgs leverage that shared type.

This way, you don't duplicate all the work, and especially with JSDocs now being a part, don't have to duplicate the docs either!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an example of how to do this, see the Node Slack SDK here: https://github.com/slackapi/node-slack-sdk/blob/main/packages/web-api/src/types/request/conversations.ts#L52-L53

There are a bunch of non-exported interfaces in the above file, and they're mixed and combined together using | and & in typescript for different *Arguments types (like ConversationsAcceptSharedInviteArguments that I linked to). It is essentially the solution to the same problem we are solving in this PR.

@cyungslack cyungslack requested a review from filmaj March 11, 2024 20:10
Copy link
Contributor

@filmaj filmaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! Thanks for addressing my comments 🙇
:shipit:

@filmaj filmaj merged commit 185ddda into main Mar 13, 2024
11 checks passed
@filmaj filmaj deleted the cyung_datastore_count branch March 13, 2024 13:08
filmaj added a commit that referenced this pull request Mar 15, 2024
filmaj pushed a commit that referenced this pull request Mar 15, 2024
filmaj added a commit that referenced this pull request Mar 15, 2024
filmaj pushed a commit that referenced this pull request Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver:minor requires a minor version number bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants