Skip to main content

API Reference

The defineTableController function sets up server-side endpoint to handle request from the page with the table component. It connects to a database and defines what data available for the table and how it behaves.

Example
export default action = 
app.defineTableController(dataSource, {
table: 'users',
primaryKeyColumn: 'id',
select: {
pageSize: 16,
},
insert: false,
update: true,
delete: false,
linked: [],
});

Usage

The defineTableController function takes two required arguments:

  • dataSource: A data source.

  • parameters: A set of parameters.

Parameters

  • table

    string, optional

    Specifies the name of the database table.

  • primaryKeyColumn

    string, optional

    Specifies the primary key column in the table. Typically, this is set to "id".

  • select

    • pageSize

      number, optional

      Sets the number of records displayed per page for table pagination.

    • columns

      string[], optional

      Specifies the columns to include in the query and display by default.

      If not specified, all columns (*) will be included in the query.

      Example
      columns: ["first_name", "last_name"]
    • excludeColumns

      string[], optional

      Specifies columns to exclude from the query, often used to omit sensitive data such as password hashes or payment details.

      Example
      excludeColumns: ["password_hash", "address", "updated_at"]
    • searchableColumns

      string[], optional

      Enables a search input at the top of the table. Specifies the columns that are searchable, typically applicable to text or numeric column types.

      Example
      searchableColumns: ["first_name", "last_name", "email"]
    • sortablecolumns

      string[], optional

      Enables sorting functionality by clicking on column headers. Specifies the columns that can be sorted.

      Example
      sortableColumns: ["created_at"]
    • filterableColumns

      string[], optional

      Adds a "Filters" button at the top of the table. Specifies the columns that can be filtered.

      Example
      filterableColumns: ["status"]
    • executeQuery

      function, optional

      Allow to define define custom fetching logic.

      Learn more: Custom queries

  • insert

    • columns

      string[], optional

      Specifies the columns to include in the insert form by default. These are the columns the user can provide values for during record creation.

      If not specified, all columns except primary key column will be included in the insert form.

    • expludeColumns

      string[], optional

      Specifies the columns to exclude from the insert query. This is commonly used for system-generated or sensitive data.

      Example
      exclude: ["secret_token", "password_hash", "created_at", "updated_at"]
    • beforeInsert

      function, optional

      A function executed on the record before it is inserted into the table. This function is often used to format data, add missing but required properties, or generate sensitive data that the user should not input directly (e.g., password hashes, access tokens).

      Example
      beforeInsert: (record) => {
      const secret_token = generate_random_token();
      const created_at = new Date();
      const updated_at = new Date();

      return {
      ...record,
      secret_token,
      created_at,
      updated_at
      }
      }
    • canBeInserted

      function, optional

      A function for server-side validation before a record is inserted.

      If the function returns true, the record is inserted.

      If it returns false or throws an Error, the record is not inserted, and the user receives an error message.

      Example
      canBeInserted: (record) => {
      if (!record.email.includes('@')) {
      throw new Error('Invalid email');
      }

      const isEmailTaken = !!(await knex('users').where('email', record.email).count());
      if (isEmailTaken) {
      throw new Error('A user with this email already exists');
      }

      return true;
      }
  • update

    • columns

      string[], optional

      Specifies the columns to include in the update form by default. These are the columns the user can provide values for during record update.

      If not specified, all columns except primary key column will be included in the update form.

    • excludeColumns

      string[], optional

      Specifies the columns to exclude from the update query. This is commonly used for system-generated or sensitive data.

      Example
      exclude: ["secret_token", "password_hash", "created_at", "updated_at"]
    • beforeUpdate

      function, optional

      A function executed on the record before it is updated in the table. This function is often used to format data, add missing but required properties, or generate sensitive data that the user should not input directly (e.g., password hashes, access tokens).

      Example
      beforeUpdate: (record) => {
      return {
      ...record,
      updated_at: new Date()
      }
      }
    • canBeUpdated

      function, optional

      A function for server-side validation before a record is updated.

      If the function returns true, the record is updated.

      If it returns false or throws an Error, the record is not updated, and the user receives an error message.

      Example
      canBeUpdated: (record) => {
      if (!record.email.includes('@')) {
      throw new Error('Invalid email');
      }

      return true;
      }
  • delete

    • canBeDeleted

      function, optional

      A function for server-side validation before a record is deleted.

      If the function returns true, the record is deleted.

      If it returns false or throws an Error, the record is not deleted, and the user receives an error message.

      Example
      canBeDeleted: (record) => {
      if (record.role === 'ADMIN') {
      throw new Error('Admin users cannot be deleted.');
      }

      return true;
      }
  • linked

    Allows linked records to be displayed in the table and enables users to select related records when adding or updating records.

    Learn more: Linked records (Joins)