Skip to content

Update or delete

A single endpoint that handles create, update, and delete based on the input fields.

Trigger: xrpc.procedure:<nsid>. Needs: happyview-db, happyview-record.

local db = require("happyview.db")
local record = require("happyview.record")
function handle(input, ctx)
if input.delete and input.uri then
record.delete(input.uri)
return { success = true }
end
if input.uri then
-- Update existing
local existing = db.get(input.uri)
if not existing then
return { error = "RecordNotFound", message = "no record at that uri" }
end
local body = existing.record
body.status = input.status
return record.put(input.uri, body)
end
if input.delete then
return { error = "UriRequired", message = "a delete needs the uri of the record" }
end
-- Create new
return record.create(ctx.collection, input)
end
  1. If input.delete is truthy and input.uri is provided, delete the record with record.delete, which removes it from the caller’s PDS and the index.
  2. If only input.uri is provided, load the stored body with db.get, change its fields, and write it back with record.put, which calls putRecord. If no record exists at that URI, return a RecordNotFound error.
  3. If input.delete is set without a uri, return a UriRequired error rather than creating a record.
  4. Otherwise, create a new record from the input with record.create.

RecordNotFound and UriRequired must be declared under defs.main.errors in the lexicon, as in "errors": [{ "name": "RecordNotFound" }, { "name": "UriRequired" }], for the response to be a 400. An undeclared error is sent as an ordinary 200 response body.

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

Create:

const response = await fetch("http://127.0.0.1:3000/xrpc/xyz.statusphere.setRecord", {
method: "POST",
headers: {
"X-Client-Key": CLIENT_KEY,
Authorization: `DPoP ${ACCESS_TOKEN}`,
DPoP: DPOP_PROOF,
"Content-Type": "application/json",
},
body: JSON.stringify({ status: "hello" }),
});
const data = await response.json();

Update:

const response = await fetch("http://127.0.0.1:3000/xrpc/xyz.statusphere.setRecord", {
method: "POST",
headers: {
"X-Client-Key": CLIENT_KEY,
Authorization: `DPoP ${ACCESS_TOKEN}`,
DPoP: DPOP_PROOF,
"Content-Type": "application/json",
},
body: JSON.stringify({
uri: "at://did:plc:abc/xyz.statusphere.record/abc123",
status: "updated",
}),
});
const data = await response.json();

Delete:

const response = await fetch("http://127.0.0.1:3000/xrpc/xyz.statusphere.setRecord", {
method: "POST",
headers: {
"X-Client-Key": CLIENT_KEY,
Authorization: `DPoP ${ACCESS_TOKEN}`,
DPoP: DPOP_PROOF,
"Content-Type": "application/json",
},
body: JSON.stringify({
uri: "at://did:plc:abc/xyz.statusphere.record/abc123",
delete: true,
}),
});
const data = await response.json();

This pattern puts create, update and delete behind one procedure, so your app needs fewer endpoints. The uri and delete fields in the input pick the action.