Docs
Script Examples
Script Examples Small examples for handlers, hooks, flows, and websocket event scripts. Repository Reads Find Rows const posts = await #post.find({ fields: "id,title", limit: 10 }) Find One Row const result = await #post.find({ filter: { id: { _eq: @PARAMS.id } }, fields: "id,tit
Script Examples
Small examples for handlers, hooks, flows, and websocket event scripts.
Repository Reads
Find Rows
const posts = await #post.find({
fields: "id,title",
limit: 10
})
Find One Row
const result = await #post.find({
filter: { id: { _eq: @PARAMS.id } },
fields: "id,title",
limit: 1
})
return result.data[0] || null
Count Rows
const result = await #post.find({
fields: "id",
limit: 1,
meta: "totalCount"
})
return result.meta.totalCount
Repository Writes
Create Row
const created = await #post.create({
data: {
title: @BODY.title,
status: "draft"
}
})
return created.data[0]
Update Row
const updated = await #post.update({
id: @PARAMS.id,
data: {
title: @BODY.title
}
})
return updated.data[0]
Delete Row
await #post.delete({
id: @PARAMS.id
})
return { deleted: true }
Errors
Required Body Field
if ([email protected]) {
@THROW400("Title is required")
}
Forbidden Action
if (!@USER) {
@THROW403("Login required")
}
Not Found
const row = await #post.find({
filter: { id: { _eq: @PARAMS.id } },
fields: "id",
limit: 1
})
if (!row.data[0]) {
@THROW404("post", @PARAMS.id)
}
Hooks
Pre-Hook Owner Scope
@QUERY.filter = {
_and: [
@QUERY.filter || {},
{ owner: { id: { _eq: @USER.id } } }
]
}
Pre-Hook Set Owner On Create
@BODY.owner = { id: @USER.id }
Pre-Hook Strip Server Field
delete @BODY.isAdmin
Post-Hook Shape Response
return {
id: @DATA.id,
title: @DATA.title
}
Flows
Trigger Flow From Handler
await @TRIGGER("send-welcome-email", {
userId: @USER.id
})
return { queued: true }
Flow Step Reads Payload
const userId = @FLOW_PAYLOAD.userId
return { userId }
Flow Step Uses Previous Step
const user = @FLOW.load_user
return {
email: user.email
}
WebSocket
Reply To Current Client
@SOCKET.reply("pong", {
at: new Date().toISOString()
})
Join Room
@SOCKET.join(`project:${@BODY.projectId}`)
Emit To Current Room
@SOCKET.emitToCurrentRoom(`project:${@BODY.projectId}`, "project:changed", {
projectId: @BODY.projectId
})
Emit To User
@SOCKET.emitToUser(@USER.id, "notification", {
title: "Done"
})
Cache
Set Cache
await @CACHE.set("report:latest", { count: 12 }, 60000)
Get Cache
const cached = await @CACHE.get("report:latest")
if (cached) return cached