This is the official documentation of Forest Admin Cloud.
A Field within a Collection specifies the attribute's structure and content. Fields are usually auto-detected and linked to the appropriate collection based on your connected data source.
To make an admin panel better for users, it is often important to create new fields that are not directly tied to a database field. These fields may be dynamically computed or sourced from other data sources or services.
For example, instead of storing a user's age, a database typically stores their birthdate. Showing the age instead of the birthdate directly on the user interface improves readability and simplifies data segmentation. Several other examples exist, such as the full name, the number of orders, the total amount spent, a tag indicating the risk level of a transaction, etc.
The following sections assume that you have correctly followed all the steps in the guide.
TL;DR
Ensure you update the collection and fields names as needed.
definition*Object: A JavaScript object that contains the definition of the field:
dependencies*[String]: An array of field or relationship names that the custom field value depends on.
getValues*Function: A function that processes new values in batches, returning an array in the same order as the input records.
records[Object]: An array of JavaScript objects representing the list of records that need to have the field value computed.
contextObject: The context data.
Importing a field
Reflecting your database structure as-is in the admin panel can make it harder for users. A good practice is to simplify the interface to help users work faster. One common way is to bring a field from a relationship directly into its parent record.
Database schema in this example
Table: users
+----+-----------+------------+------------+
| ID | firstName | lastName | addressId |
+----+-----------+------------+------------+
| | | | |
+----+-----------+------------+------------+
Table: addresses
+----+------------+--------------+-----------+------------+
| ID | streetName | streetNumber | city | countryId |
+----+------------+--------------+-----------+------------+
| | | | | |
+----+------------+--------------+-----------+------------+
Table: countries
+----+-------+
| ID | name |
+----+-------+
| | |
+----+-------+
Removing fields only affects their display in the admin panel. However, you can still access them in your code, for example, as dependencies to compute new fields.
Examples
To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and action names as needed.
import type { Agent } from '@forestadmin/forest-cloud';
import { Schema } from '../typings';
export default function customizeAgent(agent: Agent<Schema>) {
agent.customizeCollection('users', (collection) => {
// Insert the code snippet here.
});
}
Suppose we want to see if we can actually send emails to our users' email addresses. We can use a checking tool called a verification API to do this job. The API we're using is not real, and this is how it responds: