TinyBase logoTinyBase β

canSetTable

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

canSetTable(
  tableId: string,
  initialSave: boolean,
  requestOrConnection: Request | Connection,
): Promise<boolean>
TypeDescription
tableIdstring
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 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 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.

Since v4.3.13, the final parameter is the Cell previously stored on the server, if any. Use this to distinguish between the addition of a new Cell (in which case it will be undefined) and the updating of an existing one.

Return false from this method to disallow changes to this Table on the server, or true to allow them (subject to subsequent canSetRow method, canDelRow method, 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 any 'user' tabular data after the initial save:

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

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

Since

v4.3.12