LogoLogo
Join our community
  • AdminJS
  • Contribute
  • Demo
  • Addons Marketplace
  • Installation
    • Getting started
    • Plugins
      • Adonis
      • Express
      • Nest
      • Fastify
      • Hapi
      • Koa
      • Community Plugins
        • FeathersJS
        • AdonisJS
      • Matrix
    • Adapters
      • TypeORM
      • Sequelize
      • Prisma
      • MikroORM
      • Objection
      • SQL
      • Mongoose
      • Community Adapters
        • AdonisJS
    • What's new in v7?
    • Migration Guide v7
  • Basics
    • Resource
    • Action
    • Property
    • Features
      • Relations
      • Upload
      • Logger
      • Import & Export
      • Password
      • Leaflet Maps
      • Writing your own features
    • API
      • List
      • New
      • Search
      • Show
      • Edit
      • Delete
      • Bulk Delete
    • Themes
    • Authentication
      • FirebaseAuthProvider
      • MatrixAuthProvider
  • How to write an addon?
  • UI Customization
    • Writing your own Components
    • Overwriting CSS styles
    • Dashboard customization
    • Changing the form view
    • Storybook
  • Tutorials
    • Role-Based Access Control
    • Internationalization (i18n)
    • Content Management System
    • Custom components library
    • Custom component internationalization
  • FAQ
    • PDF Generator
    • Charts
    • Forgot Password
  • ⚠️Legacy documentation
Powered by GitBook
On this page
  1. Basics
  2. Features

Writing your own features

PreviousLeaflet MapsNextAPI

Last updated 2 years ago

Features simplify writing code that is shared between your resources as they automatically handle merging of configuration.

A simple feature feature could be implemented as follows:

const feature = (prevResourceOptions) {
  return {
    ...prevResourceOptions,
    actions: {
      ...prevResourceOptions.actions,
      edit: {
        ...(prevResourceOptions.actions && prevResourceOptions.actions.edit),
        //..
      }
      //..
    }
  }
}

export { feature }

As you can see, in the example above you have to take care of merging previous options, which could be problematic. Fortunately AdminJS gives you helper functions which help with this:

  • a factory function ,

  • optional helper (when you need more control)

import { buildFeature, FeatureType } from 'adminjs';

import SomeModel from 'some-model.entity.js';

const someBeforeHook = () => { /* noop */ };

const myFeature = (config = {}): FeatureType => {
  // do something with your feature config?

  return buildFeature({
    actions: {
      edit: {
        before: [someBeforeHook],
      },
    }
  });
}

const SomeResource: ResourceWithOptions = {
  resource: SomeModel,
  features: [myFeature({})],
};

/* "someBeforeHook" will be used as a "before" hook in "edit" actions
for every resource where "myFeature" is added */

This is how a feature could look like when is used:

buildFeature
mergeResourceOptions
buildFeature