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: usage/sync-rules/parameter-queries.mdx
+34-36Lines changed: 34 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Parameter queries allow parameters to be defined on a bucket to group data. Thes
8
8
bucket_definitions:
9
9
# Bucket Name
10
10
user_buckets:
11
-
#Paramter Query
11
+
#Parameter Query
12
12
parameters: SELECT request.user_id() as user_id
13
13
# Data Query
14
14
data:
@@ -47,47 +47,45 @@ request.jwt() ->> 'app_metadata.custom_field' -- custom field added by a service
47
47
A previous syntax for parameter queries used `token_parameters`. Expand the below for details on how to migrate to the recommended syntax above.
48
48
</Note>
49
49
50
-
<Accordion
51
-
title="Previous Syntax"
52
-
>
53
-
The previous syntax for parameter queries used `token_parameters.user_id` to return the JWT subject. Example:
50
+
<Accordiontitle="Previous Syntax">
51
+
The previous syntax for parameter queries used `token_parameters.user_id` to return the JWT subject. Example:
54
52
55
-
```yaml
56
-
bucket_definitions:
57
-
by_user_parameter:
58
-
parameters: SELECT token_parameters.user_id as user_id
59
-
data:
60
-
- SELECT * FROM lists WHERE lists.owner_id = bucket.user_id
61
-
```
62
-
### Migrate to Recommended Syntax
53
+
```yaml
54
+
bucket_definitions:
55
+
by_user_parameter:
56
+
parameters: SELECT token_parameters.user_id as user_id
57
+
data:
58
+
- SELECT * FROM lists WHERE lists.owner_id = bucket.user_id
59
+
```
63
60
64
-
The new functions available in sync rules are:
61
+
### Migrate to Recommended Syntax
65
62
66
-
1. `request.jwt()`: Returns the entire (signed) JWT payload as a JSON string.
67
-
2. `request.parameters()`: Returns [client parameters](/usage/sync-rules/advanced-topics/client-parameters) as a JSON string.
68
-
3. `request.user_id()`: Returns the token subject, same as `request.jwt() ->> 'sub'` and also the same as `token_parameters.user_id` in the previous syntax.
63
+
The new functions available in sync rules are:
69
64
70
-
The major difference from the previous `token_parameters` is that all payloads are preserved as-is, which can make usage a little more intuitive. This also includes JWT payload fields that were not previously accessible.
65
+
1. `request.jwt()`: Returns the entire (signed) JWT payload as a JSON string.
66
+
2. `request.parameters()`: Returns [client parameters](/usage/sync-rules/advanced-topics/client-parameters) as a JSON string.
67
+
3. `request.user_id()`: Returns the token subject, same as `request.jwt() ->> 'sub'` and also the same as `token_parameters.user_id` in the previous syntax.
71
68
72
-
Migrating to the new syntax:
69
+
The major difference from the previous `token_parameters` is that all payloads are preserved as-is, which can make usage a little more intuitive. This also includes JWT payload fields that were not previously accessible.
73
70
74
-
1. `token_parameters.user_id` references can simply be updated to `request.user_id()`
75
-
2. Custom parameters can be updated from `token_parameters.my_custom_field` to `request.jwt() ->> 'parameters.my_custom_field'`
76
-
1. This example applies if you keep your existing custom JWT as is.
77
-
2. Supabase users can now make use of [Supabase's standard JWT structure](https://supabase.com/docs/guides/auth/jwts#jwts-in-supabase) and reference `app_metadata.my_custom_field` directly.
71
+
Migrating to the new syntax:
78
72
79
-
Example:
73
+
1. `token_parameters.user_id` references can simply be updated to `request.user_id()`
74
+
2. Custom parameters can be updated from `token_parameters.my_custom_field` to `request.jwt() ->> 'parameters.my_custom_field'`
75
+
1. This example applies if you keep your existing custom JWT as is.
76
+
2. Supabase users can now make use of [Supabase's standard JWT structure](https://supabase.com/docs/guides/auth/jwts#jwts-in-supabase) and reference `app_metadata.my_custom_field` directly.
80
77
81
-
```yaml
82
-
bucket_definitions:
83
-
by_user_parameter:
84
-
# request.user_id() is the same as the previous token_parameter.user_id
85
-
parameters: SELECT request.user_id() as user_id
86
-
data:
87
-
- SELECT * FROM lists WHERE lists.owner_id = bucket.user_id
88
-
```
89
-
</Accordion>
78
+
Example:
90
79
80
+
```yaml
81
+
bucket_definitions:
82
+
by_user_parameter:
83
+
# request.user_id() is the same as the previous token_parameter.user_id
84
+
parameters: SELECT request.user_id() as user_id
85
+
data:
86
+
- SELECT * FROM lists WHERE lists.owner_id = bucket.user_id
87
+
```
88
+
</Accordion>
91
89
92
90
#### Filter on additional columns
93
91
@@ -132,7 +130,7 @@ bucket_definitions:
132
130
133
131
In this example, a single query can return multiple sets of bucket parameters for a single user.
134
132
135
-
Keep in mind that the total number of buckets per user should remain limited (< 1,000), so don't make buckets too granular.
133
+
Keep in mind that the total number of buckets per user should remain limited (\< 1,000), so don't make buckets too granular.
136
134
137
135
```yaml
138
136
bucket_definitions:
@@ -181,7 +179,7 @@ bucket_definitions:
181
179
- SELECT * FROM todos WHERE todos.list_id = bucket.list_id
182
180
```
183
181
184
-
Keep in mind that the total number of buckets per user should remain limited (< 1,000), so don't make buckets too granular.
182
+
Keep in mind that the total number of buckets per user should remain limited (\< 1,000), so don't make buckets too granular.
185
183
186
184
#### Global buckets
187
185
@@ -214,4 +212,4 @@ Notable features and restrictions:
214
212
1. Only simple `SELECT` statements are supported.
215
213
2. No `JOIN`, `GROUP BY` or other aggregation, `ORDER BY`, `LIMIT`, or subqueries are supported.
216
214
3. For token parameters, only `=` operators are supported, and `IN` to a limited extent.
217
-
4. A limited set of operators and functions are supported — see [Operators and Functions](/usage/sync-rules/operators-and-functions).
215
+
4. A limited set of operators and functions are supported — see [Operators and Functions](/usage/sync-rules/operators-and-functions).
0 commit comments