Skip to content

Batch save

Create several records from one request.

Lexicon type: procedure

local json = require("internal.json")
local record = require("happyview.record")
function handle(input, ctx)
local uris = {}
for _, item in ipairs(input.items) do
local ref = record.create(ctx.collection, item)
uris[#uris + 1] = ref.uri
end
return { uris = json.to_array(uris) }
end
  1. Iterate over input.items and create each one with record.create on happyview.record.
  2. Collect the resulting AT URIs and return them. json.to_array keeps uris a JSON array when items is empty.

Writes happen one at a time, in order. If one fails, the records before it exist and the ones after it don’t, and the error reaches the client as an error response (usually a 500; a 401 when the caller has no PDS session); wrap record.create in pcall to report partial progress instead.

The DPoP proof is signed per request; an app built on the HappyView SDK gets this from its session.

const response = await fetch("http://127.0.0.1:3000/xrpc/xyz.statusphere.batchCreate", {
method: "POST",
headers: {
"X-Client-Key": CLIENT_KEY,
Authorization: `DPoP ${ACCESS_TOKEN}`,
DPoP: DPOP_PROOF,
"Content-Type": "application/json",
},
body: JSON.stringify({
items: [
{ text: "First", createdAt: "2025-01-01T00:00:00Z" },
{ text: "Second", createdAt: "2025-01-01T00:01:00Z" },
],
}),
});
const data = await response.json();

Batch saving is useful when a single user action should create multiple records (e.g. importing data, multi-step forms).