slackapi / deno-slack-api

Slack API Client for Deno Run on Slack projects
https://api.slack.com/automation
MIT License
35 stars 17 forks source link

add datastore bulk apis #92

Closed anabulsi closed 7 months ago

anabulsi commented 7 months ago

DO NOT MERGE YET

Summary

Add support for the experimental bulk APIs

testing

Create an app with a datastore, then use the new bulk APIs to get, put and delete items in bulk

Special notes

Requirements

codecov[bot] commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (7476afc) 100.00% compared to head (3b8f6e0) 100.00%. Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #92 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 36 36 Lines 1091 1091 Branches 13 16 +3 ========================================= Hits 1091 1091 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

WilliamBergamin commented 7 months ago

I was able to test these features out with the following, seems like everything works 💯

// record_datastore.ts
import { DefineDatastore, Schema } from "deno-slack-sdk/mod.ts";

export const RecordSchema = {
  id: {
    type: Schema.types.string,
    description: "Datastore record id of the survey",
  },
};

export default DefineDatastore({
  name: "data",
  primary_key: "id",
  attributes: RecordSchema,
});
// in a tests function
import RecordDataStore from "./datastores/data.ts";

    const records = [{ id: crypto.randomUUID() }, { id: crypto.randomUUID() }];

    const putResp = await client.apps.datastore.bulkPut<
      typeof RecordDataStore.definition
    >({
      datastore: RecordDataStore.name,
      items: records,
    });

    if (!putResp.ok) {
      return { error: `Failed to bulk put the record error: ${putResp.error}` };
    }

    const getResp = await client.apps.datastore.bulkGet<
      typeof RecordDataStore.definition
    >({
      datastore: RecordDataStore.name,
      ids: records.map((r) => r.id),
    });

    if (!getResp.ok) {
      return {
        error: `Failed to bulk get the record error: ${getResp.error}`,
      };
    }

    const deletedResp = await client.apps.datastore.bulkDelete<
      typeof RecordDataStore.definition
    >({
      datastore: RecordDataStore.name,
      ids: records.map((r) => r.id),
    });

    if (!deletedResp.ok) {
      return {
        error: `Failed to bulk delete the records error: ${deletedResp.error}`,
      };
    }
// manifest.ts
import { Manifest } from "deno-slack-sdk/mod.ts";
import { PostIssueMessage } from "./functions/post_issue_message.ts";
import SubmitIssueWorkflow from "./workflows/submit_issue.ts";
import RecordDataStore from "./datastores/data.ts";

export default Manifest({
  name: "gallant-mole-238",
  description: "A basic sample that demonstrates issue submission to channel",
  icon: "assets/default_new_app_icon.png",
  workflows: [SubmitIssueWorkflow],
  functions: [PostIssueMessage],
  datastores: [RecordDataStore],
  outgoingDomains: ["raw.githubusercontent.com"],
  botScopes: [
    "commands",
    "chat:write",
    "chat:write.public",
    "datastore:read",
    "datastore:write",
  ],
});
// import_map.json
{
  "imports": {
    "deno-slack-sdk/": "https://deno.land/x/deno_slack_sdk@2.5.0/",
    "deno-slack-api/": "https://raw.githubusercontent.com/slackapi/deno-slack-api/anablsi-datastore-bulk-apis/src/"
  },
  "scopes": {
    "https://deno.land/x/deno_slack_sdk@2.5.0/": {
      "https://deno.land/x/deno_slack_api@2.1.2/": "https://raw.githubusercontent.com/slackapi/deno-slack-api/anablsi-datastore-bulk-apis/src/"
    }
  }
}