Skip to content

Instantly share code, notes, and snippets.

@igalshilman
Last active April 18, 2024 14:08
Show Gist options
  • Save igalshilman/d9169fe45e8e8ae54d93d36c3c6dd3b3 to your computer and use it in GitHub Desktop.
Save igalshilman/d9169fe45e8e8ae54d93d36c3c6dd3b3 to your computer and use it in GitHub Desktop.
type TodoItem = {
title: string,
completed: boolean,
}
export default restate.object({
name : "todo",
handlers: {
add: async (ctx: ObjectContext, item: TodoItem) => {
const items = await ctx.get("items") ?? [];
items.push(item);
ctx.set("items", items);
return { id: items.length - 1}
},
all: async(ctx: ObjectContext) => { return await ctx.get("items") ?? [] },
complete: async(ctx: ObjectContext, id: number) => {
const items = await ctx.get("items") ?? [];
const item = items[i];
item.completed = true;
ctx.set("items", items);
return item;
}
}
});
/// $ curl myapp/todo/bob/add --json '{ "title" : "Learn restate" }';
/// { "id" : 0 }
/// curl myapp/todo/bob/all
/// [ { "title" : "Learn restate", "completed" : false } ]
/// curl myapp/todo/bob/complete --json '{ "id" : 0 }'
/// { "title" : "Learn restate" , "completed" : true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment