Skip to main content

Functions

Functions are reusable logic blocks called from routes or events.

config/functions/CreateUser.json
{
"name": "CreateUser",
"params": {
"email": "string",
"name": "string"
},
"steps": [
{ "type": "create", "model": "User", "data": { "email": "$email", "name": "$name" } },
{ "type": "return", "value": "$result" }
]
}

Step types

TypeDescription
createCreate a model instance
updateUpdate a model instance
deleteDelete a model instance
queryQuery model instances
conditionConditional branch (if/else)
returnReturn a value
callCall another function

Parameter references

Inside data and where objects, use $paramName to reference a function parameter and $result to reference the last step's output.

{ "type": "create", "model": "User", "data": { "email": "$email" } }

Generated output

For each function Codabra generates src/lib/functions/<Name>.ts:

src/lib/functions/CreateUser.ts
// Auto-generated by Codabra — do not edit manually
import { db } from "../db";
import * as schema from "../../drizzle/schema";
import { eq } from "drizzle-orm";

export async function CreateUser(params: { email: string; name: string }): Promise<unknown> {
let result: unknown;

result = await db
.insert(schema.users)
.values({ email: params.email, name: params.name })
.returning()
.then((r) => r[0]);
return result;
}

Calling a function from a route

Add "function": "CreateUser" to a route definition:

{
"method": "POST",
"path": "/api/users",
"function": "CreateUser",
"body": { "email": "string", "name": "string" }
}

The generated route will import and call the function instead of writing a raw Prisma query.