sql
processor allows you to execute SQL queries against a database. it supports the following databases:
fastn
in
static site mode, then how the page looked when fastn build
was
called will be shown to everyone. But if you are using dynamic
mode then this page would be regenerated on every page load.FASTN_DB_URL
, which contains the connection string for your database.
You can define this variable in a .env
file at the root of your folder, or you can define it directly in your shell environment.
If the platform where you are hosting this website supports setting environment variables, you can declare this variable there as well..env
file is properly setup you can start querying your database
using the sql
processor.-- import: fastn/processors as pr
-- person list people:
$processor$: pr.sql
SELECT * FROM users;
SELECT *
, which will fetch all
three columns, id
, name
and department
, so your record will look something
like this:-- record person:
integer id:
string name:
string department:
Note that the type columns in query result must match the type of fields in the
record
. The order of fields of record
must also match the order of columns in
the query result.
person list
, so all data can be stored
in corresponding list.for
loop:-- show-person: $p
for: $p in $people
sql
processor allows you to pass named parameters in your queries.
You can pass a named parameter by defining it as a header/property of the section where the query is executed.
To access the named parameter in your query, use the following syntax:$<PARAM_NAME>::<PARAM_TYPE>
In this syntax, the name following the $
symbol represents the parameter's name,
and you can specify its type by appending ::<PARAM_TYPE>
to indicate the desired data type.
users
table by their id
:-- import: fastn/processors as pr
-- person jack:
$processor$: pr.sql
id: 1
SELECT * FROM users WHERE id = $id::INTEGER;
-- show-person: $jack
request-data
processor and Dynamic URLs to dynamically create user profile pages, similar to Twitter and other social networks.-- import: fastn/processors as pr
-- string username:
$processor$: pr.request-data
-- user user-data:
$processor$: pr.sql
username: $username
SELECT * FROM users WHERE username = $username::STRING;
-- user-profile: $user-data
/user/<username>
,
fastn will retrieve the username from the URL using the request-data
processor and pass it to your SQL query as a named parameter.
This allows you to retrieve the data of the user whose username matches the passed value.FASTN_DB_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}
sql
processor:
TEXT
VARCHAR
INT4
INT8
FLOAT4
FLOAT8
BOOL
FASTN_DB_URL=sqlite:///db.sqlite
sql
processor:
TEXT
INTEGER
REAL
db
and sheet
(optional, the name of the sheet you want to query) as arguments to the sql
processor, and then you can query your Google Sheet by writing queries in the Google Visualization API Query Language.-- import: fastn/processors as pr
-- person list people:
$processor$: pr.sql
db: {{ YOUR GOOGLE SHEET URL }}
sheet: {{ NAME OF THE SHEET YOU WANT TO QUERY }}
;; Your Query
SELECT * WHERE A = "John Doe"
sql
processor:
STRING
INTEGER
DECIMAL
BOOLEAN
LABEL
Clause to Rename Header Names to Match Record KeyIt is possible that some header names in your Google Sheet contain spaces, or you want to use a different name in the model record for the result you retrieve using the sql
processor. In that case, you can use the LABEL
clause to rename that header/column in the retrieved response.
Full Name
, Phone
, and Blood Group
, since you will have to create a record for mapping the results of the sql
processor, and record property names cannot contain spaces, you will have to use a property name that does not contain spaces. You can do this by setting your own label for that column by specifying it with the LABEL
clause.LABEL
to rename headers "Full Name", "Phone" and "Blood Group" to match record keys-- import: fastn/processors as pr
-- record donor:
string full-name:
string phone:
string blood-group:
-- donor list donors:
$processor$: pr.sql
db: GOOGLE_SHEET_URL
sheet: Blood Donors
SELECT A, B, C LABEL A "full-name", B "phone", C "blood-group"
-- donor-card: $d
for: $d in $donors
todo-app
fastn-google-sheets-demo
sql
processor.