FastSchema Documentation

website·Indexed 31 days ago

https://fastschema.com

FastSchema is a Backend as a Service (BaaS) and Go web framework for building dynamic web applications. It features built-in Role-Based Access Control (RBAC), JWT-based authentication, a real-time API, and an ORM-based database package. The framework provides tools for managing schemas, resources, hooks, and automated database migrations, along with support for OpenAPI specifications and rclone-based storage abstraction.

Tokens
31.5K
Snippets
187
Records
231
Agent score
53%

What's inside FastSchema

  1. Overview of the FastSchema Plugins System

    The FastSchema Plugins System allows developers to extend core functionality using JavaScript, removing the requirement for Golang knowledge. Plugins are standalone modules that can be used to add custom schemas, resources, and hooks, modify the configuration object, and inject custom logic into lifecycle events. The system uses Goja, an ECMAScript 5.1 compliant JavaScript engine written in Go, as its runtime.
  2. Overview of FastSchema JWT Authentication

    FastSchema uses JSON Web Tokens (JWT) for authentication. Upon successful login, the server generates tokens and returns them to the client. Clients must store these tokens and include them in the header of every subsequent request to access protected resources. By default, all resources are protected unless they are explicitly marked as public during resource registration or granted to the guest role.
  3. Overview of FastSchema BaaS and Go Framework

    FastSchema is a Backend as a Service (BaaS) and Go web framework designed to automate backend development. It uses a schema blueprint to automatically generate database tables and RESTful API endpoints. It supports MySQL, PostgreSQL, and SQLite databases and provides a built-in admin control panel for content and user management, as well as automatic OpenAPI Specification (OAS) generation.
  4. Overview of FastSchema Storage abstraction

    FastSchema uses the rclone package to provide a filesystem abstraction (rclonefs) that allows reading and writing files across different storage systems. Currently, it supports the local filesystem and S3-compatible storage.
  5. Overview of FastSchema Hooks

    Hooks in FastSchema are functions that execute before or after an operation on a resource. They are categorized into resource hooks, database hooks, and application hooks, enabling developers to extend functionality and add custom logic to APIs.
  6. Understand Resource Resolver routing in FastSchema

    In FastSchema, client requests are not sent directly to a Resource. Instead, they are routed by a resolver that directs the request to the appropriate Resource. The current default implementation is the RestfulResolver, which routes requests based on the HTTP method and path.
  7. Generate OpenAPI v3.1 specifications for resources

    FastSchema automatically generates OpenAPI specification v3.1 for each resource. It uses Ogen to perform the generation, inferring the resource's input and output types directly from the Go function signature of the handler.
  8. Understand FastSchema Resources

    In FastSchema, a Resource is a component that processes user requests and returns responses, functioning similarly to a controller in MVC or a resolver in GraphQL. A Resource consists of a unique ID, a name, a handler function for processing requests, metadata, and signatures that define the input and output types.
  9. Implement a Resource to process requests

    A Resource in FastSchema acts as a controller (MVC) or resolver (GraphQL) that processes user requests and returns responses.

    Key characteristics of Resources:

    • Handler: Every Resource must have a handler function to process the request and return the response.
    • Input/Output Definition: You can specify the expected input data and the returned output data for validation and API documentation.
    • Grouping: Related resources can be grouped together for better organization (e.g., grouping all user-related endpoints).
    • Routing: Requests are not sent directly to Resources but are managed by a resolver. FastSchema currently uses a RestfulResolver which routes based on HTTP method and path.
  10. Understand System Schema vs User Schema

    FastSchema distinguishes between two types of schemas:

    • UserSchema: Created by users via the Admin UI, stored in JSON format, and used to generate database tables.
    • SystemSchema: Generated from Go structs. These are designed for system-level data (users, roles, permissions) or any schema defined within Go code. They are created exclusively via code, cannot be deleted, but can be extended by users in the Admin UI.
  11. Initialize a FastSchema plugin

    Plugin initialization occurs after the FastSchema application is bootstrapped but before the server starts. The Init function provides access to the FsPlugin object, allowing developers to perform startup tasks such as querying the database via $db(), logging via $logger(), and registering API resources via plugin.resources.
    /** @param {FsPlugin} plugin */
    const Init = plugin => {
      $logger().Info('Hello plugin is initializing...');
      const products = $db().Query($context(), 'SELECT * FROM products');
      $logger().Info('Products:', products);
    
      plugin.resources
        .Group('hello')
        .Add(ping, { public: true })
        .Add(world, { public: true });
    };
  12. Add custom system schemas via plugin

    Use the config.AddSchemas() method within the Config function to define the structure of new resources in the FastSchema application. A schema is defined as a SchemaRawData object containing the resource name, namespace, label field, and an array of field definitions.
    // Example schema definition
    const product = {
      "name": "product",
      "namespace": "products",
      "label_field": "name",
      "fields": [
        {
          "type": "string",
          "name": "name",
          "label": "Name",
          "optional": true,
          "sortable": true
        }
      ]
    };
    
    const Config = config => {
      config.AddSchemas(product);
    }