ash_json_api

A JSON:API extension for the Ash Framework

Latest version: 0.34.2 registry icon
Maintenance score
100
Safety score
100
Popularity score
73
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.34.2 0 0 0 0 0
0.34.1 0 0 0 0 0
0.34.0 0 0 0 0 0
0.33.1 0 0 0 0 0
0.33.0 0 0 0 0 0
0.32.1 0 0 0 0 0
0.32.0 0 0 0 0 0
0.31.3 0 0 0 0 0
0.31.2 0 0 0 0 0
0.31.1 0 0 0 0 0
0.31.0 0 0 0 0 0
0.30.1 0 0 0 0 0
0.29.1 0 0 0 0 0
0.29.0 0 0 0 0 0
0.28.6 0 0 0 0 0
0.28.5 0 0 0 0 0
0.28.4 0 0 0 0 0
0.28.3 0 0 0 0 0
0.28.2 0 0 0 0 0
0.28.1 0 0 0 0 0
0.28.0 0 0 0 0 0
0.27.6 0 0 0 0 0
0.27.5 0 0 0 0 0
0.27.1 0 0 0 0 0
0.27.0 0 0 0 0 0
0.25.0 0 0 0 0 0
0.24.4 0 0 0 0 0
0.24.2 0 0 0 0 0
0.24.1 0 0 0 0 0
0.24.0 0 0 0 0 0
0.23.0 0 0 0 0 0
0.22.0 0 0 0 0 0
0.21.0 0 0 0 0 0
0.20.0 0 0 0 0 0
0.19.0 0 0 0 0 0
0.18.0 0 0 0 0 0
0.17.0 0 0 0 0 0
0.16.0 0 0 0 0 0
0.15.0 0 0 0 0 0
0.14.0 0 0 0 0 0
0.13.0 0 0 0 0 0
0.12.0 0 0 0 0 0
0.11.1 0 0 0 0 0
0.10.0 0 0 0 0 0
0.9.0 0 0 0 0 0
0.8.0 0 0 0 0 0
0.6.0 0 0 0 0 0
0.5.0 0 0 0 0 0
0.4.0 0 0 0 0 0
0.3.0 0 0 0 0 0
0.2.4 0 0 0 0 0
0.2.3 0 0 0 0 0
0.2.1 0 0 0 0 0
0.2.0 0 0 0 0 0
0.1.5 0 0 0 0 0
0.1.4 0 0 0 0 0
0.1.3 0 0 0 0 0
0.1.2 0 0 0 0 0
0.1.1 0 0 0 0 0
0.1.0 0 0 0 0 0

Stability
Latest release:

0.34.2 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



AshJsonApi

Elixir CI License: MIT Hex version badge

AshJsonApi allows you to take resources created with Ash and build complete JSON:API compliant endpoints with just a few lines of code.

This is what AshJsonApi does:

  1. Route Creation: AshJsonApi defines routes and actions in your app based on resource configurations
  2. Deserialization: When an incoming HTTP request hits a AshJsonApi defined route/action, AshJsonApi will parse it from /api/users?filter[admin]=true into a call to ash
  3. Query Execution: AshJsonApi then executes the parsed Ash Action (this is the integration point between AshJsonApi and Ash Core, where Ash Actions are defined)
  4. Serialization: AshJsonApi then serializes the result of the Ash Action into JSON API objects.
  5. Response: AshJsonApi then sends this JSON back to the client
  6. Schema Generation: AshJsonApi generates a machine-readable JSON Schema of your entire API and a route/action that can serve it

As you can see, Ash takes care of all of the data related work for a request (CRUD, Sorting, Filtering, Pagination, Side Loading, and Authorization) while AshJsonApi more or less replaces controllers and serializers.

The beauty of putting all of that data functionality into a non-web layer (Ash) is that it can be used in many contexts. A JSON:API is one context - but there are others such as GraphQL or just using an Ash Resource from other code within an Application. The decoupling of the web from data layers is why AshJsonApi is it's own hex package, as opposed to just a module within Ash.

Usage

Assume you have already built a resource using Ash such as this Post resource:

defmodule Post do
  use Ash.Resource,
    data_layer: Ash.DataLayer.Postgres

  # If no actions are defined, simple defaults are implemented called `:read`, `:create`, `:update`, `:destroy`

  attributes do
    attribute :name, :string
  end

  actions do
    defaults [:create, :read, :update, :destroy]
  end

  relationships do
    belongs_to :author, Author
  end
end

As you can see, the resource takes care of interacting with the database, setting up attributes and relationships, as well as specifying actions (CRUD) that can be performed on the resource. What is now needed is to add a configuration for how this resource will interact with JSON:API

defmodule Post do
  use Ash.Resource,
    data_layer: AshPostgres,
    extensions: [AshJsonApi.Resource]

  ...

  json_api do
    # Specify a json:api resource type
    type "posts"
    routes do
      base "/posts"
      # Add a `GET /posts/:id` route, that calls into the :read action called :read
      get :read
      # Add a `GET /posts` route, that calls into the :read action called :read 
      index :read
    end

  end

  ...

Then, update your API with the API extension and configuration:

  defmodule MyApp.Api do
    use Ash.Api, extensions: [AshJsonApi.Api]

    json_api do
      ...
    end

  end

See the hex documentation for more.

See AshJsonApi.Api and AshJsonApi.Resource for the DSL documentation.