Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
shared-key: rust-sqlpage-proj-test
save-if: ${{ github.ref == 'refs/heads/main' }}
- run: cargo fmt --all -- --check
- run: cargo test --locked --features config-schema --test mod configuration_docs
- run: cargo clippy --all-targets --all-features -- -D warnings
# The database matrix below runs the same Rust test harnesses against
# different DATABASE_URL values. Package the Linux test executables once
Expand Down
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tokio = { version = "1.24.1", features = ["macros", "rt", "process", "sync"] }
tokio-stream = "0.1.9"
anyhow = "1"
serde = "1"
schemars = { version = "1", optional = true }
serde_json = { version = "1.0.82", features = [
"preserve_order",
"raw_value",
Expand Down Expand Up @@ -137,6 +138,7 @@ opentelemetry-semantic-conventions = { version = "0.33", features = ["semconv_ex

[features]
default = []
config-schema = ["dep:schemars"]
odbc-static = ["odbc-sys", "odbc-sys/vendored-unix-odbc"]
lambda-web = [
"dep:actix-http",
Expand All @@ -151,3 +153,7 @@ actix-http = "3"
tempfile = "3"
tokio = { version = "1", features = ["rt", "time", "test-util"] }

[[example]]
name = "export_config_schema"
required-features = ["config-schema"]

15 changes: 15 additions & 0 deletions examples/export_config_schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use schemars::generate::SchemaSettings;
use sqlpage::app_config::AppConfig;
use std::io::{self, Write};

fn main() -> anyhow::Result<()> {
let settings =
SchemaSettings::draft2020_12().with(|settings| settings.inline_subschemas = true);
let schema = settings
.into_generator()
.into_root_schema_for::<AppConfig>();
let mut stdout = io::stdout().lock();
serde_json::to_writer_pretty(&mut stdout, &schema)?;
writeln!(stdout)?;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: 3 solutions to the 3 layer problem
date: '2023-08-10'
icon: adjustments-question
summary: What is the 3 layer problem, and how SQLPage solves it?
---

# 3 solutions to the 3 layer problem

> Some interesting questions emerged from the article [Repeating yourself thrice doesn't turn you into a 3X developer](https://yrashk.medium.com/repeating-yourself-thrice-doesnt-turn-you-into-a-3x-developer-a778495229c0).
> This short follow-up article aims to answer them and clarify some points.

Hello all,

I am Ophir Lojkine, the main contributor of the open-source application server **SQLPage**.

The previous article focused on the conventional model of splitting applications into three distinct tiers:

1. a graphical interface (_front-end_),
2. an application server (_back-end_),
3. and a database.

In many projects, this results in three distinct implementations of the application’s data model:

1. First, in SQL, in the form of tables, views, and relationships in the database,
2. Then, in _server side_ languages such as Java, Python, or PHP, to create an API managing access to the data, and to implement the business logic of the application,
3. Finally, in JavaScript or TypeScript to implement data manipulation in the user interface.

![Traditional tiers model](blog/three-layers.svg)

---

The topic of interest here is the duplication of the data model between the different layers,
and the communication overhead between them.
We are not talking about how the code is structured within each layer.
It can follow a Model-View-Controller pattern or not, it doesn't matter.

This three-layer model has several advantages:
specialization of the programmers,
parallelization of work,
scalability,
separation of concerns,
and an optimal exploitation of the capacities of the infrastructure on which each layer is deployed:
web browser, server application and database.

Nevertheless, in large-scale projects,
there is often a certain redundancy of the code between the different layers,
as well as a non-negligible share of code dedicated to communication between them.
For small teams and solo developers, this becomes a major drawback.

## 3 solutions

Fortunately, there are several approaches to solving this problem:

1. For **UI-centric applications** without complicated data processing needs,
you can almost completely abandon server-side development and **directly expose the data to the frontend**.
Open-source tools available in this space include Supabase, PocketBase or Hasura.
2. For **applications with a predominant business logic**, traditional _web frameworks_
solve this problem by centralizing frontend and database control in the backend code.
A common solution involves using an ORM and templating system instead of a dedicated javascript application.
Popular solutions include Django, Ruby on Rails, or Symphony.
3. For simpler applications, it is possible to **avoid both backend and frontend development**
by adopting a _database-first_ approach.
This alternative, although less widespread, allows taking advantage of under-exploited modern capacities of relational databases.
The purpose of the original article was to introduce this lesser known approach.
* **SQLPage** is representative of this last category,
which allows designing a complete web application _in SQL_.
This leads to a loss of control over the precise visual appearance of the application,
which will get a “standardized” look and feel.
On the other hand, this translates into significant gains in terms of development speed,
simplicity and performance.
This solution is not intended to compete with traditional _frameworks_,
but rather integrate earlier in the life cycle of a project.
It thus makes it possible to quickly develop a data structure adapted to the application,
and to iterate over it while benefiting from continuous visual feedback on the final result.
Then, when the application grows, it’s easy to add a classic frontend and backend on top of the existing database,
without having to start from scratch.

Whichever approach is chosen in the end,
a solid understanding of the conventional three-tier architecture,
as well as a clear perspective on the challenges it creates and the possible solutions,
facilitates decision-making and the evolution of the project with the best suited technologies.
25 changes: 25 additions & 0 deletions examples/official-site/content/blog/come-and-see-us-in-athens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Come and see us in Athens !
date: '2024-10-15'
icon: microphone-2
summary: PGConf 2024 is coming to Athens, Greece. And we will be there to talk about SQLPage & Archeology !
---

# PostgreSQL & SQLPage at PGConf 2024

We have been invited to give a talk at PGConf 2024 in Athens, Greece.

Last year, we gave a general introduction to SQLPage at PGConf.eu 2023 in Prague.

This year, we will focus on a more specific use case:
using SQLPage to build a power-tool for archaeologists to explore and understand archaeological sites,
radically changing the way archaeologists work, and allowing them to
drastically reduce the time spent on data entry and management.

There will be two presenters:
- **Ophir Lojkine**, the creator and maintainer of SQLPage, will present the project and its capabilities.
- **Thomas Guillemard**, an archaeologist from France's National Institute for Preventive Archaeological Research, will present the project's use case and its benefits.

Come and see us in Athens !

https://www.postgresql.eu/events/pgconfeu2024/schedule/session/5707-unearthing-the-past-with-postgresql-how-open-source-is-revolutionizing-digital-archaeology/
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Come see me build twitter live on stage in Prague
date: '2023-11-11'
icon: calendar-event
summary: I will speak about SQLPage at pgconf.eu in Prague on December 14th. Come see me !
---

# SQLPage live on stage

Hello everyone !

If some of you european SQLPagers are around Prague this december,
I will be giving a talk about SQLPage at [pgconf.eu](https://2023.pgconf.eu/) on December 14th.

I will be talking about website building in general, SQLPage in particular,
cool things you can do with it,
how it works, how and why I built it,
and why I still think it can cut web development and prototyping times by a factor of 10.

I will also be building a tiny social network live on stage, using SQLPage.

Come see me !

- [pgconf.eu](https://2023.pgconf.eu/)
- [Information about the talk](https://www.postgresql.eu/events/pgconfeu2023/schedule/session/4687-sqlpage-building-a-full-web-application-with-nothing-but-postgres-and-sql-queries/)
- When ? **December 14th, 2023**, **09:30 - 10:20**
- Where ? Zenit Room, [Clarion Congress Hotel, Prague, Czech Republic](https://maps.app.goo.gl/qHCAaRFRjXmx2kQA7)
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: File-based routing in SQLPage
date: '2025-07-28'
icon: route
summary: Understanding how SQLPage maps URLs to files and handles errors
---

SQLPage uses a simple file-based routing system that maps URLs directly to SQL files in your project directory.
No complex configuration is needed. Just create files and they become accessible endpoints.

This guide explains how SQLPage resolves URLs, handles different file types, and manages 404 errors so you can structure your application effectively.

## Request routing order

### 1. Site Prefix Handling

If you've configured a [`site_prefix`](/your-first-sql-website/nginx) in your settings,
SQLPage will redirect all requests that do not start with the prefix to `/<site_prefix>`.

### 2. Path Resolution Priority

**Directory requests (paths ending with `/`)**: SQLPage looks for an `index.sql` file in that directory and executes it if found.

**Direct SQL file requests (`.sql` extension)**: SQLPage executes the requested SQL file if it exists. The extension is matched without regard to ASCII letter case, so `/report.SQL` is also executed. It is never served as source text.

**Static asset requests (other extensions)**: SQLPage serves files like CSS, JavaScript, images, or any other static content directly.

**Clean URL requests (no extension)**: SQLPage first tries to find a matching `.sql` file. If that doesn't exist but there's an `index.sql` file in a directory with the same name, it redirects to the directory path with a trailing slash.

Paths with trailing spaces are rejected. On Windows, paths with trailing dots or NTFS stream names are rejected too. These spellings can refer to a different file than their URL suggests on some storage systems.

### Error Handling

When, after applying each of the rules above in order, SQLPage can't find a requested file,
it walks up your directory structure looking for [custom `404.sql` files](/your-first-sql-website/custom_urls).

## Dynamic Routing with SQLPage

SQLPage's file-based routing becomes powerful when combined with strategic use of 404.sql files to handle dynamic URLs. Here's how to build APIs and pages with dynamic parameters:

### Product Catalog with Dynamic IDs

**Goal**: Handle URLs like `/products/123`, `/products/abc`, `/products/new-laptop`

**Setup**:
```text
products/
├── index.sql # Lists all products (/products/)
├── 404.sql # Handles /products/<product-id>
└── categories.sql # Product categories (/products/categories)
```

**Request paths**
- `/products/` → Executes `products/index.sql` (product listing)
- `/products/123` → No `123.sql` file exists, so executes `products/404.sql`
- `/products/laptop` → No `laptop.sql` file exists, so executes `products/404.sql`

**In `products/404.sql`**:
```sql
set product_id = substr(sqlpage.path(), 1+length('/products/'));
```
Loading
Loading