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.
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
, optionalSpecifies the name of the database table.
-
primaryKeyColumn
string
, optionalSpecifies the primary key column in the table. Typically, this is set to
"id"
. -
select
-
pageSize
number
, optionalSets the number of records displayed per page for table pagination.
-
columns
string[]
, optionalSpecifies the columns to include in the query and display by default.
If not specified, all columns
(*)
will be included in the query.Examplecolumns: ["first_name", "last_name"]
-
excludeColumns
string[]
, optionalSpecifies columns to exclude from the query, often used to omit sensitive data such as password hashes or payment details.
ExampleexcludeColumns: ["password_hash", "address", "updated_at"]
-
searchableColumns
string[]
, optionalEnables a search input at the top of the table. Specifies the columns that are searchable, typically applicable to text or numeric column types.
ExamplesearchableColumns: ["first_name", "last_name", "email"]
-
sortablecolumns
string[]
, optionalEnables sorting functionality by clicking on column headers. Specifies the columns that can be sorted.
ExamplesortableColumns: ["created_at"]
-
filterableColumns
string[]
, optionalAdds a "Filters" button at the top of the table. Specifies the columns that can be filtered.
ExamplefilterableColumns: ["status"]
-
executeQuery
function
, optionalAllow to define define custom fetching logic.
Learn more: Custom queries
-
-
insert
-
columns
string[]
, optionalSpecifies 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[]
, optionalSpecifies the columns to exclude from the insert query. This is commonly used for system-generated or sensitive data.
Exampleexclude: ["secret_token", "password_hash", "created_at", "updated_at"]
-
beforeInsert
function
, optionalA 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).
ExamplebeforeInsert: (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
, optionalA 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 anError
, the record is not inserted, and the user receives an error message.ExamplecanBeInserted: (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[]
, optionalSpecifies 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[]
, optionalSpecifies the columns to exclude from the update query. This is commonly used for system-generated or sensitive data.
Exampleexclude: ["secret_token", "password_hash", "created_at", "updated_at"]
-
beforeUpdate
function
, optionalA 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).
ExamplebeforeUpdate: (record) => {
return {
...record,
updated_at: new Date()
}
} -
canBeUpdated
function
, optionalA 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 anError
, the record is not updated, and the user receives an error message.ExamplecanBeUpdated: (record) => {
if (!record.email.includes('@')) {
throw new Error('Invalid email');
}
return true;
}
-
-
delete
-
canBeDeleted
function
, optionalA 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 anError
, the record is not deleted, and the user receives an error message.ExamplecanBeDeleted: (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)