diff --git a/README.md b/README.md index 7d2a6d1c..61451248 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ ## Introduction +All documentation is contained in the generated hex documentation located [here](https://hexdocs.pm/ash). Head there for installation and usage information. What follows is only a brief introduction to Ash. + Traditional MVC Frameworks (Rails, Django, .Net, Phoenix, etc) leave it up to the user to build the glue between requests for data (HTTP requests in various forms as well as server-side domain logic) and their respective ORMs. In that space, there is an incredible amount of boilerplate code that must get written from scratch for each application (authentication, authorization, sorting, filtering, sideloading relationships, serialization, etc). Ash is an opinionated yet configurable framework designed to reduce boilerplate in an Elixir application. Ash does this by providing a layer of abstraction over your system's data layer(s) with `Resources`. It is designed to be used in conjunction with a phoenix application, or on its own. @@ -17,30 +19,6 @@ To start using Ash, first declare your `Resources` using the Ash `Resource` DSL. Ash is an open-source project and draws inspiration from similar ideas in other frameworks and concepts. The goal of Ash is to lower the barrier to adopting and using Elixir and Phoenix, and in doing so help these amazing communities attract new developers, projects, and companies. -## Example Resource - -```elixir -defmodule Post do - use Ash.Resource - use AshJsonApi.JsonApiResource - use Ash.DataLayer.Postgres - - actions do - read :default - - create :default - end - - attributes do - attribute :name, :string - end - - relationships do - belongs_to :author, Author - end -end -``` - ## Creating a new release of Ash - check out the repository locally diff --git a/documentation/Concept Graph.graffle b/documentation/Concept Graph.graffle deleted file mode 100644 index d035114b..00000000 Binary files a/documentation/Concept Graph.graffle and /dev/null differ diff --git a/documentation/Concept Graph.svg b/documentation/Concept Graph.svg deleted file mode 100644 index a43845a1..00000000 --- a/documentation/Concept Graph.svg +++ /dev/null @@ -1,281 +0,0 @@ - - - - - - - - - - - - - - - - Produced by OmniGraffle 7.7.1 - 2019-12-07 22:45:52 +0000 - - - Canvas 1 - - - Layer 1 - - - - - - - - - - - - - - - - - - - - - Postgres Database - - - - - - - MySQL Database - - - - - - - Stripe API - - - - - Phoenix Application - - - - - Data Sources - - - - - - - - - - - - - - - - - - - Comment - - - - - Ash Api: AdminApi - - - - - - - Ash - - - - - - - Ash - - - - - - - - - - AshJsonApi - - - - - - - AshGraphQL - - - - - - - AshJsonApi - - - - - - - - Ash Api: UserApi - - - - - - - - - - - - - Elixir Api - - - - - - - - Interfaces - - - - - Resources - - - - - - - - - - - Custom Controllers - - - - - - - - - - Emailers - - - - - Application Code - - - - - - - Scheduled Jobs - - - - - - - - - - - Interfaces - - - - - - - Post - - - - - - - Author - - - - - - - Paycheck - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - User Facing Front End - - - - - - - - - - - - - Admin Dashboard - - - - - - - - diff --git a/documentation/getting_started.md b/documentation/getting_started.md new file mode 100644 index 00000000..4045a7d5 --- /dev/null +++ b/documentation/getting_started.md @@ -0,0 +1,144 @@ +# Getting Started + +## Creating an application + +For information on creating a new Elixir application, see [this guide](https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html) + +## Add Ash + +Add `ash` to your dependencies in `mix.exs`. The latest version can be found by running `mix hex.info ash`. + +## Create an Ash API + +Create an API module. This will be your primary way to interact with your Ash resources. We recommend `lib/api.ex` for simple setups. + +```elixir +defmodule MyApp.Api do + use Ash.Api + + resources do + end +end +``` + +Then, add `MyApp.Api` to your `application.ex`'s start function, which should look something like this: + +```elixir +def start(_type, _args) do + children = [ + # Start the Ecto repository + MyApp.Repo, + # Start the Telemetry supervisor + MyApp.Telemetry, + # Start the PubSub system + {Phoenix.PubSub, name: MyApp.PubSub}, + # Start the Endpoint (http/https) + MyApp.Endpoint, + MyApp.Api # <- Add your API here + ] + ... +end +``` + +## Create a resource + +A resource is the primary entity in Ash. Your Api module ties your resources together and gives them an interface, but the vast majority if your configuration will live in a resource. In your typical setup, you might have a resource per database table. For those already familiar with ecto, a resource and an ecto schema are very similar. In fact, all resources define an ecto schema under the hood. This can be leveraged when you need to do things that are not yet implemented or fall outside of the scope of Ash. Here are a few examples: + +```elixir +defmodule MyApp.Tweet do + use Ash.Resource + + attributes do + attribute :id, :uuid do + # All ash resources currently require a primary key + # Eventually, we will add good defaults and/or allow + # for a global configuration of your default primary key + primary_key? true + allow_nil? false + writable? false + default &Ecto.UUID.generate/0 + end + + attribute :body, :string do + allow_nil? false + constraints [max_length: 255] + end + + # Alternatively, you can use the keyword list syntax + # `{:constant, }` is how you set a default + # You can also set functional defaults, via passing in a zero + # argument function or an MFA + attribute :public, :boolean, allow_nil?: false, default: {:constant, false} + + create_timestamp :created_at #This is set on create + update_timestamp :updated_at #This is updated on all updates + + # `create_timestamp` above is just shorthand for: + attribute :created_at, :utc_datetime, writable?: false, default: &DateTime.utc_now/0 + end + + relationships do + belongs_to :user, MyApp.User + end +end + +defmodule MyApp.User do + use Ash.Resource + + attributes do + attribute :email, :string, allow_nil?: false, constraints: [ + match: ~r/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/ + ] + end + + relationships do + has_many :tweets, MyApp.Tweet, destination_field: :user_id + end +end +``` + +## Add resources to your API + +Alter your API like so: + +```elixir +resources do + resource MyApp.User + resource MyApp.Tweet +end +``` + +## Add your datalayer + +Choose a datalayer, and see its documentation for configuring it: + +- `Ash.DataLayer.Ets` - an [ets](https://erlang.org/doc/man/ets.html) datalayer only recommended for testing +- `Ash.DataLayer.Mnesia` - an [mnesia](https://erlang.org/doc/man/mnesia.html) datalayer, not optimized, but is backed by a file and works with distributed applications +- `AshPostgres.DataLayer` - a Postgres datalayer, currently the primary supported data layer + +To add a datalayer, add it to the `use Ash.Resource` statement: + +```elixir +use Ash.Resource, + data_layer: AshPostgres.DataLayer +``` + +## Add actions to enable functionality + +Currently, actions do not offer any customization, but eventually they will be the primary driver for adding specific interactions to your resource. For now, to enable all of them, add the following to your resource: + +```elixir +actions do + create :default + read :default + update :default + destroy :default +end +``` + +## See Ash documentation for the rest + +- `Ash.Api` for what you can do with your resources. +- `Ash.Query` for the kinds of queries you can make. +- `Ash.Dsl` for the resource DSL documentation. +- `Ash.Api.Dsl` for the API DSL documentation. diff --git a/lib/ash.ex b/lib/ash.ex index 2d610aff..4e360b27 100644 --- a/lib/ash.ex +++ b/lib/ash.ex @@ -1,5 +1,6 @@ defmodule Ash do @moduledoc """ + Ash Framework ![Logo](https://github.com/ash-project/ash/blob/master/logos/cropped-for-header.png?raw=true) diff --git a/lib/ash/resource.ex b/lib/ash/resource.ex index 4f682a07..8c38a742 100644 --- a/lib/ash/resource.ex +++ b/lib/ash/resource.ex @@ -3,8 +3,6 @@ defmodule Ash.Resource do A resource is a static definition of an entity in your system. Resource DSL documentation: `Ash.Dsl` - - For more information on the resource DSL, see `Ash.Dsl` """ alias Ash.Dsl.Extension diff --git a/mix.exs b/mix.exs index a505a8a1..92058089 100644 --- a/mix.exs +++ b/mix.exs @@ -38,6 +38,7 @@ defmodule Ash.MixProject do main: "Ash", source_ref: "v#{@version}", logo: "logos/small-logo.png", + extras: ["documentation/getting_started.md"], groups_for_modules: [ entrypoint: [ Ash,