fastn
Note: This document is about querying PostgreSQL Database. You can also query SQLite using fastn
.
pg
processor allows you to execute SQL queries against a PostgreSQL database.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.CREATE TABLE users ( id SERIAL, name TEXT, department TEXT );
INSERT INTO "users" (name, department) VALUES ('jack', 'design'); INSERT INTO "users" (name, department) VALUES ('jill', 'engineering');
fastn
about your databasefastn
about your PostgreSQL database credentials. We do this by creating a .env
file:export FASTN_PG_URL=postgres://username:password@db-host/db-name
The FASTN_PG_URL
must contain a valid connection string.
.env
file must be present in the same folder as your FASTN.ftd
file..env
file is properly setup you can fetch data from the SQLite database using pg
processor:-- import: fastn/processors as pr -- person list people: $processor$: pr.pg db: db.sqlite 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.
Also note that since the result of this query can be multiple rows (or one or none), we have to read the result in aperson list
, so all data can be stored in corresponding list.$loop$
:-- show-person: $p $loop$: $people as $p
Have a question or need help?
Visit our GitHub Q&A discussion to get answers and subscribe to it to stay tuned.
Join our Discord channel and share your thoughts, suggestion, question etc.
Connect with our community!We welcome you to join our Discord community today.
We are trying to create the language for human beings and we do not believe it would be possible without your support. We would love to hear from you.