ash/README.md

144 lines
9.8 KiB
Markdown
Raw Normal View History

2020-06-04 08:19:12 +12:00
![Test Image 6](https://github.com/ash-project/ash/blob/master/logos/cropped-for-header.png)
2020-06-02 15:23:33 +12:00
2020-06-02 14:10:32 +12:00
![Elixir CI](https://github.com/ash-project/ash/workflows/Elixir%20CI/badge.svg)
2020-06-03 01:41:05 +12:00
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
2020-06-03 00:44:11 +12:00
[![Coverage Status](https://coveralls.io/repos/github/ash-project/ash/badge.svg?branch=master)](https://coveralls.io/github/ash-project/ash?branch=master)
2020-06-03 01:40:33 +12:00
[![Hex version badge](https://img.shields.io/hexpm/v/ash.svg)](https://hex.pm/packages/ash)
2019-10-03 16:08:36 +13:00
2019-12-06 14:56:01 +13:00
## Quick Links
2020-04-05 22:24:56 +12:00
- [Resource Documentation](https://hexdocs.pm/ash/Ash.Resource.html)
- [DSL Documentation](https://hexdocs.pm/ash/Ash.Resource.DSL.html)
- [Code API documentation](https://hexdocs.pm/ash/Ash.Api.Interface.html)
2019-12-06 14:56:01 +13:00
## Introduction
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).
2019-10-05 16:16:28 +13:00
2020-06-04 08:21:48 +12:00
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.
2019-10-05 16:16:28 +13:00
2019-12-12 10:21:59 +13:00
To riff on a famous JRR Tolkien quote, a `Resource`is "One Interface to rule them all, One Interface to find them" and will become an indispensable place to define contracts for interacting with data throughout your application.
2019-12-06 09:46:21 +13:00
2020-06-04 08:22:44 +12:00
To start using Ash, first declare your `Resources` using the Ash `Resource` DSL. You could technically stop there, and just leverage the Ash Elixir API to avoid writing boilerplate. More likely, you would use extensions like Ash.JsonApi or Ash.GraphQL with Phoenix to add external interfaces to those resources without having to write any extra code at all.
2019-10-05 16:16:28 +13:00
2019-12-12 10:21:59 +13:00
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.
2019-10-03 16:08:36 +13:00
2019-12-09 08:48:49 +13:00
## Example Resource
2020-04-05 22:24:56 +12:00
2019-12-09 08:48:49 +13:00
```elixir
defmodule Post do
2020-06-05 14:43:30 +12:00
use Ash.Resource
2019-12-09 08:48:49 +13:00
use AshJsonApi.JsonApiResource
use Ash.DataLayer.Postgres
actions do
2020-05-21 10:59:58 +12:00
read :default
2020-04-05 22:24:56 +12:00
2020-05-21 10:59:58 +12:00
create :default
2019-12-09 08:48:49 +13:00
end
attributes do
attribute :name, :string
end
relationships do
belongs_to :author, Author
end
end
```
2019-10-06 13:33:38 +13:00
## TODO LIST (in no order)
2020-05-15 15:51:35 +12:00
Relationships
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- right now we don't support having duplicates in `many_to_many` relationships, so we'll need to document the limits around that: the primary key of the join resource must be (at least as it is in ash) the join keys. If they don't want to do that, then they should at a minimum define a unique constraint on the two join keys.
- relationship changes are an artifact of the old way of doing things and are very ugly right now
- Figure out under what circumstances we can bulk fetch when reading before updating many_to_many and to_many relationships, and do so.
2020-04-05 22:24:56 +12:00
- Perhaps, reverse relationships should eliminate the need to set destination field.
- also, perhaps, reverse relationships suck and should not be necessary. The alternative is a `from_related` filter, that lets you use an association in reverse
- allow configuring the name of the join relationship name for many to many relationships
2020-05-15 15:51:35 +12:00
Primary Key
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- So many parts of the system are reliant on things having an `id` key explicitly. THis will need to be addressed some day, and will be a huge pain in the ass
- Raise on composite primary key if data layer can't do it
Params
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- `params` should be solidified. Perhaps as a struct. Or perhaps just renamed to `action_params` where it is used.
- Consider making a "params builder" so you can say things like `Ash.Params.add_side_load(params, [:foo, :bar, :baz])` and build params up over time.
2020-04-05 22:24:56 +12:00
- Validate that params on the way in are either all strings or all atoms
2020-05-15 15:51:35 +12:00
Ecto
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- The ecto internals that live on structs are going to cause problems w/ pluggability of backends, like the `%Ecto.Association.NotLoaded{}`. That backend may need to scrub the ecto specifics off of those structs.
Data Layer
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- Allow encoding database-level constraints into the resource, like "nullable: false" or something. This will let us validate things like not leaving orphans when bulk updating a many to many
- Eventually data_layers should state what raw types they support, and the filters they support on those raw types
- Think hard about the data_layer.can? pattern to make sure we're giving enough info, but not too much.
- Use data layer compatibility features to disallow incompatible setups. For instance, if the data layer can't transact, then they can't have an editable `has_one` or `many_to_many` resource.
- Add `can?(:bulk_update)` to data layers, so we can more efficiently update relationships
- Set up "atomic updates" (upserts). If an adapter supports them, and the auth passes precheck, we could turn `get + update` combos into `upserts`
- Make an automatic test suite that confirms that data layers behave the way they claim to behave, maybe.
2020-04-05 22:24:56 +12:00
- Is it possible/reasonable to do join tables that aren't unique on source_id/destination_id? Probably, but metadata would need to be organized differently.
2020-05-15 15:51:35 +12:00
- right now, we require the datalayer to support upserts in order to do relationship additions, but we could set it up such that if the datalayer doesn't support upserts, we first read the relationship. Its not airtight against race conditions (unless the datalayer supports transactions, also unimplemented) but its better than nothing.
2020-04-13 10:52:52 +12:00
- without transactions, we can't ensure that all changes are rolled back in the case that relationship updates are included. Don't think there is really anything to do about that, but something worth considering.
2020-05-15 15:51:35 +12:00
DSL
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- use a process to hold constructed DSL state, and then coalesce it all at the end. This can clean things up, and also allow us to potentially eliminate the registry. This will probably go hand in hand w/ the "capabilities" layer, where the DSL confirms that your data layer is capable of performing everything that your DSL declares
- Bake in descriptions to the DSL
- need to make sure that all of the dsl components are in the `.formatter.exs`. I made it so all of the options can be specified as a nested DSL, but haven't gone through and added them to the formatter file
2020-05-15 15:51:35 +12:00
- add `init` to checks, and error check their construction when building the DSL
- allow for resources to be created _without_ the dsl
2020-05-15 15:51:35 +12:00
Code Quality
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- Replace all my ugly `reduce` with tuples with `reduce_while`
Runtime
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- Add a runtime-intialization to checks that can return data loading instructions to be executed prior to pre-check
- Important: We need a framework level solution for runtime configuration, _or at minimum_ a recommended way to do it. Things like configuring the host/port of your API, or disabling features
2020-05-15 15:51:35 +12:00
2020-05-31 15:55:01 +12:00
Testing
- talk about building factory/mocking utilities. I suspect mocking will not be necessary due to eventually being able to toggle resources to use the ETS datalayer.
2020-05-15 15:51:35 +12:00
Engine
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- implement transactions in the engine. Perhaps by assigning requests transaction ids or something along those lines.
2020-05-31 15:55:01 +12:00
- fix the comment noted in the destroy action: ~the delete needs to happen _outside_ of the data fetching step, so the engine needs some kind of "after data resolved" capability~
- add a total failure mode (since JSON API just fails entirely) that will cause the engine to stop on the first error
- Get rid of all of the :lists.droplast and List.first stuff by making a `%Ash.Engine.Dependecy{path: path, field: field}`
2020-06-02 17:47:25 +12:00
- consider (some day) opening up the engine/request as a public API
2020-05-15 15:51:35 +12:00
Fields/Attributes
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- Flesh out field options (sortable, filterable, other behavior?)
- Booleans need to not support `nil` values. That has to be a different type. boolean filter/authorization logic is greatly enhanced if that is the case.
- Add the ability to configure what fields can identify a user (for instance email) and use those in checks.
- certain attribute names are not going to be allowed, like `or`, `and`, `in`, things like that.
- Make sure updating foreign key attributes behaves the same as setting a relationship, or just disallow having editable attributes for relationship fkeys
Framework
2020-05-17 10:03:29 +12:00
2020-05-15 15:51:35 +12:00
- support accepting a _resource and a filter_ in `api.update` and `api.destroy`, and doing those as bulk actions
- support something similar to the older interface we had with ash, like `Api.read(resource, filter: [...], sort: [...])`, as the `Ash.Query` method is a bit long form in some cases
2020-05-21 10:59:58 +12:00
- Add a mixin compatibility checker framework, to allow for extensions to declare what features they do/don't support.
2020-05-15 15:51:35 +12:00
- Have ecto types ask the data layer about the kinds of filtering they can do, and that kind of thing.
- Make an `Ash.Changeset` that is a superset of an ecto changeset
- consider, just for the sake of good old fashion fun/cool factor, a parser that can parse a string into a query at compile time, so that queries can look nice in code.
- Make `Ash.Type` that is a superset of things like `Ecto.Type`. If we bring in ecto database-less(looking like more and more of a good idea to me) that kind of thing gets easier and we can potentially lean on ecto for type validations well.
- When we support embedding, figure out `embed_as` on `Ash.Type`
- Consider supporting one resource being a "more specific" version of another resource at the _resource_ level, not the data layer level.
2020-06-03 03:47:34 +12:00
## Creating a new release of Ash
- check out the repository locally
- run `mix git_ops.release` (see git_ops documentation for more information)
- check the changelog/new release number
- push (with tags) and CI will automatically deploy the hex package