TinyBase logoTinyBase β

canWriteCell

The canWriteCell method is used to decide whether a client may write a given Cell.

canWriteCell(
  pathId: string,
  tableId: string,
  rowId: string,
  cellId: string,
  cell: CellOrUndefined,
  context: {[key: string]: any} | undefined,
): boolean
TypeDescription
pathIdstring

The Id of the path.

tableIdstring

The Id of the Table.

rowIdstring

The Id of the Row.

cellIdstring

The Id of the Cell.

cellCellOrUndefined

The new value of the Cell, 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 Cell.

It is called for each Cell in the changes that a writable client sends, including deletions, where the cell parameter is undefined. It must be synchronous. Cells 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 staff write anything, but lets customers write only to the orders Table.

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')},
    };
  }

  canWriteCell(pathId, tableId, rowId, cellId, cell, {role}) {
    return role == 'staff' || tableId == 'orders';
  }
}

Since

v10.1.0