Upgrading
Upgrading to 2.0
Section titled “Upgrading to 2.0”Arc 2.0 declares queries with query and observe blocks instead of the @query and @observe attributes, reads the field types of the generated TypeScript from the model’s Chronicle schema instead of the types: option, and requires Chronicle for Elixir 5.0. mix arc.upgrade rewrites an existing application for both Arc and Chronicle.
-
Bump the dependencies in
mix.exs:{:cratis_arc, "~> 2.0"},{:cratis_chronicle, "~> 5.0"} -
Fetch them:
Terminal window mix deps.get -
Rewrite your code:
Terminal window mix arc.upgradeThe task rewrites
libandtest, or the paths you pass (mix arc.upgrade lib/my_app/authors), and formats the files it changed.--dry-runlists the files it would change and its notices without writing. It does not compile your application, which cannot compile against the new versions until the task has run.It applies Chronicle’s 5.0 rewrite first, the same as
mix chronicle.upgrade: event types and read models get aschemablock, reducers and reactors lose@handles, projections becomedefine/1pipelines, and every id whose 5.0 default differs is pinned to the one it was stored under, so nothing in the event store moves. Then it applies Arc’s: every@queryor@observewith its function becomes a block, and thetypes:option ofuse Cratis.Arc.ReadModelis removed. -
Add Arc and the Chronicle client to
import_depsin.formatter.exs, sofieldand the other declarations format without parentheses:[import_deps: [:cratis_arc, :cratis_chronicle],inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]] -
Act on the notices the task printed. Each names the file and what it left for you, for example:
- the
types:it removed from a read model module, such asremoved types: [id: :uuid]. Give those fields the same types in the model’sschema. - a query function with more than one clause. Merge the clauses into one block.
- anything Chronicle’s rewrite could not decide; see Upgrading Chronicle for Elixir.
The task exits with
1while there are notices. - the
-
Compile:
Terminal window mix compile -
Review the result with
git diff.
What changed
Section titled “What changed”Queries are blocks. query :name, opts do ... end and observe :name, opts do ... end replace an @query or @observe attribute followed by def name(args, ctx). The options are the same. The block still defines name(args, ctx), so calls such as Cratis.Arc.query/5 and your tests do not change. Inside the block, ctx is the context, args the map of converted parameters, and each declared parameter is bound by its name. A @doc before the block documents the query. @query and @observe now fail compilation with a message that shows the block form. See Queries and read models.
defmodule MyApp.Authors.Listing do use Cratis.Arc.ReadModel, model: MyApp.Authors.Author, types: [id: :uuid]
@doc "One author." @query params: [id: :uuid], many: false def author(%{id: id}, ctx), do: Cratis.Chronicle.read_model(MyApp.Authors.Author, id, ctx.chronicle)
@observe many: true, watch: [MyApp.Authors.Author] def live(_args, context) do Cratis.Chronicle.all(MyApp.Authors.Author, context.chronicle) endend
# 2.0defmodule MyApp.Authors.Listing do use Cratis.Arc.ReadModel, model: MyApp.Authors.Author
@doc "One author." query :author, params: [id: :uuid], many: false do Cratis.Chronicle.read_model(MyApp.Authors.Author, id, ctx.chronicle) end
observe :live, many: true, watch: [MyApp.Authors.Author] do context = ctx Cratis.Chronicle.all(MyApp.Authors.Author, context.chronicle) endendWhere the function matched its arguments with another pattern than the parameters’ names, or named its context something other than ctx, the task binds that name at the start of the block, as context = ctx above. Tidy those lines up when you review the diff.
The proxy reads field types from the model. The types: option of use Cratis.Arc.ReadModel is removed and is now a compile error. A Chronicle read model declares its types in its schema, so field :id, :uuid is what makes the id a Guid in TypeScript:
defmodule MyApp.Authors.Author do use Cratis.Chronicle.ReadModels.ReadModel
schema do field :id, :uuid field :name, :string end
@impl true def define(projection) do projection |> from(MyApp.Authors.AuthorRegistered, set: [id: :event_source_id, name: :name]) endendA MongoDB document keeps its own types: on use Cratis.Arc.MongoDB.Document. A plain struct has its types inferred from its defaults; to give it another type, such as a UUID, use Cratis.Chronicle.Schema and declare its fields in a schema block. See React and generated proxies.
Arc requires Chronicle for Elixir 5.0. Event types and read models declare their fields in a schema block, reducers and reactors handle the events their clauses match, projections are define/1 pipelines, and an artifact without id: gets a derived kebab-case id. Upgrading Chronicle for Elixir covers each change.