Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ locals_without_parens = [
col: :*,
colgroup: :*,
command: :*,
commands: :*,
component: :*,
data: :*,
datalist: :*,
Expand All @@ -42,14 +41,11 @@ locals_without_parens = [
endpoint: :*,
enum: :*,
event: :*,
events: :*,
feature: :*,
features: :*,
field: :*,
fieldset: :*,
figcaption: :*,
figure: :*,
flows: :*,
footer: :*,
form: :*,
h1: :*,
Expand All @@ -58,7 +54,6 @@ locals_without_parens = [
h4: :*,
h5: :*,
h6: :*,
handlers: :*,
has_many: :*,
head: :*,
header: :*,
Expand All @@ -79,12 +74,10 @@ locals_without_parens = [
main: :*,
map: :*,
mapping: :*,
mappings: :*,
mark: :*,
meta: :*,
meter: :*,
model: :*,
models: :*,
mount: :*,
namespace: :*,
namespaces: :*,
Expand All @@ -106,7 +99,6 @@ locals_without_parens = [
progress: :*,
publish: :*,
q: :*,
queries: :*,
query: :*,
route: :*,
routes: :*,
Expand All @@ -115,7 +107,6 @@ locals_without_parens = [
ruby: :*,
s: :*,
samp: :*,
scopes: :*,
script: :*,
section: :*,
select: :*,
Expand All @@ -127,7 +118,6 @@ locals_without_parens = [
strong: :*,
style: :*,
sub: :*,
subscriptions: :*,
summary: :*,
sup: :*,
svg: :*,
Expand All @@ -151,7 +141,7 @@ locals_without_parens = [
var: :*,
video: :*,
view: :*,
wbr: :*,
wbr: :*
]

[
Expand Down
5 changes: 3 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
elixir 1.17.2-otp-27
erlang 27.0
elixir 1.18.3-otp-27
erlang 27.3
nodejs 24.2.0
3 changes: 1 addition & 2 deletions lib/mix/tasks/gen_migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ defmodule Mix.Tasks.Momo.Gen.Migrations do
repo = Keyword.fetch!(config, :repo)
app = Keyword.fetch!(config, :app)
migrations_dir = Mix.EctoSQL.source_repo_priv(repo)
features = app.features()

dir = Path.join([migrations_dir, "migrations"])

dir
|> Migrations.existing()
|> Migrations.missing(features)
|> Migrations.missing(app)
|> case do
%{steps: []} ->
Mix.shell().info("No migrations to write")
Expand Down
1 change: 0 additions & 1 deletion lib/momo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Momo do
"""

@dsls [
Momo.Feature.Dsl,
Momo.Command.Dsl,
Momo.Model.Dsl,
Momo.Query.Dsl,
Expand Down
154 changes: 152 additions & 2 deletions lib/momo/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,158 @@ defmodule Momo.App do
generators: [
Momo.App.Generator.Migrate,
Momo.App.Generator.Application,
Momo.App.Generator.Roles
Momo.App.Generator.Roles,
Momo.App.Generator.Graph,
Momo.App.Generator.Map
]

defstruct [:name, :roles, :module, :repos, :endpoints, :features]
defstruct [
:name,
:roles,
:module,
:repos,
:endpoints,
models: [],
commands: [],
queries: [],
events: [],
flows: [],
subscriptions: [],
mappings: [],
values: [],
scopes: []
]

require Logger

import Momo.Maps

@doc """
Map the input value, using a configured mapping or a generic one
"""
def map(app, from, to, input) do
mapping = app.mappings() |> Enum.find(&(&1.from() == from && &1.to() == to))

case mapping do
nil -> map(input, to)
mapping -> mapping.map(input)
end
end

defp map(input, to) when is_map(input) do
input |> plain_map() |> to.new()
end

defp map(input, to) when is_list(input) do
with items when is_list(items) <-
Enum.reduce_while(input, [], fn item, acc ->
case map(item, to) do
{:ok, item} -> {:cont, [item | acc]}
{:error, _} = error -> {:halt, error}
end
end),
do: {:ok, Enum.reverse(items)}
end

@doc """
Executes a command and publishes events

This function does not take a context, which will disable permission checks
"""
def execute_command(command, params) do
if command.atomic?() do
repo = command.app().repo()

repo.transaction(fn ->
with {:error, reason} <- do_execute_command(command, params) do
repo.rollback(reason)
end
end)
else
do_execute_command(command, params)
end
end

@doc """
Executes a command, publishes events, and returns a result

This function takes a context, which will trigger checking for permissions based on policies, roles and scopes
"""
def execute_command(command, params, context) do
if command.atomic?() do
repo = command.app().repo()

repo.transaction(fn ->
with {:error, reason} <- do_execute_command(command, params, context) do
repo.rollback(reason)
end
end)
else
do_execute_command(command, params, context)
end
end

defp do_execute_command(command, params) do
with {:ok, params} <- params |> plain_map() |> command.params().validate(),
{:ok, result, events} <- command.execute(params),
:ok <- publish_events(events, command.app()) do
{:ok, result}
end
end

defp do_execute_command(command, params, context) do
with {:ok, params} <- params |> plain_map() |> command.params().validate(),
context <- Map.put(context, :params, params),
:ok <- allow(command, context),
{:ok, result, events} <- command.execute(params, context),
:ok <- publish_events(events, command.app()) do
{:ok, result}
end
end

defp allow(command, context) do
if command.allowed?(context) do
:ok
else
{:error, :unauthorized}
end
end

@doc """
Publish the given list of events

Events are only published if there are subscriptions for them.
"""
def publish_events([], _app), do: :ok

def publish_events(events, app) do
events
|> Enum.flat_map(&jobs(&1, app))
|> Momo.Job.schedule_all()

:ok
end

defp jobs(event, app) do
jobs =
app.subscriptions()
|> Enum.filter(&(&1.event() == event.__struct__))
|> Enum.map(&[event: event.__struct__, params: Jason.encode!(event), subscription: &1])

if jobs == [] do
Logger.warning("No subscriptions found for event", event: event.__struct__)
end

jobs
end

@doc """
Executes a query that has parameters
"""
def execute_query(query, params, context), do: query.execute(params, context)

@doc """
Executes a query that has no parameters
"""
def execute_query(query, params), do: query.execute(params)
end
10 changes: 9 additions & 1 deletion lib/momo/app/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ defmodule Momo.App.Dsl do
tags: [
Momo.App.Dsl.Repos,
Momo.App.Dsl.Endpoints,
Momo.App.Dsl.Features
Momo.App.Dsl.Models,
Momo.App.Dsl.Commands,
Momo.App.Dsl.Queries,
Momo.App.Dsl.Events,
Momo.App.Dsl.Flows,
Momo.App.Dsl.Subscriptions,
Momo.App.Dsl.Mappings,
Momo.App.Dsl.Values,
Momo.App.Dsl.Scopes
]
end
10 changes: 9 additions & 1 deletion lib/momo/app/dsl/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ defmodule Momo.App.Dsl.App do
attribute :roles, kind: :string, required: true
child :repos, min: 0, max: 1
child :endpoints, min: 0, max: 1
child :features, min: 1, max: 1
child :models, min: 0, max: 1
child :commands, min: 0, max: 1
child :queries, min: 0, max: 1
child :events, min: 0, max: 1
child :flows, min: 0, max: 1
child :subscriptions, min: 0, max: 1
child :mappings, min: 0, max: 1
child :values, min: 0, max: 1
child :scopes, min: 0, max: 1
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/commands.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Commands do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/events.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Events do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 0 additions & 8 deletions lib/momo/app/dsl/features.ex

This file was deleted.

8 changes: 8 additions & 0 deletions lib/momo/app/dsl/flows.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Flows do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/mappings.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Mappings do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/models.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Models do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/queries.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Queries do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/scopes.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Scopes do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/subscriptions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Subscriptions do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
8 changes: 8 additions & 0 deletions lib/momo/app/dsl/values.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Momo.App.Dsl.Values do
@moduledoc false
use Diesel.Tag

tag do
child kind: :module, min: 0
end
end
Loading