SQLPage v0.20.4 documentation

If you are completely new to SQLPage, you should start by reading the get started tutorial, which will guide you through the process of creating your first SQLPage application.

Building an application with SQLPage is quite simple. To create a new web page, just create a new SQL file. For each SELECT statement that you write, the data it returns will be analyzed and rendered to the user. The two most important concepts in SQLPage are components and parameters.

To select a component and set its top-level properties, you write the following SQL statement:

SELECT 'component_name' AS component, 'my value' AS top_level_parameter_1;

Then, you can set its row-level parameters by writing a second SELECT statement:

SELECT my_column_1 AS row_level_parameter_1, my_column_2 AS row_level_parameter_2 FROM my_table;

This page documents all the components provided by default in SQLPage and their parameters. Use this as a reference when building your SQL application. If at any point you need help, you can ask for it on the SQLPage forum.

If you know some HTML, you can also easily create your own components for your application.

components

The "json" component

For advanced users, allows you to easily build an API over your database. The json component responds to the current HTTP request with a JSON object. This component must appear at the top of your SQL file, before any other data has been sent to the browser.

Introduced in SQLPage v0.9.0.

Top-level parameters

contents

REQUIRED. The JSON payload to send. You should use your database's built-in json functions to build the value to enter here.

Examples

Creates an API endpoint that will allow developers to easily query a list of users stored in your database.

You should use the json functions provided by your database to form the value you pass to the contents property. To build a json array out of rows from the database, you can use:

  • json_group_array() in SQLite,

  • json_agg() in Postgres, or

  • JSON_ARRAYAGG() in MySQL.

  • FOR JSON PATH in SQL Server.

SELECT 'json' AS component, 
        JSON_OBJECT(
            'users', (
                SELECT JSON_GROUP_ARRAY(
                    JSON_OBJECT(
                        'username', username,
                        'userid', id
                    )
                ) FROM users
            )
        ) AS contents;

This will return a JSON response that looks like this:

{ 
    "users" : [
        { "username":"James", "userid":1 }
    ]
}

Official SQLPage documentation