Skip to content

Commit 94b8c01

Browse files
authored
Merge pull request #390 from devforth/AdminForth/573
docs: add raw SQL usage example
2 parents 81617ba + 6d4775c commit 94b8c01

File tree

1 file changed

+33
-0
lines changed
  • adminforth/documentation/docs/tutorial/03-Customization

1 file changed

+33
-0
lines changed

adminforth/documentation/docs/tutorial/03-Customization/11-dataApi.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ const users = await admin.resource('adminuser').list(
123123
);
124124
```
125125

126+
## Using a raw SQL in queries.
127+
128+
Rarely you might want to add ciondition for some exotic SQL but still want to keep the rest of API.
129+
Technically it happened that AdminForth allows you to do this also
130+
131+
```js
132+
const minUgcAge = 18;
133+
const usersWithNoUgcAccess = await admin.resource('adminuser').list(
134+
[
135+
Filters.NEQ('role', 'Admin'),
136+
{
137+
insecureRawSQL: `(user_meta->>'age') < ${sqlstring.escape(minUgcAge)}`
138+
}
139+
140+
], 15, 0, Sorts.DESC('createdAt')
141+
);
142+
143+
This will produce next SQL query:
144+
```
145+
146+
```
147+
SELECT *
148+
FROM "adminuser"
149+
WHERE "role" != 'Admin'
150+
AND (user_meta->>'age') < 18
151+
ORDER BY "createdAt" DESC
152+
LIMIT 15 OFFSET 0;
153+
154+
```
155+
156+
157+
Finds users with age less then 18 from meta field which should be a JSONB field in Postgress.
158+
126159
## Create a new item in database
127160

128161
Signature:

0 commit comments

Comments
 (0)