JSON schemav1: Original formv2: Redesigned form
JSON definition · shared/definitions/relational-storage.v1.jsonView on GitHub
{
"title": "Insurance claim",
"elements": [
{
"type": "text",
"name": "q_email",
"valueName": "customer_email",
"dbColumn": "claims.customer_email",
"title": "Email",
"inputType": "email",
"isRequired": true
},
{
"type": "text",
"name": "q_amount",
"valueName": "amount",
"dbColumn": "claims.amount",
"title": "Amount claimed",
"inputType": "number",
"min": 0
},
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Priority handling"
}
]
}JSON definition · shared/definitions/relational-storage.v2.jsonView on GitHub
{
"title": "Insurance claim",
"elements": [
{
"type": "panel",
"name": "contact",
"title": "Contact",
"elements": [
{
"type": "text",
"name": "contact_email",
"valueName": "customer_email",
"dbColumn": "claims.customer_email",
"title": "Where should we write to you?",
"inputType": "email",
"isRequired": true
}
]
},
{
"type": "panel",
"name": "claim",
"title": "Claim",
"elements": [
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "text",
"name": "claimed_total",
"valueName": "amount",
"dbColumn": "claims.amount",
"title": "Total amount you are claiming",
"inputType": "number",
"min": 0
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Priority handling"
}
]
}
]
}Client · src/app/examples/relational-storage/client.tsx · sjs:relational-storage.clientView on GitHub
import { Model, Serializer } from "survey-core";
// Optional metadata describing the columns used by the server
Serializer.addProperty("question", { name: "dbColumn", category: "data" });
// Keep answer keys stable when question names change
function createSurvey(definitions: Definitions, definitionVersion: Version) {
const survey = new Model(definitions[definitionVersion]);
survey.completeText = "Submit claim";
return survey;
}Client · src/app/examples/relational-storage/client.tsx · sjs:relational-storage.saveView on GitHub
const saveClaim: SaveClaim = async function saveClaim(data, definitionVersion) {
const response = await fetch("/api/claims", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ data, definitionVersion })
});
if (!response.ok) throw new Error("Save failed");
return response.json();
};Server · src/server/routes/relational-storage.ts · sjs:relational-storage.serverView on GitHub
// POST /api/claims — original JSON first, then the columns you query
export async function saveClaim(req: Request) {
const { data, definitionVersion } = ((await readJson(req)) ?? {}) as Envelope; // the parsed raw body: {} stays {}
if (data == null || definitionVersion == null) throw httpError(400, "Expected { data, definitionVersion }");
const id = transaction(db(req), () => {
const responseId = insertResponse(req, "claim", data, definitionVersion); // the original and its version
db(req).prepare("INSERT INTO claims (response_id, customer_email, amount) VALUES (?, ?, ?)")
.run(responseId, data.customer_email ?? null, data.amount ?? null); // keys come from valueName
return responseId;
});
return Response.json({ id }, { status: 201 });
}