SQL

Perform SQL queries using the SQL built-in.

Query

Perform a SQL query against a database.
FieldTypeDefaultDescription
querystringRequiredThe SQL query to execute. Can include multiple queries separated by semicolons which will be executed within a single transaction.
argsobject{}Query arguments that can be safely referenced from a query.
transactionMode"auto" | "readOnly" | "readWrite" | "none""auto"By default, Airplane will parse the query and choose the most relevant kind of transaction. If readOnly, write operations will fail. If none, queries will be applied one-at-a-time without a transaction.
To protect against SQL injection, it is heavily encouraged that all user-provided input is passed as a query argument. Each query argument has a unique identifier that can be referenced from the query by prepending the identifier with a semicolon. For example, the following query has a query arg identified by :name:
sql
Copied
1
-- Query
2
select * from users
3
where name ilike :name;
4
-- Query Args
5
{ "name": "%colin%" }

Usage

javascript
Copied
1
import airplane from "airplane";
2
3
export default airplane.task(
4
{
5
slug: "find_user_by_name",
6
resources: ["my_sql_db"],
7
},
8
async (params) => {
9
const run = await airplane.sql.query(
10
"my_sql_db", // The slug of the SQL resource to query.
11
"select * from users where name ilike :name", // The SQL query to execute.
12
{ args: { name: `%${params.name}%` } } // Query arguments
13
);
14
return run.output;
15
}
16
);