Skip to content

FastSchema Plugins System

The FastSchema Plugins System allows developers to extend the core functionality of FastSchema by writing JavaScript code, eliminating the need for deep familiarity with Golang. This approach empowers users to introduce custom features, manage content, and interact with FastSchema’s core APIs through a flexible plugin architecture.

Plugins are standalone modules that integrate with FastSchema to enhance or modify its behavior. By writing JavaScript code, developers can tap into the FastSchema event system, customize schemas, manage resources, and hook into the platform’s lifecycle events.

Javascript Runtime

FastSchema Plugins System using Goja as the JavaScript runtime. Goja is an ECMAScript 5.1(JavaScript) engine written in pure Go. It was designed to be embedded in Go applications and provide a way to run scripts and evaluate expressions.

Overview

FastSchema plugins are designed to be modular, allowing developers to create custom features and extend the platform’s functionality. Plugins can be used to:

  • Adding custom schema.
  • Adding custom resources.
  • Adding custom hooks.
  • Modifying the FastSchema configuration object.
  • Adding custom logic to the FastSchema lifecycle events.

Structure

Basically, a FastSchema plugin is a directory located in the data/plugins directory that contains a plugin.js file. The plugin.js file is the entry point for the plugin and is responsible for exporting the plugin’s functionality.

FastSchema Plugin Directory Structure

sh
fastschema-app
└── data
    └── plugins
        └── hello
            ├── plugin.js
            ├── product.json
            └── utils.js

Lifecycle

The FastSchema Plugins System follows a lifecycle model that consists of the following stages:

  1. Configuration: The plugin configuration function is called right after the FastSchema starts the bootstrapping process. It ONLY has access to the FastSchema configuration object and can modify it as needed.

  2. Initialization: The plugin is initialized right after the FastSchema application is bootstrapped and before the server starts. It has access to various FastSchema APIs like logger, db, context, and resources.

  3. Execution: The plugin’s resources and hooks are executed during the FastSchema application lifecycle. Resources are used to handle incoming requests, while hooks are used to intercept and modify the event flow.

Example

js
const product = require('./schemas/product.json');
const { getRandomName, ping } = require('./utils');

const Config = config => {
  // Add product schema
  config.AddSchemas(product);

  // Change the fastschema port to 9000
  config.port = '9000';
}

const Init = plugin => {
  // Create a group named 'hello' with two public resources (routes):
  // - hello/ping
  // - hello/world
  plugin.resources
    .Group('hello')
    .Add(ping, { public: true })
    .Add(world, { public: true });
}

const world = async ctx => {
  const name = await getRandomName();
  return `Hello, ${name}!`;
}
js
const getRandomName = async () => {
  const names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Hank', 'Ivy', 'Jack'];
  return names[Math.floor(Math.random() * names.length)];
}

const ping = ctx => {
  return 'pong';
}

module.exports = {
  getRandomName,
  ping,
}
json
{
  "name": "product",
  "namespace": "products",
  "label_field": "name",
  "fields": [
    {
      "type": "string",
      "name": "name",
      "label": "Name",
      "optional": true,
      "sortable": true
    },
    {
      "type": "string",
      "name": "description",
      "label": "Description",
      "optional": true,
      "sortable": true,
      "renderer": {
        "class": "w-full bg-gray-50 rounded-lg border"
      }
    }
  ]
}

Rules

To ensure application stability and performance, the FastSchema Plugins System enforces a set of guidelines that developers should follow when building plugins. Please refer to the Plugins Development Rules for more information.

Released under the MIT License.