There is no separate write action on a relational database, and that is worth stating plainly: execute reads and writes, and only the statement says which.
await storage.run({
action: "execute",
sql: "UPDATE users SET active = ? WHERE id = ?",
params: [0, id],
});storage.run(
action="execute",
sql="UPDATE users SET active = ? WHERE id = ?",
params=[0, user_id],
)If your own code decides what an agent or a job may do, read the statement, not the action. « execute » is as true of a SELECT as of a DROP TABLE.
const WRITES = /^\s*(insert|update|delete|replace|create|alter|drop|truncate)\b/i;
function isWrite(sql) {
return WRITES.test(sql);
}
if (isWrite(sql) && !allowedToWrite) throw new Error("read-only here");import re
WRITES = re.compile(r"^\s*(insert|update|delete|replace|create|alter|drop|truncate)\b", re.I)
def is_write(sql):
return bool(WRITES.match(sql))
if is_write(sql) and not allowed_to_write:
raise PermissionError("read-only here")A statement with no WHERE is refused
Same reasoning as the empty filter on a document database: an UPDATE or a DELETE without a condition touches every row, and that has to be meant rather than typed.
One statement
Several in one call are refused. A batch where only the first is examined defeats every guard that looks at the statement — including yours.
Bind the values
// Never
sql: `DELETE FROM sessions WHERE token = '${token}'`
// Always
sql: "DELETE FROM sessions WHERE token = ?", params: [token]# Never
storage.run(action="execute", sql=f"DELETE FROM sessions WHERE token = '{token}'")
# Always
storage.run(action="execute", sql="DELETE FROM sessions WHERE token = ?", params=[token])Writes are refused when the space is full
Statements that can grow the database are stopped past the quota; DELETE and DROP stay allowed, so there is always a way to make room.
One call is one statement, so plan for a partial failure
A sequence of calls is not atomic: the third can fail with the first two applied. Make each step repeatable, or write a statement that is — and keep enough in your own records to say what landed.