One fundamental feature that separates Membrane from other serverless runtimes is that it is stateful.
You don’t need to store data in a database or file to persist it. Instead, the state of your program (the entire JS heap) is transparently and efficiently persisted every time it changes.
The state object
To keep data around, put it in the state object, and that’s it.
import {
const state:any
This object is automatically persisted by Membrane.
Its type is defined by the exported State interface
below.
state } from"membrane";
// Initialize a value
const state:any
This object is automatically persisted by Membrane.
Its type is defined by the exported State interface
below.
state.
any
count??=0;
exportfunction
functioncount():number
count() {
return++
const state:any
This object is automatically persisted by Membrane.
Its type is defined by the exported State interface
below.
state.
any
count;
}
Note that the entire JS heap is persistent, not just state. The reason we use state is because each deploy (i.e.
code change) creates a new ES Module, so this object serves as a convenient way to pass data from one version to the
next. Technically you could use globalThis, but this is not recommended since it’s also accesible by NPM dependencies
and our IDE provides tooling to conveniently inspect state.
In most cases Membrane will generate typing information for state based on usage. For example, from the code above, we
will automatically infer the type of state to be:
type
type State = {
count:number;
}
State= {
count: number
count:number;
};
As an alternative, you can also provide typing information for state by exporting a State type or interface from index.ts:
exportinterface
interface State
State {
State.count: number
count:number;
}
JavaScript objects as the database
Membrane’s memory-based durability means the data you put in state doesn’t need to be JSON-serializable. It’s useful
to use JavaScript Maps or Sets to store data that can be efficiently read or written to.