Fields
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 Code Customization Getting Started guide.
TL;DR
Ensure you update the collection and fields names as needed.
To make the code easier to read, all the code snippets below should be wrapped in the following code.
Creating a Field
First, you need to call the customizeCollection()
method on the agent.
Arguments:
name
* String: The name of the collection to customize.handle
* Function: A function that has the collection instance as an argument to start customizing it.
To create a field, use the addField()
method on your collection instance.
Arguments:
name
* String: The name of the field.definition
* Object: A JavaScript object that contains the definition of the field:columnType
* String: The type of the new field which can be any primitive or composite type.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.context
Object: The context data.
Creating fields can impact the performance of your admin panel. It is crucial to review best practices for fields to guarantee an optimal experience for your admin panel users.
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.
The users
collection now includes two direct fields: city
and country
. You can set these fields to be editable or not by toggling the readonly
option.
Renaming a field
Renaming a field can improve readability and can be done using the renameField()
method.
Renaming fields only affects their display in the admin panel. To access them in your code, always use their original names.
Removing a field
To hide fields in the UI for technical, confidentiality, or any other reasons, use the removeField()
method.
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.
Concatenating two fields
Depending on a "many-to-one" relationship
In the example below, adding address:city
to the list of dependencies
makes the related data available in the getValues()
function.
Depending on a "one-to-many" relationship
In the example below, we want to add a users.totalSpending
field by summing the amounts of all orders
.
Retrieve record IDs: Start by getting all the record IDs.
Filter orders for current users: Use these IDs to filter orders that belong to current users.
Aggregate orders by user: Group the orders by
customer_id
and sum up the total order amount for each user.Display totals: Finally, show the total amount spent by each user. If a user hasn't placed any orders, their total will be shown as 0.
Fetching data from an API
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:
Last updated