ForestAdmin / agent-nodejs

🌱 Node.js agent for Forest Admin
GNU General Public License v3.0
67 stars 8 forks source link

Feature request: reformat a field #461

Closed Segfaultd closed 2 years ago

Segfaultd commented 2 years ago

Expected behavior

Would be nice to be able to reformat a field "on-the-fly". Something like this

collection.reformatField('created_at', (field) => {
    return (field as Date).getDay();
});

Actual behavior

This would return a field on a newer format

romain-gilliotte commented 2 years ago

Hi segfaultd

Sadly that would not work with just a handler.

You would need to provide at least:

If you need neither of those features, you can do the following:

collection
  .addField('created_at_new', {
    columnType: 'Number',
    dependencies: ['created_at'],
    getValues: records  => records.map(r => (field as Date).getDay())
  })
  .removeField('created_at')
  .renameField('created_at_new', 'created_at')

We'll be releasing a plugin system shortly so that customers can write plugins as they need and use the following syntax:

import { reformatField } from './my-plugins';

collection
  .use(reformatField, { name: 'created_at', handler: value => value.getDay() })
romain-gilliotte commented 2 years ago

Also, I see that you have a cast in your code.

Did you setup the typing generation? You really should! It helps a ton when building an agent

https://docs.forestadmin.com/beta-developer-guide-agents-v2/getting-started/install/autocompletion-and-typings

Segfaultd commented 2 years ago

Hi @romain-gilliotte and thanks for the feedback!

My issue is that in case of CSV export, some of my fields return a full timestamp like "2022-06-07T13:21:06.150Z" when I would actually appreciate to only extract "DD/MM/YYYY" thus why I need to reformat the field "otf" :D

romain-gilliotte commented 2 years ago

As of today, our CSV exporter is not customizable in any way (it's a feature from the early days of the company).

You can generate files from smart actions to customize the output as you want If generating a big CSV, providing a stream to the resultBuilder is supported

https://docs.forestadmin.com/beta-developer-guide-agents-v2/agent-customization/actions/responses#file-generation

collection.addAction('Export Data', {
  scope: 'Global',
  generateFile: true,
  execute: async (context, resultBuilder) => {
    return resultBuilder.file('StringThatWillBeInTheFile', 'filename.csv', 'application/csv');
  },
});

You can then remove the default export from the frontend using Roles

https://docs.forestadmin.com/user-guide/project-settings/teams-and-users/manage-roles

Segfaultd commented 2 years ago

Make sense! Thank you @romain-gilliotte