createTableRpc

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

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

Parameters

table

Specifies the name of the database table.

primaryKeyColumn

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

select

Defines settings for selecting and displaying data in the table.

pageSize

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

columns

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

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

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

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

Example
sortableColumns: ["created_at"]

filterableColumns

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

Example
sortableColumns: ["created_at"]

insert

Defines settings for inserting records into the table.

columns

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 will be available in the insert form.

excludeColumns

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

Example
exclude: ["secret_token", "created_at", "updated_at"]

beforeInsert()

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()

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

Defines settings for updating records in the table.

columns

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 will be available in the update form.

excludeColumns

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

Example
exclude: ["secret_token", "created_at", "updated_at"]

beforeUpdate()

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()

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()

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

Preloads records from related tables or creates joins. It displays linked records in the table and lets users select records from the related table when inserting or updating records.

oneToOne

Defines a one-to-one relationship between two tables.

Example
{
    relation: 'oneToOne',
    
    /** Foreign key column in the current table */   
    foreignKeyColumn: 'user_id',
    
    /** The name of the target table */      
    targetTable: 'users',
    
    /** The primary key column in the target table 
        that the foreign key refers to */ 
    targetTableKeyColumn: 'id',
    
    /** The columns in the target table to include 
        in queries and display by default */
    columns: ['last_name', 'first_name'],
    
    /** The columns in the target table available for search */
    searchableColumns: ['last_name', 'first_name'],
}

oneToMany

Defines a one-to-many relationship where one record in the current table can be linked to multiple records in the related table.

Example
{
      relation: 'oneToMany',
      
      /** The name of the target table */
      targetTable: 'orders',
      
      /** The primary key column in the target table */ 
      targetTableKeyColumn: 'id',
      
      /** The foreign key column in the target table 
          that refers to the current table */ 
      targetTableForeignKeyColumn: 'courier_id',
      
      /** The columns in the target table to include 
          in queries and display by default */ 
      columns: ['title', 'sum'],
      
      /** The columns in the target table available for search */ 
      searchableColumns: ['title'],
      
      /** Maximum number of records to show in the table preview */
      previewMaxRecords: 3,
}

manyToMany

This feature is not yet supported. In the future, it will allow users to link records using junction tables.

Last updated