Kwil v2
Search
⌃K

Actions

How to use actions to execute insert/update/delete operations on your database
Kwil databases use actions—predefined SQL statements—that can be called to execute queries against a database. Actions are either public or private. Public actions can be used by anybody. The database owner can only use private actions.
To see the actions available on a database, call the .getSchema() method.

Action Inputs

First, use the ActionInput class to construct the values you want to pass to the action. ActionInput can be accessed by importing Utils.

Executing Single Actions

For executing a single action (only one value for each action input), instantiate the ActionInput class and call .put() to set input values.
import { Utils } from "kwil"
const inputs = new Utils.ActionInput()
.put("input1", "value")
.put("input2", "value")
.put("input3", "value")
Alternatively, you can set inputs from an object with the input names and values using .putFromObject().
import { Utils } from "kwil"
const inputs = new Utils.ActionInput()
.putFromObject({
"input1": "value1",
"input2": "value2",
"input3": "value3"
})
Alternatively, you can statically set input values without directly instantiating the class.
import { Utils } from "kwil"
const inputs = Utils.ActionInput.of()
.put("input1", "value")
.put("input2", "value")
.put("input3", "value")
OR
import { Utils } from "kwil"
const inputs = Utils.ActionInput
.fromObject({
"input1": "value1",
"input2": "value2",
"input3": "value3"
})

Executing Batch Actions

For executing batch actions (the same action with multiple inputs), instantiate the ActionInput class and pass an array of objects to .putFromObjects().
import { Utils } from "kwil"
const inputs = new Utils.ActionInput()
.putFromObjects([
{
"input1": "value1",
"input2": "value2",
"input3": "value3"
},
{
"input1": "value4",
"input2": "value5",
"input3": "value6"
}
])
Alternatively, batch actions can also be set statically.
import { Utils } from "kwil"
const inputs = Utils.ActionInput
.fromObjects([
{
"input1": "value1",
"input2": "value2",
"input3": "value3"
},
{
"input1": "value4",
"input2": "value5",
"input3": "value6"
}
])

Action Builder

After the Action Inputs are ready, use your kwil object to begin constructing an action transaction with kwil.actionBuilder(). You use method chaining to specify the required action information.
const actionTx = await kwil
.actionBuilder()
.dbid("your_DBID")
.name("your_action_name")
.concat(inputs)
.signer(await provider.getSigner()) // can use any ethereum signer
.buildTx();
Note that all methods should be called to build a complete transaction. If an action does not require inputs, .concat() is not required.

Broadcast Transaction

To broadcast your transaction to the Kwil network, call kwil.broadcast() and pass the action transaction.
const res = await kwil.broadcast(actionTx)
/*
res.data = {
txHash: "0xsome_hash",
fee: "some_fee",
body: "data_here_if_action_returns_data"
}
*/