The Slack CLI allows you to interact with a datastore via your terminal window, as opposed to via your code. This can be great if you want to audit your datastore, test your queries, or if you just want to feel a little more like a hacker. Hack on, my friend.
You'll get the most of out of this page if you read about the Slack CLI and datastores first. If you haven't, go and do that — then come back here; we'll wait for you.
Running slack datastore --help
displays the following usage summary related to datastores:
USAGE
$ slack datastore <subcommand> [flags]
SUBCOMMANDS
delete Delete an item from a datstore.
get Get an item from a datastore.
put Create or replace an item in a datastore.
query Query a datastore for items.
update Create or update an item in a datastore.
FLAGS
-h, --help help for datastore
EXAMPLE
$ slack datastore put '{"datastore": "todos", "item": {"id": "42", "description": "Create a PR", "status": "Done"}}'
Each command uses a simple JSON data string. Let's look at these commands in a little more detail, but first:
A note on SQL injection
If Breaking Bad taught us anything, it's that you can't trust anybody. Be sure to sanitize any strings received from a user and never use untrusted data in your query expressions.
The put
command will create or replace an entire item in your datastore
. Let's use the datastore we defined in the Virtual Running Buddies sample app to add an item to the datastore
. Given our datastore definition:
// datastores/run_data.ts
import { DefineDatastore, Schema } from "deno-slack-sdk/mod.ts";
export const RUN_DATASTORE = "running_datastore";
const RunningDatastore = DefineDatastore({
name: RUN_DATASTORE,
primary_key: "id",
attributes: {
id: {
type: Schema.types.string,
},
runner: {
type: Schema.slack.types.user_id,
},
distance: {
type: Schema.types.number,
},
rundate: {
type: Schema.slack.types.date,
},
},
});
export default RunningDatastore;
The put
request can look like:
slack datastore put '{ "datastore": "running_datastore", "item": { "id": "50", "runner": "ABCD1234EFG", "distance": "26.2", "rundate": "2023-03-19"} }'
You'll see the following in your terminal:
🎉 Stored below record in the datastore: running_datastore
{
"distance": 26.2,
"id": "50",
"rundate": "2023-03-19",
"runner": "ABCD1234EFG"
}
To inspect the datastore after updates, run slack datastore query [expression]
Be sure to double-check that the item's property names are correct when executing the put
command; if they are not, they will be ignored. If the API call is otherwise successful, no error code will be returned, and you'll have some garbage data on your hands.
Replacing data is done in the same way as creating data: simply provide the primary key of the item you'd like to replace.
The get
command is used to retrieve a single item by its primary key. Here's an example:
$ slack datastore get '{ "datastore": "running_datastore", "id": "50" }'
You'll see the following in your terminal:
🎉 Get from Datastore: running_datastore
{
"distance": 26.2,
"id": "50",
"rundate": "2023-03-19",
"runner": "ABCD1234EFG"
}
To inspect the datastore after updates, run slack datastore get [expression]
Use the query
command when you're looking for a number of items, or when you don't know the ID of a particular item.
✨ To learn more about the language used for making queries, check out querying the datastore.
Let's check out what a query for runs logged on a certain date (March 19th, 2023) looks like:
$ slack datastore query '{ "datastore": "running_datastore", "expression": "rundate = :rundate", "expression_values": {":rundate": "2023-03-19"} }'
You'll see the following in your terminal:
🎉 Retrieved 1 items from datastore: running_datastore
{
"distance": 26.3,
"id": "50",
"rundate": "2023-03-19",
"runner": "ABCD1234EFG"
}
To create or update existing items run slack datastore put [item]
If you were to run this command without any expression, as in the following example:
slack datastore query '{"datastore": "running_datastore"}'
It would return all of the items in the datastore. Be careful though, as you could end up thinking you've lost data, when it could actually just be on another page.
The delete
operation takes a primary key and deletes an item as in the following example:
$ slack datastore delete '{ "datastore": "running_datastore", "id": "50" }'
You'll see the following in your terminal:
🎉 Deleted from datastore: running_datastore
primary_key: 50
To inspect the datastore after updates, run slack datastore query [expression]
Unlike the put
command, which will overwrite an entire datastore item, the update
command will only overwrite the individual fields included in the command. For example:
$ slack datastore update '{ "app_id": "UVW987XYZ65", "datastore": "running_datastore", "item": { "id": "50", "runner": "ABCD1234EFG", "distance": "26.3", "rundate": "2023-03-19"} }'
You'll see the following in your terminal:
🎉 Stored below record in the datastore: running_datastore
{
"distance": 26.3,
"id": "50",
"rundate": "2023-03-19",
"runner": "ABCD1234EFG"
}
To inspect the datastore after updates, run slack datastore query [expression]
✨ To learn about how to use these operators in your custom functions, check out the datastores page.