I want to use workers as serverless API but not sure how to save data in database. please help how to create sample serverless api with database connectivity.
The best way to do this is either with the feature Durable Objects or the feature Workers KV. They both have their pros and cons which you can learn from their explanation pages.
https://developers.cloudflare.com/workers/runtime-apis/kv
https://developers.cloudflare.com/workers/learning/using-durable-objects
thanks for assist, I checked KV and Durable Objects but still confused how can I query on saved data, could you please provide some examples?
1 Like
async function handleRequest(request) {
const value = await FIRST_KV_NAMESPACE.get("first-key")
if (value === null) {
return new Response("Value not found", {status: 404})
}
return new Response(value)
}
1 Like