TinyBase logoTinyBase

canSetRow

The canSetRow method lets you allow or disallow any changes to a Row stored on the server, as sent from a client.

canSetRow(
  tableId: string,
  rowId: string,
  initialSave: boolean,
  requestOrConnection: Request | Connection,
): Promise<boolean>
TypeDescription
tableIdstring
rowIdstring
initialSaveboolean
requestOrConnectionRequest | Connection
returnsPromise<boolean>

This is one of the functions use to sanitize the data that is being sent from a client. Perhaps you might want to make sure the server-stored data adheres to a particular schema, or you might want to make certain data read-only. Remember that you cannot trust the client to only send data that the server considers valid or safe.

This method is passed the Table Id and Row Id that the client is trying to change. The initialSave parameter distinguishes between the first bulk save of the Store to the PartyKit room over HTTP (true), and subsequent incremental updates over a web sockets (false).

The final requestOrConnection parameter will either be the HTTP(S) request or the web socket connection, in those two cases respectively. You can, for instance, use this to distinguish between different users.

Return false from this method to disallow changes to this Row on the server, or true to allow them (subject to subsequent canSetCell method and canSetCell method checks). The default implementation returns true to allow all changes.

Example

The following implementation will strip out any attempts by the client to update the 'me' Row of the 'user' Table after the initial save:

import {TinyBasePartyKitServer} from 'tinybase/persisters/persister-partykit-server';

export class MyServer extends TinyBasePartyKitServer {
  canSetRow(tableId, rowId, initialSave) {
    return initialSave || tableId != 'user' || rowId != 'me';
  }
}

Since

v4.3.12