AdonisJS v5 Documentation

website·Indexed 37 days ago

https://v5-docs.adonisjs.com/

Reference documentation for AdonisJS v5, featuring comprehensive guides on the Validator system, including schema types (array, object, number, date, boolean, file, enum) and validation rules such as unique, exists, email, and uuid. Includes release notes and updates from the v5 preview through 2022.

Tokens
142.9K
Snippets
1K
Records
1.1K
Agent score
54%

What's inside AdonisJS

  1. Overview of AdonisJS Drive

    AdonisJS Drive provides a consistent API for managing files across different storage providers, such as the local file system, Amazon S3, DigitalOcean Spaces, and Google Cloud Storage. This allows developers to use local storage during development and switch to cloud storage in production without changing application code. It is primarily intended for managing user-uploaded files (e.g., avatars, cover images) rather than static website assets.
  2. Overview of AdonisJS Web Guard

    The web guard uses sessions and cookies to authenticate users. It is the required choice for server-rendered applications or APIs with a first-party client running on the same domain or sub-domain. The web guard depends on the @adonisjs/session package, which must be installed and configured before use.
  3. Overview of the AdonisRC configuration file

    The .adonisrc.json file is located in the project root and configures the workspace and runtime settings for an AdonisJS application. By default, it contains only minimum required settings. To view the full configuration including defaults, use the Ace command node ace dump:rcfile.
    node ace dump:rcfile
  4. Overview of the Edge template engine

    AdonisJS uses Edge, a logical and batteries-included template engine for Node.js. Edge can render any text-based format, including HTML, Markdown, and plain text. Unlike some other engines, Edge allows the use of native JavaScript expressions directly within templates and does not require a custom syntax for HTML structure.
  5. Add TypeScript type definitions for custom Request methods

    To inform TypeScript about methods added to the Request class at runtime, use declaration merging. Create a definition file (e.g., contracts/request.ts) and extend the RequestContract interface within the @ioc:Adonis/Core/Request module.
    // contracts/request.ts
    declare module '@ioc:Adonis/Core/Request' {
      interface RequestContract {
        wantsJSON(): boolean
      }
    }
  6. Access session data in AdonisJS views

    The session helper provides read-only access to the session store for the current HTTP request. This helper is only available when views are rendered using the ctx.view.render or ctx.view.renderAsync methods.
  7. In-memory TypeScript compilation in development

    AdonisJS now supports in-memory compilation of TypeScript, meaning compiled JavaScript is no longer written to disk. This eliminates issues with stale build folders. It uses a custom implementation (via @adonisjs/require-ts) that hooks into Node.js require extensions and uses the TypeScript transpileModule API. To optimize performance, it employs on-disk caching based on MD5 hashes of file contents.
  8. Reuse markup with Edge partials

    Partials allow you to extract chunks of markup (such as headers, footers, or sidebars) into separate files and reuse them across multiple templates. This promotes maintainability and reduces duplication.
  9. Access session flash messages in views using flashMessages

    The flashMessages helper allows you to access session flash messages within views. This helper is exclusively available when rendering views via ctx.view.render or ctx.view.renderAsync. To debug and view all currently available flash messages, you can combine it with the inspect helper.
    {{ inspect(flashMessages.all()) }}
  10. Access and use the Schema builder in migrations

    The schema builder is used to perform SQL DDL (Data Definition Language) operations. In AdonisJS migration files, the schema builder instance is accessed via the this.schema property within a class that extends BaseSchema.
    import BaseSchema from '@ioc:Adonis/Lucid/Schema'
    
    class UserSchema extends BaseSchema {
      public up() {
        // Access the schema builder via this.schema
        console.log(this.schema)
      }
    }
  11. Run AdonisJS in development with in-memory compilation

    AdonisJS uses an in-memory compiler (via @adonisjs/require-ts) during development to avoid writing compiled JavaScript to disk. This allows for a faster development cycle and first-class TypeScript support in the REPL.

    Key characteristics:

    • It uses the official TypeScript compiler API and respects tsconfig.json.
    • Type-checking is disabled during development (relying on the IDE).
    • Compiled output is stored in a cache folder to prevent recompiling unchanged files on server restarts.
    • The server and compiler are started together using the serve --watch command.
    node ace serve --watch
  12. Import and use the AdonisJS Database module

    The Database module provides the primary API for interacting with SQL databases in AdonisJS. It serves as a gateway to query clients, transaction management, and connection handling.
    import Database from '@ioc:Adonis/Lucid/Database'