TinyBase logoTinyBase β

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
TypeDescription
pathIdstring

The Id of the path.

valueIdstring

The Id of the Value.

valueValueOrUndefined

The new Value, or undefined if it is being deleted.

context{[key: string]: any} | undefined

The context from the client's ClientAccess.

returnsboolean

Whether the client may write the Value.

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