You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: examples/official-site/sqlpage/migrations/11_json.sql
+44-12
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,22 @@ INSERT INTO
3
3
VALUES
4
4
(
5
5
'json',
6
-
'For advanced users, allows you to easily build an API over your database.
7
-
The json component responds to the current HTTP request with a JSON object.
8
-
This component must appear at the top of your SQL file, before any other data has been sent to the browser.',
6
+
'Converts SQL query results into the JSON machine-readable data format. Ideal to quickly build APIs for interfacing with external systems.
7
+
8
+
**JSON** is a widely used data format for programmatic data exchange.
9
+
For example, you can use it to integrate with web services written in different languages,
10
+
with mobile or desktop apps, or with [custom client-side components](/custom_components.sql) inside your SQLPage app.
11
+
12
+
Use it when your application needs to expose data to external systems.
13
+
If you only need to render standard web pages,
14
+
and do not need other software to access your data,
15
+
you can ignore this component.
16
+
17
+
This component **must appear at the top of your SQL file**, before any other data has been sent to the browser.
18
+
An HTTP response can have only a single datatype, and it must be declared in the headers.
19
+
So if you have already called the `shell` component, or another traditional HTML component,
20
+
you cannot use this component in the same file.
21
+
',
9
22
'code',
10
23
'0.9.0'
11
24
);
@@ -32,7 +45,10 @@ VALUES
32
45
(
33
46
'json',
34
47
'type',
35
-
'The type of the JSON payload to send. Defaults to "array" (each query result is rendered as a JSON object in the array). Other possible values are "jsonlines" (each query result is rendered as a JSON object in a new line, without a top-level array) and "sse" (each query result is rendered as a JSON object in a new line, prefixed by "data: ", which allows you to read the results as server-sent events in real-time from javascript).',
48
+
'The type of the JSON payload to send: "array", "jsonlines", or "sse".
49
+
In "array" mode, each query result is rendered as a JSON object in a single top-level array.
50
+
In "jsonlines" mode, results are rendered as JSON objects in separate lines, without a top-level array.
51
+
In "sse" mode, results are rendered as JSON objects in separate lines, prefixed by "data: ", which allows you to read the results as server-sent events in real-time from javascript.',
36
52
'TEXT',
37
53
TRUE,
38
54
TRUE
@@ -45,7 +61,14 @@ VALUES
45
61
(
46
62
'json',
47
63
'
48
-
## Send query results as a JSON array
64
+
## Send query results as a single JSON array: `''array'' as type`
65
+
66
+
The default `array` mode sends the query results as a single JSON array.
67
+
68
+
If a query returns an error, the array will contain an object with an `error` property.
69
+
70
+
If multiple queries are executed, all query results will be concatenated into a single array
71
+
of heterogeneous objects.
49
72
50
73
### SQL
51
74
@@ -67,10 +90,21 @@ select * from users;
67
90
(
68
91
'json',
69
92
'
70
-
## Send a single JSON object
93
+
## Send a single JSON object: `''jsonlines'' as type`
94
+
95
+
In `jsonlines` mode, each query result is rendered as a JSON object in a separate line,
96
+
without a top-level array.
97
+
98
+
If there is a single query result, the response will be a valid JSON object.
99
+
If there are multiple query results, you will need to parse each line of the response as a separate JSON object.
100
+
101
+
If a query returns an error, the response will be a JSON object with an `error` property.
71
102
72
103
### SQL
73
104
105
+
The following SQL creates an API endpoint that takes a `user_id` URL parameter
106
+
and returns a single JSON object containing the user''s details, with one json object key per column in the `users` table.
107
+
74
108
```sql
75
109
select ''json'' AS component, ''jsonlines'' AS type;
76
110
select * from users where id = $user_id LIMIT 1;
@@ -91,7 +125,7 @@ select * from users where id = $user_id LIMIT 1;
91
125
(
92
126
'json',
93
127
'
94
-
## Create a complex API endpoint
128
+
## Create a complex API endpoint: the `''contents''` property
95
129
96
130
You can create an API endpoint that will return a JSON value in any format you want,
97
131
to implement a complex API.
@@ -138,12 +172,12 @@ you can use
138
172
(
139
173
'json',
140
174
'
141
-
## Access query results in real-time with server-sent events
175
+
## Access query results in real-time with server-sent events: `''sse'' as type`
142
176
143
177
Using server-sent events, you can stream large query results to the client in real-time,
144
178
row by row.
145
179
146
-
This allows building sophisticated dynamic applications that will start processing and displaying
180
+
This allows building sophisticated dynamic web applications that will start processing and displaying
147
181
the first rows of data in the browser while the database server is still processing the end of the query.
148
182
149
183
### SQL
@@ -161,9 +195,7 @@ eventSource.onmessage = function (event) {
161
195
const user = JSON.parse(event.data);
162
196
console.log(user.username);
163
197
}
164
-
eventSource.onerror = function () {
165
-
eventSource.close(); // do not reconnect after reading all the data
166
-
}
198
+
eventSource.onerror = () => eventSource.close(); // do not reconnect after reading all the data
Copy file name to clipboardexpand all lines: examples/official-site/sqlpage/migrations/48_status_code.sql
+27-8
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,31 @@ INSERT INTO
4
4
VALUES
5
5
(
6
6
'status_code',
7
-
'A simple component to set the HTTP status code for the response. This can be used to indicate the result of the request, such as 200 for success, 404 for not found, or 500 for server error.
7
+
'Sets the HTTP response code for the current page.
8
8
9
-
The status code should be set according to the HTTP standard status codes.
9
+
This is an advanced technical component.
10
+
You typically need it when building internet-facing APIs and websites,
11
+
but you may not need it for simple internal applications.
10
12
11
-
This component should be used when you need to explicitly set the status code of the HTTP response.
12
-
',
13
+
- Indicating operation results when using [SQLPage as an API](?component=json)
14
+
- `200`: *OK*, for successful operations
15
+
- `201`: *Created*, for successful record insertion
16
+
- `404`: *Not Found*, for missing resources
17
+
- `500`: *Internal Server Error*, for failed operations
18
+
- Handling data validation errors
19
+
- `400`: *Bad Request*, for invalid data
20
+
- Enforcing access controls
21
+
- `403`: *Forbidden*, for unauthorized access
22
+
- `401`: *Unauthorized*, for unauthenticated access
23
+
- Tracking system health
24
+
- `500`: *Internal Server Error*, for failed operations
25
+
26
+
For search engine optimization:
27
+
- Use `404` for deleted content to remove outdated URLs from search engines
28
+
- For redirection from one page to another, use
29
+
- `301` (moved permanently), or
30
+
- `302` (moved temporarily)
31
+
- Use `503` during maintenance',
13
32
'error-404'
14
33
);
15
34
@@ -27,20 +46,20 @@ VALUES
27
46
(
28
47
'status_code',
29
48
'status',
30
-
'The HTTP status code to be set for the response. This should be an integer value representing a valid HTTP status code.',
49
+
'HTTP status code (e.g., 200 OK, 401 Unauthorized, 409 Conflict)',
31
50
'INTEGER',
32
51
TRUE,
33
52
FALSE
34
53
);
35
54
36
-
37
55
INSERT INTO example (component, description)
38
56
VALUES (
39
57
'status_code',
40
-
'Set the HTTP status code to 404, indicating that the requested resource was not found.
58
+
'
59
+
Set the HTTP status code to 404, indicating that the requested resource was not found.
41
60
Useful in combination with [`404.sql` files](/your-first-sql-website/custom_urls.sql):
42
61
43
62
```sql
44
-
select''status_code'' as component, 404 as status;
63
+
SELECT''status_code'' as component, 404 as status;
0 commit comments