Node.js Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
    • Install
      • Create your agent
      • Expose an HTTP endpoint
        • For standalone agents
        • On Express
        • On Koa
        • On Fastify
        • On NestJS
      • Autocompletion & Typings
      • Troubleshooting
    • Migrating legacy agents
      • What's new
      • Pre-requisites
      • Recommendations
      • Migration steps
        • Run new agent in parallel
        • Configure database connection
        • Code transformations
          • API Charts
          • Live Queries
          • Smart Charts
          • Route overrides
          • Smart Actions
          • Smart Fields
          • Smart Relationships
          • Smart Segments
        • Compare schemas
        • Swap agents
      • Post-migration
        • Dropping Sequelize
        • Optimize your agent
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Cross-data source relationships
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • SQL (without ORM)
      • Sequelize
      • Mongoose
      • MongoDB
    • Write your own
      • Replication strategy
        • Persistent cache
        • Updating the replica
          • Scheduled rebuilds
          • Change polling
          • Push & Webhooks
        • Schema & References
        • Write handlers
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Provided plugins
        • AWS S3
        • Advanced Export
        • Flattener
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
      • Deploy on Azure
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page
  • How does it work?
  • Examples
  • Performance

Was this helpful?

  1. Agent customization
  2. Fields

Add fields

PreviousFieldsNextMove, rename and remove fields

Last updated 7 months ago

Was this helpful?

This is the official documentation of the @forestadmin/agent Node.js agent.

Forest Admin allows creating new Fields on any Collection, either computationally, by fetching data on an external API or based on other data that is available on the connected data sources.

By default, the fields that you create will be read-only, but you can make them , , and by using the relevant methods.

How does it work?

When creating a new field you will need to provide:

Field
Description

columnType

dependencies

List of fields that you need from the source records and linked records in order to run the handler

getValues

Handler which computes the new value for a batch of records

enumValues (optional)

When columnType is Enum, you must specify the values that the field will support

Examples

Adding a field by concatenating other fields

This example adds a user.displayName field, which is computed by concatenating the first and last names.

// "user" Collection has the following structure: { id, firstName, lastName }
agent.customizeCollection('user', collection => {
  collection.addField('displayName', {
    // Type of the new field
    columnType: 'String',

    // Dependencies which are needed to compute the new field (must not be empty)
    dependencies: ['firstName', 'lastName'],

    // Compute function for the new field
    // Note that the function computes the new values in batches: the return value
    // must be an array which contains the new values in the same order than the
    // provided records.
    getValues: (records, context) =>
      records.map(r => `${r.firstName} ${r.lastName}`),
  });
});

Adding a field that depends on another computed field

This example adds a user.displayName field, which is computed by concatenating the first and last names, and then another which capitalize it.

// "user" Collection has the following structure: { id, firstName, lastName }
agent.customizeCollection('user', collection => {
  collection
    // Create a field which is computed by concatenating the first and last names
    .addField('displayName', {
      columnType: 'String',
      dependencies: ['firstName', 'lastName'],
      getValues: (records, context) =>
        records.map(r => `${r.firstName} ${r.lastName}`),
    })

    // Create another field which is computed by uppercasing the first field
    .addField('displayNameCaps', {
      columnType: 'String',
      dependencies: ['displayName'], // You can depend on other computed fields
      getValues: (records, context) => records.map(r => r.displayName.toUpperCase()),
    });
});

Adding a field that depends on a many-to-one relationship

We can improve the previous example by adding the city of the user to the display name.

// Structure:
// User    { id, addressId, firstName, lastName }
// Address { id, city }

agent.customizeCollection('user', collection => {
  collection.addField('displayName', {
    columnType: 'String',

    // We added 'address:city' in the list of dependencies,
    // which tells forest to fetch the related record
    dependencies: ['firstName', 'lastName', 'address:city'],

    // The address is now available in the parameters
    getValues: (records, context) =>
      records.map(r => `${r.firstName} ${r.lastName} (from ${r.address.city})`),
  });
});

Adding a field that depends on a one-to-many relationship

Let's now add a user.totalSpending field by summing the amount of all orders.

// Structure
// User  { id }
// Order { id, customer_id, amount }

agent.customizeCollection('user', collection => {
  collection.addField('totalSpending', {
    columnType: 'Number',
    dependencies: ['id'],
    getValues: async (records, context) => {
      const recordIds = records.map(r => r.id);

      // We're using Forest Admin's query interface
      // (you can use an ORM or a plain SQL query)
      const filter = {
        conditionTree: { field: 'customer_id', operator: 'In', value: recordIds },
      };
      const aggregation = {
        operation: 'Sum',
        field: 'amount',
        groups: [{ field: 'customer_id' }],
      };
      const rows = await context.dataSource
        .getCollection('order')
        .aggregate(filter, aggregation);

      return records.map(record => {
        const row = rows.find(r => r.group.customer_id === record.id);
        return row?.value ?? 0;
      });
    },
  });
});

Adding a field fetching data from an API

Let's imagine that we want to check if the email address of our users is deliverable. We can use a verification API to perform that work.

The API we're using is fictional, and the structure of the response is:

{
  "username1@domain.com": {
    "usernameChecked": false,
    "usernameValid": null,
    "domainValid": true
  },
  "username2@domain.com": {
    "usernameChecked": false,
    "usernameValid": null,
    "domainValid": true
  }
}
const emailVerificationClient = require('@sendchimplio/client');
emailVerificationClient.setApiKey(process.env.SENDCHIMPLIO_API_KEY);

// "User" Collection has the following structure: { id, email }
agent.customizeCollection('user', collection => {
  collection.addField('emailDeliverable', {
    columnType: 'Boolean',
    dependencies: ['email'],
    getValues: async (records, context) => {
      // Call the API to verify emails
      const response = await emailVerificationClient.verifyEmails(
        records.map(r => r.email),
      );

      // Return values in the same order than the source records
      return records.map(r => {
        const check = response[r.email];
        return check.domainValid && (!usernameChecked || usernameValid);
      });
    },
  });
});

Performance

When adding many fields, keep in mind that:

  • You should refrain from making queries to external services

    • Use relationships in the dependencies array when that is possible

    • Use batch APIs calls instead of performing requests one by one inside of the records.map handler.

  • Only add fields you need in the dependencies list

    • This will reduce the pressure on your data sources (fewer columns to fetch)

    • And increase the probability of reducing the number of records that will be passed to your handler (records are deduplicated).

  • Do not duplicate code between handlers of different fields: fields can depend on each other (no cycles allowed).

Type of the new field which can be or

filterable
sortable
writable
any primitive
composite type