Swiftybase
Function StackDatabasePostgresExamples

First Query

Example 1: Select First

In this example, we'll use the Postgres Execute function with the Type set to First to get data from a Postgres database. The statement is expected to return one value; otherwise, it returns an error.

Add Postgres Execute Function

Start by adding the Postgres Execute function. Give it a Result Name and set the Type to First.

Write the SQL Query

In the function configuration, write the SQL query you want to use to fetch data.

Assume we have a users table and want to fetch one user by ID. The users table schema looks like this:

CREATE TABLE public.users (
  id bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  created_at timestamp with time zone NOT NULL DEFAULT now(),
  name text NULL,
  email text NULL,
  age integer NULL,
  activated boolean NULL,
  updated timestamp without time zone NULL,
  CONSTRAINT users_pkey PRIMARY KEY (id)
);

Now write the following query:

SELECT email FROM users WHERE id = $1

Fill the Arguments

Now provide the ID for the filtration.

[7]

Define a Type

Next, define a primitive type like String to hold the email of the user.

Map Struct Fields to SQL Query

Because we only selected one field from the database, we don't need to map fields. Just provide an empty List[string] value.

Testing

Once you’ve configured everything, if the result exists, it will be available in the Data field.

In case the data is not found, it returns an error.

On this page