// The only SQL operations exposed by the adapter are fixed, bound queries. // Owner identity comes exclusively from the authenticated server session. function createStore(db,decide,origin,owner){ function result(status,body){return {status,body}} async function execute(model,action,{id="",label="",value="",revision=0}={}){ let p=decide(model,action,label,value),kind=p["@2"],quota=p["@3"],canonical=p["@4"]; if(p["@1"]!==200)return result(p["@1"],{error:p["@1"]===404?"Unknown model":"Invalid record command"}); const bind=(sql,...args)=>db.prepare(sql).bind(...args); const shape=r=>({id:r.id,label:r.label,value:kind===2?Number(r.value):r.value,revision:r.revision,created_at:r.created_at}); if(action==="list"){let r=await bind("SELECT id,label,value,revision,created_at FROM bry_records WHERE origin=? AND owner=? AND model=? ORDER BY created_at,id LIMIT ?",origin,owner,model,quota).all();return result(200,{model,kind:kind===2?"integer":"text",quota,records:r.results.map(shape)})} if(action==="read"){let r=await bind("SELECT id,label,value,revision,created_at FROM bry_records WHERE origin=? AND owner=? AND model=? AND id=?",origin,owner,model,id).first();return r?result(200,shape(r)):result(404,{error:"Record not found"})} if(action==="create"){let newId=crypto.randomUUID();let r=await bind("INSERT INTO bry_records(origin,owner,model,id,label,value) SELECT ?,?,?,?,?,? WHERE (SELECT COUNT(*) FROM bry_records WHERE origin=? AND owner=? AND model=?)