Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow updating the entire key value store at once #412

Open
PfisterFactor opened this issue Aug 6, 2024 · 2 comments
Open

Allow updating the entire key value store at once #412

PfisterFactor opened this issue Aug 6, 2024 · 2 comments

Comments

@PfisterFactor
Copy link

PfisterFactor commented Aug 6, 2024

There is an issue I run into with having a typed context.

Say I have a type like:

interface Lead {
    Status: "NONEXISTANT";
} |
{
    Status: "ACTIVE",
    Details: {
    	SomeField: number,
    	SomeOtherField: string
	};
}

And my context looks like:

ctx: restate.ObjectContext<Lead>

Currently there is no typesafe way for me to update the key value store. This is annoying when trying to implement a state-machine pattern for the key value store.

Example:

ctx.set("Status", "ACTIVE"); // Context is now in non-typesafe state because Details doesn't exist
ctx.set("Details", ...) // Get a type error because Details field isn't present when Status is "NONEXISTANT"

If we were to introduce some method to update the entire key/value state at once, we can resolve this. It would be akin to a map operation.

await ctx.update((oldState) => {
	return {
		Status: "ACTIVE",
		Details: {
			SomeField: 1,
			SomeOtherField: "hello"
		}
	}
} // All fields are updated atomically and type safety is preserved
@mupperton
Copy link
Contributor

mupperton commented Aug 7, 2024

Could you not achieve the same thing with the following:

interface State {
    Lead: {
        Status: "NONEXISTANT"
    } |
    {
        Status: "ACTIVE"
        Details: {
    	    SomeField: number
    	    SomeOtherField: string
	}
    }
}

const handler = async (ctx: restate.ObjectContext<State>) => {
    const oldState = await ctx.get("Lead") // typed: Lead | null

    ctx.set("Lead", {
        Status: "ACTIVE",
        Details: {
            SomeField: 1,
            SomeOtherField: "hello",
        },
    })
}

@PfisterFactor
Copy link
Author

PfisterFactor commented Aug 29, 2024

Sure - but now we have to nest it arbitrarily. Plus the issue remains that you can't atomically update one or more fields within the context store at the first level

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants