Listing data
Let's create a tool called List products
by adding a simple list
action to the Product
model. This will display a data table with a list of all Products in the database.
As we recall, this is our full schema from the previous section:
model Product {
fields {
name Text
description Text?
imageUrl Text?
stock StockItem[]
}
}
model StockItem {
fields {
product Product
quantity Number
location StockLocation
}
}
model StockLocation {
fields {
name Text @unique
items StockItem[]
}
}
Creating a list action
Let's augment it with a list
action on the Product
model:
model Product {
fields {
name Text
description Text?
imageUrl Text?
stock StockItem[]
}
actions {
list listProducts()
}
}
// Other models omitted for brevity
Now, if we git push
this and deploy it to Keel, we can see the new action in the Console:
However, when we try to run this action, we get an error:
This is because Keel is secure by default and requires you to explicitly set permissions for each action. Let's do that now.
Setting permissions
In order for a Console user to be able to run any action they need permissions. If you want to just allow anyone with access to your Keel project to run an action you can use the expression ctx.isAuthenticated
. If you want only certain members of your team to be able to use an action you can use roles.
model Product {
fields {
name Text
description Text?
imageUrl Text?
stock StockItem[]
}
actions {
list listProducts()
}
@permission(
expression: ctx.isAuthenticated,
actions: [list]
)
}
// Other models omitted for brevity
Now we can run the list action and see the results. Since we haven't created any entries yet, the list is empty.
Let's explore how we can create products and insert them into our database using generated internal tools in the next section.