canWriteValue
The canWriteValue method is used to decide whether a client may write a given Value.
canWriteValue(
pathId: string,
valueId: string,
value: ValueOrUndefined,
context: {[key: string]: any} | undefined,
): boolean| Type | Description | |
|---|---|---|
pathId | string | The |
valueId | string | |
value | ValueOrUndefined | The new |
context | {[key: string]: any} | undefined | The context from the client's |
| returns | boolean | Whether the client may write the |
It is called for each Value in the changes that a writable client sends, including deletions, where the value parameter is undefined. It must be synchronous. Values for which it returns false are neither merged into the Durable Object's MergeableStore nor relayed to other clients, and a client that wrote a newer version is brought back into line with the server.
Example
This example lets only staff change the shop's opening hours.
import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
export class MyDurableObject extends WsServerDurableObject {
authorize(pathId, request) {
return {
context: {role: new URL(request.url).searchParams.get('role')},
};
}
canWriteValue(pathId, valueId, value, {role}) {
return role == 'staff' || valueId != 'openingHours';
}
}
Since
v10.1.0