Guide: Write to a SQL database with JavaScript
In this example, we'll use JavaScript and node-postgres
to write a task to make an update to a PostgreSQL database. To follow along, you'll need a DB you
can connect to and the
airplane
CLI (see Install the Airplane CLI).Example database
In our example below, we'll assume there's a PostgreSQL database we can connect to that has the
following table of users:
sqlCopied1CREATE TABLE users (2id TEXT NOT NULL,3email TEXT NOT NULL,4is_suspended BOOLEAN NOT NULL DEFAULT false,5suspended_at TIMESTAMP WITH TIME ZONE6);
We'll write a task that accepts a user as input and updates the DB to suspend or un-suspend them.
Create a new task
To start, make a new JavaScript task and give it a
name ("Suspend User"):

Parameters
Let's add parameters for "User Email" (short text) and "Suspended" (boolean):

These parameters will get passed to our JavaScript inside a JSON object—more on that later.
Select Create task to finish.
Initialize a new script
The task page shows instructions on how to initialize your task. You'll need the slug, which is
referenced in the commands for you:

Run the
init
command to create a script at a path of your choice:bashCopied1$ airplane tasks init --from suspend_user2? Where should the script be created? suspend_user.js
bashCopied1$ airplane tasks init --from suspend_user2? Where should the script be created? suspend_user.ts
This should create a new file for you:
javascriptCopied1// suspend_user.js2// Linked to https://app.airplane.dev/t/suspend_user [do not edit this line]34export default async function (params) {5console.log("parameters:", params);6}
typescriptCopied1// suspend_user.ts2// Linked to https://app.airplane.dev/t/suspend_user [do not edit this line]34type Params = {5user_email: string;6suspended: boolean;7};89export default async function (params: Params) {10console.log("parameters:", params);11}
Install dependencies
We will use the node-postgres package to query our
PostgreSQL database. Go ahead and install it as a dependency:
bashCopied1npm install pg
bashCopied1yarn add pg
Make sure you're in the directory with the
package.json
file. Running npm install
will have
created or updated a package-lock.json
file and updated package.json
to include pg
:javascriptCopied1// package.json2{3"dependencies": {4"pg": "^8.7.1"5}6}
Write a script
Next, let's fill in
suspend_user.js
(or suspend_user.ts
if TypeScript) with our actual update
logic:javascriptCopied1// suspend_user.js2import { Client } from "pg";34export default async function ({ user_email, suspended }) {5// Connect using DATABASE_URL (which we'll set later)6const client = new Client({7connectionString: process.env.DATABASE_URL,8});9await client.connect();1011// Run the UPDATE query12const res = await client.query(13`UPDATE users14SET15is_suspended = $1,16suspended_at = (CASE WHEN is_suspended THEN NOW() ELSE NULL END)17WHERE email = $2 RETURNING id`,18[suspended, user_email]19);20const updatedID = res.rows[0].id;2122// Close the database connection.23await client.end();2425return {26userID: updatedID,27userEmail: user_email,28message: `Set suspended=${suspended}`,29};30}
As you can see, this is not too different from a basic script you'd run outside of Airplane.
There are two main differences:
- We define a
default
function export that takes in the parameters as a JSON object. Each parameter is keyed by its slug. - We return Output from our function. This results in a nicer, user-friendly table for users to see.
Configure an environment variable
Before we deploy this, we need to set
process.env.DATABASE_URL
, which our script above references.To do this, let's first create a new config variable by opening a new tab,
going to the Config Variables page, and selecting
"New." Enter a name and value (e.g.
postgresql://username:password@rds.amazonaws.com:5432/database
):
From your task page, select Edit task and, under Advanced, add
DATABASE_URL
as an environment
variable from config:
Select Update task to save.
Deploy the task
Now you're ready to deploy your code. All you have to do is run:
bashCopied1airplane deploy ./suspend_user.js
This will package, build, and deploy the task to Airplane.
The task page now shows a form that teammates can run.

Note how the
console.log
in the script above produces a structured output section:
Reference
For a full reference to writing JavaScript tasks, see the JavaScript docs.